PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | js/minify.js +179 -114 3.3.2 → 4.7.0 View file →
@@ -1,6 +1,7 @@
1 1 (function ($) {
2 2 var wp_optimize = window.wp_optimize || {};
3 + var block_ui = wp_optimize.block_ui;
3 4 var send_command = wp_optimize.send_command;
4 5 var refresh_frequency = wpoptimize.refresh_frequency || 30000;
5 6 var heartbeat = WP_Optimize_Heartbeat();
6 7 var heartbeat_agents = [];
@@ -32,9 +33,9 @@
32 33 * The standard handler for clearing the cache. Safe to use
33 34 */
34 35 $('.purge_minify_cache').on('click', function(e) {
35 36 e.preventDefault();
36 - $.blockUI();
37 + block_ui(wpoptimize.clearing_cache);
37 38 send_command('purge_minify_cache', null, function(response) {
38 39 if (response.hasOwnProperty('files')) {
39 40 minify.updateFilesLists(response.files);
40 41 minify.updateStats(response.files);
@@ -48,9 +49,9 @@
48 49 * Removes the entire cache dir.
49 50 * Use with caution, as cached html may still reference those files.
50 51 */
51 52 $('.purge_all_minify_cache').on('click', function() {
52 - $.blockUI();
53 + block_ui(wpoptimize.clearing_cache);
53 54 send_command('purge_all_minify_cache', null, function(response) {
54 55 if (response.hasOwnProperty('files')) {
55 56 minify.updateFilesLists(response.files);
56 57 minify.updateStats(response.files);
@@ -60,12 +61,12 @@
60 61 });
61 62 });
62 63
63 64 /**
64 - * Forces minifiy to create a new cache, safe to use
65 + * Forces minify to create a new cache, safe to use
65 66 */
66 67 $('.minify_increment_cache').on('click', function() {
67 - $.blockUI();
68 + block_ui(wpoptimize.creating_cache);
68 69 send_command('minify_increment_cache', null, function(response) {
69 70 if (response.hasOwnProperty('files')) {
70 71 minify.updateFilesLists(response.files);
71 72 minify.updateStats(response.files);
@@ -73,13 +74,12 @@
73 74 }).always(function() {
74 75 $.unblockUI();
75 76 });
76 77 });
77 -
78 78
79 79 // ======= SLIDERS ========
80 80 // Generic slider save
81 - $('#wp-optimize-nav-tab-wpo_minify-status-contents form :input, #wp-optimize-nav-tab-wpo_minify-js-contents form :input, #wp-optimize-nav-tab-wpo_minify-css-contents form :input, #wp-optimize-nav-tab-wpo_minify-font-contents form :input, #wp-optimize-nav-tab-wpo_minify-settings-contents form :input, #wp-optimize-nav-tab-wpo_minify-advanced-contents form :input').on('change', function() {
81 + $('#wp-optimize-nav-tab-wpo_minify-status-contents form :input, #wp-optimize-nav-tab-wpo_minify-js-contents form :input, #wp-optimize-nav-tab-wpo_minify-css-contents form :input, #wp-optimize-nav-tab-wpo_minify-font-contents form :input, #wp-optimize-nav-tab-wpo_minify-analytics-contents form :input, #wp-optimize-nav-tab-wpo_minify-settings-contents form :input, #wp-optimize-nav-tab-wpo_minify-advanced-contents form :input').on('change', function() {
82 82 $(this).closest('form').data('need_saving', true);
83 83 });
84 84
85 85 $('input[type=checkbox].wpo-save-setting').on('change', function(e) {
@@ -87,9 +87,9 @@
87 87 val = input.prop('checked'),
88 88 name = input.prop('name'),
89 89 data = {};
90 90 data[name] = val;
91 - $.blockUI();
91 + block_ui(wpoptimize.saving);
92 92 send_command('save_minify_settings', data, function(response) {
93 93 if (response.success) {
94 94 input.trigger('wp-optimize/minify/saved_setting');
95 95 if (response.hasOwnProperty('files')) {
@@ -114,24 +114,45 @@
114 114 $('#wpo_min_enable_minify').on('wp-optimize/minify/saved_setting', function() {
115 115
116 116 $(this).closest('.wpo_section').toggleClass('wpo-feature-is-disabled', !$(this).is(':checked'));
117 117 });
118 -
118 +
119 119 // Toggle wpo-feature-is-disabled class
120 120 $('#wpo_min_enable_minify_css, #wpo_min_enable_minify_js')
121 - // Set value on status change
121 + // Update UI when setting is saved
122 122 .on('wp-optimize/minify/saved_setting', function() {
123 - $('#wp-optimize-nav-tab-wrapper__wpo_minify a[data-tab="' + $(this).data('tabname') + '"] span.disabled').toggleClass('hidden', $(this).is(':checked'));
123 + toggle_minify_tab_section($(this));
124 124 })
125 - // Set value on page load
125 + // Initialize UI on page load
126 126 .each(function() {
127 - $('#wp-optimize-nav-tab-wrapper__wpo_minify a[data-tab="' + $(this).data('tabname') + '"] span.disabled').toggleClass('hidden', $(this).is(':checked'));
127 + toggle_minify_tab_section($(this));
128 128 });
129 -
129 +
130 + /**
131 + * Toggle the UI state of a minify tab section based on the associated checkbox state.
132 + *
133 + * - For JS minification, only elements marked with `.show-if-enabled` are toggled.
134 + *
135 + * @param {jQuery} $el The checkbox element controlling the minified setting
136 + *
137 + * @return {void}
138 + */
139 + function toggle_minify_tab_section($el) {
140 + var tab_name = $el.data('tabname');
141 + var tab_id = '#wpo_section_' + tab_name;
142 + var is_checked = $el.is(':checked');
143 +
144 + var $section = $el.is('#wpo_min_enable_minify_js') ? $(tab_id + ' .show-if-enabled') : $(tab_id);
145 +
146 + $('#wp-optimize-nav-tab-wrapper__wpo_minify a[data-tab="' + tab_name + '"] span.disabled').toggleClass('hidden', is_checked);
147 +
148 + $section.toggleClass('wpo-disabled-section', !is_checked);
149 + }
150 +
130 151 // slider enable Debug mode
131 152 $('#wpo_min_enable_minify_debug').on('wp-optimize/minify/saved_setting', function() {
132 153 // Refresh the page as it's needed to show the extra options
133 - $.blockUI({message: '<h1>'+wpoptimize.page_refresh+'</h1>'});
154 + block_ui(wpoptimize.page_refresh);
134 155 location.href = $('#wp-optimize-nav-tab-wpo_minify-advanced').prop('href');
135 156 });
136 157
137 158 // Edit default exclusions
@@ -139,8 +160,18 @@
139 160 // Show exclusions section
140 161 $('.wpo-minify-default-exclusions').toggleClass('hidden', !$(this).prop('checked'));
141 162 });
142 163
164 + var disable_google_fonts_processing = $('#disable_google_fonts_processing');
165 +
166 + function handle_disable_google_fonts_processing() {
167 + $('#wp-optimize-nav-tab-wpo_minify-font-contents .google_fonts_option:not(.skip-disable-processing)').prop('disabled', disable_google_fonts_processing.is(':checked'));
168 + }
169 +
170 + disable_google_fonts_processing.on('change', handle_disable_google_fonts_processing);
171 +
172 + handle_disable_google_fonts_processing();
173 +
143 174 // Save settings
144 175 $('.wp-optimize-save-minify-settings').on('click', function(e) {
145 176 e.preventDefault();
146 177 var btn = $(this),
@@ -148,11 +179,16 @@
148 179 success_icon = spinner.next(),
149 180 $need_refresh_btn = null;
150 181
151 182 spinner.show();
152 - $.blockUI();
183 + block_ui(wpoptimize.saving);
153 184
154 185 var data = {};
186 +
187 + var merge_state = {
188 + js: null,
189 + css: null
190 + };
155 191
156 192 var tabs = $('[data-whichpage="wpo_minify"] .wp-optimize-nav-tab-contents form');
157 193 tabs.each(function() {
158 194 var tab = $(this);
@@ -158,8 +194,16 @@
158 194 var tab = $(this);
159 195 if (true === tab.data('need_saving')) {
160 196 data = Object.assign(data, gather_data(tab));
161 197 tab.data('need_saving', false);
198 +
199 + if (typeof data.enable_merging_of_js !== 'undefined') {
200 + merge_state.js = data.enable_merging_of_js === 'true';
201 + }
202 +
203 + if (typeof data.enable_merging_of_css !== 'undefined') {
204 + merge_state.css = data.enable_merging_of_css === 'true';
205 + }
162 206 }
163 207 });
164 208
165 209 send_command('save_minify_settings', data, function(response) {
@@ -174,8 +218,14 @@
174 218 if (response.hasOwnProperty('files')) {
175 219 minify.updateFilesLists(response.files);
176 220 minify.updateStats(response.files);
177 221 }
222 +
223 + $.each(merge_state, function(type, enabled) {
224 + if (null !== enabled) {
225 + toggle_minify_notice(type, enabled, false);
226 + }
227 + });
178 228
179 229 spinner.hide();
180 230 success_icon.show();
181 231 setTimeout(function() {
@@ -186,19 +236,64 @@
186 236 }).always(function() {
187 237 $.unblockUI();
188 238 });
189 239 })
190 -
240 +
191 241 // Dismiss information notice
192 - $('.wp-optimize-minify-status-information-notice').on('click', '.notice-dismiss', function(e) {
242 + $('.wpo_section_notice_minify').on('click', '.notice-dismiss', function(e) {
193 243 e.preventDefault();
194 - send_command('hide_minify_notice');
244 + toggle_minify_notice('minify', null, true);
195 245 });
246 +
247 + $('.wpo_section_notice_js').on('click', '.notice-dismiss', function(e) {
248 + e.preventDefault();
249 + toggle_minify_notice('js', null, true);
250 + });
251 +
252 + $('.wpo_section_notice_css').on('click', '.notice-dismiss', function(e) {
253 + e.preventDefault();
254 + toggle_minify_notice('css', null, true);
255 + });
256 +
257 + /**
258 + * Toggle visibility of a WP-Optimize minify notice and persist user preference.
259 + *
260 + * @param {string} type - Notice type ('minify', 'js', 'css').
261 + * @param {boolean|null} merging_enabled - Whether the JS/CSS merging checkbox is enabled (true = checked, false = unchecked, null = not applicable).
262 + * @param {boolean} force_hide - Whether to hide forcefully (true) or (false).
263 + *
264 + * @returns {void}
265 + */
266 + function toggle_minify_notice(type, merging_enabled, force_hide) {
267 + var $notice = $('.wpo_section_notice_' + type);
268 + if (!$notice.length) return;
269 +
270 + send_command('toggle_minify_notice', { type: type, merging_enabled: merging_enabled, force_hide: force_hide }, function(response) {
271 + if (response.hasOwnProperty('success') && response.success === true) {
272 + if (response.hasOwnProperty('hide') && response.hide === true) {
273 + $notice.stop(true, true).slideUp(300);
274 + }
275 +
276 + if (response.hasOwnProperty('hide') && response.hide === false) {
277 + $notice.stop(true, true).slideDown(300);
278 + }
279 + }
280 + if (response.hasOwnProperty('message') && response.success !== true) {
281 + wp_optimize.notices.show_notice('error', response.message);
282 + }
283 + });
284 + }
196 285
197 286 // Show logs
198 287 $('#wpo_min_jsprocessed, #wpo_min_cssprocessed').on('click', '.log', function(e) {
199 288 e.preventDefault();
200 289 $(this).nextAll('.wpo_min_log').slideToggle('fast');
290 + var link_text = $(this).text().trim();
291 + if (wpoptimize.show_information === link_text) {
292 + $(this).text(wpoptimize.hide_information);
293 + } else {
294 + $(this).text(wpoptimize.show_information);
295 + }
201 296 });
202 297
203 298 // Delete log file
204 299 $('#wpo_min_jsprocessed, #wpo_min_cssprocessed').on('click', '.delete-file', function(e) {
@@ -210,9 +305,9 @@
210 305 alert(wpoptimize.cache_file_not_found);
211 306 return false;
212 307 }
213 308
214 - $.blockUI();
309 + block_ui(wpoptimize.deleting);
215 310 send_command('delete_minify_cache_file', filename, function(response) {
216 311 if('' !== response.files) {
217 312 minify.updateFilesLists(response.files);
218 313 minify.updateStats(response.files);
@@ -227,28 +322,8 @@
227 322 $.unblockUI();
228 323 });
229 324 });
230 325
231 - // Handle js excludes
232 - $('#wpo_min_jsprocessed').on('click', '.exclude', function(e) {
233 - e.preventDefault();
234 - var el = $(this);
235 - var excluded_file = get_excluded_file(el);
236 - add_excluded_js_file(excluded_file);
237 - tab_need_saving('js');
238 - highlight_excluded_item(el);
239 - });
240 -
241 - // Handle css excludes
242 - $('#wpo_min_cssprocessed').on('click', '.exclude', function(e) {
243 - e.preventDefault();
244 - var el = $(this);
245 - var excluded_file = get_excluded_file(el);
246 - add_excluded_css_file(excluded_file);
247 - tab_need_saving('css');
248 - highlight_excluded_item(el);
249 - });
250 -
251 326 // Trigger combined meta.json file download
252 327 $('.wpo-minify-download-metas-button').on('click', function(e) {
253 328 e.preventDefault();
254 329 send_command('get_minify_meta_files', null, function(response) {
@@ -265,83 +340,68 @@
265 340 link.click();
266 341 })
267 342 });
268 343
269 - /**
270 - * Get excluded file url
271 - *
272 - * @param {HTMLElement} el
273 - *
274 - * @return {string}
275 - */
276 - function get_excluded_file(el) {
277 - return el.data('url');
278 - }
344 + // Handle js excludes
345 + $('#wpo_min_jsprocessed').on('click', '.exclude', function(e) {
346 + e.preventDefault();
347 + get_url_and_tab_saving($(this), '#exclude_js', 'js');
348 + });
279 349
280 - /**
281 - * Exclude js file
282 - *
283 - * @param {string} excluded_file File url
284 - */
285 - function add_excluded_js_file(excluded_file) {
286 - var $js_textarea = $('#exclude_js');
287 - var list_of_excluded_files = $js_textarea.val();
288 - list_of_excluded_files += excluded_file + '\n';
289 - $js_textarea.val(list_of_excluded_files);
290 - }
350 + // Handle css excludes
351 + $('#wpo_min_cssprocessed').on('click', '.exclude', function(e) {
352 + e.preventDefault();
353 + get_url_and_tab_saving($(this), '#exclude_css', 'css');
354 + });
291 355
292 - /**
293 - * Exclude css file
294 - *
295 - * @param {string} excluded_file File url
296 - */
297 - function add_excluded_css_file(excluded_file) {
298 - var $css_textarea = $('#exclude_css');
299 - var list_of_excluded_files = $css_textarea.val();
300 - list_of_excluded_files += excluded_file + '\n';
301 - $css_textarea.val(list_of_excluded_files);
302 - }
303 -
304 356 // Handle defer
305 357 $('#wpo_min_jsprocessed').on('click', '.defer', function(e) {
306 358 e.preventDefault();
307 - add_deferred_file($(this));
359 + get_url_and_tab_saving($(this), '#async_js', 'js');
308 360 });
309 361
310 362 // Handle async loading
311 363 $('#wpo_min_cssprocessed').on('click', '.async', function(e) {
312 364 e.preventDefault();
313 - add_async_file($(this));
365 + get_url_and_tab_saving($(this), '#async_css', 'css');
314 366 });
315 -
367 +
316 368 /**
317 - * Add deferred file
369 + * Get url from an element and then save it
318 370 *
319 - * @param {HTMLElement} el target element
371 + * @param {HTMLElement} el
372 + * @param {string} target_el_id
373 + * @param {string} tab_name
374 + *
375 + * @return void
320 376 */
321 - function add_deferred_file(el) {
322 - var deferred_file = el.data('url');
323 - var $async_js_textarea = $('#async_js');
324 - var list_of_deferred_files = $async_js_textarea.val();
325 - list_of_deferred_files += deferred_file + '\n';
326 - $async_js_textarea.val(list_of_deferred_files);
327 - tab_need_saving('js');
377 + function get_url_and_tab_saving(el, target_el_id, tab_name) {
378 + var file_url = el.data('url');
379 + if ('' === file_url || undefined === file_url) {
380 + return;
381 + }
382 +
383 + add_url_to_next_line(target_el_id, file_url);
384 + tab_need_saving(tab_name);
328 385 highlight_excluded_item(el);
329 386 }
330 -
387 +
331 388 /**
332 - * Add asynchronously loading file
389 + * Add url to the next line of the element textarea
333 390 *
334 - * @param {HTMLElement} el target element
391 + * @param {string} target_el_id
392 + * @param {string} file_url
393 + *
394 + * @return void
335 395 */
336 - function add_async_file(el) {
337 - var async_file = el.data('url');
338 - var $async_css_textarea = $('#async_css');
339 - var list_of_async_files = $async_css_textarea.val();
340 - list_of_async_files += async_file + '\n';
341 - $async_css_textarea.val(list_of_async_files);
342 - tab_need_saving('css');
343 - highlight_excluded_item(el);
396 + function add_url_to_next_line(target_el_id, file_url) {
397 + var $textarea = $(target_el_id);
398 + var list_of_files = $textarea.val().trim();
399 + if ('' !== list_of_files) {
400 + list_of_files += '\n';
401 + }
402 + list_of_files += file_url;
403 + $textarea.val(list_of_files);
344 404 }
345 405
346 406 /**
347 407 *
@@ -405,12 +465,9 @@
405 465
406 466 if (is_running) {
407 467 btn.data('running', false);
408 468
409 -
410 - while(agent_id = heartbeat_agents.shift()) {
411 - heartbeat.cancel_agent(agent_id);
412 - }
469 + heartbeat.cancel_agents(heartbeat_agents);
413 470
414 471 send_command(
415 472 'cancel_minify_preload',
416 473 null,
@@ -481,9 +538,10 @@
481 538 */
482 539 function run_update_minify_preload_status() {
483 540 var agent = heartbeat.add_agent({
484 541 command: 'get_minify_preload_status',
485 - callback: update_minify_preload_status
542 + callback: update_minify_preload_status,
543 + _keep: false
486 544 });
487 545
488 546 if (null !== agent) heartbeat_agents.push(agent);
489 547 }
@@ -514,8 +572,16 @@
514 572 function update_minify_size_information(response) {
515 573 $('#wpo_min_cache_size').text(response.size);
516 574 $('#wpo_min_cache_total_size').text(response.total_size);
517 575 }
576 +
577 + // switch for visibility (analytics setting tab).
578 + var $analytics = $('#wpo-analytics-hidden-content');
579 +
580 + $('#wpo_enable_analytics').on('change', function () {
581 + $analytics.css('display', $(this).prop('checked') ? 'block' : 'none');
582 + });
583 +
518 584 return this;
519 585 }
520 586
521 587 /**
@@ -549,9 +615,9 @@
549 615 if ($('#'+this.uid).length == 0) {
550 616 $('#wpo_min_jsprocessed ul.processed').append('\
551 617 <li id="'+this.uid+'">\
552 618 <span class="filename"><a href="'+this.file_url+'" target="_blank">'+this.filename+'</a> ('+this.fsize+')</span>\
553 - <a href="#" class="log">' + wpoptimize.toggle_info + '</a>\
619 + <a href="#" class="log">' + wpoptimize.show_information + '</a>\
554 620 <a href="#" class="delete-file" data-filename="' + this.filename + '">' + wpoptimize.delete_file + '</a>\
555 621 <div class="hidden save_notice">\
556 622 <p>' + wpoptimize.added_notice + '</p>\
557 623 <p><button class="button button-primary save-exclusions">' + wpoptimize.save_notice + '</button></p>\
@@ -572,9 +638,9 @@
572 638 if ($('#'+this.uid).length == 0) {
573 639 $('#wpo_min_cssprocessed ul.processed').append('\
574 640 <li id="'+this.uid+'">\
575 641 <span class="filename"><a href="'+this.file_url+'" target="_blank">'+this.filename+'</a> ('+this.fsize+')</span>\
576 - <a href="#" class="log">' + wpoptimize.toggle_info + '</a>\
642 + <a href="#" class="log">' + wpoptimize.show_information + '</a>\
577 643 <a href="#" class="delete-file" data-filename="' + this.filename + '">' + wpoptimize.delete_file + '</a>\
578 644 <div class="hidden save_notice">\
579 645 <p>' + wpoptimize.added_to_list + '</p>\
580 646 <p><button class="button button-primary save-exclusions">' + wpoptimize.save_notice + '</button></p>\
@@ -584,9 +650,8 @@
584 650 ');
585 651 }
586 652 });
587 653 }
588 -
589 654 $('#wpo_min_cssprocessed ul.processed .no-files-yet').toggle(!data.css.length);
590 655
591 656 // Remove <li> if it's not in the files array
592 657 $('#wpo_min_jsprocessed ul.processed > li, #wpo_min_cssprocessed ul.processed > li').each(function () {
@@ -607,9 +672,9 @@
607 672 }
608 673 };
609 674
610 675 /**
611 - * Gather data from the given form
676 + * Gather data from the given form
612 677 *
613 678 * @param {HTMLFormElement} form
614 679 *
615 680 * @returns {Array} Array of collected data from the form
@@ -629,17 +694,17 @@
629 694 });
630 695 return data;
631 696 }
632 697
633 - /**
634 - * Reduces the form elements array into an object
635 - *
636 - * @param {Object} collection An empty object
637 - * @param {*} item form input element as array element
638 - *
639 - * @returns {Object} collection An object of form data
640 - */
641 - function form_serialize_reduce_cb(collection, item) {
698 + /**
699 + * Reduces the form elements array into an object
700 + *
701 + * @param {Object} collection An empty object
702 + * @param {*} item form input element as array element
703 + *
704 + * @returns {Object} collection An object of form data
705 + */
706 + function form_serialize_reduce_cb(collection, item) {
642 707 // Ignore items containing [], which we expect to be returned as arrays
643 708 if (item.name.includes('[]')) return collection;
644 709 collection[item.name] = item.value;
645 710 return collection;
@@ -657,5 +722,5 @@
657 722 }
658 723
659 724 wp_optimize.minify = minify;
660 725
661 -})(jQuery);
726 +})(jQuery);