wcs-upgrade.js
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
jQuery(document).ready(function($){
var upgrade_start_time = null,
total_subscriptions = wcs_update_script_data.subscription_count;
$('#update-messages').slideUp();
$('#upgrade-step-3').slideUp();
$('form#subscriptions-upgrade').on('submit',function(e){
$('#update-welcome').slideUp(600);
$('#update-messages').slideDown(600);
if('true'==wcs_update_script_data.really_old_version){
wcs_ajax_update_really_old_version();
} else if('true'==wcs_update_script_data.upgrade_to_1_5){
wcs_ajax_update_products();
wcs_ajax_update_hooks();
} else if('true'==wcs_update_script_data.upgrade_to_2_0){
wcs_ajax_update_subscriptions();
} else if('true'==wcs_update_script_data.repair_2_0){
wcs_ajax_repair_subscriptions();
} else {
wcs_ajax_update_complete();
}
e.preventDefault();
});
function wcs_ajax_update_really_old_version(){
$.ajax({
url: wcs_update_script_data.ajax_url,
type: 'POST',
data: {
action: 'wcs_upgrade',
upgrade_step: 'really_old_version',
nonce: wcs_update_script_data.upgrade_nonce
},
success: function(results) {
$('#update-messages ol').append($('<li />').text(results.message));
wcs_ajax_update_products();
wcs_ajax_update_hooks();
},
error: function(results,status,errorThrown){
wcs_ajax_update_error();
}
});
}
function wcs_ajax_update_products(){
$.ajax({
url: wcs_update_script_data.ajax_url,
type: 'POST',
data: {
action: 'wcs_upgrade',
upgrade_step: 'products',
nonce: wcs_update_script_data.upgrade_nonce
},
success: function(results) {
$('#update-messages ol').append($('<li />').text(results.message));
},
error: function(results,status,errorThrown){
wcs_ajax_update_error();
}
});
}
function wcs_ajax_update_hooks() {
var start_time = new Date();
$.ajax({
url: wcs_update_script_data.ajax_url,
type: 'POST',
data: {
action: 'wcs_upgrade',
upgrade_step: 'hooks',
nonce: wcs_update_script_data.upgrade_nonce
},
success: function(results) {
if(results.message){
var end_time = new Date(),
execution_time = Math.ceil( ( end_time.getTime() - start_time.getTime() ) / 1000 );
$('#update-messages ol').append($('<li />').text(results.message.replace('{execution_time}',execution_time)));
}
if( undefined == typeof(results.upgraded_count) || parseInt(results.upgraded_count) <= ( wcs_update_script_data.hooks_per_request - 1 ) ){
wcs_ajax_update_subscriptions();
} else {
wcs_ajax_update_hooks();
}
},
error: function(results,status,errorThrown){
wcs_ajax_update_error();
}
});
}
function wcs_ajax_update_subscriptions() {
var start_time = new Date();
if ( null === upgrade_start_time ) {
upgrade_start_time = start_time;
}
$.ajax({
url: wcs_update_script_data.ajax_url,
type: 'POST',
data: {
action: 'wcs_upgrade',
upgrade_step: 'subscriptions',
nonce: wcs_update_script_data.upgrade_nonce
},
success: function(results) {
if('success'==results.status){
var end_time = new Date(),
execution_time = Math.ceil( ( end_time.getTime() - start_time.getTime() ) / 1000 );
$('#update-messages ol').append($('<li />').text(results.message.replace('{execution_time}',execution_time)));
wcs_update_script_data.subscription_count -= results.upgraded_count;
if( "undefined" === typeof(results.upgraded_count) || parseInt(wcs_update_script_data.subscription_count) <= 0 ) {
wcs_ajax_update_complete();
} else {
wcs_ajax_update_estimated_time(results.time_message);
wcs_ajax_update_subscriptions();
}
} else {
wcs_ajax_update_error(results.message);
}
},
error: function(results,status,errorThrown){
$('<br/><span>Error: ' + results.status + ' ' + errorThrown + '</span>').appendTo('#update-error p');
wcs_ajax_update_error( $('#update-error p').html() );
}
});
}
function wcs_ajax_repair_subscriptions() {
var start_time = new Date();
if ( null === upgrade_start_time ) {
upgrade_start_time = start_time;
}
$.ajax({
url: wcs_update_script_data.ajax_url,
type: 'POST',
data: {
action: 'wcs_upgrade',
upgrade_step: 'subscription_dates_repair',
nonce: wcs_update_script_data.upgrade_nonce
},
success: function(results) {
if('success'==results.status){
var end_time = new Date(),
execution_time = Math.ceil( ( end_time.getTime() - start_time.getTime() ) / 1000 );
$('#update-messages ol').append($('<li />').text(results.message.replace('{execution_time}',execution_time)));
wcs_update_script_data.subscription_count -= results.repaired_count;
wcs_update_script_data.subscription_count -= results.unrepaired_count;
if( parseInt(wcs_update_script_data.subscription_count) <= 0 ) {
wcs_ajax_update_complete();
} else {
wcs_ajax_update_estimated_time(results.time_message);
wcs_ajax_repair_subscriptions();
}
} else {
wcs_ajax_update_error(results.message);
}
},
error: function(results,status,errorThrown){
$('<br/><span>Error: ' + results.status + ' ' + errorThrown + '</span>').appendTo('#update-error p');
wcs_ajax_update_error( $('#update-error p').html() );
}
});
}
function wcs_ajax_update_complete() {
$('#update-ajax-loader, #estimated_time').slideUp(function(){
$('#update-complete').slideDown();
});
}
function wcs_ajax_update_error(message) {
message = message || '';
if ( message.length > 0 ){
$('#update-error p').html(message);
}
$('#update-ajax-loader, #estimated_time').slideUp(function(){
$('#update-error').slideDown();
});
}
function wcs_ajax_update_estimated_time(message) {
var total_updated = total_subscriptions - wcs_update_script_data.subscription_count,
now = new Date(),
execution_time,
time_per_update,
time_left,
time_left_minutes,
time_left_seconds;
execution_time = Math.ceil( ( now.getTime() - upgrade_start_time.getTime() ) / 1000 );
time_per_update = execution_time / total_updated;
time_left = Math.floor( wcs_update_script_data.subscription_count * time_per_update );
time_left_minutes = Math.floor( time_left / 60 );
time_left_seconds = time_left % 60;
$('#estimated_time').html(message.replace( '{time_left}', time_left_minutes + ":" + zeropad(time_left_seconds) ));
}
function zeropad(number) {
var pad_char = 0,
pad = new Array(3).join(pad_char);
return (pad + number).slice(-pad.length);
}
});