PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.8.0
TinyPNG – JPEG, PNG & WebP image compression v3.8.0
3.8.0 3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / js / admin.js
tiny-compress-images / src / js Last commit date
admin.js 2 months ago bulk-optimization.js 1 week ago dashboard-widget.js 1 year ago
admin.js
378 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 compressImageSelection();
305 watchCompressingImages();
306
307 jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action]');
308 jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action2]');
309 jQuery('<option>').val('tiny_bulk_mark_compressed').text(tinyCompress.L10nBulkMarkCompressed).appendTo('select[name=action]');
310 jQuery('<option>').val('tiny_bulk_mark_compressed').text(tinyCompress.L10nBulkMarkCompressed).appendTo('select[name=action2]');
311 break;
312 case 'post-php':
313 eventOn('click', 'button.tiny-compress', compressImage);
314 break;
315 case 'settings_page_tinify':
316 changeEnterKeyTarget('div.tiny-account-status create', '[data-tiny-action=create-key]');
317 changeEnterKeyTarget('div.tiny-account-status update', '[data-tiny-action=update-key]');
318
319 eventOn('click', '[data-tiny-action=create-key]', submitKey);
320 eventOn('click', '[data-tiny-action=update-key]', submitKey);
321 eventOn('click', '#change-key', toggleChangeKey);
322 eventOn('click', '#cancel-change-key', toggleChangeKey);
323
324 var target = jQuery('#tiny-account-status[data-state=pending]');
325 if (target.length) {
326 jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) {
327 target.replaceWith(data);
328 });
329 }
330
331 async function refreshSizeDescriptionNotice() {
332 const totalSelectedSizes = document.querySelectorAll('input[name*=tinypng_sizes]:checked').length;
333 const compressWr2x = document.querySelector('#tinypng_sizes_wr2x')?.checked ?? false;
334 const selectedCount = compressWr2x ? totalSelectedSizes - 1 : totalSelectedSizes;
335
336 const separator = ajaxurl.includes('?') ? '&' : '?';
337 let imageCountUrl = `${ajaxurl}${separator}action=tiny_image_sizes_notice&image_sizes_selected=${selectedCount}&_ajax_nonce=${tinyCompress.nonce}`;
338
339 const resizeOriginalChecked = document.querySelector('#tinypng_resize_original_enabled')?.checked;
340 const compressOriginalChecked = document.querySelector('#tinypng_sizes_0')?.checked;
341 if (resizeOriginalChecked && compressOriginalChecked) {
342 imageCountUrl += '&resize_original=true';
343 }
344 if (compressWr2x) {
345 imageCountUrl += '&compress_wr2x=true';
346 }
347 try {
348 const sizeDescriptionHtml = await fetch(imageCountUrl).then(r => r.text());
349 document.querySelector('#tiny-image-sizes-notice').innerHTML = sizeDescriptionHtml;
350 } catch (err) {
351 document.querySelector('#tiny-image-sizes-notice').innerHTML = '';
352 console.error(err);
353 }
354 }
355
356 eventOn('click', 'input[name*=tinypng_sizes], #tinypng_resize_original_enabled', refreshSizeDescriptionNotice);
357 eventOn('click', '#tinypng_auto_compress_enabled', updateSettings);
358 jQuery('#tinypng_sizes_0, #tinypng_resize_original_enabled').click(updateSettings);
359 updateSettings();
360 }
361
362 jQuery('.tiny-notice a.tiny-dismiss').click(dismissNotice);
363 jQuery(function() {
364 jQuery('.tiny-notice.is-dismissible button').unbind('click').click(dismissNotice);
365 });
366
367
368 function onConvertChange(e) {
369 const newValue = e.target.checked;
370 if (newValue) {
371 jQuery('#tinypng_convert_convert_to').removeAttr('disabled');
372 } else {
373 jQuery('#tinypng_convert_convert_to').attr('disabled', true);
374 }
375 }
376 jQuery('#tinypng_conversion_convert').on('change', onConvertChange);
377 }).call();
378