admin.js
375 lines
| 1 | (function() { |
| 2 | function downloadDiagnostics() { |
| 3 | try { |
| 4 | jQuery('#download-diagnostics-spinner').show(); |
| 5 | jQuery('#tiny-download-diagnostics').attr('disabled', true); |
| 6 | const downloadURL = `${ajaxurl}?action=tiny_download_diagnostics&security=${tinyCompress.nonce}`; |
| 7 | window.location.href = downloadURL; |
| 8 | } finally { |
| 9 | jQuery('#tiny-download-diagnostics').attr('disabled', false); |
| 10 | jQuery('#download-diagnostics-spinner').hide(); |
| 11 | } |
| 12 | } |
| 13 | jQuery('#tiny-download-diagnostics').click(downloadDiagnostics); |
| 14 | |
| 15 | function compressImage(event) { |
| 16 | var element = jQuery(event.target); |
| 17 | var container = element.closest('div.tiny-ajax-container'); |
| 18 | element.attr('disabled', 'disabled'); |
| 19 | container.find('span.spinner').removeClass('hidden'); |
| 20 | container.find('span.dashicons').remove(); |
| 21 | jQuery.ajax({ |
| 22 | url: ajaxurl, |
| 23 | type: 'POST', |
| 24 | data: { |
| 25 | _nonce: tinyCompress.nonce, |
| 26 | action: 'tiny_compress_image_from_library', |
| 27 | id: element.data('id') || element.attr('data-id') |
| 28 | }, |
| 29 | success: function(data) { |
| 30 | container.html(data); |
| 31 | }, |
| 32 | error: function() { |
| 33 | element.removeAttr('disabled'); |
| 34 | container.find('span.spinner').addClass('hidden'); |
| 35 | } |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | function compressImageSelection() { |
| 40 | const queryParams = new URLSearchParams(window.location.search); |
| 41 | const action = queryParams.get('action'); |
| 42 | jQuery('span.auto-compress').each(function(index, element) { |
| 43 | if (action === 'tiny_bulk_mark_compressed') { |
| 44 | jQuery(element).siblings('button.tiny-mark-as-compressed').click() |
| 45 | } |
| 46 | if (action === 'tiny_bulk_action') { |
| 47 | jQuery(element).siblings('button.tiny-compress').click() |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | function watchCompressingImages() { |
| 53 | if (jQuery('.details-container[data-status="compressing"]').length > 0) { |
| 54 | statusCheckIntervalId = setInterval(checkCompressingImages, 5000); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | function checkCompressingImages() { |
| 59 | jQuery('.details-container[data-status="compressing"]').each(function(index, element) { |
| 60 | element = jQuery(element); |
| 61 | var container = element.closest('div.tiny-ajax-container'); |
| 62 | jQuery.ajax({ |
| 63 | url: ajaxurl, |
| 64 | type: 'POST', |
| 65 | data: { |
| 66 | _nonce: tinyCompress.nonce, |
| 67 | action: 'tiny_get_compression_status', |
| 68 | id: element.attr('data-id') |
| 69 | }, |
| 70 | success: function(data) { |
| 71 | container.html(data); |
| 72 | if (jQuery('.details-container[data-status="compressing"]').length === 0) { |
| 73 | clearInterval(statusCheckIntervalId); |
| 74 | } |
| 75 | }, |
| 76 | error: function() { |
| 77 | element.removeAttr('disabled'); |
| 78 | container.find('span.spinner').addClass('hidden'); |
| 79 | } |
| 80 | }); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Marks an image attachment as compressed without actually compressing it. |
| 86 | * |
| 87 | * This function sends an AJAX request to mark an image as compressed by creating |
| 88 | * fake compression metadata. This is useful for images that are already optimized |
| 89 | * or when you want to skip compression for specific images while still marking |
| 90 | * them as processed in the system. |
| 91 | * |
| 92 | * @param {string} attachmentID - The WordPress attachment ID of the image to mark as compressed. |
| 93 | * @returns {Promise<string>} - string of html displaying the updated compressions. |
| 94 | */ |
| 95 | function markAsCompressed(attachmentID) { |
| 96 | return new Promise((resolve, reject) => { |
| 97 | jQuery.ajax({ |
| 98 | url: ajaxurl, |
| 99 | type: 'POST', |
| 100 | data: { |
| 101 | _nonce: tinyCompress.nonce, |
| 102 | action: 'tiny_mark_image_as_compressed', |
| 103 | id: attachmentID, |
| 104 | }, |
| 105 | success: function (data) { |
| 106 | resolve(data); |
| 107 | }, |
| 108 | error: function (err) { |
| 109 | reject(err); |
| 110 | }, |
| 111 | }); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | async function onClickButtonMarkAsCompressed(event) { |
| 116 | const element = jQuery(event.target); |
| 117 | var container = element.closest('div.tiny-ajax-container'); |
| 118 | element.attr('disabled', 'disabled'); |
| 119 | container.find('span.spinner').removeClass('hidden'); |
| 120 | container.find('span.dashicons').remove(); |
| 121 | const attachmentID = element.data('id') || element.attr('data-id'); |
| 122 | try { |
| 123 | const result = await markAsCompressed(attachmentID); |
| 124 | container.html(result); |
| 125 | } finally { |
| 126 | element.removeAttr('disabled'); |
| 127 | container.find('span.spinner').addClass('hidden'); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | function toggleChangeKey(event) { |
| 132 | jQuery('div.tiny-account-status div.update').toggle(); |
| 133 | jQuery('div.tiny-account-status div.status').toggle(); |
| 134 | jQuery('div.tiny-account-status div.upgrade').toggle(); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | function submitKey(event) { |
| 139 | event.preventDefault(); |
| 140 | jQuery(event.target).attr({disabled: true}).addClass('loading'); |
| 141 | |
| 142 | var action; |
| 143 | var parent = jQuery(event.target).closest('div'); |
| 144 | var key; |
| 145 | var email; |
| 146 | var name; |
| 147 | |
| 148 | if (jQuery(event.target).data('tiny-action') === 'update-key') { |
| 149 | action = 'update'; |
| 150 | key = parent.find('#tinypng_api_key').val(); |
| 151 | } else if (jQuery(event.target).data('tiny-action') === 'create-key') { |
| 152 | action = 'create'; |
| 153 | name = parent.find('#tinypng_api_key_name').val(); |
| 154 | email = parent.find('#tinypng_api_key_email').val(); |
| 155 | } else { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | jQuery.ajax({ |
| 160 | url: ajaxurl, |
| 161 | type: 'POST', |
| 162 | data: { |
| 163 | _nonce: tinyCompress.nonce, |
| 164 | action: 'tiny_settings_' + action + '_api_key', |
| 165 | key: key, |
| 166 | name: name, |
| 167 | email: email |
| 168 | }, |
| 169 | success: function(json) { |
| 170 | var status = jQuery.parseJSON(json); |
| 171 | |
| 172 | if (status.ok) { |
| 173 | var target = jQuery('#tiny-account-status'); |
| 174 | if (target.length) { |
| 175 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 176 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 177 | target.replaceWith(data); |
| 178 | }); |
| 179 | } |
| 180 | jQuery('div.tiny-notice[data-name="setting"]').remove(); |
| 181 | } else { |
| 182 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 183 | parent.addClass('failure'); |
| 184 | parent.find('p.message').text(status.message).show(); |
| 185 | } |
| 186 | }, |
| 187 | error: function() { |
| 188 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 189 | parent.addClass('failure'); |
| 190 | parent.find('p.message').text('Something went wrong, try again soon').show(); |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | function dismissNotice(event) { |
| 198 | var element = jQuery(event.target); |
| 199 | var notice = element.closest('.tiny-notice'); |
| 200 | element.attr('disabled', 'disabled'); |
| 201 | jQuery.ajax({ |
| 202 | url: ajaxurl, |
| 203 | type: 'POST', |
| 204 | dataType: 'json', |
| 205 | data: { |
| 206 | _nonce: tinyCompress.nonce, |
| 207 | action: 'tiny_dismiss_notice', |
| 208 | name: notice.data('name') || notice.attr('data-name') |
| 209 | }, |
| 210 | success: function(data) { |
| 211 | if (data) { |
| 212 | notice.remove(); |
| 213 | } |
| 214 | }, |
| 215 | error: function() { |
| 216 | element.removeAttr('disabled'); |
| 217 | } |
| 218 | }); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | function updateResizeSettings() { |
| 223 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 224 | jQuery('.tiny-resize-available').show(); |
| 225 | jQuery('.tiny-resize-unavailable').hide(); |
| 226 | } else { |
| 227 | jQuery('.tiny-resize-available').hide(); |
| 228 | jQuery('.tiny-resize-unavailable').show(); |
| 229 | } |
| 230 | |
| 231 | var original_enabled = propOf('#tinypng_resize_original_enabled', 'checked'); |
| 232 | jQuery('#tinypng_resize_original_width, #tinypng_resize_original_height').each(function (i, el) { |
| 233 | el.disabled = !original_enabled; |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | function updatePreserveSettings() { |
| 238 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 239 | jQuery('.tiny-preserve').show(); |
| 240 | } else { |
| 241 | jQuery('.tiny-preserve').hide(); |
| 242 | jQuery('#tinypng_preserve_data_creation').attr('checked', false); |
| 243 | jQuery('#tinypng_preserve_data_copyright').attr('checked', false); |
| 244 | jQuery('#tinypng_preserve_data_location').attr('checked', false); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | function updateSettings() { |
| 249 | updateResizeSettings(); |
| 250 | updatePreserveSettings(); |
| 251 | } |
| 252 | |
| 253 | var adminpage = ''; |
| 254 | if (typeof window.adminpage !== 'undefined') { |
| 255 | adminpage = window.adminpage; |
| 256 | } |
| 257 | |
| 258 | var statusCheckIntervalId; |
| 259 | |
| 260 | function eventOn(event, eventSelector, callback) { |
| 261 | if (typeof jQuery.fn.on === 'function') { |
| 262 | jQuery(document).on(event, eventSelector, callback); |
| 263 | } else { |
| 264 | jQuery(eventSelector).live(event, callback); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | function propOf(selector, property) { |
| 269 | if (typeof jQuery.fn.prop === 'function') { |
| 270 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 271 | property values into account. */ |
| 272 | return jQuery(selector).prop(property); |
| 273 | } else { |
| 274 | return jQuery(selector).attr(property); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | function setPropOf(selector, property, value) { |
| 279 | if (typeof jQuery.fn.prop === 'function') { |
| 280 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 281 | property values into account. */ |
| 282 | jQuery(selector).prop(property, value); |
| 283 | } else { |
| 284 | jQuery(selector).attr(property, value); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | function changeEnterKeyTarget(selector, button) { |
| 289 | eventOn('keyup keypress', selector, function(event) { |
| 290 | var code = event.keyCode || event.which; |
| 291 | if (code === 13) { |
| 292 | jQuery(button).click(); |
| 293 | return false; |
| 294 | } |
| 295 | }); |
| 296 | } |
| 297 | |
| 298 | switch (adminpage) { |
| 299 | case 'upload-php': |
| 300 | eventOn('click', 'button.tiny-compress', compressImage); |
| 301 | eventOn('click', 'button.tiny-mark-as-compressed', onClickButtonMarkAsCompressed); |
| 302 | |
| 303 | setPropOf('button.tiny-compress', 'disabled', null); |
| 304 | |
| 305 | compressImageSelection(); |
| 306 | watchCompressingImages(); |
| 307 | |
| 308 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action]'); |
| 309 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action2]'); |
| 310 | jQuery('<option>').val('tiny_bulk_mark_compressed').text(tinyCompress.L10nBulkMarkCompressed).appendTo('select[name=action]'); |
| 311 | jQuery('<option>').val('tiny_bulk_mark_compressed').text(tinyCompress.L10nBulkMarkCompressed).appendTo('select[name=action2]'); |
| 312 | break; |
| 313 | case 'post-php': |
| 314 | eventOn('click', 'button.tiny-compress', compressImage); |
| 315 | break; |
| 316 | case 'settings_page_tinify': |
| 317 | changeEnterKeyTarget('div.tiny-account-status create', '[data-tiny-action=create-key]'); |
| 318 | changeEnterKeyTarget('div.tiny-account-status update', '[data-tiny-action=update-key]'); |
| 319 | |
| 320 | eventOn('click', '[data-tiny-action=create-key]', submitKey); |
| 321 | eventOn('click', '[data-tiny-action=update-key]', submitKey); |
| 322 | eventOn('click', '#change-key', toggleChangeKey); |
| 323 | eventOn('click', '#cancel-change-key', toggleChangeKey); |
| 324 | |
| 325 | var target = jQuery('#tiny-account-status[data-state=pending]'); |
| 326 | if (target.length) { |
| 327 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 328 | target.replaceWith(data); |
| 329 | }); |
| 330 | } |
| 331 | |
| 332 | eventOn('click', 'input[name*=tinypng_sizes], #tinypng_resize_original_enabled', function() { |
| 333 | /* Unfortunately, we need some additional information to display |
| 334 | the correct notice. */ |
| 335 | var totalSelectedSizes = jQuery('input[name*=tinypng_sizes]:checked').length; |
| 336 | var compressWr2x = propOf('#tinypng_sizes_wr2x', 'checked'); |
| 337 | if (compressWr2x) { |
| 338 | totalSelectedSizes--; |
| 339 | } |
| 340 | |
| 341 | var image_count_url = ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes; |
| 342 | if (propOf('#tinypng_resize_original_enabled', 'checked') && propOf('#tinypng_sizes_0', 'checked')) { |
| 343 | image_count_url += '&resize_original=true'; |
| 344 | } |
| 345 | if (compressWr2x) { |
| 346 | image_count_url += '&compress_wr2x=true'; |
| 347 | } |
| 348 | jQuery('#tiny-image-sizes-notice').load(image_count_url); |
| 349 | }); |
| 350 | |
| 351 | eventOn('click', '#tinypng_auto_compress_enabled', function() { |
| 352 | updateSettings(); |
| 353 | }); |
| 354 | |
| 355 | jQuery('#tinypng_sizes_0, #tinypng_resize_original_enabled').click(updateSettings); |
| 356 | updateSettings(); |
| 357 | } |
| 358 | |
| 359 | jQuery('.tiny-notice a.tiny-dismiss').click(dismissNotice); |
| 360 | jQuery(function() { |
| 361 | jQuery('.tiny-notice.is-dismissible button').unbind('click').click(dismissNotice); |
| 362 | }); |
| 363 | |
| 364 | |
| 365 | function onConvertChange(e) { |
| 366 | const newValue = e.target.checked; |
| 367 | if (newValue) { |
| 368 | jQuery('#tinypng_convert_convert_to').removeAttr('disabled'); |
| 369 | } else { |
| 370 | jQuery('#tinypng_convert_convert_to').attr('disabled', true); |
| 371 | } |
| 372 | } |
| 373 | jQuery('#tinypng_conversion_convert').on('change', onConvertChange); |
| 374 | }).call(); |
| 375 |