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 +168 -109 3.4.1 → 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,26 +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'));
124 - $('#wpo_section_' + $(this).data('tabname')).toggleClass('wpo-disabled-section', $(this).is(':not(:checked)'));
123 + toggle_minify_tab_section($(this));
125 124 })
126 - // Set value on page load
125 + // Initialize UI on page load
127 126 .each(function() {
128 - $('#wp-optimize-nav-tab-wrapper__wpo_minify a[data-tab="' + $(this).data('tabname') + '"] span.disabled').toggleClass('hidden', $(this).is(':checked'));
129 - $('#wpo_section_' + $(this).data('tabname')).toggleClass('wpo-disabled-section', $(this).is(':not(:checked)'));
127 + toggle_minify_tab_section($(this));
130 128 });
131 -
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 +
132 151 // slider enable Debug mode
133 152 $('#wpo_min_enable_minify_debug').on('wp-optimize/minify/saved_setting', function() {
134 153 // Refresh the page as it's needed to show the extra options
135 - $.blockUI({message: '<h1>'+wpoptimize.page_refresh+'</h1>'});
154 + block_ui(wpoptimize.page_refresh);
136 155 location.href = $('#wp-optimize-nav-tab-wpo_minify-advanced').prop('href');
137 156 });
138 157
139 158 // Edit default exclusions
@@ -141,8 +160,18 @@
141 160 // Show exclusions section
142 161 $('.wpo-minify-default-exclusions').toggleClass('hidden', !$(this).prop('checked'));
143 162 });
144 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 +
145 174 // Save settings
146 175 $('.wp-optimize-save-minify-settings').on('click', function(e) {
147 176 e.preventDefault();
148 177 var btn = $(this),
@@ -150,11 +179,16 @@
150 179 success_icon = spinner.next(),
151 180 $need_refresh_btn = null;
152 181
153 182 spinner.show();
154 - $.blockUI();
183 + block_ui(wpoptimize.saving);
155 184
156 185 var data = {};
186 +
187 + var merge_state = {
188 + js: null,
189 + css: null
190 + };
157 191
158 192 var tabs = $('[data-whichpage="wpo_minify"] .wp-optimize-nav-tab-contents form');
159 193 tabs.each(function() {
160 194 var tab = $(this);
@@ -160,8 +194,16 @@
160 194 var tab = $(this);
161 195 if (true === tab.data('need_saving')) {
162 196 data = Object.assign(data, gather_data(tab));
163 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 + }
164 206 }
165 207 });
166 208
167 209 send_command('save_minify_settings', data, function(response) {
@@ -176,8 +218,14 @@
176 218 if (response.hasOwnProperty('files')) {
177 219 minify.updateFilesLists(response.files);
178 220 minify.updateStats(response.files);
179 221 }
222 +
223 + $.each(merge_state, function(type, enabled) {
224 + if (null !== enabled) {
225 + toggle_minify_notice(type, enabled, false);
226 + }
227 + });
180 228
181 229 spinner.hide();
182 230 success_icon.show();
183 231 setTimeout(function() {
@@ -188,14 +236,53 @@
188 236 }).always(function() {
189 237 $.unblockUI();
190 238 });
191 239 })
192 -
240 +
193 241 // Dismiss information notice
194 - $('.wp-optimize-minify-status-information-notice').on('click', '.notice-dismiss', function(e) {
242 + $('.wpo_section_notice_minify').on('click', '.notice-dismiss', function(e) {
195 243 e.preventDefault();
196 - send_command('hide_minify_notice');
244 + toggle_minify_notice('minify', null, true);
197 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 + }
198 285
199 286 // Show logs
200 287 $('#wpo_min_jsprocessed, #wpo_min_cssprocessed').on('click', '.log', function(e) {
201 288 e.preventDefault();
@@ -205,9 +292,9 @@
205 292 $(this).text(wpoptimize.hide_information);
206 293 } else {
207 294 $(this).text(wpoptimize.show_information);
208 295 }
209 - });
296 + });
210 297
211 298 // Delete log file
212 299 $('#wpo_min_jsprocessed, #wpo_min_cssprocessed').on('click', '.delete-file', function(e) {
213 300 e.preventDefault();
@@ -218,9 +305,9 @@
218 305 alert(wpoptimize.cache_file_not_found);
219 306 return false;
220 307 }
221 308
222 - $.blockUI();
309 + block_ui(wpoptimize.deleting);
223 310 send_command('delete_minify_cache_file', filename, function(response) {
224 311 if('' !== response.files) {
225 312 minify.updateFilesLists(response.files);
226 313 minify.updateStats(response.files);
@@ -235,28 +322,8 @@
235 322 $.unblockUI();
236 323 });
237 324 });
238 325
239 - // Handle js excludes
240 - $('#wpo_min_jsprocessed').on('click', '.exclude', function(e) {
241 - e.preventDefault();
242 - var el = $(this);
243 - var excluded_file = get_excluded_file(el);
244 - add_excluded_js_file(excluded_file);
245 - tab_need_saving('js');
246 - highlight_excluded_item(el);
247 - });
248 -
249 - // Handle css excludes
250 - $('#wpo_min_cssprocessed').on('click', '.exclude', function(e) {
251 - e.preventDefault();
252 - var el = $(this);
253 - var excluded_file = get_excluded_file(el);
254 - add_excluded_css_file(excluded_file);
255 - tab_need_saving('css');
256 - highlight_excluded_item(el);
257 - });
258 -
259 326 // Trigger combined meta.json file download
260 327 $('.wpo-minify-download-metas-button').on('click', function(e) {
261 328 e.preventDefault();
262 329 send_command('get_minify_meta_files', null, function(response) {
@@ -273,83 +340,68 @@
273 340 link.click();
274 341 })
275 342 });
276 343
277 - /**
278 - * Get excluded file url
279 - *
280 - * @param {HTMLElement} el
281 - *
282 - * @return {string}
283 - */
284 - function get_excluded_file(el) {
285 - return el.data('url');
286 - }
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 + });
287 349
288 - /**
289 - * Exclude js file
290 - *
291 - * @param {string} excluded_file File url
292 - */
293 - function add_excluded_js_file(excluded_file) {
294 - var $js_textarea = $('#exclude_js');
295 - var list_of_excluded_files = $js_textarea.val();
296 - list_of_excluded_files += excluded_file + '\n';
297 - $js_textarea.val(list_of_excluded_files);
298 - }
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 + });
299 355
300 - /**
301 - * Exclude css file
302 - *
303 - * @param {string} excluded_file File url
304 - */
305 - function add_excluded_css_file(excluded_file) {
306 - var $css_textarea = $('#exclude_css');
307 - var list_of_excluded_files = $css_textarea.val();
308 - list_of_excluded_files += excluded_file + '\n';
309 - $css_textarea.val(list_of_excluded_files);
310 - }
311 -
312 356 // Handle defer
313 357 $('#wpo_min_jsprocessed').on('click', '.defer', function(e) {
314 358 e.preventDefault();
315 - add_deferred_file($(this));
359 + get_url_and_tab_saving($(this), '#async_js', 'js');
316 360 });
317 361
318 362 // Handle async loading
319 363 $('#wpo_min_cssprocessed').on('click', '.async', function(e) {
320 364 e.preventDefault();
321 - add_async_file($(this));
365 + get_url_and_tab_saving($(this), '#async_css', 'css');
322 366 });
323 -
367 +
324 368 /**
325 - * Add deferred file
369 + * Get url from an element and then save it
326 370 *
327 - * @param {HTMLElement} el target element
371 + * @param {HTMLElement} el
372 + * @param {string} target_el_id
373 + * @param {string} tab_name
374 + *
375 + * @return void
328 376 */
329 - function add_deferred_file(el) {
330 - var deferred_file = el.data('url');
331 - var $async_js_textarea = $('#async_js');
332 - var list_of_deferred_files = $async_js_textarea.val();
333 - list_of_deferred_files += deferred_file + '\n';
334 - $async_js_textarea.val(list_of_deferred_files);
335 - 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);
336 385 highlight_excluded_item(el);
337 386 }
338 -
387 +
339 388 /**
340 - * Add asynchronously loading file
389 + * Add url to the next line of the element textarea
341 390 *
342 - * @param {HTMLElement} el target element
391 + * @param {string} target_el_id
392 + * @param {string} file_url
393 + *
394 + * @return void
343 395 */
344 - function add_async_file(el) {
345 - var async_file = el.data('url');
346 - var $async_css_textarea = $('#async_css');
347 - var list_of_async_files = $async_css_textarea.val();
348 - list_of_async_files += async_file + '\n';
349 - $async_css_textarea.val(list_of_async_files);
350 - tab_need_saving('css');
351 - 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);
352 404 }
353 405
354 406 /**
355 407 *
@@ -520,8 +572,16 @@
520 572 function update_minify_size_information(response) {
521 573 $('#wpo_min_cache_size').text(response.size);
522 574 $('#wpo_min_cache_total_size').text(response.total_size);
523 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 +
524 584 return this;
525 585 }
526 586
527 587 /**
@@ -590,9 +650,8 @@
590 650 ');
591 651 }
592 652 });
593 653 }
594 -
595 654 $('#wpo_min_cssprocessed ul.processed .no-files-yet').toggle(!data.css.length);
596 655
597 656 // Remove <li> if it's not in the files array
598 657 $('#wpo_min_jsprocessed ul.processed > li, #wpo_min_cssprocessed ul.processed > li').each(function () {
@@ -635,17 +694,17 @@
635 694 });
636 695 return data;
637 696 }
638 697
639 - /**
640 - * Reduces the form elements array into an object
641 - *
642 - * @param {Object} collection An empty object
643 - * @param {*} item form input element as array element
644 - *
645 - * @returns {Object} collection An object of form data
646 - */
647 - 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) {
648 707 // Ignore items containing [], which we expect to be returned as arrays
649 708 if (item.name.includes('[]')) return collection;
650 709 collection[item.name] = item.value;
651 710 return collection;
@@ -663,5 +722,5 @@
663 722 }
664 723
665 724 wp_optimize.minify = minify;
666 725
667 -})(jQuery);
726 +})(jQuery);