PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | js/wposmush.js +198 -271 3.4.2 → 4.7.0 View file →
@@ -10,16 +10,15 @@
10 10 var heartbeat_agents = [];
11 11
12 12 var last_ui_update_agent_uid = null;
13 13
14 + var block_ui = wp_optimize.block_ui;
15 +
14 16 /**
15 17 * Variables for smushing.
16 18 */
17 - var smush_nav_tab = $('#wp-optimize-images-nav-tab-smush'),
18 - smush_images_grid = $('#wpo_smush_images_grid'),
19 + var smush_images_grid = $('#wpo_smush_images_grid'),
19 20 smush_images_optimization_message = $('#smush_info_images'),
20 - smush_images_pending_tasks_container = $('#wpo_smush_images_pending_tasks_container'),
21 - smush_images_pending_tasks_btn = $('#wpo_smush_images_pending_tasks_button'),
22 21 smush_images_refresh_btn = $('#wpo_smush_images_refresh'),
23 22 smush_images_select_all_btn = $('#wpo_smush_images_select_all'),
24 23 smush_images_select_none_btn = $('#wpo_smush_images_select_none'),
25 24 smush_images_stats_clear_btn = $('#wpo_smush_clear_stats_btn'),
@@ -24,11 +23,8 @@
24 23 smush_images_select_none_btn = $('#wpo_smush_images_select_none'),
25 24 smush_images_stats_clear_btn = $('#wpo_smush_clear_stats_btn'),
26 25 smush_selected_images_btn = $('#wpo_smush_images_btn'),
27 26 smush_mark_as_compressed_btn = $('#wpo_smush_mark_as_compressed'),
28 - smush_single_image_btn = $('.wpo_smush_single_image .button'),
29 - smush_single_restore_btn = $('.wpo_restore_single_image .button'),
30 - convert_to_webp_btn = $('.wpo_smush.column-wpo_smush .convert-to-webp');
31 27 smush_mark_all_as_uncompressed_btn = $('#wpo_smush_mark_all_as_uncompressed_btn'),
32 28 restore_all_compressed_images_btn = $('#wpo_smush_restore_all_compressed_images_btn'),
33 29 smush_view_logs_btn = $('.wpo_smush_get_logs'),
34 30 smush_delete_backup_images_btn = $('#wpo_smush_delete_backup_btn'),
@@ -46,8 +42,22 @@
46 42 smush_affected_images = {},
47 43 pending_tasks_cancel_btn = $('#wpo_smush_images_pending_tasks_cancel_button'),
48 44 uncompressed_images_sites_select = $('#wpo_uncompressed_images_sites_select');
49 45
46 + /**
47 + * These options used for block UI with custom content.
48 + */
49 + var block_ui_custom_options = {
50 + baseZ: 160001,
51 + css: {
52 + top: '50%',
53 + transform: 'translate(0, -50%)',
54 + width: '400px',
55 + padding: '20px',
56 + cursor: 'pointer'
57 + }
58 + }
59 +
50 60 $('#doaction, #doaction2').on('click', function(e) {
51 61 e.stopImmediatePropagation();
52 62 var action = $(this).prev('select').val();
53 63 if ('wp_optimize_bulk_compression' !== action && 'wp_optimize_bulk_restore' !== action ) return;
@@ -64,12 +74,11 @@
64 74 *
65 75 * @param {Object} $selected_images
66 76 */
67 77 function bulk_compression($selected_images) {
68 - $('#smush-information-modal .smush-information').text(wposmush.server_check);
69 - update_view_modal_message($('#smush-information-modal'));
78 + block_ui(wposmush.server_check);
70 79
71 - data = { 'server': wposmush.smush_settings.compression_server };
80 + data = { 'server': wposmush.smush_settings.compression_server, skip_notice: true };
72 81 smush_manager_send_command('check_server_status', data, function(resp) {
73 82 if (resp.online) {
74 83 $selected_images.each(function(index, element) {
75 84 smush_image_list.push({
@@ -91,24 +100,16 @@
91 100 }
92 101 smush_from_media_library = true;
93 102 smush_completed = false;
94 103 if (smush_timer_locked) return;
95 - update_view_modal_message($('#wpo_smush_images_information_container'));
96 -
97 - $('#wpo_smush_images_information_server').html(wposmush.smush_settings.compression_server);
98 -
104 + block_ui('', $.extend(block_ui_custom_options, {message: $('#wpo_smush_images_information_container')}), false, true);
99 105 clear_smush_stats();
100 106
101 107 smush_timer_handle = window.setInterval(smush_timer, 1000);
102 108 smush_manager_send_command('process_bulk_smush', data);
103 109 } else {
104 - if (resp.error) {
105 - error_message = resp.error + '<br>' + wposmush.server_error
106 - $('#smush-information-modal .smush-information').html(error_message);
107 - } else {
108 - $('#smush-information-modal .smush-information').text(wposmush.server_error);
109 - }
110 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
110 + error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error;
111 + block_ui(error_message);
111 112 }
112 113 });
113 114 }
114 115
@@ -140,9 +141,8 @@
140 141
141 142 /**
142 143 * Handle Image Selection
143 144 */
144 -
145 145 smush_images_grid.on('click', '.thumbnail', function (e) {
146 146 $(this).closest('input[type="checkbox"]').prop('checked', true);
147 147 });
148 148
@@ -163,14 +163,13 @@
163 163
164 164 // Resets server rewrite capability
165 165 reset_webp_serving_method_btn.on('click', function(e) {
166 166 e.preventDefault();
167 - smush_manager_send_command('reset_webp_serving_method', {}, function(resp) {
167 + smush_manager_send_command('reset_webp_serving_method', { skip_notice: true }, function(resp) {
168 168 if (!resp.success) {
169 - console.log('[Failed] WebP server capability detection');
170 169 $('#enable_webp_conversion').prop("checked", false);
171 170 $('#smush-information-modal .smush-information').text(resp.error_message);
172 - update_view_modal_message($('#smush-information-modal'))
171 + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}), false, true);
173 172 } else {
174 173 $('#wpo_reset_webp_serving_method_done').show().delay(3000).fadeOut();
175 174 }
176 175 });
@@ -260,28 +259,21 @@
260 259 */
261 260 smush_selected_images_btn.off().on('click', function() {
262 261
263 262 if (0 == $('#wpo_smush_images_grid input[type="checkbox"]:checked').length) {
264 - $('#smush-information-modal .smush-information').text(wposmush.please_select_images)
265 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
263 + block_ui(wposmush.please_select_images);
266 264 return;
267 265 }
268 266
269 - $('#smush-information-modal .smush-information').text(wposmush.server_check);
270 - update_view_modal_message($('#smush-information-modal'));
267 + block_ui(wposmush.server_check);
271 268
272 - data = { 'server': $("input[name='compression_server']:checked").val() };
269 + data = { 'server': $("input[name='compression_server']:checked").val(), skip_notice: true };
273 270 smush_manager_send_command('check_server_status', data, function(resp) {
274 271 if (resp.online) {
275 272 bulk_smush_selected_images();
276 273 } else {
277 - if (resp.error) {
278 - error_message = resp.error + '<br>' + wposmush.server_error
279 - $('#smush-information-modal .smush-information').html(error_message);
280 - } else {
281 - $('#smush-information-modal .smush-information').text(wposmush.server_error);
282 - }
283 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
274 + error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error;
275 + block_ui(error_message);
284 276 }
285 277 });
286 278 });
287 279
@@ -288,13 +280,8 @@
288 280 /**
289 281 * Mark as compressed
290 282 */
291 283 smush_mark_as_compressed_btn.off().on('click', function() {
292 - if (0 == $('#wpo_smush_images_grid input[type="checkbox"]:checked').length) {
293 - $('#smush-information-modal .smush-information').text(wposmush.please_select_compressed_images)
294 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
295 - return;
296 - }
297 284
298 285 var selected_images = [],
299 286 image;
300 287
@@ -305,15 +292,15 @@
305 292 };
306 293 selected_images.push(image);
307 294 });
308 295
309 - update_view_modal_message(wposmush.please_updating_images_info);
296 + block_ui(wposmush.please_updating_images_info);
310 297
311 298 smush_manager_send_command('mark_as_compressed', {selected_images: selected_images}, function(response) {
312 299 $('#smush-information-modal .smush-information').text(response.summary);
313 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
300 + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}));
314 301 // refresh images list.
315 - get_info_from_smush_manager();
302 + get_info_from_smush_manager(false);
316 303 });
317 304 });
318 305
319 306 /**
@@ -323,10 +310,9 @@
323 310 if (!confirm(wposmush.mark_all_images_uncompressed)) return;
324 311
325 312 var restore_backup = confirm(wposmush.restore_images_from_backup);
326 313
327 - update_view_modal_message($('#smush-information-modal-cancel-btn'));
328 - $('#smush-information-modal-cancel-btn .smush-information').text(wposmush.please_wait);
314 + block_ui(wposmush.please_wait);
329 315
330 316 smush_mark_all_as_uncompressed = true;
331 317 run_mark_all_as_uncompressed(restore_backup);
332 318 });
@@ -336,10 +322,9 @@
336 322 */
337 323 restore_all_compressed_images_btn.on('click', function() {
338 324 if (!confirm(wposmush.restore_all_compressed_images)) return;
339 325
340 - update_view_modal_message($('#smush-information-modal-cancel-btn'));
341 - $('#smush-information-modal-cancel-btn .smush-information').text(wposmush.please_wait);
326 + block_ui(wposmush.please_wait);
342 327
343 328 smush_mark_all_as_uncompressed = true;
344 329 run_mark_all_as_uncompressed(true, true);
345 330 });
@@ -368,9 +353,10 @@
368 353 var info = $('#smush-information-modal-cancel-btn .smush-information');
369 354 smush_manager_send_command('mark_all_as_uncompressed',
370 355 {
371 356 restore_backup: restore_backup,
372 - delete_only_backups_meta: delete_only_backups_meta
357 + delete_only_backups_meta: delete_only_backups_meta,
358 + skip_notice: true
373 359 },
374 360 function(resp) {
375 361 // if cancel button pressed then exit
376 362 if (!smush_mark_all_as_uncompressed) return;
@@ -376,10 +362,9 @@
376 362 if (!smush_mark_all_as_uncompressed) return;
377 363
378 364 // if we get an error then show it
379 365 if (resp.hasOwnProperty('error')) {
380 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
381 - $('#smush-information-modal .smush-information').text(resp.error);
366 + block_ui(resp.error);
382 367 get_info_from_smush_manager();
383 368 return;
384 369 }
385 370
@@ -384,9 +369,9 @@
384 369 }
385 370
386 371 // if completed then refresh uncompressed images list and show final message.
387 372 if (resp.completed) {
388 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
373 + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}));
389 374 $('#smush-information-modal .smush-information').text(resp.message);
390 375 get_info_from_smush_manager();
391 376 } else {
392 377 info.text(resp.message);
@@ -476,35 +461,8 @@
476 461 });
477 462 });
478 463
479 464 /**
480 - * Binds pending tasks button
481 - */
482 - smush_images_pending_tasks_btn.off().on('click', function(e) {
483 - $('#smush-information-modal .smush-information').text(wposmush.server_check);
484 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
485 - data = { 'server': $("input[name='compression_server']:checked").val() };
486 - smush_manager_send_command('check_server_status', data, function(resp) {
487 - if (resp.online) {
488 - update_view_bulk_smush_start();
489 - smush_manager_send_command('process_pending_images', {}, function(resp) {
490 - handle_response_from_smush_manager(resp, update_view_bulk_smush_progress);
491 - });
492 - } else {
493 - if (resp.error) {
494 - error_message = resp.error + '<br>' + wposmush.server_error
495 - $('#smush-information-modal .smush-information').html(error_message);
496 - } else {
497 - $('#smush-information-modal .smush-information').text(wposmush.server_error);
498 - }
499 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
500 - }
501 - });
502 -
503 - });
504 -
505 -
506 - /**
507 465 * Binds smush cancel button
508 466 */
509 467 $('body').on('click', '#wpo_smush_images_pending_tasks_cancel_button', function(e) {
510 468
@@ -511,9 +469,9 @@
511 469 if (wposmush.cancel === pending_tasks_cancel_btn.val()) {
512 470 pending_tasks_cancel_btn.val(wposmush.cancelling);
513 471 pending_tasks_cancel_btn.prop('disabled', true);
514 472 }
515 - smush_manager_send_command('clear_pending_images', {}, function(resp) {
473 + smush_manager_send_command('clear_pending_images', { restore_images: smush_image_list }, function(resp) {
516 474 $.unblockUI();
517 475 if (resp.status) {
518 476 get_info_from_smush_manager();
519 477 reset_view_bulk_smush();
@@ -525,8 +483,47 @@
525 483 });
526 484 });
527 485
528 486 /**
487 + * Handle the click on the 'Compress' button within the Media Library page and open a dialog for the image compression action.
488 + */
489 + $('.column-wpo_smush').on('click', '.wpo-smush-compress-popup-btn', function(e) {
490 + var attachment_id = $(this).data('id'),
491 + blog_id = $(this).data('blog');
492 +
493 + e.preventDefault();
494 +
495 + smush_manager_send_command('get_smush_settings_form', {
496 + attachment_id: attachment_id,
497 + blog_id: blog_id
498 + }, function(resp) {
499 + if (resp.success) {
500 + var html = [
501 + '<div class="wpo-smush-compress-popup-content">',
502 + '<h3>',wposmush.compress_image,'</h3>',
503 + resp.html,
504 + '</div>',
505 + '<div class="wpo_smush_single_image action_button" style="display:inline-block;">',
506 + '<input type="button" data-blog="',blog_id,'" data-id="',attachment_id,'" id="smush_compress_',attachment_id,'" class="button-primary button" value="',wposmush.compress,'">',
507 + '</div>',
508 + '&nbsp;&nbsp;',
509 + '<a href="#" class="button wpo-smush-popup-cancel-btn">',wposmush.close,'</a>'
510 + ].join('');
511 + block_ui('', $.extend(block_ui_custom_options, {message: html}));
512 + }
513 + });
514 +
515 + });
516 +
517 + /**
518 + * Handle the 'Cancel' button in the dialog for the image compression action.
519 + */
520 + $('body').on('click', '.wpo-smush-popup-cancel-btn', function(e) {
521 + e.preventDefault();
522 + $.unblockUI();
523 + });
524 +
525 + /**
529 526 * Single image compression
530 527 */
531 528 $('body').on('click', '.wpo_smush_single_image .button', function() {
532 529
@@ -551,20 +548,15 @@
551 548 'preserve_exif': $('#smush_exif_' + image.attachment_id).is(":checked")
552 549 }
553 550
554 551 data = { 'server': $("input[name='compression_server_" + $(this).attr('id').substring(15) + "']:checked").val() };
555 - update_view_modal_message(wposmush.server_check);
552 + block_ui(wposmush.server_check);
556 553 smush_manager_send_command('check_server_status', data, function(resp) {
557 554 if (resp.online) {
558 555 smush_selected_image(image, smush_options);
559 556 } else {
560 - if (resp.error) {
561 - error_message = resp.error + '<br>' + wposmush.server_error
562 -
563 - update_view_modal_message(error_message, $.unblockUI);
564 - } else {
565 - update_view_modal_message(wposmush.server_error, $.unblockUI);
566 - }
557 + error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error;
558 + block_ui(error_message);
567 559 }
568 560 });
569 561 });
570 562
@@ -570,13 +562,15 @@
570 562
571 563 /**
572 564 * Single image restore
573 565 */
574 - $('body').on('click', '.wpo_restore_single_image .button', function() {
566 + $('body').on('click', '.wpo_restore_single_image .button, .wpo_restore_single_image a', function(e) {
575 567 var clicked_image = $(this);
576 568 blog_id = clicked_image.data('blog');
577 569 image_id = clicked_image.data('id');
578 570
571 + e.preventDefault();
572 +
579 573 if (!image_id || !blog_id) return;
580 574 restore_selected_image(blog_id, image_id);
581 575 });
582 576
@@ -582,58 +576,76 @@
582 576
583 577 /**
584 578 * Mark as compressed
585 579 */
586 - $('body').on('click', '.wpo_smush_mark_single_image .button', function() {
587 - var image = {
588 - 'attachment_id':$(this).data('id'),
589 - 'blog_id': $(this).data('blog')
590 - },
591 - wrapper = $(this).closest('#smush-metabox-inside-wrapper');
580 + $('body').on('click', '.wpo_smush_mark_single_image .button, .wpo_smush_mark_single_image a', function(e) {
581 + mark_unmark_as_compressed_handler($(this), e, true);
582 + });
592 583
593 - update_view_modal_message(wposmush.please_updating_images_info);
594 -
595 - smush_manager_send_command('mark_as_compressed', {selected_images: [ image ]}, function(response) {
596 - $('#smush-information-modal .smush-information').text(response.summary);
597 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
598 -
599 - if (response.status) {
600 - $('.wpo_smush_single_image', wrapper).hide();
601 - $('.wpo-toggle-advanced-options', wrapper).removeClass('opened');
602 - $('.wpo_smush_mark_single_image', wrapper).hide();
603 - $('.wpo_smush_unmark_single_image', wrapper).show();
604 - $('.wpo_restore_single_image', wrapper).show();
605 - $('#smush_info', wrapper).text(response.info);
606 - }
607 - });
584 + /**
585 + * Unmark as compressed
586 + */
587 + $('body').on('click', '.wpo_smush_unmark_single_image .button, .wpo_smush_unmark_single_image a', function(e) {
588 + mark_unmark_as_compressed_handler($(this), e, false);
608 589 });
609 590
610 591 /**
611 - * Unmark as uncompressed
592 + * Handler for Mark/Unmark as compressed event.
593 + *
594 + * @param {jQuery} target jQuery object for target element
595 + * @param {Event} e
596 + * @param {Boolean} mark True or false - mark / unmark
612 597 */
613 - $('body').on('click', '.wpo_smush_unmark_single_image .button', function() {
614 - var image = {
615 - 'attachment_id':$(this).data('id'),
616 - 'blog_id': $(this).data('blog')
598 + function mark_unmark_as_compressed_handler(target, e, mark) {
599 + var blog_id = target.data('blog'),
600 + attachment_id = target.data('id'),
601 + image = {
602 + 'attachment_id': attachment_id,
603 + 'blog_id': blog_id
617 604 },
618 - wrapper = $(this).closest('#smush-metabox-inside-wrapper');
605 + wrapper = target.closest('#smush-metabox-inside-wrapper')
606 + data = {selected_images: [ image ]};
619 607
620 - update_view_modal_message(wposmush.please_updating_images_info);
608 + if (mark === false) {
609 + data.unmark = true;
610 + }
611 +
612 + e.preventDefault();
613 +
614 + block_ui(wposmush.please_updating_images_info);
615 +
616 + smush_manager_send_command('mark_as_compressed', data, function(response) {
621 617
622 - smush_manager_send_command('mark_as_compressed', {selected_images: [ image ], unmark: true}, function(response) {
623 618 $('#smush-information-modal .smush-information').text(response.summary);
624 - update_view_modal_message($('#smush-information-modal'), $.unblockUI);
625 619
620 + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}));
621 +
626 622 if (response.status) {
627 - $('.wpo_smush_single_image', wrapper).show();
628 - $('.wpo_smush_mark_single_image', wrapper).show();
629 - $('.wpo_smush_unmark_single_image', wrapper).hide();
630 - $('.wpo_restore_single_image', wrapper).hide();
631 - $('#smush_info', wrapper).text('');
623 + if (wrapper.length) {
624 + if (mark) {
625 + $('.wpo_smush_single_image', wrapper).hide();
626 + $('.wpo-toggle-advanced-options', wrapper).removeClass('opened');
627 + $('.wpo_smush_mark_single_image', wrapper).hide();
628 + $('.wpo_smush_unmark_single_image', wrapper).show();
629 + $('.wpo_restore_single_image', wrapper).show();
630 + $('#smush_info', wrapper).text(response.info);
631 + } else {
632 + $('.wpo_smush_single_image', wrapper).show();
633 + $('.wpo_smush_mark_single_image', wrapper).show();
634 + $('.wpo_smush_unmark_single_image', wrapper).hide();
635 + $('.wpo_restore_single_image', wrapper).hide();
636 + $('#smush_info', wrapper).text(response.info);
637 + }
638 + }
639 +
640 + if (response.hasOwnProperty('media_column_html')) {
641 + update_smush_information_media_library_column(blog_id, attachment_id, response.media_column_html);
642 + }
632 643 }
633 644 });
634 - });
645 + }
635 646
647 +
636 648 $('body').on('click', '#wpo_smush_details .wpo-collapsible', toggle_smush_details);
637 649 $('body').on('click', '.column-wpo_smush .wpo-collapsible', toggle_smush_details);
638 650
639 651 function toggle_smush_details() {
@@ -698,8 +710,10 @@
698 710 $('#wpo_smush_images_save_options_spinner').show().delay(3000).fadeOut();
699 711 $('#enable_webp_conversion').prop("disabled", true);
700 712
701 713 var smush_options = get_smush_options();
714 + // Skip default notice and use popup to show error messages
715 + smush_options.skip_notice = true;
702 716
703 717 smush_manager_send_command('update_webp_options', smush_options, function(resp) {
704 718 $('#wpo_smush_images_save_options_spinner').hide();
705 719 if (resp.hasOwnProperty('saved') && resp.saved) {
@@ -708,9 +722,9 @@
708 722 $('#enable_webp_conversion').prop("checked", false);
709 723 if ('update_failed_no_working_webp_converter' === resp.error_code) {
710 724 var html_msg = '<p>'
711 725 + wposmush.webp_conversion_tool_error
712 - + ' <a href="https://getwpo.com/faqs/#How-can-I-get-WebP-conversion-tools-to-work-" target="_blank">'
726 + + ' <a href="https://teamupdraft.com/documentation/wp-optimize/topics/image-compression/troubleshooting/the-webp-conversion-tool-is-not-working/" target="_blank">'
713 727 + wposmush.webp_conversion_tool_how_to
714 728 + '</a></p>';
715 729 $('#smush-information-modal .smush-information').html(html_msg);
716 730 } else {
@@ -715,34 +729,30 @@
715 729 $('#smush-information-modal .smush-information').html(html_msg);
716 730 } else {
717 731 $('#smush-information-modal .smush-information').text(resp.error_message);
718 732 }
719 - update_view_modal_message($('#smush-information-modal'));
733 + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}), false, true);
720 734 }
721 735 $('#enable_webp_conversion').prop("disabled", false);
722 736 });
723 737 });
724 738
725 - convert_to_webp_btn.on('click', function(e){
739 + $('body').on('click', '.wpo_smush.column-wpo_smush .convert-to-webp', function(e){
726 740 e.preventDefault();
727 741 var $link = $(this);
728 742 data = {
729 - 'attachment_id': $(this).data('attachment-id')
743 + 'attachment_id': $(this).data('attachment-id'),
744 + skip_notice: true
730 745 };
731 - update_view_modal_message(wposmush.converting_to_webp);
746 + block_ui(wposmush.converting_to_webp, {}, 0, true);
732 747 smush_manager_send_command('convert_to_webp_format', data, function(response) {
733 - if (response.error) {
734 - update_view_modal_message(response.error, $.unblockUI);
735 - setTimeout(function() {
736 - $.unblockUI();
737 - }, 2000);
738 - } else {
739 - update_view_modal_message(response.success, $.unblockUI);
740 - setTimeout(function() {
741 - $.unblockUI();
742 - }, 2000);
748 + if (response.success) {
749 + block_ui(response.success, {}, 2000);
743 750 $link.next().remove();
751 + if ($link.prev().is('.wpo-action-separator')) $link.prev().remove();
744 752 $link.remove();
753 + } else {
754 + block_ui(response.error || response.error_message || wposmush.server_error , {}, 2000);
745 755 }
746 756 });
747 757 });
748 758
@@ -758,9 +768,8 @@
758 768 var use_cache = (typeof use_cache === 'undefined') ? true : use_cache;
759 769 var data = { 'use_cache': use_cache };
760 770
761 771 smush_images_optimization_message.html('...');
762 - smush_images_pending_tasks_container.hide();
763 772
764 773 disable_image_optimization_controls(true);
765 774
766 775 heartbeat_agents.push(heartbeat.add_agent({
@@ -917,9 +926,9 @@
917 926
918 927 /**
919 928 * Update images optimization tab view with data returned from images optimization.
920 929 *
921 - * @param {Object} data - meta data returned from task manager
930 + * @param {Object} data - metadata returned from task manager
922 931 *
923 932 * @return void
924 933 */
925 934 function update_view_show_uncompressed_images(data) {
@@ -934,12 +943,8 @@
934 943 if (0 == unsmushed_images.length && 0 == pending_tasks) {
935 944 smush_images_grid.text(wposmush.all_images_compressed).wrapInner("<div class='wpo-fieldgroup'> </div>");
936 945 }
937 946
938 - if (0 != pending_tasks) {
939 - smush_images_pending_tasks_container.show().find('.red').text(data.pending);
940 - }
941 -
942 947 if (1 === data.is_multisite) {
943 948 blog_id = uncompressed_images_sites_select.find(":selected").val();
944 949 add_unsmushed_images_to_grid(blog_id, data);
945 950 } else {
@@ -981,12 +986,10 @@
981 986 * @return void
982 987 */
983 988 function update_view_bulk_smush_start() {
984 989 if (smush_timer_locked) return;
985 - update_view_modal_message($('#wpo_smush_images_information_container'));
986 990
987 - service = $('.compression_server input[type="radio"]:checked + label small').text();
988 - $('#wpo_smush_images_information_server').html(service);
991 + block_ui('', $.extend(block_ui_custom_options, {message: $('#wpo_smush_images_information_container')}), false, true);
989 992
990 993 clear_smush_stats();
991 994
992 995 smush_timer_handle = window.setInterval(smush_timer, 1000);
@@ -1061,8 +1064,11 @@
1061 1064 };
1062 1065
1063 1066 (function(single_callback) {
1064 1067 heartbeat_agents.push(heartbeat.add_agent({
1068 + // We have to use a non-unique agent in the complete action because the info update subactions
1069 + // have the same names, and the agent may not be added. This ensures that the "smush_complete" action will run.
1070 + _unique: false,
1065 1071 _wait: false,
1066 1072 _keep: false,
1067 1073 command: 'updraft_smush_ajax',
1068 1074 command_data: {data: data, subaction: 'get_ui_update'},
@@ -1068,8 +1074,12 @@
1068 1074 command_data: {data: data, subaction: 'get_ui_update'},
1069 1075 callback: function(resp) {
1070 1076
1071 1077 summary = resp.session_stats;
1078 + // Prevent pops with undefined headers
1079 + if (!resp.session_stats) {
1080 + return;
1081 + }
1072 1082
1073 1083 if (0 != resp.completed_task_count) {
1074 1084 summary += '<hr>' + resp.summary;
1075 1085 }
@@ -1096,9 +1106,9 @@
1096 1106 if (smush_completed) return;
1097 1107
1098 1108 $('#summary-message').html(summary);
1099 1109 reset_view_bulk_smush();
1100 - update_view_modal_message($('#smush-complete-summary'));
1110 + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-complete-summary')}));
1101 1111 smush_completed = true;
1102 1112
1103 1113 heartbeat.cancel_agents(heartbeat_agents);
1104 1114 }
@@ -1181,9 +1191,8 @@
1181 1191 smush_selected_images_btn,
1182 1192 smush_images_select_all_btn,
1183 1193 smush_images_select_none_btn,
1184 1194 smush_images_refresh_btn,
1185 - smush_images_pending_tasks_btn,
1186 1195 smush_mark_as_compressed_btn,
1187 1196 ], function(i, el) {
1188 1197 el.prop('disabled', disable);
1189 1198 });
@@ -1211,12 +1220,13 @@
1211 1220 if (0 == selected_image.length) return;
1212 1221
1213 1222 data = {
1214 1223 selected_image: selected_image,
1215 - smush_options: smush_options
1224 + smush_options: smush_options,
1225 + skip_notice: true
1216 1226 }
1217 1227
1218 - update_view_modal_message(wposmush.compress_single_image_dialog);
1228 + block_ui(wposmush.compress_single_image_dialog, {}, false, true);
1219 1229 smush_manager_send_command('compress_single_image', data, function(resp) {
1220 1230 handle_response_from_smush_manager(resp, update_view_single_image_complete);
1221 1231 });
1222 1232 }
@@ -1234,10 +1244,10 @@
1234 1244
1235 1245 // if no selected images then exit.
1236 1246 if (0 == selected_image.length) return;
1237 1247
1238 - update_view_modal_message(wposmush.please_wait, $.unblockUI);
1239 - var data = { 'blog_id': blog_id, 'selected_image': selected_image };
1248 + block_ui(wposmush.please_wait);
1249 + var data = { 'blog_id': blog_id, 'selected_image': selected_image, skip_notice: true };
1240 1250
1241 1251 smush_manager_send_command.apply({unique: false}, ['restore_single_image', data, function(resp) {
1242 1252 var done = function(resp_summary_alt) {
1243 1253 if ('undefined' != typeof(resp_summary_alt)) {
@@ -1265,9 +1275,9 @@
1265 1275 function update_view_single_image_complete(resp) {
1266 1276
1267 1277 if (resp.hasOwnProperty('success') && resp.success) {
1268 1278 $(".smush-information").text(resp.summary);
1269 - update_view_modal_message($("#smush-information-modal"), $.unblockUI);
1279 + block_ui('', $.extend(block_ui_custom_options, {message: $("#smush-information-modal")}));
1270 1280
1271 1281 $('.wpo-toggle-advanced-options.wpo_smush_single_image').removeClass('opened');
1272 1282
1273 1283 update_view_singe_image_compress(resp.operation, resp.summary, resp.restore_possible, resp);
@@ -1280,8 +1290,12 @@
1280 1290
1281 1291 if (!smush_affected_images.hasOwnProperty(blog_id)) smush_affected_images[blog_id] = {};
1282 1292 if (!smush_affected_images[blog_id].hasOwnProperty(image_id)) smush_affected_images[blog_id][image_id] = {};
1283 1293
1294 + if (resp.hasOwnProperty('media_column_html')) {
1295 + update_smush_information_media_library_column(blog_id, image_id, resp.media_column_html);
1296 + }
1297 +
1284 1298 if ('compress' == resp.operation) {
1285 1299 smush_affected_images[blog_id][image_id] = {
1286 1300 operation: resp.operation,
1287 1301 summary: resp.summary,
@@ -1288,9 +1302,8 @@
1288 1302 restore_possible: resp.restore_possible
1289 1303 }
1290 1304 } else {
1291 1305 if (image_id) {
1292 - update_media_library_wp_optimize_column(image_id);
1293 1306 reset_bulk_actions_dropdown();
1294 1307 }
1295 1308 smush_affected_images[blog_id][image_id] = {
1296 1309 operation: resp.operation
@@ -1296,14 +1309,26 @@
1296 1309 operation: resp.operation
1297 1310 }
1298 1311 }
1299 1312 } else {
1300 - $(".smush-information").text(resp.error_message);
1301 - update_view_modal_message($("#smush-information-modal"), $.unblockUI);
1313 + // Update the error message to be user-friendly
1314 + if (/exceeded_max_filesize/.test(resp.error_message)) resp.error_message = wposmush.exceeded_max_filesize
1315 + block_ui(resp.error_message, {}, 4000);
1302 1316 }
1303 1317 }
1304 1318
1305 1319 /**
1320 + * Update information for the WP-Optimize column in the Media Library.
1321 + *
1322 + * @param {number} blog_id
1323 + * @param {number} image_id
1324 + * @param {string} html
1325 + */
1326 + function update_smush_information_media_library_column(blog_id, image_id, html) {
1327 + $(['.wpo-smush-media-library-column[data-blog="',blog_id,'"][data-id="',image_id,'"]'].join('')).parent().html(html);
1328 + }
1329 +
1330 + /**
1306 1331 * Update metabox view depending on a command response.
1307 1332 *
1308 1333 * @param {string} operation
1309 1334 * @param {string} summary
@@ -1312,8 +1337,10 @@
1312 1337 */
1313 1338 function update_view_singe_image_compress(operation, summary, restore_possible, smush_image_data) {
1314 1339 var wrapper = $("#smush_info").closest('#smush-metabox-inside-wrapper');
1315 1340
1341 + if (0 === wrapper.length) return;
1342 +
1316 1343 if ('compress' == operation) {
1317 1344 $(".wpo_smush_single_image").hide();
1318 1345 $(".wpo_restore_single_image").show();
1319 1346
@@ -1360,30 +1387,8 @@
1360 1387 }
1361 1388 });
1362 1389
1363 1390 /**
1364 - * Display a modal message
1365 - *
1366 - * @param {string} message The message or element to display
1367 - * @param {Function} callback Called when the overlay is clicked
1368 - *
1369 - * @return void
1370 - */
1371 - function update_view_modal_message(message, callback) {
1372 -
1373 - $.blockUI({
1374 - message: message,
1375 - onOverlayClick: callback,
1376 - baseZ: 160001,
1377 - css: {
1378 - width: '400px',
1379 - padding: '20px',
1380 - cursor: 'pointer'
1381 - }
1382 - });
1383 - }
1384 -
1385 - /**
1386 1391 * Check returned response from the smush manager and call update view callback.
1387 1392 *
1388 1393 * @param {Object} resp - response from smush manager.
1389 1394 * @param {Function} update_view_callback - callback function to update view.
@@ -1422,20 +1427,8 @@
1422 1427 return selected_images_list;
1423 1428 }
1424 1429
1425 1430 /**
1426 - * Updates the media library WP Optimize column for a specified image.
1427 - *
1428 - * @param {number} image_id - The ID of the image to update the column for.
1429 - */
1430 - function update_media_library_wp_optimize_column(image_id) {
1431 - var admin_url = ajaxurl.replace('/admin-ajax.php', '/');
1432 - admin_url += 'post.php?post=' + image_id + '&action=edit';
1433 - $('#post-' + image_id + ' .column-wpo_smush').html('<a href="' + admin_url + '">' + wposmush.compress + '</a><br>');
1434 - }
1435 -
1436 -
1437 - /**
1438 1431 * Resets the bulk action dropdown by setting its value to "-1" and unchecking all the selected checkboxes.
1439 1432 *
1440 1433 * @returns {void}
1441 1434 */
@@ -1450,19 +1443,27 @@
1450 1443 * @param {string} action The action to send
1451 1444 * @param {[type]} data Data to send
1452 1445 * @param {Function} callback Will be called with the results
1453 1446 * @param {boolean} json_parse JSON parse the results
1447 + * @param {boolean} wait Wait for the response
1454 1448 *
1455 1449 * @return {JSON}
1456 1450 */
1457 - function smush_manager_send_command(action, data, callback, json_parse) {
1451 + function smush_manager_send_command(action, data, callback, json_parse, wait) {
1458 1452
1459 1453 json_parse = ('undefined' === typeof json_parse) ? true : json_parse;
1460 - data = $.isEmptyObject(data) ? {'use_cache' : false} : data;
1454 + var _wait = ('undefined' === typeof wait) ? false : wait;
1461 1455
1456 + var skip_notice = data.hasOwnProperty('skip_notice');
1457 + var data_has_only_skip_notice = skip_notice && 1 === Object.keys(data).length;
1458 + var is_data_empty = $.isEmptyObject(data);
1459 +
1460 + data = is_data_empty ? {'use_cache' : false} : data;
1461 + if (data_has_only_skip_notice) data.use_cache = false;
1462 +
1462 1463 (function(single_callback, _keep, _unique) {
1463 1464 heartbeat_agents.push(heartbeat.add_agent({
1464 - _wait: false,
1465 + _wait: _wait,
1465 1466 _keep: _keep,
1466 1467 _unique: _unique,
1467 1468 command: 'updraft_smush_ajax',
1468 1469 command_data: {data: data, subaction: action},
@@ -1514,78 +1515,4 @@
1514 1515 }
1515 1516 wp_optimize.smush_settings = get_smush_options;
1516 1517 } // END WP_Optimize_Smush
1517 1518
1518 -/**
1519 - * Parse JSON string, including automatically detecting unwanted extra input and skipping it
1520 - *
1521 - * @param {string|object} json_mix_str - JSON string which need to parse and convert to object
1522 - *
1523 - * @throws SyntaxError|String (including passing on what JSON.parse may throw) if a parsing error occurs.
1524 - *
1525 - * @return mixed parsed JSON object. Will only return if parsing is successful (otherwise, will throw)
1526 - */
1527 -function wpo_parse_json(json_mix_str) {
1528 - // When using wp_send_json to return the value, the format is already parsed.
1529 - if ('object' === typeof json_mix_str) return json_mix_str;
1530 -
1531 - // Just try it - i.e. the 'default' case where things work (which can include extra whitespace/line-feeds, and simple strings, etc.).
1532 - try {
1533 - var result = JSON.parse(json_mix_str);
1534 - return result;
1535 - } catch (e) {
1536 - console.log("WPO: Exception when trying to parse JSON (1) - will attempt to fix/re-parse");
1537 - console.log(json_mix_str);
1538 - }
1539 -
1540 - var json_start_pos = json_mix_str.indexOf('{');
1541 - var json_last_pos = json_mix_str.lastIndexOf('}');
1542 -
1543 - // Case where some php notice may be added after or before json string
1544 - if (json_start_pos > -1 && json_last_pos > -1) {
1545 - var json_str = json_mix_str.slice(json_start_pos, json_last_pos + 1);
1546 - try {
1547 - var parsed = JSON.parse(json_str);
1548 - return parsed;
1549 - } catch (e) {
1550 - console.log("WPO: Exception when trying to parse JSON (2) - will attempt to fix/re-parse based upon bracket counting");
1551 -
1552 - var cursor = json_start_pos;
1553 - var open_count = 0;
1554 - var last_character = '';
1555 - var inside_string = false;
1556 -
1557 - // Don't mistake this for a real JSON parser. Its aim is to improve the odds in real-world cases seen, not to arrive at universal perfection.
1558 - while ((open_count > 0 || cursor == json_start_pos) && cursor <= json_last_pos) {
1559 -
1560 - var current_character = json_mix_str.charAt(cursor);
1561 -
1562 - if (!inside_string && '{' == current_character) {
1563 - open_count++;
1564 - } else if (!inside_string && '}' == current_character) {
1565 - open_count--;
1566 - } else if ('"' == current_character && '\\' != last_character) {
1567 - inside_string = inside_string ? false : true;
1568 - }
1569 -
1570 - last_character = current_character;
1571 - cursor++;
1572 - }
1573 -
1574 - console.log("Started at cursor="+json_start_pos+", ended at cursor="+cursor+" with result following:");
1575 - console.log(json_mix_str.substring(json_start_pos, cursor));
1576 -
1577 - try {
1578 - var parsed = JSON.parse(json_mix_str.substring(json_start_pos, cursor));
1579 - // console.log('WPO: JSON re-parse successful');
1580 - return parsed;
1581 - } catch (e) {
1582 - // Throw it again, so that our function works just like JSON.parse() in its behaviour.
1583 - throw e;
1584 - }
1585 -
1586 - }
1587 - }
1588 -
1589 - throw "WPO: could not parse the JSON";
1590 -
1591 -}