| @@ -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 | }); |
| @@ -176,13 +175,21 @@ | ||
| 176 | 175 | }); |
| 177 | 176 | }); |
| 178 | 177 | |
| 179 | 178 | /** |
| 179 | + * Boolean flag to track whether uncompressed images have already been loaded, | |
| 180 | + * preventing multiple unnecessary loads | |
| 181 | + */ | |
| 182 | + var uncompressed_images_already_loaded = false; | |
| 183 | + | |
| 184 | + /** | |
| 180 | 185 | * Checks if smush is active and loads images if yes - image tabs change. |
| 181 | 186 | */ |
| 182 | 187 | $('#wp-optimize-nav-tab-wrapper__wpo_images .nav-tab').on('click', function() { |
| 188 | + if (uncompressed_images_already_loaded) return; | |
| 189 | + | |
| 183 | 190 | if ($(this).is('#wp-optimize-nav-tab-wpo_images-smush')) { |
| 184 | - get_info_from_smush_manager(); | |
| 191 | + get_info_from_smush_manager(false); | |
| 185 | 192 | } |
| 186 | 193 | }); |
| 187 | 194 | |
| 188 | 195 | /** |
| @@ -188,11 +195,13 @@ | ||
| 188 | 195 | /** |
| 189 | 196 | * Checks if smush is active and loads images if yes - main menu change. |
| 190 | 197 | */ |
| 191 | 198 | $('#wp-optimize-wrap').on('page-change', function(e, params) { |
| 199 | + if (uncompressed_images_already_loaded) return; | |
| 200 | + | |
| 192 | 201 | if ('wpo_images' == params.page) { |
| 193 | 202 | if ($('#wp-optimize-nav-tab-wrapper__wpo_images .nav-tab-active').is('#wp-optimize-nav-tab-wpo_images-smush')) { |
| 194 | - get_info_from_smush_manager(); | |
| 203 | + get_info_from_smush_manager(false); | |
| 195 | 204 | } |
| 196 | 205 | } |
| 197 | 206 | }); |
| 198 | 207 | |
| @@ -250,28 +259,21 @@ | ||
| 250 | 259 | */ |
| 251 | 260 | smush_selected_images_btn.off().on('click', function() { |
| 252 | 261 | |
| 253 | 262 | if (0 == $('#wpo_smush_images_grid input[type="checkbox"]:checked').length) { |
| 254 | - $('#smush-information-modal .smush-information').text(wposmush.please_select_images) | |
| 255 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 263 | + block_ui(wposmush.please_select_images); | |
| 256 | 264 | return; |
| 257 | 265 | } |
| 258 | 266 | |
| 259 | - $('#smush-information-modal .smush-information').text(wposmush.server_check); | |
| 260 | - update_view_modal_message($('#smush-information-modal')); | |
| 267 | + block_ui(wposmush.server_check); | |
| 261 | 268 | |
| 262 | - data = { 'server': $("input[name='compression_server']:checked").val() }; | |
| 269 | + data = { 'server': $("input[name='compression_server']:checked").val(), skip_notice: true }; | |
| 263 | 270 | smush_manager_send_command('check_server_status', data, function(resp) { |
| 264 | 271 | if (resp.online) { |
| 265 | 272 | bulk_smush_selected_images(); |
| 266 | 273 | } else { |
| 267 | - if (resp.error) { | |
| 268 | - error_message = resp.error + '<br>' + wposmush.server_error | |
| 269 | - $('#smush-information-modal .smush-information').html(error_message); | |
| 270 | - } else { | |
| 271 | - $('#smush-information-modal .smush-information').text(wposmush.server_error); | |
| 272 | - } | |
| 273 | - 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); | |
| 274 | 276 | } |
| 275 | 277 | }); |
| 276 | 278 | }); |
| 277 | 279 | |
| @@ -278,13 +280,8 @@ | ||
| 278 | 280 | /** |
| 279 | 281 | * Mark as compressed |
| 280 | 282 | */ |
| 281 | 283 | smush_mark_as_compressed_btn.off().on('click', function() { |
| 282 | - if (0 == $('#wpo_smush_images_grid input[type="checkbox"]:checked').length) { | |
| 283 | - $('#smush-information-modal .smush-information').text(wposmush.please_select_compressed_images) | |
| 284 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 285 | - return; | |
| 286 | - } | |
| 287 | 284 | |
| 288 | 285 | var selected_images = [], |
| 289 | 286 | image; |
| 290 | 287 | |
| @@ -295,15 +292,15 @@ | ||
| 295 | 292 | }; |
| 296 | 293 | selected_images.push(image); |
| 297 | 294 | }); |
| 298 | 295 | |
| 299 | - update_view_modal_message(wposmush.please_updating_images_info); | |
| 296 | + block_ui(wposmush.please_updating_images_info); | |
| 300 | 297 | |
| 301 | 298 | smush_manager_send_command('mark_as_compressed', {selected_images: selected_images}, function(response) { |
| 302 | 299 | $('#smush-information-modal .smush-information').text(response.summary); |
| 303 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 300 | + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')})); | |
| 304 | 301 | // refresh images list. |
| 305 | - get_info_from_smush_manager(); | |
| 302 | + get_info_from_smush_manager(false); | |
| 306 | 303 | }); |
| 307 | 304 | }); |
| 308 | 305 | |
| 309 | 306 | /** |
| @@ -313,10 +310,9 @@ | ||
| 313 | 310 | if (!confirm(wposmush.mark_all_images_uncompressed)) return; |
| 314 | 311 | |
| 315 | 312 | var restore_backup = confirm(wposmush.restore_images_from_backup); |
| 316 | 313 | |
| 317 | - update_view_modal_message($('#smush-information-modal-cancel-btn')); | |
| 318 | - $('#smush-information-modal-cancel-btn .smush-information').text(wposmush.please_wait); | |
| 314 | + block_ui(wposmush.please_wait); | |
| 319 | 315 | |
| 320 | 316 | smush_mark_all_as_uncompressed = true; |
| 321 | 317 | run_mark_all_as_uncompressed(restore_backup); |
| 322 | 318 | }); |
| @@ -326,10 +322,9 @@ | ||
| 326 | 322 | */ |
| 327 | 323 | restore_all_compressed_images_btn.on('click', function() { |
| 328 | 324 | if (!confirm(wposmush.restore_all_compressed_images)) return; |
| 329 | 325 | |
| 330 | - update_view_modal_message($('#smush-information-modal-cancel-btn')); | |
| 331 | - $('#smush-information-modal-cancel-btn .smush-information').text(wposmush.please_wait); | |
| 326 | + block_ui(wposmush.please_wait); | |
| 332 | 327 | |
| 333 | 328 | smush_mark_all_as_uncompressed = true; |
| 334 | 329 | run_mark_all_as_uncompressed(true, true); |
| 335 | 330 | }); |
| @@ -358,9 +353,10 @@ | ||
| 358 | 353 | var info = $('#smush-information-modal-cancel-btn .smush-information'); |
| 359 | 354 | smush_manager_send_command('mark_all_as_uncompressed', |
| 360 | 355 | { |
| 361 | 356 | restore_backup: restore_backup, |
| 362 | - delete_only_backups_meta: delete_only_backups_meta | |
| 357 | + delete_only_backups_meta: delete_only_backups_meta, | |
| 358 | + skip_notice: true | |
| 363 | 359 | }, |
| 364 | 360 | function(resp) { |
| 365 | 361 | // if cancel button pressed then exit |
| 366 | 362 | if (!smush_mark_all_as_uncompressed) return; |
| @@ -366,10 +362,9 @@ | ||
| 366 | 362 | if (!smush_mark_all_as_uncompressed) return; |
| 367 | 363 | |
| 368 | 364 | // if we get an error then show it |
| 369 | 365 | if (resp.hasOwnProperty('error')) { |
| 370 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 371 | - $('#smush-information-modal .smush-information').text(resp.error); | |
| 366 | + block_ui(resp.error); | |
| 372 | 367 | get_info_from_smush_manager(); |
| 373 | 368 | return; |
| 374 | 369 | } |
| 375 | 370 | |
| @@ -374,9 +369,9 @@ | ||
| 374 | 369 | } |
| 375 | 370 | |
| 376 | 371 | // if completed then refresh uncompressed images list and show final message. |
| 377 | 372 | if (resp.completed) { |
| 378 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 373 | + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')})); | |
| 379 | 374 | $('#smush-information-modal .smush-information').text(resp.message); |
| 380 | 375 | get_info_from_smush_manager(); |
| 381 | 376 | } else { |
| 382 | 377 | info.text(resp.message); |
| @@ -466,35 +461,8 @@ | ||
| 466 | 461 | }); |
| 467 | 462 | }); |
| 468 | 463 | |
| 469 | 464 | /** |
| 470 | - * Binds pending tasks button | |
| 471 | - */ | |
| 472 | - smush_images_pending_tasks_btn.off().on('click', function(e) { | |
| 473 | - $('#smush-information-modal .smush-information').text(wposmush.server_check); | |
| 474 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 475 | - data = { 'server': $("input[name='compression_server']:checked").val() }; | |
| 476 | - smush_manager_send_command('check_server_status', data, function(resp) { | |
| 477 | - if (resp.online) { | |
| 478 | - update_view_bulk_smush_start(); | |
| 479 | - smush_manager_send_command('process_pending_images', {}, function(resp) { | |
| 480 | - handle_response_from_smush_manager(resp, update_view_bulk_smush_progress); | |
| 481 | - }); | |
| 482 | - } else { | |
| 483 | - if (resp.error) { | |
| 484 | - error_message = resp.error + '<br>' + wposmush.server_error | |
| 485 | - $('#smush-information-modal .smush-information').html(error_message); | |
| 486 | - } else { | |
| 487 | - $('#smush-information-modal .smush-information').text(wposmush.server_error); | |
| 488 | - } | |
| 489 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 490 | - } | |
| 491 | - }); | |
| 492 | - | |
| 493 | - }); | |
| 494 | - | |
| 495 | - | |
| 496 | - /** | |
| 497 | 465 | * Binds smush cancel button |
| 498 | 466 | */ |
| 499 | 467 | $('body').on('click', '#wpo_smush_images_pending_tasks_cancel_button', function(e) { |
| 500 | 468 | |
| @@ -501,9 +469,9 @@ | ||
| 501 | 469 | if (wposmush.cancel === pending_tasks_cancel_btn.val()) { |
| 502 | 470 | pending_tasks_cancel_btn.val(wposmush.cancelling); |
| 503 | 471 | pending_tasks_cancel_btn.prop('disabled', true); |
| 504 | 472 | } |
| 505 | - smush_manager_send_command('clear_pending_images', {}, function(resp) { | |
| 473 | + smush_manager_send_command('clear_pending_images', { restore_images: smush_image_list }, function(resp) { | |
| 506 | 474 | $.unblockUI(); |
| 507 | 475 | if (resp.status) { |
| 508 | 476 | get_info_from_smush_manager(); |
| 509 | 477 | reset_view_bulk_smush(); |
| @@ -515,8 +483,47 @@ | ||
| 515 | 483 | }); |
| 516 | 484 | }); |
| 517 | 485 | |
| 518 | 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 | + ' ', | |
| 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 | + /** | |
| 519 | 526 | * Single image compression |
| 520 | 527 | */ |
| 521 | 528 | $('body').on('click', '.wpo_smush_single_image .button', function() { |
| 522 | 529 | |
| @@ -541,20 +548,15 @@ | ||
| 541 | 548 | 'preserve_exif': $('#smush_exif_' + image.attachment_id).is(":checked") |
| 542 | 549 | } |
| 543 | 550 | |
| 544 | 551 | data = { 'server': $("input[name='compression_server_" + $(this).attr('id').substring(15) + "']:checked").val() }; |
| 545 | - update_view_modal_message(wposmush.server_check); | |
| 552 | + block_ui(wposmush.server_check); | |
| 546 | 553 | smush_manager_send_command('check_server_status', data, function(resp) { |
| 547 | 554 | if (resp.online) { |
| 548 | 555 | smush_selected_image(image, smush_options); |
| 549 | 556 | } else { |
| 550 | - if (resp.error) { | |
| 551 | - error_message = resp.error + '<br>' + wposmush.server_error | |
| 552 | - | |
| 553 | - update_view_modal_message(error_message, $.unblockUI); | |
| 554 | - } else { | |
| 555 | - update_view_modal_message(wposmush.server_error, $.unblockUI); | |
| 556 | - } | |
| 557 | + error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error; | |
| 558 | + block_ui(error_message); | |
| 557 | 559 | } |
| 558 | 560 | }); |
| 559 | 561 | }); |
| 560 | 562 | |
| @@ -560,13 +562,15 @@ | ||
| 560 | 562 | |
| 561 | 563 | /** |
| 562 | 564 | * Single image restore |
| 563 | 565 | */ |
| 564 | - $('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) { | |
| 565 | 567 | var clicked_image = $(this); |
| 566 | 568 | blog_id = clicked_image.data('blog'); |
| 567 | 569 | image_id = clicked_image.data('id'); |
| 568 | 570 | |
| 571 | + e.preventDefault(); | |
| 572 | + | |
| 569 | 573 | if (!image_id || !blog_id) return; |
| 570 | 574 | restore_selected_image(blog_id, image_id); |
| 571 | 575 | }); |
| 572 | 576 | |
| @@ -572,58 +576,76 @@ | ||
| 572 | 576 | |
| 573 | 577 | /** |
| 574 | 578 | * Mark as compressed |
| 575 | 579 | */ |
| 576 | - $('body').on('click', '.wpo_smush_mark_single_image .button', function() { | |
| 577 | - var image = { | |
| 578 | - 'attachment_id':$(this).data('id'), | |
| 579 | - 'blog_id': $(this).data('blog') | |
| 580 | - }, | |
| 581 | - 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 | + }); | |
| 582 | 583 | |
| 583 | - update_view_modal_message(wposmush.please_updating_images_info); | |
| 584 | - | |
| 585 | - smush_manager_send_command('mark_as_compressed', {selected_images: [ image ]}, function(response) { | |
| 586 | - $('#smush-information-modal .smush-information').text(response.summary); | |
| 587 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 588 | - | |
| 589 | - if (response.status) { | |
| 590 | - $('.wpo_smush_single_image', wrapper).hide(); | |
| 591 | - $('.wpo-toggle-advanced-options', wrapper).removeClass('opened'); | |
| 592 | - $('.wpo_smush_mark_single_image', wrapper).hide(); | |
| 593 | - $('.wpo_smush_unmark_single_image', wrapper).show(); | |
| 594 | - $('.wpo_restore_single_image', wrapper).show(); | |
| 595 | - $('#smush_info', wrapper).text(response.info); | |
| 596 | - } | |
| 597 | - }); | |
| 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); | |
| 598 | 589 | }); |
| 599 | 590 | |
| 600 | 591 | /** |
| 601 | - * 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 | |
| 602 | 597 | */ |
| 603 | - $('body').on('click', '.wpo_smush_unmark_single_image .button', function() { | |
| 604 | - var image = { | |
| 605 | - 'attachment_id':$(this).data('id'), | |
| 606 | - '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 | |
| 607 | 604 | }, |
| 608 | - wrapper = $(this).closest('#smush-metabox-inside-wrapper'); | |
| 605 | + wrapper = target.closest('#smush-metabox-inside-wrapper') | |
| 606 | + data = {selected_images: [ image ]}; | |
| 609 | 607 | |
| 610 | - 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) { | |
| 611 | 617 | |
| 612 | - smush_manager_send_command('mark_as_compressed', {selected_images: [ image ], unmark: true}, function(response) { | |
| 613 | 618 | $('#smush-information-modal .smush-information').text(response.summary); |
| 614 | - update_view_modal_message($('#smush-information-modal'), $.unblockUI); | |
| 615 | 619 | |
| 620 | + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')})); | |
| 621 | + | |
| 616 | 622 | if (response.status) { |
| 617 | - $('.wpo_smush_single_image', wrapper).show(); | |
| 618 | - $('.wpo_smush_mark_single_image', wrapper).show(); | |
| 619 | - $('.wpo_smush_unmark_single_image', wrapper).hide(); | |
| 620 | - $('.wpo_restore_single_image', wrapper).hide(); | |
| 621 | - $('#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 | + } | |
| 622 | 643 | } |
| 623 | 644 | }); |
| 624 | - }); | |
| 645 | + } | |
| 625 | 646 | |
| 647 | + | |
| 626 | 648 | $('body').on('click', '#wpo_smush_details .wpo-collapsible', toggle_smush_details); |
| 627 | 649 | $('body').on('click', '.column-wpo_smush .wpo-collapsible', toggle_smush_details); |
| 628 | 650 | |
| 629 | 651 | function toggle_smush_details() { |
| @@ -688,8 +710,10 @@ | ||
| 688 | 710 | $('#wpo_smush_images_save_options_spinner').show().delay(3000).fadeOut(); |
| 689 | 711 | $('#enable_webp_conversion').prop("disabled", true); |
| 690 | 712 | |
| 691 | 713 | var smush_options = get_smush_options(); |
| 714 | + // Skip default notice and use popup to show error messages | |
| 715 | + smush_options.skip_notice = true; | |
| 692 | 716 | |
| 693 | 717 | smush_manager_send_command('update_webp_options', smush_options, function(resp) { |
| 694 | 718 | $('#wpo_smush_images_save_options_spinner').hide(); |
| 695 | 719 | if (resp.hasOwnProperty('saved') && resp.saved) { |
| @@ -698,9 +722,9 @@ | ||
| 698 | 722 | $('#enable_webp_conversion').prop("checked", false); |
| 699 | 723 | if ('update_failed_no_working_webp_converter' === resp.error_code) { |
| 700 | 724 | var html_msg = '<p>' |
| 701 | 725 | + wposmush.webp_conversion_tool_error |
| 702 | - + ' <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">' | |
| 703 | 727 | + wposmush.webp_conversion_tool_how_to |
| 704 | 728 | + '</a></p>'; |
| 705 | 729 | $('#smush-information-modal .smush-information').html(html_msg); |
| 706 | 730 | } else { |
| @@ -705,34 +729,30 @@ | ||
| 705 | 729 | $('#smush-information-modal .smush-information').html(html_msg); |
| 706 | 730 | } else { |
| 707 | 731 | $('#smush-information-modal .smush-information').text(resp.error_message); |
| 708 | 732 | } |
| 709 | - update_view_modal_message($('#smush-information-modal')); | |
| 733 | + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}), false, true); | |
| 710 | 734 | } |
| 711 | 735 | $('#enable_webp_conversion').prop("disabled", false); |
| 712 | 736 | }); |
| 713 | 737 | }); |
| 714 | 738 | |
| 715 | - convert_to_webp_btn.on('click', function(e){ | |
| 739 | + $('body').on('click', '.wpo_smush.column-wpo_smush .convert-to-webp', function(e){ | |
| 716 | 740 | e.preventDefault(); |
| 717 | 741 | var $link = $(this); |
| 718 | 742 | data = { |
| 719 | - 'attachment_id': $(this).data('attachment-id') | |
| 743 | + 'attachment_id': $(this).data('attachment-id'), | |
| 744 | + skip_notice: true | |
| 720 | 745 | }; |
| 721 | - update_view_modal_message(wposmush.converting_to_webp); | |
| 746 | + block_ui(wposmush.converting_to_webp, {}, 0, true); | |
| 722 | 747 | smush_manager_send_command('convert_to_webp_format', data, function(response) { |
| 723 | - if (response.error) { | |
| 724 | - update_view_modal_message(response.error, $.unblockUI); | |
| 725 | - setTimeout(function() { | |
| 726 | - $.unblockUI(); | |
| 727 | - }, 2000); | |
| 728 | - } else { | |
| 729 | - update_view_modal_message(response.success, $.unblockUI); | |
| 730 | - setTimeout(function() { | |
| 731 | - $.unblockUI(); | |
| 732 | - }, 2000); | |
| 748 | + if (response.success) { | |
| 749 | + block_ui(response.success, {}, 2000); | |
| 733 | 750 | $link.next().remove(); |
| 751 | + if ($link.prev().is('.wpo-action-separator')) $link.prev().remove(); | |
| 734 | 752 | $link.remove(); |
| 753 | + } else { | |
| 754 | + block_ui(response.error || response.error_message || wposmush.server_error , {}, 2000); | |
| 735 | 755 | } |
| 736 | 756 | }); |
| 737 | 757 | }); |
| 738 | 758 | |
| @@ -748,9 +768,8 @@ | ||
| 748 | 768 | var use_cache = (typeof use_cache === 'undefined') ? true : use_cache; |
| 749 | 769 | var data = { 'use_cache': use_cache }; |
| 750 | 770 | |
| 751 | 771 | smush_images_optimization_message.html('...'); |
| 752 | - smush_images_pending_tasks_container.hide(); | |
| 753 | 772 | |
| 754 | 773 | disable_image_optimization_controls(true); |
| 755 | 774 | |
| 756 | 775 | heartbeat_agents.push(heartbeat.add_agent({ |
| @@ -764,8 +783,10 @@ | ||
| 764 | 783 | disable_image_optimization_controls(false); |
| 765 | 784 | update_smush_action_buttons_state(); |
| 766 | 785 | } |
| 767 | 786 | })); |
| 787 | + | |
| 788 | + uncompressed_images_already_loaded = true; | |
| 768 | 789 | } |
| 769 | 790 | |
| 770 | 791 | |
| 771 | 792 | /** |
| @@ -905,9 +926,9 @@ | ||
| 905 | 926 | |
| 906 | 927 | /** |
| 907 | 928 | * Update images optimization tab view with data returned from images optimization. |
| 908 | 929 | * |
| 909 | - * @param {Object} data - meta data returned from task manager | |
| 930 | + * @param {Object} data - metadata returned from task manager | |
| 910 | 931 | * |
| 911 | 932 | * @return void |
| 912 | 933 | */ |
| 913 | 934 | function update_view_show_uncompressed_images(data) { |
| @@ -922,12 +943,8 @@ | ||
| 922 | 943 | if (0 == unsmushed_images.length && 0 == pending_tasks) { |
| 923 | 944 | smush_images_grid.text(wposmush.all_images_compressed).wrapInner("<div class='wpo-fieldgroup'> </div>"); |
| 924 | 945 | } |
| 925 | 946 | |
| 926 | - if (0 != pending_tasks) { | |
| 927 | - smush_images_pending_tasks_container.show().find('.red').text(data.pending); | |
| 928 | - } | |
| 929 | - | |
| 930 | 947 | if (1 === data.is_multisite) { |
| 931 | 948 | blog_id = uncompressed_images_sites_select.find(":selected").val(); |
| 932 | 949 | add_unsmushed_images_to_grid(blog_id, data); |
| 933 | 950 | } else { |
| @@ -969,12 +986,10 @@ | ||
| 969 | 986 | * @return void |
| 970 | 987 | */ |
| 971 | 988 | function update_view_bulk_smush_start() { |
| 972 | 989 | if (smush_timer_locked) return; |
| 973 | - update_view_modal_message($('#wpo_smush_images_information_container')); | |
| 974 | 990 | |
| 975 | - service = $('.compression_server input[type="radio"]:checked + label small').text(); | |
| 976 | - $('#wpo_smush_images_information_server').html(service); | |
| 991 | + block_ui('', $.extend(block_ui_custom_options, {message: $('#wpo_smush_images_information_container')}), false, true); | |
| 977 | 992 | |
| 978 | 993 | clear_smush_stats(); |
| 979 | 994 | |
| 980 | 995 | smush_timer_handle = window.setInterval(smush_timer, 1000); |
| @@ -1049,8 +1064,11 @@ | ||
| 1049 | 1064 | }; |
| 1050 | 1065 | |
| 1051 | 1066 | (function(single_callback) { |
| 1052 | 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, | |
| 1053 | 1071 | _wait: false, |
| 1054 | 1072 | _keep: false, |
| 1055 | 1073 | command: 'updraft_smush_ajax', |
| 1056 | 1074 | command_data: {data: data, subaction: 'get_ui_update'}, |
| @@ -1056,8 +1074,12 @@ | ||
| 1056 | 1074 | command_data: {data: data, subaction: 'get_ui_update'}, |
| 1057 | 1075 | callback: function(resp) { |
| 1058 | 1076 | |
| 1059 | 1077 | summary = resp.session_stats; |
| 1078 | + // Prevent pops with undefined headers | |
| 1079 | + if (!resp.session_stats) { | |
| 1080 | + return; | |
| 1081 | + } | |
| 1060 | 1082 | |
| 1061 | 1083 | if (0 != resp.completed_task_count) { |
| 1062 | 1084 | summary += '<hr>' + resp.summary; |
| 1063 | 1085 | } |
| @@ -1084,9 +1106,9 @@ | ||
| 1084 | 1106 | if (smush_completed) return; |
| 1085 | 1107 | |
| 1086 | 1108 | $('#summary-message').html(summary); |
| 1087 | 1109 | reset_view_bulk_smush(); |
| 1088 | - update_view_modal_message($('#smush-complete-summary')); | |
| 1110 | + block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-complete-summary')})); | |
| 1089 | 1111 | smush_completed = true; |
| 1090 | 1112 | |
| 1091 | 1113 | heartbeat.cancel_agents(heartbeat_agents); |
| 1092 | 1114 | } |
| @@ -1169,9 +1191,8 @@ | ||
| 1169 | 1191 | smush_selected_images_btn, |
| 1170 | 1192 | smush_images_select_all_btn, |
| 1171 | 1193 | smush_images_select_none_btn, |
| 1172 | 1194 | smush_images_refresh_btn, |
| 1173 | - smush_images_pending_tasks_btn, | |
| 1174 | 1195 | smush_mark_as_compressed_btn, |
| 1175 | 1196 | ], function(i, el) { |
| 1176 | 1197 | el.prop('disabled', disable); |
| 1177 | 1198 | }); |
| @@ -1199,12 +1220,13 @@ | ||
| 1199 | 1220 | if (0 == selected_image.length) return; |
| 1200 | 1221 | |
| 1201 | 1222 | data = { |
| 1202 | 1223 | selected_image: selected_image, |
| 1203 | - smush_options: smush_options | |
| 1224 | + smush_options: smush_options, | |
| 1225 | + skip_notice: true | |
| 1204 | 1226 | } |
| 1205 | 1227 | |
| 1206 | - update_view_modal_message(wposmush.compress_single_image_dialog); | |
| 1228 | + block_ui(wposmush.compress_single_image_dialog, {}, false, true); | |
| 1207 | 1229 | smush_manager_send_command('compress_single_image', data, function(resp) { |
| 1208 | 1230 | handle_response_from_smush_manager(resp, update_view_single_image_complete); |
| 1209 | 1231 | }); |
| 1210 | 1232 | } |
| @@ -1222,10 +1244,10 @@ | ||
| 1222 | 1244 | |
| 1223 | 1245 | // if no selected images then exit. |
| 1224 | 1246 | if (0 == selected_image.length) return; |
| 1225 | 1247 | |
| 1226 | - update_view_modal_message(wposmush.please_wait, $.unblockUI); | |
| 1227 | - 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 }; | |
| 1228 | 1250 | |
| 1229 | 1251 | smush_manager_send_command.apply({unique: false}, ['restore_single_image', data, function(resp) { |
| 1230 | 1252 | var done = function(resp_summary_alt) { |
| 1231 | 1253 | if ('undefined' != typeof(resp_summary_alt)) { |
| @@ -1253,9 +1275,9 @@ | ||
| 1253 | 1275 | function update_view_single_image_complete(resp) { |
| 1254 | 1276 | |
| 1255 | 1277 | if (resp.hasOwnProperty('success') && resp.success) { |
| 1256 | 1278 | $(".smush-information").text(resp.summary); |
| 1257 | - update_view_modal_message($("#smush-information-modal"), $.unblockUI); | |
| 1279 | + block_ui('', $.extend(block_ui_custom_options, {message: $("#smush-information-modal")})); | |
| 1258 | 1280 | |
| 1259 | 1281 | $('.wpo-toggle-advanced-options.wpo_smush_single_image').removeClass('opened'); |
| 1260 | 1282 | |
| 1261 | 1283 | update_view_singe_image_compress(resp.operation, resp.summary, resp.restore_possible, resp); |
| @@ -1268,8 +1290,12 @@ | ||
| 1268 | 1290 | |
| 1269 | 1291 | if (!smush_affected_images.hasOwnProperty(blog_id)) smush_affected_images[blog_id] = {}; |
| 1270 | 1292 | if (!smush_affected_images[blog_id].hasOwnProperty(image_id)) smush_affected_images[blog_id][image_id] = {}; |
| 1271 | 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 | + | |
| 1272 | 1298 | if ('compress' == resp.operation) { |
| 1273 | 1299 | smush_affected_images[blog_id][image_id] = { |
| 1274 | 1300 | operation: resp.operation, |
| 1275 | 1301 | summary: resp.summary, |
| @@ -1276,9 +1302,8 @@ | ||
| 1276 | 1302 | restore_possible: resp.restore_possible |
| 1277 | 1303 | } |
| 1278 | 1304 | } else { |
| 1279 | 1305 | if (image_id) { |
| 1280 | - update_media_library_wp_optimize_column(image_id); | |
| 1281 | 1306 | reset_bulk_actions_dropdown(); |
| 1282 | 1307 | } |
| 1283 | 1308 | smush_affected_images[blog_id][image_id] = { |
| 1284 | 1309 | operation: resp.operation |
| @@ -1284,14 +1309,26 @@ | ||
| 1284 | 1309 | operation: resp.operation |
| 1285 | 1310 | } |
| 1286 | 1311 | } |
| 1287 | 1312 | } else { |
| 1288 | - $(".smush-information").text(resp.error_message); | |
| 1289 | - 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); | |
| 1290 | 1316 | } |
| 1291 | 1317 | } |
| 1292 | 1318 | |
| 1293 | 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 | + /** | |
| 1294 | 1331 | * Update metabox view depending on a command response. |
| 1295 | 1332 | * |
| 1296 | 1333 | * @param {string} operation |
| 1297 | 1334 | * @param {string} summary |
| @@ -1300,8 +1337,10 @@ | ||
| 1300 | 1337 | */ |
| 1301 | 1338 | function update_view_singe_image_compress(operation, summary, restore_possible, smush_image_data) { |
| 1302 | 1339 | var wrapper = $("#smush_info").closest('#smush-metabox-inside-wrapper'); |
| 1303 | 1340 | |
| 1341 | + if (0 === wrapper.length) return; | |
| 1342 | + | |
| 1304 | 1343 | if ('compress' == operation) { |
| 1305 | 1344 | $(".wpo_smush_single_image").hide(); |
| 1306 | 1345 | $(".wpo_restore_single_image").show(); |
| 1307 | 1346 | |
| @@ -1348,30 +1387,8 @@ | ||
| 1348 | 1387 | } |
| 1349 | 1388 | }); |
| 1350 | 1389 | |
| 1351 | 1390 | /** |
| 1352 | - * Display a modal message | |
| 1353 | - * | |
| 1354 | - * @param {string} message The message or element to display | |
| 1355 | - * @param {Function} callback Called when the overlay is clicked | |
| 1356 | - * | |
| 1357 | - * @return void | |
| 1358 | - */ | |
| 1359 | - function update_view_modal_message(message, callback) { | |
| 1360 | - | |
| 1361 | - $.blockUI({ | |
| 1362 | - message: message, | |
| 1363 | - onOverlayClick: callback, | |
| 1364 | - baseZ: 160001, | |
| 1365 | - css: { | |
| 1366 | - width: '400px', | |
| 1367 | - padding: '20px', | |
| 1368 | - cursor: 'pointer' | |
| 1369 | - } | |
| 1370 | - }); | |
| 1371 | - } | |
| 1372 | - | |
| 1373 | - /** | |
| 1374 | 1391 | * Check returned response from the smush manager and call update view callback. |
| 1375 | 1392 | * |
| 1376 | 1393 | * @param {Object} resp - response from smush manager. |
| 1377 | 1394 | * @param {Function} update_view_callback - callback function to update view. |
| @@ -1410,20 +1427,8 @@ | ||
| 1410 | 1427 | return selected_images_list; |
| 1411 | 1428 | } |
| 1412 | 1429 | |
| 1413 | 1430 | /** |
| 1414 | - * Updates the media library WP Optimize column for a specified image. | |
| 1415 | - * | |
| 1416 | - * @param {number} image_id - The ID of the image to update the column for. | |
| 1417 | - */ | |
| 1418 | - function update_media_library_wp_optimize_column(image_id) { | |
| 1419 | - var admin_url = ajaxurl.replace('/admin-ajax.php', '/'); | |
| 1420 | - admin_url += 'post.php?post=' + image_id + '&action=edit'; | |
| 1421 | - $('#post-' + image_id + ' .column-wpo_smush').html('<a href="' + admin_url + '">' + wposmush.compress + '</a><br>'); | |
| 1422 | - } | |
| 1423 | - | |
| 1424 | - | |
| 1425 | - /** | |
| 1426 | 1431 | * Resets the bulk action dropdown by setting its value to "-1" and unchecking all the selected checkboxes. |
| 1427 | 1432 | * |
| 1428 | 1433 | * @returns {void} |
| 1429 | 1434 | */ |
| @@ -1438,19 +1443,27 @@ | ||
| 1438 | 1443 | * @param {string} action The action to send |
| 1439 | 1444 | * @param {[type]} data Data to send |
| 1440 | 1445 | * @param {Function} callback Will be called with the results |
| 1441 | 1446 | * @param {boolean} json_parse JSON parse the results |
| 1447 | + * @param {boolean} wait Wait for the response | |
| 1442 | 1448 | * |
| 1443 | 1449 | * @return {JSON} |
| 1444 | 1450 | */ |
| 1445 | - function smush_manager_send_command(action, data, callback, json_parse) { | |
| 1451 | + function smush_manager_send_command(action, data, callback, json_parse, wait) { | |
| 1446 | 1452 | |
| 1447 | 1453 | json_parse = ('undefined' === typeof json_parse) ? true : json_parse; |
| 1448 | - data = $.isEmptyObject(data) ? {'use_cache' : false} : data; | |
| 1454 | + var _wait = ('undefined' === typeof wait) ? false : wait; | |
| 1449 | 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 | + | |
| 1450 | 1463 | (function(single_callback, _keep, _unique) { |
| 1451 | 1464 | heartbeat_agents.push(heartbeat.add_agent({ |
| 1452 | - _wait: false, | |
| 1465 | + _wait: _wait, | |
| 1453 | 1466 | _keep: _keep, |
| 1454 | 1467 | _unique: _unique, |
| 1455 | 1468 | command: 'updraft_smush_ajax', |
| 1456 | 1469 | command_data: {data: data, subaction: action}, |
| @@ -1500,83 +1513,6 @@ | ||
| 1500 | 1513 | 'webp_conversion': $('#enable_webp_conversion').is(":checked") |
| 1501 | 1514 | }; |
| 1502 | 1515 | } |
| 1503 | 1516 | wp_optimize.smush_settings = get_smush_options; |
| 1504 | - | |
| 1505 | - setTimeout(get_info_from_smush_manager, 1000, false); | |
| 1506 | - | |
| 1507 | 1517 | } // END WP_Optimize_Smush |
| 1508 | 1518 | |
| 1509 | -/** | |
| 1510 | - * Parse JSON string, including automatically detecting unwanted extra input and skipping it | |
| 1511 | - * | |
| 1512 | - * @param {string|object} json_mix_str - JSON string which need to parse and convert to object | |
| 1513 | - * | |
| 1514 | - * @throws SyntaxError|String (including passing on what JSON.parse may throw) if a parsing error occurs. | |
| 1515 | - * | |
| 1516 | - * @return mixed parsed JSON object. Will only return if parsing is successful (otherwise, will throw) | |
| 1517 | - */ | |
| 1518 | -function wpo_parse_json(json_mix_str) { | |
| 1519 | - // When using wp_send_json to return the value, the format is already parsed. | |
| 1520 | - if ('object' === typeof json_mix_str) return json_mix_str; | |
| 1521 | - | |
| 1522 | - // Just try it - i.e. the 'default' case where things work (which can include extra whitespace/line-feeds, and simple strings, etc.). | |
| 1523 | - try { | |
| 1524 | - var result = JSON.parse(json_mix_str); | |
| 1525 | - return result; | |
| 1526 | - } catch (e) { | |
| 1527 | - console.log("WPO: Exception when trying to parse JSON (1) - will attempt to fix/re-parse"); | |
| 1528 | - console.log(json_mix_str); | |
| 1529 | - } | |
| 1530 | - | |
| 1531 | - var json_start_pos = json_mix_str.indexOf('{'); | |
| 1532 | - var json_last_pos = json_mix_str.lastIndexOf('}'); | |
| 1533 | - | |
| 1534 | - // Case where some php notice may be added after or before json string | |
| 1535 | - if (json_start_pos > -1 && json_last_pos > -1) { | |
| 1536 | - var json_str = json_mix_str.slice(json_start_pos, json_last_pos + 1); | |
| 1537 | - try { | |
| 1538 | - var parsed = JSON.parse(json_str); | |
| 1539 | - return parsed; | |
| 1540 | - } catch (e) { | |
| 1541 | - console.log("WPO: Exception when trying to parse JSON (2) - will attempt to fix/re-parse based upon bracket counting"); | |
| 1542 | - | |
| 1543 | - var cursor = json_start_pos; | |
| 1544 | - var open_count = 0; | |
| 1545 | - var last_character = ''; | |
| 1546 | - var inside_string = false; | |
| 1547 | - | |
| 1548 | - // 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. | |
| 1549 | - while ((open_count > 0 || cursor == json_start_pos) && cursor <= json_last_pos) { | |
| 1550 | - | |
| 1551 | - var current_character = json_mix_str.charAt(cursor); | |
| 1552 | - | |
| 1553 | - if (!inside_string && '{' == current_character) { | |
| 1554 | - open_count++; | |
| 1555 | - } else if (!inside_string && '}' == current_character) { | |
| 1556 | - open_count--; | |
| 1557 | - } else if ('"' == current_character && '\\' != last_character) { | |
| 1558 | - inside_string = inside_string ? false : true; | |
| 1559 | - } | |
| 1560 | - | |
| 1561 | - last_character = current_character; | |
| 1562 | - cursor++; | |
| 1563 | - } | |
| 1564 | - | |
| 1565 | - console.log("Started at cursor="+json_start_pos+", ended at cursor="+cursor+" with result following:"); | |
| 1566 | - console.log(json_mix_str.substring(json_start_pos, cursor)); | |
| 1567 | - | |
| 1568 | - try { | |
| 1569 | - var parsed = JSON.parse(json_mix_str.substring(json_start_pos, cursor)); | |
| 1570 | - // console.log('WPO: JSON re-parse successful'); | |
| 1571 | - return parsed; | |
| 1572 | - } catch (e) { | |
| 1573 | - // Throw it again, so that our function works just like JSON.parse() in its behaviour. | |
| 1574 | - throw e; | |
| 1575 | - } | |
| 1576 | - | |
| 1577 | - } | |
| 1578 | - } | |
| 1579 | - | |
| 1580 | - throw "WPO: could not parse the JSON"; | |
| 1581 | - | |
| 1582 | -} | |