-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3io.js
416 lines (416 loc) · 14 KB
/
s3io.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
jQuery(document).ready(function($) {
var s3io_error_counter = 30;
$(function() {
$("#s3io-delay-slider").slider({
min: 0,
max: 30,
value: $("#s3io-delay").val(),
slide: function(event, ui) {
$("#s3io-delay").val(ui.value);
}
});
});
var s3io_attachments = s3io_vars.attachments;
var s3io_checked = 0;
var s3io_i = 0;
var s3io_k = 0;
var s3io_delay = 0;
// initialize the ajax actions
var s3io_init_action = 's3io_bulk_init';
var s3io_scan_action = 's3io_image_scan';
var s3io_loop_action = 's3io_bulk_loop';
var s3io_cleanup_action = 's3io_bulk_cleanup';
var s3io_init_data = {
action: s3io_init_action,
s3io_wpnonce: s3io_vars._wpnonce,
};
var s3io_table_action = 's3io_query_table';
// get the urls from the textarea
var s3io_queue = '';
var s3io_url_count = 0;
$('.s3io-hndle').click(function() {
$(this).next('.inside').toggle();
var button = $(this).prev('.button-link');
if ('true' == button.attr('aria-expanded')) {
button.attr('aria-expanded', 'false');
button.closest('.postbox').addClass('closed');
button.children('.toggle-indicator').attr('aria-hidden', 'true');
} else {
button.attr('aria-expanded', 'true');
button.closest('.postbox').removeClass('closed');
button.children('.toggle-indicator').attr('aria-hidden', 'false');
}
});
$('.s3io-handlediv').click(function() {
$(this).parent().children('.inside').toggle();
if ('true' == $(this).attr('aria-expanded')) {
$(this).attr('aria-expanded', 'false');
$(this).closest('.postbox').addClass('closed');
$(this).children('.toggle-indicator').attr('aria-hidden', 'true');
} else {
$(this).attr('aria-expanded', 'true');
$(this).closest('.postbox').removeClass('closed');
$(this).children('.toggle-indicator').attr('aria-hidden', 'false');
}
});
$('#s3io-bulk-stop').submit(function() {
s3io_k = 9;
$('#s3io-bulk-stop').hide();
return false;
});
$('#s3io-url-start').submit(function() {
s3io_k = 0;
s3io_queue = $('#s3io-url-image-queue').val().match(/\S+/g);
s3io_url_count = s3io_queue.length;
s3io_loop_action = 's3io_url_images_loop';
var s3io_url = s3io_queue.pop();
var s3io_url_loop_data = {
action: s3io_loop_action,
ewww_force: true,
s3io_url: s3io_url,
s3io_wpnonce: s3io_vars._wpnonce,
};
if ( ! $('#s3io-delay').val().match( /^[1-9][0-9]*$/) ) {
s3io_delay = 0;
} else {
s3io_delay = $('#s3io-delay').val();
}
$('#s3io-bulk-stop').show();
$('.s3io-bulk-form').hide();
$('.s3io-bulk-info').hide();
$('#s3io-url-image-queue').hide();
$('#s3io-bulk-loading').html(s3io_vars.optimizing + ' ' + s3io_url + ' ' + s3io_vars.spinner);
$('#s3io-bulk-counter').html(s3io_vars.optimized + ' ' + s3io_i + '/' + s3io_url_count);
$('#s3io-bulk-progressbar').progressbar({ max: s3io_url_count });
$.post(ajaxurl, s3io_url_loop_data, function(response) {
$('#s3io-bulk-widgets').show();
s3io_i++;
var s3io_response = JSON.parse(response);
$('#s3io-bulk-progressbar').progressbar("option", "value", s3io_i );
$('#s3io-bulk-counter').html(s3io_vars.optimized + ' ' + s3io_i + '/' + s3io_url_count);
if (s3io_response.error) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_response.error + '</b></p>');
}
else if (s3io_response.results) {
$('#s3io-bulk-status .inside').append( s3io_response.results );
if ( s3io_queue.length > 0 ) {
setTimeout( s3ioProcessImageByURL, s3io_delay * 1000);
} else {
$('#s3io-bulk-loading').html(s3io_vars.finished);
$('#s3io-bulk-stop').hide();
}
}
})
.fail(function() {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_interrupted + '</b></p>');
});
return false;
});
function s3ioProcessImageByURL() {
s3io_error_counter = 30;
var s3io_url = s3io_queue.pop();
var s3io_url_loop_data = {
action: s3io_loop_action,
ewww_force: true,
s3io_url: s3io_url,
s3io_wpnonce: s3io_vars._wpnonce,
};
$('#s3io-bulk-loading').html(s3io_vars.optimizing + ' ' + s3io_url + ' ' + s3io_vars.spinner);
$.post(ajaxurl, s3io_url_loop_data, function(response) {
s3io_i++;
$('#s3io-bulk-progressbar').progressbar("option", "value", s3io_i );
$('#s3io-bulk-counter').html(s3io_vars.optimized + ' ' + s3io_i + '/' + s3io_url_count);
var s3io_response = JSON.parse(response);
if (s3io_response.error) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_response.error + '</b></p>');
}
else if (s3io_k == 9) {
s3io_jqxhr.abort();
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_stopped + '</b></p>');
}
else if ( response == 0 ) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_stopped + '</b></p>');
}
else if ( s3io_response.results) {
$('#s3io-bulk-status .inside').append( s3io_response.results );
if ( s3io_queue.length > 0 ) {
setTimeout( s3ioProcessImageByURL, s3io_delay * 1000);
} else {
$('#s3io-bulk-loading').html(s3io_vars.finished);
$('#s3io-bulk-stop').hide();
}
}
})
.fail(function() {
if (s3io_error_counter == 0) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_interrupted + '</b></p>');
} else {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.temporary_failure + ' ' + s3io_error_counter + '</b></p>');
s3io_error_counter--;
s3io_queue.push( s3io_url );
setTimeout(function() {
s3ioProcessImageByURL();
}, 1000);
}
});
}
$('#s3io-show-table').submit(function() {
var s3io_pointer = 0;
var s3io_total_pages = Math.ceil(s3io_vars.image_count / 50);
$('.s3io-table').show();
$('#s3io-show-table').hide();
if (s3io_vars.image_count >= 50) {
$('.tablenav').show();
$('#next-images').show();
$('.last-page').show();
}
var s3io_table_data = {
action: s3io_table_action,
s3io_wpnonce: s3io_vars._wpnonce,
s3io_offset: s3io_pointer,
};
$('.displaying-num').text(s3io_vars.count_string);
$.post(ajaxurl, s3io_table_data, function(response) {
$('#s3io-bulk-table').html(response);
});
$('.current-page').text(s3io_pointer + 1);
$('.total-pages').text(s3io_total_pages);
$('#s3io-pointer').text(s3io_pointer);
return false;
});
$('#next-images').click(function() {
var s3io_pointer = $('#s3io-pointer').text();
s3io_pointer++;
var s3io_table_data = {
action: s3io_table_action,
s3io_wpnonce: s3io_vars._wpnonce,
s3io_offset: s3io_pointer,
};
$.post(ajaxurl, s3io_table_data, function(response) {
$('#s3io-bulk-table').html(response);
});
if (s3io_vars.image_count <= ((s3io_pointer + 1) * 50)) {
$('#next-images').hide();
$('.last-page').hide();
}
$('.current-page').text(s3io_pointer + 1);
$('#s3io-pointer').text(s3io_pointer);
$('#prev-images').show();
$('.first-page').show();
return false;
});
$('#prev-images').click(function() {
var s3io_pointer = $('#s3io-pointer').text();
s3io_pointer--;
var s3io_table_data = {
action: s3io_table_action,
s3io_wpnonce: s3io_vars._wpnonce,
s3io_offset: s3io_pointer,
};
$.post(ajaxurl, s3io_table_data, function(response) {
$('#s3io-bulk-table').html(response);
});
if (!s3io_pointer) {
$('#prev-images').hide();
$('.first-page').hide();
}
$('.current-page').text(s3io_pointer + 1);
$('#s3io-pointer').text(s3io_pointer);
$('#next-images').show();
$('.last-page').show();
return false;
});
$('.last-page').click(function() {
var s3io_pointer = $('.total-pages').text();
s3io_pointer--;
var s3io_table_data = {
action: s3io_table_action,
s3io_wpnonce: s3io_vars._wpnonce,
s3io_offset: s3io_pointer,
};
$.post(ajaxurl, s3io_table_data, function(response) {
$('#s3io-bulk-table').html(response);
});
$('#next-images').hide();
$('.last-page').hide();
$('.current-page').text(s3io_pointer + 1);
$('#s3io-pointer').text(s3io_pointer);
$('#prev-images').show();
$('.first-page').show();
return false;
});
$('.first-page').click(function() {
var s3io_pointer = 0;
var s3io_table_data = {
action: s3io_table_action,
s3io_wpnonce: s3io_vars._wpnonce,
s3io_offset: s3io_pointer,
};
$.post(ajaxurl, s3io_table_data, function(response) {
$('#s3io-bulk-table').html(response);
});
$('#prev-images').hide();
$('.first-page').hide();
$('.current-page').text(s3io_pointer + 1);
$('#s3io-pointer').text(s3io_pointer);
$('#next-images').show();
$('.last-page').show();
return false;
});
$('#s3io-scan').submit(function() {
$('.s3io-bulk-form').hide();
$('.s3io-bulk-info').hide();
$('#s3io-bulk-loading').html(s3io_vars.starting_scan);
s3ioScanBuckets();
return false;
});
function s3ioScanBuckets() {
var s3io_scan_data = {
action: s3io_scan_action,
s3io_wpnonce: s3io_vars._wpnonce,
};
var s3io_jqxhr = $.post(ajaxurl, s3io_scan_data, function(response) {
var s3io_response = JSON.parse(response);
if (s3io_response.error) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_response.error + '</b></p>');
} else if (s3io_response.current) {
s3io_checked += s3io_response.completed;
$('#s3io-bulk-loading').html('<p>' + s3io_response.current + '<br>' + s3io_vars.completed_string + '</p>');
$('#s3io-completed-count').text(s3io_checked);
/*if (s3io_response.new_nonce) {
s3io_vars._wpnonce = s3io_response.new_nonce;
}*/
s3io_error_counter = 30;
s3ioScanBuckets();
} else {
if ( s3io_response.message ) {
$('#s3io-bulk-loading').html('');
if (s3io_response.pending > 0) {
$('#s3io-delay-slider-form').show();
$('#s3io-start').show();
}
$('#s3io-found-images').text(s3io_response.message);
$('#s3io-found-images').show();
s3io_attachments = s3io_response.pending;
s3io_error_counter = 30;
} else {
$('#s3io-bulk-loading').html('invalid response, check JS console');
}
}
})
.fail(function() {
if (s3io_error_counter == 0) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_interrupted + '</b></p>');
} else {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.temporary_failure + ' ' + s3io_error_counter + '</b></p>');
s3io_error_counter--;
setTimeout(function() {
s3ioScanBuckets();
}, 1000);
}
});
}
$('#s3io-start').submit(function() {
s3ioStartOpt();
return false;
});
function s3ioStartOpt () {
s3io_k = 0;
if ( ! $('#s3io-delay').val().match( /^[1-9][0-9]*$/) ) {
s3io_delay = 0;
} else {
s3io_delay = $('#s3io-delay').val();
}
$('#s3io-bulk-stop').show();
$('.s3io-bulk-form').hide();
$('.s3io-bulk-info').hide();
$('#s3io-force-empty').hide();
$.post(ajaxurl, s3io_init_data, function(response) {
var s3io_init_response = JSON.parse(response);
$('#s3io-bulk-loading').html(s3io_init_response.results);
$('#s3io-bulk-progressbar').progressbar({ max: s3io_attachments });
$('#s3io-bulk-counter').html(s3io_vars.optimized + ' 0/' + s3io_attachments);
s3ioProcessImage();
});
}
function s3ioProcessImage() {
var s3io_loop_data = {
action: s3io_loop_action,
s3io_wpnonce: s3io_vars._wpnonce,
};
var s3io_jqxhr = $.post(ajaxurl, s3io_loop_data, function(response) {
s3io_i++;
var s3io_response = JSON.parse(response);
$('#s3io-bulk-progressbar').progressbar("option", "value", s3io_i );
$('#s3io-bulk-counter').html(s3io_vars.optimized + ' ' + s3io_i + '/' + s3io_attachments);
if (s3io_response.error) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_response.error + '</b></p>');
}
else if (s3io_k == 9) {
s3io_jqxhr.abort();
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_stopped + '</b></p>');
}
else if ( response == 0 ) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_stopped + '</b></p>');
}
else if (s3io_i < s3io_attachments) {
$('#s3io-bulk-widgets').show();
if (s3io_response.results) {
$('#s3io-bulk-last .inside').html( s3io_response.results );
$('#s3io-bulk-status .inside').append( s3io_response.results );
}
if (s3io_response.next_file) {
$('#s3io-bulk-loading').html(s3io_response.next_file);
}
if (s3io_response.new_nonce) {
s3io_vars._wpnonce = s3io_response.new_nonce;
}
s3io_error_counter = 30;
setTimeout(s3ioProcessImage, s3io_delay * 1000);
}
else {
if ( s3io_response.results ) {
$('#s3io-bulk-status .inside').append( s3io_response.results );
}
var s3io_cleanup_data = {
action: s3io_cleanup_action,
s3io_wpnonce: s3io_vars._wpnonce,
};
$.post(ajaxurl, s3io_cleanup_data, function(response) {
$('#s3io-bulk-loading').html(response);
$('#s3io-bulk-stop').hide();
$('#s3io-bulk-last').hide();
});
}
})
.fail(function() {
if (s3io_error_counter == 0) {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.operation_interrupted + '</b></p>');
} else {
$('#s3io-bulk-loading').html('<p style="color: red"><b>' + s3io_vars.temporary_failure + ' ' + s3io_error_counter + '</b></p>');
s3io_error_counter--;
setTimeout(function() {
s3ioProcessImage();
}, 1000);
}
});
}
});
function s3ioRemoveImage(imageID) {
var s3io_image_removal = {
action: 's3io_table_remove',
s3io_wpnonce: s3io_vars._wpnonce,
s3io_image_id: imageID,
};
jQuery.post(ajaxurl, s3io_image_removal, function(response) {
if(response == '1') {
jQuery('#s3io-image-' + imageID).remove();
var s3io_prev_count = s3io_vars.image_count;
s3io_vars.image_count--;
s3io_vars.count_string = s3io_vars.count_string.replace( s3io_prev_count, s3io_vars.image_count );
jQuery('.displaying-num').text(s3io_vars.count_string);
} else {
alert(s3io_vars.remove_failed);
}
});
}