PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.4
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.4
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
wp-optimize / js / minify.js

minify.js in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.4, at js/minify.js

761 lines 23.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 var wp_optimize = window.wp_optimize || {};
3 var block_ui = wp_optimize.block_ui;
4 var send_command = wp_optimize.send_command;
5 var refresh_frequency = wpoptimize.refresh_frequency || 30000;
6 var heartbeat = WP_Optimize_Heartbeat();
7 var heartbeat_agents = [];
8
9 if (!send_command) {
10 console.error('WP-Optimize Minify: wp_optimize.send_command is required.');
11 return;
12 }
13
14 var minify = {};
15
16 /**
17 * Initializing the minify feature and events
18 */
19 minify.init = function () {
20
21 var minify = this;
22 this.enabled = false;
23
24 $(document).on('wp-optimize/minify/toggle-status', function(e, params) {
25 if (params.hasOwnProperty('enabled')) {
26 $('[data-whichpage="wpo_minify"]').toggleClass('is-enabled', params.enabled)
27 minify.enabled = params.enabled;
28 if (minify.enabled) minify.getFiles();
29 }
30 });
31
32 /**
33 * The standard handler for clearing the cache. Safe to use
34 */
35 $('.purge_minify_cache').on('click', function(e) {
36 e.preventDefault();
37 block_ui(wpoptimize.clearing_cache);
38 send_command('purge_minify_cache', null, function(response) {
39 if (response.hasOwnProperty('files')) {
40 minify.updateFilesLists(response.files);
41 minify.updateStats(response.files);
42 }
43 }).always(function() {
44 $.unblockUI();
45 });
46 });
47
48 /**
49 * Removes the entire cache dir.
50 * Use with caution, as cached html may still reference those files.
51 */
52 $('.purge_all_minify_cache').on('click', function() {
53 block_ui(wpoptimize.clearing_cache);
54 send_command('purge_all_minify_cache', null, function(response) {
55 if (response.hasOwnProperty('files')) {
56 minify.updateFilesLists(response.files);
57 minify.updateStats(response.files);
58 }
59 }).always(function() {
60 $.unblockUI();
61 });
62 });
63
64 /**
65 * Forces minify to create a new cache, safe to use
66 */
67 $('.minify_increment_cache').on('click', function() {
68 block_ui(wpoptimize.creating_cache);
69 send_command('minify_increment_cache', null, function(response) {
70 if (response.hasOwnProperty('files')) {
71 minify.updateFilesLists(response.files);
72 minify.updateStats(response.files);
73 }
74 }).always(function() {
75 $.unblockUI();
76 });
77 });
78
79 // ======= SLIDERS ========
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-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 $(this).closest('form').data('need_saving', true);
83 });
84
85 $('input[type=checkbox].wpo-save-setting').on('change', function(e) {
86 var input = $(this),
87 val = input.prop('checked'),
88 name = input.prop('name'),
89 data = {};
90 data[name] = val;
91 block_ui(wpoptimize.saving);
92 send_command('save_minify_settings', data, function(response) {
93 if (response.success) {
94 input.trigger('wp-optimize/minify/saved_setting');
95 if (response.hasOwnProperty('files')) {
96 minify.updateFilesLists(response.files);
97 minify.updateStats(response.files);
98 }
99 } else {
100 console.log('Settings not saved', data)
101 }
102 }).always(function() {
103 $.unblockUI();
104 });
105 });
106
107 // Slider enable minify
108 $('#wpo_min_enable_minify').on('wp-optimize/minify/saved_setting', function() {
109 this.enabled = $(this).prop('checked');
110 $(document).trigger('wp-optimize/minify/toggle-status', {enabled: this.enabled});
111 });
112
113 // Toggle wpo-feature-is-disabled class
114 $('#wpo_min_enable_minify').on('wp-optimize/minify/saved_setting', function() {
115
116 $(this).closest('.wpo_section').toggleClass('wpo-feature-is-disabled', !$(this).is(':checked'));
117 });
118
119 // Toggle wpo-feature-is-disabled class
120 $('#wpo_min_enable_minify_css, #wpo_min_enable_minify_js')
121 // Update UI when setting is saved
122 .on('wp-optimize/minify/saved_setting', function() {
123 toggle_minify_tab_section($(this));
124 })
125 // Initialize UI on page load
126 .each(function() {
127 toggle_minify_tab_section($(this));
128 });
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
151 // slider enable Debug mode
152 $('#wpo_min_enable_minify_debug').on('wp-optimize/minify/saved_setting', function() {
153 // Refresh the page as it's needed to show the extra options
154 block_ui(wpoptimize.page_refresh);
155 location.href = $('#wp-optimize-nav-tab-wpo_minify-advanced').prop('href');
156 });
157
158 // Edit default exclusions
159 $('#wpo_min_edit_default_exclutions').on('wp-optimize/minify/saved_setting', function() {
160 // Show exclusions section
161 $('.wpo-minify-default-exclusions').toggleClass('hidden', !$(this).prop('checked'));
162 });
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
174 // Save settings
175 $('.wp-optimize-save-minify-settings').on('click', function(e) {
176 e.preventDefault();
177 var btn = $(this),
178 spinner = btn.next(),
179 success_icon = spinner.next(),
180 $need_refresh_btn = null;
181
182 spinner.show();
183 block_ui(wpoptimize.saving);
184
185 var data = {};
186
187 var merge_state = {
188 js: null,
189 css: null
190 };
191
192 var tabs = $('[data-whichpage="wpo_minify"] .wp-optimize-nav-tab-contents form');
193 tabs.each(function() {
194 var tab = $(this);
195 if (true === tab.data('need_saving')) {
196 data = Object.assign(data, gather_data(tab));
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 }
206 }
207 });
208
209 send_command('save_minify_settings', data, function(response) {
210 if (response.hasOwnProperty('error')) {
211 // show error
212 console.log(response.error);
213 wp_optimize.notices.show_notice(response.success, response.error);
214 } else {
215 $('.wpo-error__enabling-cache').addClass('wpo_hidden').find('p').text('');
216 }
217
218 if (response.hasOwnProperty('files')) {
219 minify.updateFilesLists(response.files);
220 minify.updateStats(response.files);
221 }
222
223 $.each(merge_state, function(type, enabled) {
224 if (null !== enabled) {
225 toggle_minify_notice(type, enabled, false);
226 }
227 });
228
229 spinner.hide();
230 success_icon.show();
231 setTimeout(function() {
232 success_icon.fadeOut('slow', function() {
233 success_icon.hide();
234 });
235 }, 5000);
236 }).always(function() {
237 $.unblockUI();
238 });
239 })
240
241 // Dismiss information notice
242 $('.wpo_section_notice_minify').on('click', '.notice-dismiss', function(e) {
243 e.preventDefault();
244 toggle_minify_notice('minify', null, true);
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 }
285
286 // Show logs
287 $('#wpo_min_jsprocessed, #wpo_min_cssprocessed').on('click', '.log', function(e) {
288 e.preventDefault();
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 }
296 });
297
298 // Delete log file
299 $('#wpo_min_jsprocessed, #wpo_min_cssprocessed').on('click', '.delete-file', function(e) {
300 e.preventDefault();
301
302 var filename = $(this).data('filename');
303
304 if ('' === filename || undefined === filename) {
305 alert(wpoptimize.cache_file_not_found);
306 return false;
307 }
308
309 block_ui(wpoptimize.deleting);
310 send_command('delete_minify_cache_file', filename, function(response) {
311 if('' !== response.files) {
312 minify.updateFilesLists(response.files);
313 minify.updateStats(response.files);
314 }
315 if('' !== response.result) {
316 alert(response.result);
317 }
318 if('' !== response.error) {
319 alert(response.error);
320 }
321 }).always(function() {
322 $.unblockUI();
323 });
324 });
325
326 // Handle js excludes
327 $('#wpo_min_jsprocessed').on('click', '.exclude', function(e) {
328 e.preventDefault();
329 var el = $(this);
330 var excluded_file = get_excluded_file(el);
331 add_excluded_js_file(excluded_file);
332 tab_need_saving('js');
333 highlight_excluded_item(el);
334 });
335
336 // Handle css excludes
337 $('#wpo_min_cssprocessed').on('click', '.exclude', function(e) {
338 e.preventDefault();
339 var el = $(this);
340 var excluded_file = get_excluded_file(el);
341 add_excluded_css_file(excluded_file);
342 tab_need_saving('css');
343 highlight_excluded_item(el);
344 });
345
346 // Trigger combined meta.json file download
347 $('.wpo-minify-download-metas-button').on('click', function(e) {
348 e.preventDefault();
349 send_command('get_minify_meta_files', null, function(response) {
350 if (response.hasOwnProperty('error')) {
351 // show error
352 alert(response.error);
353 return;
354 }
355
356 var link = document.body.appendChild(document.createElement('a'));
357 link.setAttribute('download', 'combined_metas.json');
358 link.setAttribute('style', "display:none;");
359 link.setAttribute('href', 'data:text/json' + ';charset=UTF-8,' + encodeURIComponent(JSON.stringify(response.combined_metas)));
360 link.click();
361 })
362 });
363
364 /**
365 * Get excluded file url
366 *
367 * @param {HTMLElement} el
368 *
369 * @return {string}
370 */
371 function get_excluded_file(el) {
372 return el.data('url');
373 }
374
375 /**
376 * Exclude js file
377 *
378 * @param {string} excluded_file File url
379 */
380 function add_excluded_js_file(excluded_file) {
381 var $js_textarea = $('#exclude_js');
382 var list_of_excluded_files = $js_textarea.val();
383 list_of_excluded_files += excluded_file + '\n';
384 $js_textarea.val(list_of_excluded_files);
385 }
386
387 /**
388 * Exclude css file
389 *
390 * @param {string} excluded_file File url
391 */
392 function add_excluded_css_file(excluded_file) {
393 var $css_textarea = $('#exclude_css');
394 var list_of_excluded_files = $css_textarea.val();
395 list_of_excluded_files += excluded_file + '\n';
396 $css_textarea.val(list_of_excluded_files);
397 }
398
399 // Handle defer
400 $('#wpo_min_jsprocessed').on('click', '.defer', function(e) {
401 e.preventDefault();
402 add_deferred_file($(this));
403 });
404
405 // Handle async loading
406 $('#wpo_min_cssprocessed').on('click', '.async', function(e) {
407 e.preventDefault();
408 add_async_file($(this));
409 });
410
411 /**
412 * Add deferred file
413 *
414 * @param {HTMLElement} el target element
415 */
416 function add_deferred_file(el) {
417 var deferred_file = el.data('url');
418 var $async_js_textarea = $('#async_js');
419 var list_of_deferred_files = $async_js_textarea.val();
420 list_of_deferred_files += deferred_file + '\n';
421 $async_js_textarea.val(list_of_deferred_files);
422 tab_need_saving('js');
423 highlight_excluded_item(el);
424 }
425
426 /**
427 * Add asynchronously loading file
428 *
429 * @param {HTMLElement} el target element
430 */
431 function add_async_file(el) {
432 var async_file = el.data('url');
433 var $async_css_textarea = $('#async_css');
434 var list_of_async_files = $async_css_textarea.val();
435 list_of_async_files += async_file + '\n';
436 $async_css_textarea.val(list_of_async_files);
437 tab_need_saving('css');
438 highlight_excluded_item(el);
439 }
440
441 /**
442 *
443 * @param {string} tab_name Name of the tab that need saving
444 */
445 function tab_need_saving(tab_name) {
446 $('#wp-optimize-nav-tab-wpo_minify-' + tab_name + '-contents form').data('need_saving', true);
447 }
448
449 /**
450 * Update UI after excluding the file
451 *
452 * @param {HTMLElement} el Target element
453 */
454 function highlight_excluded_item(el) {
455 el.closest('.wpo_min_log').prev().removeClass('hidden').addClass('updated').slideDown();
456 el.text(wpoptimize.added_to_list);
457 el.removeClass('exclude');
458 el.parent().addClass('disable-list-item');
459 el.replaceWith($('<span>' + el.text() + '</span>'));
460 }
461
462 $('#wp-optimize-minify-advanced').on('click', '.save-exclusions', function(e) {
463 e.preventDefault();
464 $('.wp-optimize-save-minify-settings').first().trigger('click');
465 });
466
467 // Set the initial `enabled` value
468 this.enabled = $('#wpo_min_enable_minify').prop('checked');
469 $(document).trigger('wp-optimize/minify/toggle-status', {enabled: this.enabled});
470
471 // When loading the page and minify is disabled, make sure that the status tab is active.
472 if (!this.enabled && !$('#wp-optimize-nav-tab-wrapper__wpo_minify a[data-tab="status"]').is('.nav-tab-active')) {
473 $('#wp-optimize-nav-tab-wrapper__wpo_minify a[data-tab="status"]').trigger('click');
474 }
475
476 // Enable / disable defer_jquery
477 function check_defer_status( e ) {
478 $('input[name="enable_defer_js"]').each(function(index, element) {
479 $(element).closest('fieldset').removeClass('selected').find('.defer-js-settings').slideUp('fast');
480 });
481 $('input[name="enable_defer_js"]:checked').closest('fieldset').addClass('selected').find('.defer-js-settings').slideDown('fast');
482 }
483
484 $('input[name="enable_defer_js"]').on('change', check_defer_status);
485
486 check_defer_status();
487
488 /**
489 * Minify Preloader functionality
490 */
491 var run_minify_preload_btn = $('#wp_optimize_run_minify_preload'),
492 minify_preload_status_el = $('#wp_optimize_preload_minify_status');
493
494 run_minify_preload_btn.on('click', function() {
495 var btn = $(this),
496 is_running = btn.data('running'),
497 status = minify_preload_status_el.text();
498
499 btn.prop('disabled', true);
500
501 if (is_running) {
502 btn.data('running', false);
503
504 heartbeat.cancel_agents(heartbeat_agents);
505
506 send_command(
507 'cancel_minify_preload',
508 null,
509 function(response) {
510 if (response && response.hasOwnProperty('message')) {
511 minify_preload_status_el.text(response.message);
512 }
513 }
514 ).always(function() {
515 btn.val(wpoptimize.run_now);
516 btn.prop('disabled', false);
517 });
518 } else {
519 minify_preload_status_el.text(wpoptimize.starting_preload);
520 btn.data('running', true);
521 send_command(
522 'run_minify_preload',
523 null,
524 null,
525 true,
526 {
527 timeout: 3000 // set a timeout in case the server doesn't support our close browser connection function.
528 }
529 ).always(function(response) {
530 try {
531 var resp = wpo_parse_json(response);
532 } catch (e) {
533 }
534
535 if (resp && resp.error) {
536
537 var error_text = wpoptimize.error_unexpected_response;
538
539 if (typeof resp.error != 'function') {
540 error_text = resp.error;
541 } else if (resp.status) {
542 error_text = resp.status + ': ' + resp.statusText;
543 }
544
545 alert(error_text);
546
547 minify_preload_status_el.text(status);
548 btn.prop('disabled', false);
549 btn.data('running', false);
550
551 return;
552 }
553
554 minify_preload_status_el.text(wpoptimize.loading_urls);
555 btn.val(wpoptimize.cancel);
556 btn.prop('disabled', false);
557 run_update_minify_preload_status();
558 });
559 }
560 });
561
562 /**
563 * If already running then update status
564 */
565 if (run_minify_preload_btn.data('running')) {
566 run_update_minify_preload_status();
567 }
568
569 /**
570 * Create heartbeat agent action for update preloader status.
571 *
572 * @return void
573 */
574 function run_update_minify_preload_status() {
575 var agent = heartbeat.add_agent({
576 command: 'get_minify_preload_status',
577 callback: update_minify_preload_status,
578 _keep: false
579 });
580
581 if (null !== agent) heartbeat_agents.push(agent);
582 }
583
584 /**
585 * Update minify preload status ajax action.
586 *
587 * @return void
588 */
589 function update_minify_preload_status(response) {
590 if (response.done) {
591 run_minify_preload_btn.val(wpoptimize.run_now);
592 run_minify_preload_btn.data('running', false);
593 } else {
594 run_minify_preload_btn.val(wpoptimize.cancel);
595 run_minify_preload_btn.data('running', true);
596 run_update_minify_preload_status();
597 }
598 minify_preload_status_el.text(response.message);
599 update_minify_size_information(response);
600 }
601
602 /**
603 * Run update information about minify size.
604 *
605 * @return void
606 */
607 function update_minify_size_information(response) {
608 $('#wpo_min_cache_size').text(response.size);
609 $('#wpo_min_cache_total_size').text(response.total_size);
610 }
611
612 // switch for visibility (analytics setting tab).
613 var $analytics = $('#wpo-analytics-hidden-content');
614
615 $('#wpo_enable_analytics').on('change', function () {
616 $analytics.css('display', $(this).prop('checked') ? 'block' : 'none');
617 });
618
619 return this;
620 }
621
622 /**
623 * Get the list of files generated by Minify and update the markup.
624 */
625 minify.getFiles = function() {
626 // Only run if the feature is enabled
627 if (!this.enabled) return;
628
629 var data = {
630 stamp: new Date().getTime()
631 };
632
633 send_command('get_minify_cached_files', data, function(response) {
634 minify.updateFilesLists(response);
635 minify.updateStats(response);
636 });
637
638 if (refresh_frequency) setTimeout(minify.getFiles.bind(this), refresh_frequency);
639 }
640
641 minify.updateFilesLists = function(data) {
642 if (null === data) return;
643 // reset
644 var wpominarr = [];
645
646 // js
647 if (data.js.length > 0) {
648 $(data.js).each(function () {
649 wpominarr.push(this.uid);
650 if ($('#'+this.uid).length == 0) {
651 $('#wpo_min_jsprocessed ul.processed').append('\
652 <li id="'+this.uid+'">\
653 <span class="filename"><a href="'+this.file_url+'" target="_blank">'+this.filename+'</a> ('+this.fsize+')</span>\
654 <a href="#" class="log">' + wpoptimize.show_information + '</a>\
655 <a href="#" class="delete-file" data-filename="' + this.filename + '">' + wpoptimize.delete_file + '</a>\
656 <div class="hidden save_notice">\
657 <p>' + wpoptimize.added_notice + '</p>\
658 <p><button class="button button-primary save-exclusions">' + wpoptimize.save_notice + '</button></p>\
659 </div>\
660 <div class="hidden wpo_min_log">'+this.log+'</div>\
661 </li>\
662 ');
663 }
664 });
665 }
666
667 $('#wpo_min_jsprocessed ul.processed .no-files-yet').toggle(!data.js.length);
668
669 // css
670 if (data.css.length > 0) {
671 $(data.css).each(function () {
672 wpominarr.push(this.uid);
673 if ($('#'+this.uid).length == 0) {
674 $('#wpo_min_cssprocessed ul.processed').append('\
675 <li id="'+this.uid+'">\
676 <span class="filename"><a href="'+this.file_url+'" target="_blank">'+this.filename+'</a> ('+this.fsize+')</span>\
677 <a href="#" class="log">' + wpoptimize.show_information + '</a>\
678 <a href="#" class="delete-file" data-filename="' + this.filename + '">' + wpoptimize.delete_file + '</a>\
679 <div class="hidden save_notice">\
680 <p>' + wpoptimize.added_to_list + '</p>\
681 <p><button class="button button-primary save-exclusions">' + wpoptimize.save_notice + '</button></p>\
682 </div>\
683 <div class="hidden wpo_min_log">'+this.log+'</div>\
684 </li>\
685 ');
686 }
687 });
688 }
689 $('#wpo_min_cssprocessed ul.processed .no-files-yet').toggle(!data.css.length);
690
691 // Remove <li> if it's not in the files array
692 $('#wpo_min_jsprocessed ul.processed > li, #wpo_min_cssprocessed ul.processed > li').each(function () {
693 if (-1 == jQuery.inArray($(this).attr('id'), wpominarr)) {
694 if (!$(this).is('.no-files-yet')) {
695 $(this).remove();
696 }
697 }
698 });
699 };
700
701 minify.updateStats = function(data) {
702 if (data.cachesize.length > 0) {
703 $("#wpo_min_cache_size").html(this.enabled ? data.cachesize : wpoptimize.no_minified_assets);
704 $("#wpo_min_cache_total_size").html(this.enabled ? data.total_cache_size : wpoptimize.no_minified_assets);
705 $("#wpo_min_cache_time").html(this.enabled ? data.cacheTime : '-');
706 $("#wpo_min_cache_path").html(data.cachePath);
707 }
708 };
709
710 /**
711 * Gather data from the given form
712 *
713 * @param {HTMLFormElement} form
714 *
715 * @returns {Array} Array of collected data from the form
716 */
717 function gather_data(form) {
718 var data = $(form).serializeArray().reduce(form_serialize_reduce_cb, {});
719 $(form).find('input[type="checkbox"]').each(function (i) {
720 var name = $(this).prop("name");
721 if (name.includes('[]')) {
722 if (!$(this).is(':checked')) return;
723 var newName = name.replace('[]', '');
724 if (!data[newName]) data[newName] = [];
725 data[newName].push($(this).val());
726 } else {
727 data[name] = $(this).is(':checked') ? 'true' : 'false';
728 }
729 });
730 return data;
731 }
732
733 /**
734 * Reduces the form elements array into an object
735 *
736 * @param {Object} collection An empty object
737 * @param {*} item form input element as array element
738 *
739 * @returns {Object} collection An object of form data
740 */
741 function form_serialize_reduce_cb(collection, item) {
742 // Ignore items containing [], which we expect to be returned as arrays
743 if (item.name.includes('[]')) return collection;
744 collection[item.name] = item.value;
745 return collection;
746 }
747
748 // Gather minify settings from all tabs
749 wp_optimize.minify_settings = function() {
750 var tabs = $('[data-whichpage="wpo_minify"] .wp-optimize-nav-tab-contents form');
751 var data = {}
752 tabs.each(function() {
753 var tab = $(this);
754 data = Object.assign(data, gather_data(tab));
755 });
756 return data;
757 }
758
759 wp_optimize.minify = minify;
760
761 })(jQuery);