| 1 |
jQuery(function($) { |
| 2 |
WP_Optimize_Smush = WP_Optimize_Smush(); |
| 3 |
}); |
| 4 |
|
| 5 |
var WP_Optimize_Smush = function() { |
| 6 |
|
| 7 |
var $ = jQuery; |
| 8 |
|
| 9 |
var heartbeat = WP_Optimize_Heartbeat(); |
| 10 |
var heartbeat_agents = []; |
| 11 |
|
| 12 |
var last_ui_update_agent_uid = null; |
| 13 |
|
| 14 |
var block_ui = wp_optimize.block_ui; |
| 15 |
|
| 16 |
/** |
| 17 |
* Variables for smushing. |
| 18 |
*/ |
| 19 |
var smush_images_grid = $('#wpo_smush_images_grid'), |
| 20 |
smush_images_optimization_message = $('#smush_info_images'), |
| 21 |
smush_images_refresh_btn = $('#wpo_smush_images_refresh'), |
| 22 |
smush_images_select_all_btn = $('#wpo_smush_images_select_all'), |
| 23 |
smush_images_select_none_btn = $('#wpo_smush_images_select_none'), |
| 24 |
smush_images_stats_clear_btn = $('#wpo_smush_clear_stats_btn'), |
| 25 |
smush_selected_images_btn = $('#wpo_smush_images_btn'), |
| 26 |
smush_mark_as_compressed_btn = $('#wpo_smush_mark_as_compressed'), |
| 27 |
smush_mark_all_as_uncompressed_btn = $('#wpo_smush_mark_all_as_uncompressed_btn'), |
| 28 |
restore_all_compressed_images_btn = $('#wpo_smush_restore_all_compressed_images_btn'), |
| 29 |
smush_view_logs_btn = $('.wpo_smush_get_logs'), |
| 30 |
smush_delete_backup_images_btn = $('#wpo_smush_delete_backup_btn'), |
| 31 |
compression_server_select = $('.compression_server'), |
| 32 |
reset_webp_serving_method_btn = $('#wpo_reset_webp_serving_method'), |
| 33 |
smush_images_tab_loaded = false, |
| 34 |
smush_service_features = [], |
| 35 |
smush_total_seconds = 0, |
| 36 |
smush_timer_locked = false, |
| 37 |
smush_timer_handle = 0, |
| 38 |
smush_image_list = [], |
| 39 |
smush_completed = false, |
| 40 |
smush_from_media_library = false, |
| 41 |
smush_mark_all_as_uncompressed = false, |
| 42 |
smush_affected_images = {}, |
| 43 |
pending_tasks_cancel_btn = $('#wpo_smush_images_pending_tasks_cancel_button'), |
| 44 |
uncompressed_images_sites_select = $('#wpo_uncompressed_images_sites_select'); |
| 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 |
|
| 60 |
$('#doaction, #doaction2').on('click', function(e) { |
| 61 |
e.stopImmediatePropagation(); |
| 62 |
var action = $(this).prev('select').val(); |
| 63 |
if ('wp_optimize_bulk_compression' !== action && 'wp_optimize_bulk_restore' !== action ) return; |
| 64 |
|
| 65 |
var $selected_images = get_media_library_selected_images(); |
| 66 |
if (0 === $selected_images.length) return; |
| 67 |
e.preventDefault(); |
| 68 |
if ('wp_optimize_bulk_compression' === action) bulk_compression($selected_images); |
| 69 |
if ('wp_optimize_bulk_restore' === action) bulk_restore($selected_images); |
| 70 |
}); |
| 71 |
|
| 72 |
/** |
| 73 |
* Handles bulk compression action from media library |
| 74 |
* |
| 75 |
* @param {Object} $selected_images |
| 76 |
*/ |
| 77 |
function bulk_compression($selected_images) { |
| 78 |
block_ui(wposmush.server_check); |
| 79 |
|
| 80 |
data = { 'server': wposmush.smush_settings.compression_server, skip_notice: true }; |
| 81 |
smush_manager_send_command('check_server_status', data, function(resp) { |
| 82 |
if (resp.online) { |
| 83 |
$selected_images.each(function(index, element) { |
| 84 |
smush_image_list.push({ |
| 85 |
'attachment_id': parseInt(element.value), |
| 86 |
'blog_id': wposmush.blog_id |
| 87 |
}); |
| 88 |
}); |
| 89 |
|
| 90 |
data = { |
| 91 |
optimization_id: 'smush', |
| 92 |
selected_images: smush_image_list, |
| 93 |
smush_options: { |
| 94 |
'compression_server': wposmush.smush_settings.compression_server, |
| 95 |
'image_quality': wposmush.smush_settings.image_quality, |
| 96 |
'lossy_compression': wposmush.smush_settings.lossy_compression, |
| 97 |
'back_up_original': wposmush.smush_settings.back_up_original, |
| 98 |
'preserve_exif': wposmush.smush_settings.preserve_exif |
| 99 |
} |
| 100 |
} |
| 101 |
smush_from_media_library = true; |
| 102 |
smush_completed = false; |
| 103 |
if (smush_timer_locked) return; |
| 104 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#wpo_smush_images_information_container')}), false, true); |
| 105 |
clear_smush_stats(); |
| 106 |
|
| 107 |
smush_timer_handle = window.setInterval(smush_timer, 1000); |
| 108 |
smush_manager_send_command('process_bulk_smush', data); |
| 109 |
} else { |
| 110 |
error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error; |
| 111 |
block_ui(error_message); |
| 112 |
} |
| 113 |
}); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Handles bulk restore action from media library |
| 118 |
* |
| 119 |
* @param {Object} $selected_images |
| 120 |
*/ |
| 121 |
function bulk_restore($selected_images) { |
| 122 |
smush_from_media_library = true; |
| 123 |
|
| 124 |
var processed_elements = []; |
| 125 |
var progress = function(element_id) { |
| 126 |
return function(callback) { |
| 127 |
processed_elements.push(element_id); |
| 128 |
|
| 129 |
if (1 < processed_elements.length && processed_elements.length === $selected_images.length) { |
| 130 |
callback(wposmush.images_restored_successfully); |
| 131 |
} else { |
| 132 |
callback(); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
$selected_images.each(function(index, element) { |
| 138 |
restore_selected_image(wposmush.blog_id, parseInt(element.value), progress(parseInt(element.value))); |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Handle Image Selection |
| 144 |
*/ |
| 145 |
smush_images_grid.on('click', '.thumbnail', function (e) { |
| 146 |
$(this).closest('input[type="checkbox"]').prop('checked', true); |
| 147 |
}); |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Handle Shift Ctrl keys state. |
| 152 |
*/ |
| 153 |
var ctrl_shift_on_image_held = false; |
| 154 |
|
| 155 |
|
| 156 |
smush_images_grid.on('mousedown', '.thumbnail', function (e) { |
| 157 |
ctrl_shift_on_image_held = e.shiftKey || e.ctrlKey; |
| 158 |
}); |
| 159 |
|
| 160 |
smush_images_grid.on('mouseup', '.thumbnail', function (e) { |
| 161 |
ctrl_shift_on_image_held = e.shiftKey || e.ctrlKey; |
| 162 |
}); |
| 163 |
|
| 164 |
// Resets server rewrite capability |
| 165 |
reset_webp_serving_method_btn.on('click', function(e) { |
| 166 |
e.preventDefault(); |
| 167 |
smush_manager_send_command('reset_webp_serving_method', { skip_notice: true }, function(resp) { |
| 168 |
if (!resp.success) { |
| 169 |
$('#enable_webp_conversion').prop("checked", false); |
| 170 |
$('#smush-information-modal .smush-information').text(resp.error_message); |
| 171 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}), false, true); |
| 172 |
} else { |
| 173 |
$('#wpo_reset_webp_serving_method_done').show().delay(3000).fadeOut(); |
| 174 |
} |
| 175 |
}); |
| 176 |
}); |
| 177 |
|
| 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 |
/** |
| 185 |
* Checks if smush is active and loads images if yes - image tabs change. |
| 186 |
*/ |
| 187 |
$('#wp-optimize-nav-tab-wrapper__wpo_images .nav-tab').on('click', function() { |
| 188 |
if (uncompressed_images_already_loaded) return; |
| 189 |
|
| 190 |
if ($(this).is('#wp-optimize-nav-tab-wpo_images-smush')) { |
| 191 |
get_info_from_smush_manager(false); |
| 192 |
} |
| 193 |
}); |
| 194 |
|
| 195 |
/** |
| 196 |
* Checks if smush is active and loads images if yes - main menu change. |
| 197 |
*/ |
| 198 |
$('#wp-optimize-wrap').on('page-change', function(e, params) { |
| 199 |
if (uncompressed_images_already_loaded) return; |
| 200 |
|
| 201 |
if ('wpo_images' == params.page) { |
| 202 |
if ($('#wp-optimize-nav-tab-wrapper__wpo_images .nav-tab-active').is('#wp-optimize-nav-tab-wpo_images-smush')) { |
| 203 |
get_info_from_smush_manager(false); |
| 204 |
} |
| 205 |
} |
| 206 |
}); |
| 207 |
|
| 208 |
if ($('#smush-metabox').length > 0) { |
| 209 |
update_view_available_options(); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
var lastclick = null; |
| 214 |
smush_images_grid.on('click' , '.wpo_smush_image' , function(e) { |
| 215 |
var grid_input = $('#wpo_smush_images_grid input[type="checkbox"]'); |
| 216 |
var input = $(this).find('.wpo_smush_image__input'); |
| 217 |
var input_val = (!(input.prop('checked'))); |
| 218 |
if (!lastclick) { |
| 219 |
$(this).find('.wpo_smush_image__input').prop('checked',input_val); |
| 220 |
lastclick = input; |
| 221 |
} |
| 222 |
if (true === ctrl_shift_on_image_held) { |
| 223 |
var start = grid_input.index(input); |
| 224 |
var end = grid_input.index(lastclick); |
| 225 |
if (start === end) { |
| 226 |
grid_input.slice(Math.min(start,end), Math.max(start,end)+ 1).prop('checked', input_val); |
| 227 |
} else { |
| 228 |
if (true === lastclick.prop('checked')) { |
| 229 |
grid_input.slice(Math.min(start,end), Math.max(start,end)+ 1).prop('checked', input_val); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
lastclick = input; |
| 234 |
update_smush_action_buttons_state(); |
| 235 |
}); |
| 236 |
|
| 237 |
/** |
| 238 |
* Disable smush actions buttons if no images selected. |
| 239 |
*/ |
| 240 |
function update_smush_action_buttons_state() { |
| 241 |
var state = (0 == $('input[type="checkbox"]:checked', smush_images_grid).length); |
| 242 |
|
| 243 |
smush_selected_images_btn.prop('disabled', state); |
| 244 |
smush_mark_as_compressed_btn.prop('disabled', state); |
| 245 |
} |
| 246 |
|
| 247 |
update_smush_action_buttons_state(); |
| 248 |
|
| 249 |
/** |
| 250 |
* Handles change of smush service provider |
| 251 |
*/ |
| 252 |
compression_server_select.on('change', function(e) { |
| 253 |
update_view_available_options(); |
| 254 |
save_options(); |
| 255 |
}); |
| 256 |
|
| 257 |
/** |
| 258 |
* Process bulk smush |
| 259 |
*/ |
| 260 |
smush_selected_images_btn.off().on('click', function() { |
| 261 |
|
| 262 |
if (0 == $('#wpo_smush_images_grid input[type="checkbox"]:checked').length) { |
| 263 |
block_ui(wposmush.please_select_images); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
block_ui(wposmush.server_check); |
| 268 |
|
| 269 |
data = { 'server': $("input[name='compression_server']:checked").val(), skip_notice: true }; |
| 270 |
smush_manager_send_command('check_server_status', data, function(resp) { |
| 271 |
if (resp.online) { |
| 272 |
bulk_smush_selected_images(); |
| 273 |
} else { |
| 274 |
error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error; |
| 275 |
block_ui(error_message); |
| 276 |
} |
| 277 |
}); |
| 278 |
}); |
| 279 |
|
| 280 |
/** |
| 281 |
* Mark as compressed |
| 282 |
*/ |
| 283 |
smush_mark_as_compressed_btn.off().on('click', function() { |
| 284 |
|
| 285 |
var selected_images = [], |
| 286 |
image; |
| 287 |
|
| 288 |
$('#wpo_smush_images_grid input:checked').each(function() { |
| 289 |
image = { |
| 290 |
'attachment_id':$(this).val(), |
| 291 |
'blog_id': $(this).data('blog') |
| 292 |
}; |
| 293 |
selected_images.push(image); |
| 294 |
}); |
| 295 |
|
| 296 |
block_ui(wposmush.please_updating_images_info); |
| 297 |
|
| 298 |
smush_manager_send_command('mark_as_compressed', {selected_images: selected_images}, function(response) { |
| 299 |
$('#smush-information-modal .smush-information').text(response.summary); |
| 300 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')})); |
| 301 |
// refresh images list. |
| 302 |
get_info_from_smush_manager(false); |
| 303 |
}); |
| 304 |
}); |
| 305 |
|
| 306 |
/** |
| 307 |
* Handle "Mark all images as uncompressed" click. |
| 308 |
*/ |
| 309 |
smush_mark_all_as_uncompressed_btn.on('click', function() { |
| 310 |
if (!confirm(wposmush.mark_all_images_uncompressed)) return; |
| 311 |
|
| 312 |
var restore_backup = confirm(wposmush.restore_images_from_backup); |
| 313 |
|
| 314 |
block_ui(wposmush.please_wait); |
| 315 |
|
| 316 |
smush_mark_all_as_uncompressed = true; |
| 317 |
run_mark_all_as_uncompressed(restore_backup); |
| 318 |
}); |
| 319 |
|
| 320 |
/** |
| 321 |
* Handle "Restore all compressed images" click. |
| 322 |
*/ |
| 323 |
restore_all_compressed_images_btn.on('click', function() { |
| 324 |
if (!confirm(wposmush.restore_all_compressed_images)) return; |
| 325 |
|
| 326 |
block_ui(wposmush.please_wait); |
| 327 |
|
| 328 |
smush_mark_all_as_uncompressed = true; |
| 329 |
run_mark_all_as_uncompressed(true, true); |
| 330 |
}); |
| 331 |
|
| 332 |
/** |
| 333 |
* Handle "Cancel" button click for mark all images as uncompressed process |
| 334 |
*/ |
| 335 |
$('#smush-information-modal-cancel-btn input[type="button"]').on('click', function() { |
| 336 |
smush_mark_all_as_uncompressed = false; |
| 337 |
get_info_from_smush_manager(); |
| 338 |
$.unblockUI(); |
| 339 |
}); |
| 340 |
|
| 341 |
/** |
| 342 |
* Send command for mark all compressed images as uncompressed. |
| 343 |
* |
| 344 |
* @param {bool} restore_backup if set to true then images will restored from backup if possible. |
| 345 |
* @param {bool} delete_only_backups_meta if set to true the only backup meta will be deleted |
| 346 |
*/ |
| 347 |
function run_mark_all_as_uncompressed(restore_backup, delete_only_backups_meta) { |
| 348 |
if (!smush_mark_all_as_uncompressed) return; |
| 349 |
|
| 350 |
restore_backup = restore_backup ? 1 : 0; |
| 351 |
delete_only_backups_meta = delete_only_backups_meta ? 1 : 0; |
| 352 |
|
| 353 |
var info = $('#smush-information-modal-cancel-btn .smush-information'); |
| 354 |
smush_manager_send_command('mark_all_as_uncompressed', |
| 355 |
{ |
| 356 |
restore_backup: restore_backup, |
| 357 |
delete_only_backups_meta: delete_only_backups_meta, |
| 358 |
skip_notice: true |
| 359 |
}, |
| 360 |
function(resp) { |
| 361 |
// if cancel button pressed then exit |
| 362 |
if (!smush_mark_all_as_uncompressed) return; |
| 363 |
|
| 364 |
// if we get an error then show it |
| 365 |
if (resp.hasOwnProperty('error')) { |
| 366 |
block_ui(resp.error); |
| 367 |
get_info_from_smush_manager(); |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
// if completed then refresh uncompressed images list and show final message. |
| 372 |
if (resp.completed) { |
| 373 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')})); |
| 374 |
$('#smush-information-modal .smush-information').text(resp.message); |
| 375 |
get_info_from_smush_manager(); |
| 376 |
} else { |
| 377 |
info.text(resp.message); |
| 378 |
run_mark_all_as_uncompressed(restore_backup, delete_only_backups_meta); |
| 379 |
} |
| 380 |
} |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Refresh image list |
| 386 |
*/ |
| 387 |
smush_images_refresh_btn.off().on('click', function() { |
| 388 |
// The refresh image list button should not fetch from cache but run the query |
| 389 |
get_info_from_smush_manager(false); |
| 390 |
}); |
| 391 |
|
| 392 |
/** |
| 393 |
* Bind select all / select none controls |
| 394 |
*/ |
| 395 |
smush_images_select_all_btn.off().on('click', function() { |
| 396 |
$('#wpo_smush_images_grid input[type="checkbox"]').prop("checked", true); |
| 397 |
lastclick = null; |
| 398 |
update_smush_action_buttons_state(); |
| 399 |
}); |
| 400 |
|
| 401 |
|
| 402 |
/** |
| 403 |
* Bind select all / select none controls |
| 404 |
*/ |
| 405 |
smush_images_select_none_btn.off().on('click', function() { |
| 406 |
$('#wpo_smush_images_grid input[type="checkbox"]').prop("checked", false); |
| 407 |
lastclick = null; |
| 408 |
update_smush_action_buttons_state(); |
| 409 |
}); |
| 410 |
|
| 411 |
/** |
| 412 |
* Displays logs in a modal |
| 413 |
*/ |
| 414 |
smush_view_logs_btn.off().on('click', function() { |
| 415 |
$("#log-panel").text("Please wait, fetching logs."); |
| 416 |
smush_manager_send_command('get_smush_logs', {}, function(resp) { |
| 417 |
$.blockUI({ |
| 418 |
message: $("#smush-log-modal"), |
| 419 |
onOverlayClick: $.unblockUI(), |
| 420 |
css: { |
| 421 |
width: '80%', |
| 422 |
height: '80%', |
| 423 |
top: '15%', |
| 424 |
left: '15%' |
| 425 |
} |
| 426 |
}); |
| 427 |
$("#log-panel").html("<pre>" + resp + "</pre>"); |
| 428 |
download_link = ajaxurl + "?action=updraft_smush_ajax&subaction=get_smush_logs&nonce=" + wposmush.smush_ajax_nonce; |
| 429 |
$("#smush-log-modal a").attr('href', download_link); |
| 430 |
}, false); |
| 431 |
}); |
| 432 |
|
| 433 |
/** |
| 434 |
* Handle delete all backup images button click. |
| 435 |
*/ |
| 436 |
smush_delete_backup_images_btn.on('click', function() { |
| 437 |
|
| 438 |
if (!confirm(wposmush.delete_image_backup_confirm)) return; |
| 439 |
smush_delete_backup_images_btn.prop('disabled', true); |
| 440 |
var spinner = $('#wpo_smush_delete_backup_spinner'), |
| 441 |
done = $('#wpo_smush_delete_backup_done'); |
| 442 |
|
| 443 |
spinner.show(); |
| 444 |
|
| 445 |
smush_manager_send_command('clean_all_backup_images', {}, function() { |
| 446 |
spinner.hide(); |
| 447 |
smush_delete_backup_images_btn.prop('disabled', false); |
| 448 |
done.css('display', 'inline-block').delay(3000).fadeOut(); |
| 449 |
}); |
| 450 |
}); |
| 451 |
|
| 452 |
/** |
| 453 |
* Binds clear stats button |
| 454 |
*/ |
| 455 |
smush_images_stats_clear_btn.off().on('click', function(e) { |
| 456 |
$('#wpo_smush_images_clear_stats_spinner').show().delay(3000).fadeOut(); |
| 457 |
|
| 458 |
smush_manager_send_command('clear_smush_stats', {}, function(resp) { |
| 459 |
$('#wpo_smush_images_clear_stats_spinner').hide(); |
| 460 |
$('#wpo_smush_images_clear_stats_done').show().delay(3000).fadeOut(); |
| 461 |
}); |
| 462 |
}); |
| 463 |
|
| 464 |
/** |
| 465 |
* Binds smush cancel button |
| 466 |
*/ |
| 467 |
$('body').on('click', '#wpo_smush_images_pending_tasks_cancel_button', function(e) { |
| 468 |
|
| 469 |
if (wposmush.cancel === pending_tasks_cancel_btn.val()) { |
| 470 |
pending_tasks_cancel_btn.val(wposmush.cancelling); |
| 471 |
pending_tasks_cancel_btn.prop('disabled', true); |
| 472 |
} |
| 473 |
smush_manager_send_command('clear_pending_images', { restore_images: smush_image_list }, function(resp) { |
| 474 |
$.unblockUI(); |
| 475 |
if (resp.status) { |
| 476 |
get_info_from_smush_manager(); |
| 477 |
reset_view_bulk_smush(); |
| 478 |
} else { |
| 479 |
console.log('Cancelling pending images apparently failed.', resp); |
| 480 |
} |
| 481 |
pending_tasks_cancel_btn.val(wposmush.cancel); |
| 482 |
pending_tasks_cancel_btn.prop('disabled', false); |
| 483 |
}); |
| 484 |
}); |
| 485 |
|
| 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 |
/** |
| 526 |
* Single image compression |
| 527 |
*/ |
| 528 |
$('body').on('click', '.wpo_smush_single_image .button', function() { |
| 529 |
|
| 530 |
image = { |
| 531 |
'attachment_id':$(this).data('id'), |
| 532 |
'blog_id': $(this).data('blog') |
| 533 |
}; |
| 534 |
|
| 535 |
if ($('#enable_custom_compression').is(":checked")) { |
| 536 |
image_quality = $('#custom_compression_slider').val(); |
| 537 |
} else { |
| 538 |
// The '60' here has to be kept in sync with WP_Optimize::admin_page_wpo_images_smush() |
| 539 |
image_quality = $('#enable_lossy_compression').is(":checked") ? 60 : 92; |
| 540 |
} |
| 541 |
lossy_compression = image_quality < 92 ? true : false; |
| 542 |
|
| 543 |
smush_options = { |
| 544 |
'compression_server': $("input[name='compression_server_" + image.attachment_id + "']:checked").val(), |
| 545 |
'image_quality': image_quality, |
| 546 |
'lossy_compression': lossy_compression, |
| 547 |
'back_up_original': $('#smush_backup_' + image.attachment_id).is(":checked"), |
| 548 |
'preserve_exif': $('#smush_exif_' + image.attachment_id).is(":checked") |
| 549 |
} |
| 550 |
|
| 551 |
data = { 'server': $("input[name='compression_server_" + $(this).attr('id').substring(15) + "']:checked").val() }; |
| 552 |
block_ui(wposmush.server_check); |
| 553 |
smush_manager_send_command('check_server_status', data, function(resp) { |
| 554 |
if (resp.online) { |
| 555 |
smush_selected_image(image, smush_options); |
| 556 |
} else { |
| 557 |
error_message = resp.error ? resp.error + '<br>' + wposmush.server_error : wposmush.server_error; |
| 558 |
block_ui(error_message); |
| 559 |
} |
| 560 |
}); |
| 561 |
}); |
| 562 |
|
| 563 |
/** |
| 564 |
* Single image restore |
| 565 |
*/ |
| 566 |
$('body').on('click', '.wpo_restore_single_image .button, .wpo_restore_single_image a', function(e) { |
| 567 |
var clicked_image = $(this); |
| 568 |
blog_id = clicked_image.data('blog'); |
| 569 |
image_id = clicked_image.data('id'); |
| 570 |
|
| 571 |
e.preventDefault(); |
| 572 |
|
| 573 |
if (!image_id || !blog_id) return; |
| 574 |
restore_selected_image(blog_id, image_id); |
| 575 |
}); |
| 576 |
|
| 577 |
/** |
| 578 |
* Mark as compressed |
| 579 |
*/ |
| 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 |
}); |
| 583 |
|
| 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); |
| 589 |
}); |
| 590 |
|
| 591 |
/** |
| 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 |
| 597 |
*/ |
| 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 |
| 604 |
}, |
| 605 |
wrapper = target.closest('#smush-metabox-inside-wrapper') |
| 606 |
data = {selected_images: [ image ]}; |
| 607 |
|
| 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) { |
| 617 |
|
| 618 |
$('#smush-information-modal .smush-information').text(response.summary); |
| 619 |
|
| 620 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')})); |
| 621 |
|
| 622 |
if (response.status) { |
| 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 |
} |
| 643 |
} |
| 644 |
}); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
$('body').on('click', '#wpo_smush_details .wpo-collapsible', toggle_smush_details); |
| 649 |
$('body').on('click', '.column-wpo_smush .wpo-collapsible', toggle_smush_details); |
| 650 |
|
| 651 |
function toggle_smush_details() { |
| 652 |
$(this).toggleClass('opened'); |
| 653 |
if ($(this).hasClass('opened')) { |
| 654 |
$(this).text(wposmush.less); |
| 655 |
} else { |
| 656 |
$(this).text(wposmush.more); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
$('body').on('click', '#smush-log-modal .close, #smush-information-modal .information-modal-close', function() { |
| 661 |
$.unblockUI(); |
| 662 |
if (smush_from_media_library) { |
| 663 |
reset_bulk_actions_dropdown(); |
| 664 |
} |
| 665 |
}); |
| 666 |
|
| 667 |
$('body').on('click', '.wpo_smush_stats_cta_btn, .wpo_smush_get_logs, #smush-complete-summary .close', function() { |
| 668 |
$.unblockUI(); |
| 669 |
if (smush_from_media_library) { |
| 670 |
var selected_images_list = get_media_library_selected_images_list(); |
| 671 |
smush_manager_send_command('get_smush_details', {selected_images: selected_images_list}, function(response) { |
| 672 |
if (response.success) { |
| 673 |
window.clearInterval(smush_timer_handle); |
| 674 |
update_media_library_screen(response.smush_details); |
| 675 |
} else { |
| 676 |
console.log(response) |
| 677 |
} |
| 678 |
}); |
| 679 |
} else { |
| 680 |
get_info_from_smush_manager(); |
| 681 |
setTimeout(reset_view_bulk_smush, 500); |
| 682 |
} |
| 683 |
}); |
| 684 |
|
| 685 |
$('body').on('click', '.wpo-toggle-advanced-options', function(e) { |
| 686 |
e.preventDefault(); |
| 687 |
$(this).toggleClass('opened'); |
| 688 |
}); |
| 689 |
|
| 690 |
$('.wpo-fieldgroup .autosmush input, .wpo-fieldgroup .compression_level, .wpo-fieldgroup .image_options, #smush-show-metabox').on('change', function(e) { |
| 691 |
save_options(); |
| 692 |
}); |
| 693 |
|
| 694 |
$('body').on('change', '.smush-options.compression_level', function() { |
| 695 |
if ($('#enable_custom_compression').is(':checked')) { |
| 696 |
$('.smush-options.custom_compression').show(); |
| 697 |
} else { |
| 698 |
$('.smush-options.custom_compression').hide(); |
| 699 |
} |
| 700 |
}); |
| 701 |
|
| 702 |
$('body').on('change', '.smush-advanced input[type="radio"]', function() { |
| 703 |
update_view_available_options(); |
| 704 |
}); |
| 705 |
|
| 706 |
$('#enable_webp_conversion').on('change', function(e) { |
| 707 |
// only save when changing the options on the wpo dashboard. |
| 708 |
if (!$('#wp-optimize-wrap').length) return; |
| 709 |
|
| 710 |
$('#wpo_smush_images_save_options_spinner').show().delay(3000).fadeOut(); |
| 711 |
$('#enable_webp_conversion').prop("disabled", true); |
| 712 |
|
| 713 |
var smush_options = get_smush_options(); |
| 714 |
// Skip default notice and use popup to show error messages |
| 715 |
smush_options.skip_notice = true; |
| 716 |
|
| 717 |
smush_manager_send_command('update_webp_options', smush_options, function(resp) { |
| 718 |
$('#wpo_smush_images_save_options_spinner').hide(); |
| 719 |
if (resp.hasOwnProperty('saved') && resp.saved) { |
| 720 |
$('#wpo_smush_images_save_options_done').show().delay(3000).fadeOut(); |
| 721 |
} else { |
| 722 |
$('#enable_webp_conversion').prop("checked", false); |
| 723 |
if ('update_failed_no_working_webp_converter' === resp.error_code) { |
| 724 |
var html_msg = '<p>' |
| 725 |
+ wposmush.webp_conversion_tool_error |
| 726 |
+ ' <a href="https://teamupdraft.com/documentation/wp-optimize/topics/image-compression/troubleshooting/the-webp-conversion-tool-is-not-working/" target="_blank">' |
| 727 |
+ wposmush.webp_conversion_tool_how_to |
| 728 |
+ '</a></p>'; |
| 729 |
$('#smush-information-modal .smush-information').html(html_msg); |
| 730 |
} else { |
| 731 |
$('#smush-information-modal .smush-information').text(resp.error_message); |
| 732 |
} |
| 733 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-information-modal')}), false, true); |
| 734 |
} |
| 735 |
$('#enable_webp_conversion').prop("disabled", false); |
| 736 |
}); |
| 737 |
}); |
| 738 |
|
| 739 |
$('body').on('click', '.wpo_smush.column-wpo_smush .convert-to-webp', function(e){ |
| 740 |
e.preventDefault(); |
| 741 |
var $link = $(this); |
| 742 |
data = { |
| 743 |
'attachment_id': $(this).data('attachment-id'), |
| 744 |
skip_notice: true |
| 745 |
}; |
| 746 |
block_ui(wposmush.converting_to_webp, {}, 0, true); |
| 747 |
smush_manager_send_command('convert_to_webp_format', data, function(response) { |
| 748 |
if (response.success) { |
| 749 |
block_ui(response.success, {}, 2000); |
| 750 |
$link.next().remove(); |
| 751 |
if ($link.prev().is('.wpo-action-separator')) $link.prev().remove(); |
| 752 |
$link.remove(); |
| 753 |
} else { |
| 754 |
block_ui(response.error || response.error_message || wposmush.server_error , {}, 2000); |
| 755 |
} |
| 756 |
}); |
| 757 |
}); |
| 758 |
|
| 759 |
/** |
| 760 |
* Load and show information about uncompressed images. |
| 761 |
* |
| 762 |
* @param {Boolean} use_cache Use the request cache |
| 763 |
* |
| 764 |
* @return void |
| 765 |
*/ |
| 766 |
function get_info_from_smush_manager(use_cache) { |
| 767 |
|
| 768 |
var use_cache = (typeof use_cache === 'undefined') ? true : use_cache; |
| 769 |
var data = { 'use_cache': use_cache }; |
| 770 |
|
| 771 |
smush_images_optimization_message.html('...'); |
| 772 |
|
| 773 |
disable_image_optimization_controls(true); |
| 774 |
|
| 775 |
heartbeat_agents.push(heartbeat.add_agent({ |
| 776 |
_wait: false, |
| 777 |
_keep: false, |
| 778 |
command: 'updraft_smush_ajax', |
| 779 |
command_data: {data: data, subaction: 'get_ui_update'}, |
| 780 |
callback: function(resp) { |
| 781 |
handle_response_from_smush_manager(resp, update_view_show_uncompressed_images); |
| 782 |
update_view_available_options(); |
| 783 |
disable_image_optimization_controls(false); |
| 784 |
update_smush_action_buttons_state(); |
| 785 |
} |
| 786 |
})); |
| 787 |
|
| 788 |
uncompressed_images_already_loaded = true; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
/** |
| 793 |
* Get selected images and make an ajax request to compress them. |
| 794 |
* |
| 795 |
* @return void |
| 796 |
*/ |
| 797 |
function bulk_smush_selected_images() { |
| 798 |
|
| 799 |
$('#wpo_smush_images_grid input:checked').each(function() { |
| 800 |
image = { |
| 801 |
'attachment_id':$(this).val(), |
| 802 |
'blog_id': $(this).data('blog') |
| 803 |
}; |
| 804 |
smush_image_list.push(image); |
| 805 |
}); |
| 806 |
|
| 807 |
data = { |
| 808 |
optimization_id: 'smush', |
| 809 |
selected_images: smush_image_list, |
| 810 |
smush_options: { |
| 811 |
'compression_server': $("input[name='compression_server']:checked").val(), |
| 812 |
'image_quality': $('#image_quality').val(), |
| 813 |
'lossy_compression': $('#smush-lossy-compression').is(":checked"), |
| 814 |
'back_up_original': $('#smush-backup-original').is(":checked"), |
| 815 |
'preserve_exif': $('#smush-preserve-exif').is(":checked") |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
update_view_bulk_smush_start(); |
| 820 |
heartbeat_agents.push(heartbeat.add_agent({ |
| 821 |
_wait: false, |
| 822 |
_keep: false, |
| 823 |
command: 'updraft_smush_ajax', |
| 824 |
command_data: {data: data, subaction: 'process_bulk_smush'} |
| 825 |
})); |
| 826 |
} |
| 827 |
|
| 828 |
/** |
| 829 |
* Save options in the DB |
| 830 |
* |
| 831 |
* @return void |
| 832 |
*/ |
| 833 |
function save_options() { |
| 834 |
// only save when changing the options on the wpo dashboard. |
| 835 |
if (!$('#wp-optimize-wrap').length) return; |
| 836 |
|
| 837 |
$('#wpo_smush_images_save_options_spinner').show().delay(3000).fadeOut(); |
| 838 |
|
| 839 |
var smush_options = get_smush_options(); |
| 840 |
|
| 841 |
smush_manager_send_command('update_smush_options', smush_options, function(resp) { |
| 842 |
$('#wpo_smush_images_save_options_spinner').hide(); |
| 843 |
if (resp.hasOwnProperty('saved') && resp.saved) { |
| 844 |
$('#wpo_smush_images_save_options_done').show().delay(3000).fadeOut(); |
| 845 |
} else { |
| 846 |
$('#wpo_smush_images_save_options_fail').show().delay(3000).fadeOut(); |
| 847 |
} |
| 848 |
}); |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* A timer to run for the duration of the bulk smush operation. |
| 853 |
* |
| 854 |
* @return void |
| 855 |
*/ |
| 856 |
function smush_timer() { |
| 857 |
smush_timer_locked = true; |
| 858 |
smush_total_seconds++; |
| 859 |
seconds = (smush_total_seconds % 60) + '' < 10 ? '0' + (smush_total_seconds % 60) : (smush_total_seconds % 60); |
| 860 |
minutes = parseInt(smush_total_seconds / 60) + '' < 10 ? '0' + parseInt(smush_total_seconds / 60) : parseInt(smush_total_seconds / 60); |
| 861 |
|
| 862 |
$('#smush_stats_timer').text(minutes + ":" + seconds); |
| 863 |
trigger_events(smush_total_seconds); |
| 864 |
} |
| 865 |
|
| 866 |
/** |
| 867 |
* A timer to run for the duration of the bulk smush operation. |
| 868 |
* |
| 869 |
* @param {Number} time_elapsed - time in seconds |
| 870 |
* |
| 871 |
* @return void |
| 872 |
*/ |
| 873 |
function trigger_events(time_elapsed) { |
| 874 |
if (0 == time_elapsed % 3) { |
| 875 |
update_smush_stats(time_elapsed); |
| 876 |
} |
| 877 |
|
| 878 |
if (0 == time_elapsed % 60) { |
| 879 |
if (null != last_ui_update_agent_uid) { |
| 880 |
heartbeat.cancel_agent(last_ui_update_agent_uid); |
| 881 |
last_ui_update_agent_uid = null; |
| 882 |
} |
| 883 |
|
| 884 |
heartbeat_agents.push(heartbeat.add_agent({ |
| 885 |
_wait: false, |
| 886 |
command: 'updraft_smush_ajax', |
| 887 |
command_data: {data: {}, subaction: 'process_pending_images'}, |
| 888 |
callback: function(resp) { |
| 889 |
handle_response_from_smush_manager(resp, update_view_bulk_smush_progress); |
| 890 |
} |
| 891 |
})); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
/** |
| 896 |
* Updates the UI with stats |
| 897 |
* |
| 898 |
* @param {Int} time_elapsed Elapsed time since process start |
| 899 |
* |
| 900 |
* @return void |
| 901 |
*/ |
| 902 |
function update_smush_stats(time_elapsed) { |
| 903 |
data = { |
| 904 |
update_ui: true, |
| 905 |
use_cache: false |
| 906 |
} |
| 907 |
|
| 908 |
var initial_requests = time_elapsed < 5; |
| 909 |
|
| 910 |
var agent = heartbeat.add_agent({ |
| 911 |
_wait: !initial_requests, |
| 912 |
_keep: false, |
| 913 |
command: 'updraft_smush_ajax', |
| 914 |
command_data: {data: data, subaction: 'get_ui_update'}, |
| 915 |
callback: function(resp) { |
| 916 |
handle_response_from_smush_manager(resp, update_view_bulk_smush_progress); |
| 917 |
} |
| 918 |
}); |
| 919 |
|
| 920 |
if (null !== agent) { |
| 921 |
last_ui_update_agent_uid = agent; |
| 922 |
|
| 923 |
heartbeat_agents.push(agent); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
/** |
| 928 |
* Update images optimization tab view with data returned from images optimization. |
| 929 |
* |
| 930 |
* @param {Object} data - metadata returned from task manager |
| 931 |
* |
| 932 |
* @return void |
| 933 |
*/ |
| 934 |
function update_view_show_uncompressed_images(data) { |
| 935 |
smush_images_grid.html(''); |
| 936 |
|
| 937 |
if (!data || !data.hasOwnProperty('unsmushed_images')) return; |
| 938 |
|
| 939 |
var unsmushed_images = data.unsmushed_images, |
| 940 |
pending_tasks = data.pending_tasks, |
| 941 |
blog_id = 0; |
| 942 |
|
| 943 |
if (0 == unsmushed_images.length && 0 == pending_tasks) { |
| 944 |
smush_images_grid.text(wposmush.all_images_compressed).wrapInner("<div class='wpo-fieldgroup'> </div>"); |
| 945 |
} |
| 946 |
|
| 947 |
if (1 === data.is_multisite) { |
| 948 |
blog_id = uncompressed_images_sites_select.find(":selected").val(); |
| 949 |
add_unsmushed_images_to_grid(blog_id, data); |
| 950 |
} else { |
| 951 |
for (blog_id in data.unsmushed_images) { |
| 952 |
add_unsmushed_images_to_grid(blog_id, data); |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
function sort_unsmushed_images(a, b) { |
| 958 |
return a.id - b.id; |
| 959 |
} |
| 960 |
|
| 961 |
function add_unsmushed_images_to_grid(blog_id, data) { |
| 962 |
// Used to have upload.php?item= on multisite (using data.is_multisite), and no suffix |
| 963 |
var admin_url_pre_id = 'post.php?post=', |
| 964 |
admin_url_post_id = '&action=edit', |
| 965 |
admin_url = data.admin_urls[blog_id], |
| 966 |
unsmushed_images = data.unsmushed_images[blog_id]; |
| 967 |
|
| 968 |
if ('undefined' !== typeof unsmushed_images) { |
| 969 |
unsmushed_images.sort(sort_unsmushed_images); |
| 970 |
} |
| 971 |
|
| 972 |
for (i in unsmushed_images) { |
| 973 |
if (!unsmushed_images.hasOwnProperty(i)) continue; |
| 974 |
var image = unsmushed_images[i]; |
| 975 |
add_image_to_grid(image, blog_id, admin_url + admin_url_pre_id + image.id + admin_url_post_id); |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
uncompressed_images_sites_select.on('change', function() { |
| 980 |
get_info_from_smush_manager(); |
| 981 |
}); |
| 982 |
|
| 983 |
/** |
| 984 |
* Updates the view when bulk smush starts |
| 985 |
* |
| 986 |
* @return void |
| 987 |
*/ |
| 988 |
function update_view_bulk_smush_start() { |
| 989 |
if (smush_timer_locked) return; |
| 990 |
|
| 991 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#wpo_smush_images_information_container')}), false, true); |
| 992 |
|
| 993 |
clear_smush_stats(); |
| 994 |
|
| 995 |
smush_timer_handle = window.setInterval(smush_timer, 1000); |
| 996 |
disable_image_optimization_controls(true); |
| 997 |
} |
| 998 |
|
| 999 |
/** |
| 1000 |
* Clears the statistics for image smushing in popup. |
| 1001 |
* |
| 1002 |
* This function clears the statistics displayed on the popup for image smushing. |
| 1003 |
* |
| 1004 |
* @returns {void} |
| 1005 |
*/ |
| 1006 |
function clear_smush_stats() { |
| 1007 |
$('#smush_stats_pending_images').html("..."); |
| 1008 |
$('#smush_stats_completed_images').html("..."); |
| 1009 |
$('#smush_stats_bytes_saved').html("..."); |
| 1010 |
$('#smush_stats_percent_saved').html("..."); |
| 1011 |
$('#smush_stats_timer').html("..."); |
| 1012 |
} |
| 1013 |
|
| 1014 |
/** |
| 1015 |
* Updates the media library screen with the provided smush details. |
| 1016 |
* |
| 1017 |
* @param {Object} smush_details - The details of the smushed images |
| 1018 |
* @return {void} |
| 1019 |
*/ |
| 1020 |
function update_media_library_screen(smush_details) { |
| 1021 |
for (var image_id in smush_details) { |
| 1022 |
if(smush_details.hasOwnProperty(image_id)) { |
| 1023 |
$('#post-' + image_id + ' .column-wpo_smush').html(smush_details[image_id]); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
reset_bulk_actions_dropdown(); |
| 1027 |
} |
| 1028 |
|
| 1029 |
/** |
| 1030 |
* Updates the view with progress related stats |
| 1031 |
* |
| 1032 |
* @param {Object} resp - response from smush manager. |
| 1033 |
* |
| 1034 |
* @return void |
| 1035 |
*/ |
| 1036 |
function update_view_bulk_smush_progress(resp) { |
| 1037 |
|
| 1038 |
// Update stats |
| 1039 |
$('#smush_stats_pending_images').html(resp.pending_tasks); |
| 1040 |
$('#smush_stats_completed_images').html(resp.completed_task_count); |
| 1041 |
$('#smush_stats_bytes_saved').html(resp.bytes_saved); |
| 1042 |
$('#smush_stats_percent_saved').html(resp.percent_saved); |
| 1043 |
|
| 1044 |
// Show summary and close the modal |
| 1045 |
if (true == resp.smush_complete) { |
| 1046 |
// Force a delay here to avoid stale data |
| 1047 |
setTimeout(function() { |
| 1048 |
update_view_bulk_smush_complete(function() { get_info_from_smush_manager(false); }); |
| 1049 |
}, 1500); |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
/** |
| 1054 |
* Updates the view when bulk smush completes |
| 1055 |
* |
| 1056 |
* @return void |
| 1057 |
*/ |
| 1058 |
function update_view_bulk_smush_complete(callback) { |
| 1059 |
|
| 1060 |
data = { |
| 1061 |
update_ui: true, |
| 1062 |
use_cache: false, |
| 1063 |
image_list: smush_image_list |
| 1064 |
}; |
| 1065 |
|
| 1066 |
(function(single_callback) { |
| 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, |
| 1071 |
_wait: false, |
| 1072 |
_keep: false, |
| 1073 |
command: 'updraft_smush_ajax', |
| 1074 |
command_data: {data: data, subaction: 'get_ui_update'}, |
| 1075 |
callback: function(resp) { |
| 1076 |
|
| 1077 |
summary = resp.session_stats; |
| 1078 |
// Prevent pops with undefined headers |
| 1079 |
if (!resp.session_stats) { |
| 1080 |
return; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if (0 != resp.completed_task_count) { |
| 1084 |
summary += '<hr>' + resp.summary; |
| 1085 |
} |
| 1086 |
|
| 1087 |
show_smush_summary(summary); |
| 1088 |
|
| 1089 |
if (single_callback instanceof Function) { |
| 1090 |
single_callback(); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
})); |
| 1094 |
})(callback); |
| 1095 |
} |
| 1096 |
|
| 1097 |
/** |
| 1098 |
* Displays a modal with compression data |
| 1099 |
* |
| 1100 |
* @param {string} summary - stats and info |
| 1101 |
* |
| 1102 |
* @return void |
| 1103 |
*/ |
| 1104 |
function show_smush_summary(summary) { |
| 1105 |
|
| 1106 |
if (smush_completed) return; |
| 1107 |
|
| 1108 |
$('#summary-message').html(summary); |
| 1109 |
reset_view_bulk_smush(); |
| 1110 |
block_ui('', $.extend(block_ui_custom_options, {message: $('#smush-complete-summary')})); |
| 1111 |
smush_completed = true; |
| 1112 |
|
| 1113 |
heartbeat.cancel_agents(heartbeat_agents); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/** |
| 1117 |
* Updates the view when bulk smush completes |
| 1118 |
* |
| 1119 |
* @return void |
| 1120 |
*/ |
| 1121 |
function reset_view_bulk_smush() { |
| 1122 |
// Reset timer and locks |
| 1123 |
smush_total_seconds = 0; |
| 1124 |
smush_timer_locked = false; |
| 1125 |
smush_completed = false; |
| 1126 |
smush_image_list = []; |
| 1127 |
|
| 1128 |
heartbeat.cancel_agents(heartbeat_agents); |
| 1129 |
clearInterval(smush_timer_handle); |
| 1130 |
|
| 1131 |
disable_image_optimization_controls(false); |
| 1132 |
} |
| 1133 |
|
| 1134 |
/** |
| 1135 |
* Append the image to the grid |
| 1136 |
* |
| 1137 |
* @param {Object} image - image data returned from smush manager |
| 1138 |
* @param {int} blog_id - The blog id the image |
| 1139 |
* @param {String} admin_url - The URL to link to for viewing the image |
| 1140 |
* |
| 1141 |
* @return void |
| 1142 |
*/ |
| 1143 |
function add_image_to_grid(image, blog_id, admin_url) { |
| 1144 |
|
| 1145 |
var dom_id = ['wpo_smush_', blog_id, '_', image.id].join(''); |
| 1146 |
|
| 1147 |
image_html = '<div class="wpo_smush_image" data-filesize="'+image.filesize+'">'; |
| 1148 |
image_html += '<a class="button" href="'+admin_url+'" target="_blank"> ' + wposmush.view_image + ' </a>'; |
| 1149 |
image_html += '<input id="' + dom_id + '" type="checkbox" data-blog="'+blog_id+ '" class="wpo_smush_image__input" value="'+image.id+'">'; |
| 1150 |
image_html += '<label for="' + dom_id + '"></a>'; |
| 1151 |
image_html += '<div class="thumbnail">'; |
| 1152 |
image_html += '<img class="lazyload" src="'+image.thumb_url+'">'; |
| 1153 |
image_html += '</div></label></div>'; |
| 1154 |
|
| 1155 |
smush_images_grid.append(image_html); |
| 1156 |
} |
| 1157 |
|
| 1158 |
/** |
| 1159 |
* Updates UI based on service provider selected |
| 1160 |
* |
| 1161 |
* @param {Object} features - image data returned from smush manager |
| 1162 |
* |
| 1163 |
* @return void |
| 1164 |
*/ |
| 1165 |
function update_view_available_options() { |
| 1166 |
features = wposmush.features; |
| 1167 |
service = $("input[name^='compression_server']:checked").val(); |
| 1168 |
|
| 1169 |
for (feature in features[service]) { |
| 1170 |
$('.' + feature).prop('disabled', !features[service][feature]); |
| 1171 |
} |
| 1172 |
|
| 1173 |
$('.wpo_smush_image').each(function() { |
| 1174 |
if ($(this).data('filesize') > wposmush.features[service]["max_filesize"]) { |
| 1175 |
$(this).hide(); |
| 1176 |
} else { |
| 1177 |
$(this).show(); |
| 1178 |
} |
| 1179 |
}) |
| 1180 |
} |
| 1181 |
|
| 1182 |
/** |
| 1183 |
* Disable smush controls (buttons, checkboxes) in bulk mode |
| 1184 |
* |
| 1185 |
* @param {boolean} disable - if true then disable controls, false - enable. |
| 1186 |
* |
| 1187 |
* @return void |
| 1188 |
*/ |
| 1189 |
function disable_image_optimization_controls(disable) { |
| 1190 |
$.each([ |
| 1191 |
smush_selected_images_btn, |
| 1192 |
smush_images_select_all_btn, |
| 1193 |
smush_images_select_none_btn, |
| 1194 |
smush_images_refresh_btn, |
| 1195 |
smush_mark_as_compressed_btn, |
| 1196 |
], function(i, el) { |
| 1197 |
el.prop('disabled', disable); |
| 1198 |
}); |
| 1199 |
|
| 1200 |
if (disable) { |
| 1201 |
$('#wpo_smush_images_refresh').hide(); |
| 1202 |
$('.wpo_smush_images_loader').show(); |
| 1203 |
} else { |
| 1204 |
$('#wpo_smush_images_refresh').show(); |
| 1205 |
$('.wpo_smush_images_loader').hide(); |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
/** |
| 1210 |
* Gets selected image and make an ajax request to compress it. |
| 1211 |
* |
| 1212 |
* @param {Object} selected_image - { attachment_id: ..., blog_id: ... } |
| 1213 |
* @param {Array} smush_options - The options to use |
| 1214 |
* |
| 1215 |
* @return void |
| 1216 |
*/ |
| 1217 |
function smush_selected_image(selected_image, smush_options) { |
| 1218 |
|
| 1219 |
// if no selected images then exit. |
| 1220 |
if (0 == selected_image.length) return; |
| 1221 |
|
| 1222 |
data = { |
| 1223 |
selected_image: selected_image, |
| 1224 |
smush_options: smush_options, |
| 1225 |
skip_notice: true |
| 1226 |
} |
| 1227 |
|
| 1228 |
block_ui(wposmush.compress_single_image_dialog, {}, false, true); |
| 1229 |
smush_manager_send_command('compress_single_image', data, function(resp) { |
| 1230 |
handle_response_from_smush_manager(resp, update_view_single_image_complete); |
| 1231 |
}); |
| 1232 |
} |
| 1233 |
|
| 1234 |
/** |
| 1235 |
* Get selected image and make an ajax request to compress it. |
| 1236 |
* |
| 1237 |
* @param {Number} blog_id - The blog id |
| 1238 |
* @param {Number} selected_image - The image id |
| 1239 |
* @param {Function} done_callback - Optional. It will be called when the AJAX command is done, used for multiple calls that need a single `done` action (like bulk_restore) |
| 1240 |
* |
| 1241 |
* @return void |
| 1242 |
*/ |
| 1243 |
function restore_selected_image(blog_id, selected_image, done_callback) { |
| 1244 |
|
| 1245 |
// if no selected images then exit. |
| 1246 |
if (0 == selected_image.length) return; |
| 1247 |
|
| 1248 |
block_ui(wposmush.please_wait); |
| 1249 |
var data = { 'blog_id': blog_id, 'selected_image': selected_image }; |
| 1250 |
|
| 1251 |
smush_manager_send_command.apply({unique: false}, ['restore_single_image', data, function(resp) { |
| 1252 |
var done = function(resp_summary_alt) { |
| 1253 |
if ('undefined' != typeof(resp_summary_alt)) { |
| 1254 |
resp.summary = resp_summary_alt; |
| 1255 |
} |
| 1256 |
|
| 1257 |
handle_response_from_smush_manager(resp, update_view_single_image_complete); |
| 1258 |
} |
| 1259 |
|
| 1260 |
if (done_callback instanceof Function) { |
| 1261 |
done_callback(done); |
| 1262 |
} else { |
| 1263 |
done(); |
| 1264 |
} |
| 1265 |
}]); |
| 1266 |
} |
| 1267 |
|
| 1268 |
/** |
| 1269 |
* Updates the view once a single image is compressed or restored. |
| 1270 |
* |
| 1271 |
* @param {Object} resp - response from smush manager. |
| 1272 |
* |
| 1273 |
* @return void |
| 1274 |
*/ |
| 1275 |
function update_view_single_image_complete(resp) { |
| 1276 |
|
| 1277 |
if (resp.hasOwnProperty('success') && resp.success) { |
| 1278 |
$(".smush-information").text(resp.summary); |
| 1279 |
block_ui('', $.extend(block_ui_custom_options, {message: $("#smush-information-modal")})); |
| 1280 |
|
| 1281 |
$('.wpo-toggle-advanced-options.wpo_smush_single_image').removeClass('opened'); |
| 1282 |
|
| 1283 |
update_view_singe_image_compress(resp.operation, resp.summary, resp.restore_possible, resp); |
| 1284 |
|
| 1285 |
// here we store data from the the response |
| 1286 |
// this information will be used to show correct UI elements |
| 1287 |
// when smush metabox will shown again without reloading main page |
| 1288 |
var blog_id = resp.blog_id || resp.options.blog_id, |
| 1289 |
image_id = resp.image || resp.options.attachment_id; |
| 1290 |
|
| 1291 |
if (!smush_affected_images.hasOwnProperty(blog_id)) smush_affected_images[blog_id] = {}; |
| 1292 |
if (!smush_affected_images[blog_id].hasOwnProperty(image_id)) smush_affected_images[blog_id][image_id] = {}; |
| 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 |
|
| 1298 |
if ('compress' == resp.operation) { |
| 1299 |
smush_affected_images[blog_id][image_id] = { |
| 1300 |
operation: resp.operation, |
| 1301 |
summary: resp.summary, |
| 1302 |
restore_possible: resp.restore_possible |
| 1303 |
} |
| 1304 |
} else { |
| 1305 |
if (image_id) { |
| 1306 |
reset_bulk_actions_dropdown(); |
| 1307 |
} |
| 1308 |
smush_affected_images[blog_id][image_id] = { |
| 1309 |
operation: resp.operation |
| 1310 |
} |
| 1311 |
} |
| 1312 |
} else { |
| 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); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 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 |
/** |
| 1331 |
* Update metabox view depending on a command response. |
| 1332 |
* |
| 1333 |
* @param {string} operation |
| 1334 |
* @param {string} summary |
| 1335 |
* @param {boolean} restore_possible |
| 1336 |
* @param {object} smush_image_data |
| 1337 |
*/ |
| 1338 |
function update_view_singe_image_compress(operation, summary, restore_possible, smush_image_data) { |
| 1339 |
var wrapper = $("#smush_info").closest('#smush-metabox-inside-wrapper'); |
| 1340 |
|
| 1341 |
if (0 === wrapper.length) return; |
| 1342 |
|
| 1343 |
if ('compress' == operation) { |
| 1344 |
$(".wpo_smush_single_image").hide(); |
| 1345 |
$(".wpo_restore_single_image").show(); |
| 1346 |
|
| 1347 |
if (smush_image_data && smush_image_data.hasOwnProperty('sizes-info')) { |
| 1348 |
$("#smush_info").text(summary); |
| 1349 |
$("#wpo_smush_details").html(smush_image_data['sizes-info']); |
| 1350 |
} else { |
| 1351 |
$("#smush_info").text(summary); |
| 1352 |
$("#wpo_smush_details").text('').hide(); |
| 1353 |
} |
| 1354 |
|
| 1355 |
$('.wpo_smush_mark_single_image').hide(); |
| 1356 |
|
| 1357 |
if (restore_possible) { |
| 1358 |
$(".restore_possible").show(); |
| 1359 |
} else { |
| 1360 |
$(".restore_possible").hide(); |
| 1361 |
} |
| 1362 |
} else { |
| 1363 |
$(".wpo_smush_single_image").show(); |
| 1364 |
$(".wpo_restore_single_image").hide(); |
| 1365 |
|
| 1366 |
$('.wpo_smush_mark_single_image').show(); |
| 1367 |
$('.wpo_smush_unmark_single_image', wrapper).hide(); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
/** |
| 1372 |
* Handle smush metabox load event. This handler is used to restore correct smush metabox view. |
| 1373 |
*/ |
| 1374 |
$(document).on('admin-metabox-smush-loaded', function() { |
| 1375 |
var image_data = $('.wpo_restore_single_image input[type="button"]').first().data(); |
| 1376 |
|
| 1377 |
if (!image_data) return; |
| 1378 |
|
| 1379 |
if (smush_affected_images.hasOwnProperty(image_data.blog) && smush_affected_images[image_data.blog].hasOwnProperty(image_data.id)) { |
| 1380 |
var smush_image_data = smush_affected_images[image_data.blog][image_data.id]; |
| 1381 |
|
| 1382 |
if ('compress' == smush_image_data.operation) { |
| 1383 |
update_view_singe_image_compress(smush_image_data.operation, smush_image_data.summary, smush_image_data.restore_possible, smush_image_data); |
| 1384 |
} else { |
| 1385 |
update_view_singe_image_compress(smush_image_data.operation); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
}); |
| 1389 |
|
| 1390 |
/** |
| 1391 |
* Check returned response from the smush manager and call update view callback. |
| 1392 |
* |
| 1393 |
* @param {Object} resp - response from smush manager. |
| 1394 |
* @param {Function} update_view_callback - callback function to update view. |
| 1395 |
* |
| 1396 |
* @return void |
| 1397 |
*/ |
| 1398 |
function handle_response_from_smush_manager(resp, update_view_callback) { |
| 1399 |
if (resp && resp.hasOwnProperty('status') && resp.status) { |
| 1400 |
if (update_view_callback) update_view_callback(resp); |
| 1401 |
} else { |
| 1402 |
alert(wposmush.error_unexpected_response); |
| 1403 |
console.log(resp); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
/** |
| 1408 |
* Retrieves the selected images from the media library. |
| 1409 |
* |
| 1410 |
* @return {jQuery} - The selected images as a jQuery object. |
| 1411 |
*/ |
| 1412 |
function get_media_library_selected_images() { |
| 1413 |
return $('input[name="media[]"]:checked'); |
| 1414 |
} |
| 1415 |
|
| 1416 |
/** |
| 1417 |
* Retrieves the list of selected images from the media library. |
| 1418 |
* |
| 1419 |
* @return {Array.<number>} The list of selected image IDs. |
| 1420 |
*/ |
| 1421 |
function get_media_library_selected_images_list() { |
| 1422 |
var selected_images_list = []; |
| 1423 |
var $selected_images = get_media_library_selected_images(); |
| 1424 |
$selected_images.each(function(index, element) { |
| 1425 |
selected_images_list.push(parseInt(element.value)); |
| 1426 |
}); |
| 1427 |
return selected_images_list; |
| 1428 |
} |
| 1429 |
|
| 1430 |
/** |
| 1431 |
* Resets the bulk action dropdown by setting its value to "-1" and unchecking all the selected checkboxes. |
| 1432 |
* |
| 1433 |
* @returns {void} |
| 1434 |
*/ |
| 1435 |
function reset_bulk_actions_dropdown() { |
| 1436 |
$('#bulk-action-selector-top, #bulk-action-selector-bottom').val("-1"); |
| 1437 |
$('input[name="media[]"]:checked, #cb-select-all-1, #cb-select-all-2').prop('checked', false); |
| 1438 |
} |
| 1439 |
|
| 1440 |
/** |
| 1441 |
* Send an action to the task manager via admin-ajax.php. |
| 1442 |
* |
| 1443 |
* @param {string} action The action to send |
| 1444 |
* @param {[type]} data Data to send |
| 1445 |
* @param {Function} callback Will be called with the results |
| 1446 |
* @param {boolean} json_parse JSON parse the results |
| 1447 |
* @param {boolean} wait Wait for the response |
| 1448 |
* |
| 1449 |
* @return {JSON} |
| 1450 |
*/ |
| 1451 |
function smush_manager_send_command(action, data, callback, json_parse, wait) { |
| 1452 |
|
| 1453 |
json_parse = ('undefined' === typeof json_parse) ? true : json_parse; |
| 1454 |
var _wait = ('undefined' === typeof wait) ? false : wait; |
| 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 |
|
| 1463 |
(function(single_callback, _keep, _unique) { |
| 1464 |
heartbeat_agents.push(heartbeat.add_agent({ |
| 1465 |
_wait: _wait, |
| 1466 |
_keep: _keep, |
| 1467 |
_unique: _unique, |
| 1468 |
command: 'updraft_smush_ajax', |
| 1469 |
command_data: {data: data, subaction: action}, |
| 1470 |
callback: function(response) { |
| 1471 |
if (json_parse) { |
| 1472 |
try { |
| 1473 |
var resp = wpo_parse_json(response); |
| 1474 |
} catch (e) { |
| 1475 |
console.log("smush_manager_send_command JSON parse error"); |
| 1476 |
console.log(e); |
| 1477 |
console.log(response); |
| 1478 |
alert(wposmush.error_unexpected_response); |
| 1479 |
} |
| 1480 |
if ('undefined' !== typeof single_callback) single_callback(resp); |
| 1481 |
} else { |
| 1482 |
if ('undefined' !== typeof single_callback) single_callback(response); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
})); |
| 1486 |
})(callback, this.keep, this.unique); |
| 1487 |
}; |
| 1488 |
|
| 1489 |
// Attach heartbeat API events |
| 1490 |
heartbeat.setup(); |
| 1491 |
|
| 1492 |
// Gather smush options |
| 1493 |
var get_smush_options = function() { |
| 1494 |
var image_quality = ''; |
| 1495 |
if ($('#enable_custom_compression').is(":checked")) { |
| 1496 |
image_quality = $('#custom_compression_slider').val(); |
| 1497 |
} else { |
| 1498 |
// The '90' here has to be kept in sync with WP_Optimize::admin_page_wpo_images_smush() |
| 1499 |
image_quality = $('#enable_lossy_compression').is(":checked") ? 60 : 92; |
| 1500 |
} |
| 1501 |
var lossy_compression = image_quality < 92 ? true : false; |
| 1502 |
|
| 1503 |
return { |
| 1504 |
'compression_server': $("input[name='compression_server']:checked").val(), |
| 1505 |
'image_quality': image_quality, |
| 1506 |
'lossy_compression': lossy_compression, |
| 1507 |
'back_up_original': $('#smush-backup-original').is(":checked"), |
| 1508 |
'back_up_delete_after': $('#smush-backup-delete').is(":checked"), |
| 1509 |
'back_up_delete_after_days': $('#smush-backup-delete-days').val(), |
| 1510 |
'preserve_exif': $('#smush-preserve-exif').is(":checked"), |
| 1511 |
'autosmush': $('#smush-automatically').is(":checked"), |
| 1512 |
'show_smush_metabox': $('#smush-show-metabox').is(":checked"), |
| 1513 |
'webp_conversion': $('#enable_webp_conversion').is(":checked") |
| 1514 |
}; |
| 1515 |
} |
| 1516 |
wp_optimize.smush_settings = get_smush_options; |
| 1517 |
} // END WP_Optimize_Smush |
| 1518 |
|
| 1519 |
|