var WP_Optimize_Cache = function () { var $ = jQuery; var send_command = wp_optimize.send_command; var browser_cache_enable_btn = $('#wp_optimize_browser_cache_enable'), purge_cache_btn = $('#wp-optimize-purge-cache'), enable_page_caching_switch = $('#enable_page_caching'), page_cache_length_value_inp = $('#page_cache_length_value'); /** * Handle purge cache btn. */ purge_cache_btn.on('click', function() { var btn = $(this), spinner = btn.next(), success_icon = spinner.next(); spinner.show(); send_command('purge_page_cache', {}, function(response) { spinner.hide(); success_icon.show(); setTimeout(function() { success_icon.fadeOut('slow', function() { success_icon.hide(); }); run_update_cache_preload_status(); }, 5000); update_cache_size_information(response); }); }); /** * Trigger purge cache button click if wpo_purge_cache event fired. */ $('body').on('wpo_purge_cache', function() { purge_cache_btn.trigger('click'); }); /** * Trigger click Browser cache button if user push Enter and form start submitting. */ browser_cache_enable_btn.closest('form').on( 'submit', function(e) { e.preventDefault(); browser_cache_enable_btn.trigger('click'); return false; } ); /** * Disable or enable preload cache lifespan value */ page_cache_length_value_inp.on('change', function() { var value = parseInt(page_cache_length_value_inp.val(), 10); $('#preload_schedule_type option[value="wpo_use_cache_lifespan"]').prop('disabled', isNaN(value) || value <= 0); }); /** * Handle Enable Gzip compression button click. */ $('#wp_optimize_gzip_compression_enable').on('click', function() { var button = $(this), loader = button.next(); loader.show(); send_command('enable_gzip_compression', {enable: button.data('enable')}, function(response) { var gzip_status_message = $('#wpo_gzip_compression_status'); if (response) { if (response.enabled) { button.text(wpoptimize.disable); button.data('enable', '0'); gzip_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); } else { button.text(wpoptimize.enable); button.data('enable', '1'); gzip_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); } if (response.message) { $('#wpo_gzip_compression_error_message').text(response.message).show(); } else { $('#wpo_gzip_compression_error_message').hide(); } if (response.output) { $('#wpo_gzip_compression_output').html(response.output).show(); } else { $('#wpo_gzip_compression_output').hide(); } } else { alert(wpoptimize.error_unexpected_response); } loader.hide(); }).fail(function() { alert(wpoptimize.error_unexpected_response); loader.hide(); }); }); /** * Manually check gzip status */ $('.wpo-refresh-gzip-status').on('click', function(e) { e.preventDefault(); $link = $(this); $link.addClass('loading'); send_command('get_gzip_compression_status', null, function(response) { $link.removeClass('loading'); var gzip_status_message = $('#wpo_gzip_compression_status'); if (response.hasOwnProperty('status')) { if (response.status) { // gzip is enabled gzip_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); } else { // gzip is not enabled gzip_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); } } else if (response.hasOwnProperty('error')) { alert(response.error); console.log('Gzip status error code: ' + response.code); console.log('Gzip status error message: ' + response.message); } }); }); /** * Handle Enable browser cache button click. */ browser_cache_enable_btn.on('click', function() { var browser_cache_expire_days_el = $('#wpo_browser_cache_expire_days'), browser_cache_expire_hours_el = $('#wpo_browser_cache_expire_hours'), browser_cache_expire_days = parseInt(browser_cache_expire_days_el.val(), 10), browser_cache_expire_hours = parseInt(browser_cache_expire_hours_el.val(), 10), button = $(this), loader = button.next(); // check for invalid integer. if (isNaN(browser_cache_expire_days)) browser_cache_expire_days = 0; if (isNaN(browser_cache_expire_hours)) browser_cache_expire_hours = 0; if (browser_cache_expire_days < 0 || browser_cache_expire_hours < 0) { $('#wpo_browser_cache_error_message').text(wpoptimize.please_use_positive_integers).show(); return false; } else if (browser_cache_expire_hours > 23) { $('#wpo_browser_cache_error_message').text(wpoptimize.please_use_valid_values).show(); return false; } else { $('#wpo_browser_cache_error_message').hide(); } // set parsed values into input fields. browser_cache_expire_days_el.val(browser_cache_expire_days); browser_cache_expire_hours_el.val(browser_cache_expire_hours); loader.show(); send_command('enable_browser_cache', {browser_cache_expire_days: browser_cache_expire_days, browser_cache_expire_hours: browser_cache_expire_hours}, function(response) { var cache_status_message = $('#wpo_browser_cache_status'); if (response) { if (response.enabled) { button.text(wpoptimize.update); cache_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); } else { button.text(wpoptimize.enable); cache_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); } if (response.message) { $('#wpo_browser_cache_message').text(response.message).show(); } else { $('#wpo_browser_cache_message').hide(); } if (response.error_message) { $('#wpo_browser_cache_error_message').text(response.error_message).show(); } else { $('#wpo_browser_cache_error_message').hide(); } if (response.output) { $('#wpo_browser_cache_output').html(response.output).show(); } else { $('#wpo_browser_cache_output').hide(); } } else { alert(wpoptimize.error_unexpected_response); } loader.hide(); }).fail(function() { alert(wpoptimize.error_unexpected_response); loader.hide(); }); }); /** * Gather cache settings from forms and return it as an object. * * @return object */ function gather_cache_settings() { var settings = {}; $('.cache-settings').each(function() { var el = $(this), name = el.attr('name'); if (el.is('input[type="checkbox"]')) { settings[name] = el.is(':checked') ? 1 : 0; } else if (el.is('textarea')) { settings[name] = el.val().split("\n"); } else { settings[name] = el.val(); } }); $('.cache-settings-array').each(function() { var el = $(this), name = el.attr('name'); if (!settings.hasOwnProperty(name)) { settings[name] = []; } if (el.is('input[type="checkbox"]')) { if ('value' == el.data('saveas')) { if (el.is(':checked')) settings[name].push(el.val()); } else { settings[name].push(el.is(':checked') ? 1 : 0); } } else if (el.is('textarea')) { settings[name].push(el.val().split("\n")); } else { settings[name].push(el.val()); } }); return settings; } /** * Handle click on the save settings button for cache. */ $('#wp-optimize-save-cache-settings, #wp-optimize-save-cache-advanced-rules, #wp-optimize-save-cache-preload-settings').on('click', function() { var btn = $(this), spinner = btn.next(), success_icon = spinner.next(); spinner.show(); $.blockUI(); send_command('save_cache_settings', { 'cache-settings': gather_cache_settings() }, function(response) { if (response.hasOwnProperty('js_trigger')) { $(document).trigger(response.js_trigger, response); } if (response.hasOwnProperty('error')) { // show error console.log(response.error); $('.wpo-error__enabling-cache').removeClass('wpo_hidden').find('p').text(response.error.message); } else { $('.wpo-error__enabling-cache').addClass('wpo_hidden').find('p').text(''); } if (response.hasOwnProperty('warnings')) { // show error console.log(response.warnings); $('.wpo-warnings__enabling-cache').removeClass('wpo_hidden') .find('p').text(response.warnings_label); var ul = $('.wpo-warnings__enabling-cache').find('ul').html(''); $.each(response.warnings, function(index, warning) { ul.append('