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