| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | var WP_Optimize_Cache = function () { |
| 2 | 2 | |
| 3 | 3 | var $ = jQuery; |
| 4 | + var block_ui = wp_optimize.block_ui; | |
| 4 | 5 | var send_command = wp_optimize.send_command; |
| 5 | 6 | var heartbeat = WP_Optimize_Heartbeat(); |
| 6 | 7 | var heartbeat_agents = []; |
| 7 | 8 | |
| @@ -67,9 +68,9 @@ | ||
| 67 | 68 | |
| 68 | 69 | /** |
| 69 | 70 | * Handle Enable Gzip compression button click. |
| 70 | 71 | */ |
| 71 | - $('#wp_optimize_gzip_compression_enable').on('click', function() { | |
| 72 | + $('#wp-optimize-nav-tab-wpo_cache-gzip-contents').on('click', '#wp_optimize_gzip_compression_enable', function() { | |
| 72 | 73 | var button = $(this), |
| 73 | 74 | loader = button.next(); |
| 74 | 75 | |
| 75 | 76 | loader.show(); |
| @@ -74,20 +75,13 @@ | ||
| 74 | 75 | |
| 75 | 76 | loader.show(); |
| 76 | 77 | |
| 77 | 78 | send_command('enable_gzip_compression', {enable: button.data('enable')}, function(response) { |
| 78 | - var gzip_status_message = $('#wpo_gzip_compression_status'); | |
| 79 | 79 | if (response) { |
| 80 | - if (response.enabled) { | |
| 81 | - button.text(wpoptimize.disable); | |
| 82 | - button.data('enable', '0'); | |
| 83 | - gzip_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); | |
| 84 | - } else { | |
| 85 | - button.text(wpoptimize.enable); | |
| 86 | - button.data('enable', '1'); | |
| 87 | - gzip_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); | |
| 88 | - } | |
| 89 | 80 | |
| 81 | + $('#wpo_gzip_compression_test_btn').trigger('click'); | |
| 82 | + | |
| 83 | + // Handle error message | |
| 90 | 84 | if (response.message) { |
| 91 | 85 | $('#wpo_gzip_compression_error_message').text(response.message).show(); |
| 92 | 86 | } else { |
| 93 | 87 | $('#wpo_gzip_compression_error_message').hide(); |
| @@ -110,11 +104,52 @@ | ||
| 110 | 104 | }); |
| 111 | 105 | }); |
| 112 | 106 | |
| 113 | 107 | /** |
| 108 | + * Handle Test again compressions button click | |
| 109 | + */ | |
| 110 | + $('#wp-optimize-nav-tab-wpo_cache-gzip-contents').on('click', '#wpo_gzip_compression_test_btn', function() { | |
| 111 | + var btn = $(this); | |
| 112 | + | |
| 113 | + if (btn.prop('disabled')) return; | |
| 114 | + | |
| 115 | + btn.prop('disabled', true); | |
| 116 | + $('#wpo_gzip_compression_details').hide(); | |
| 117 | + $('#wpo_gzip_control_panel .wpo_shade').removeClass('hidden'); | |
| 118 | + | |
| 119 | + send_command('get_compression_test_results_html', {}, function(response) { | |
| 120 | + if (response.results_html) { | |
| 121 | + $('#wpo_gzip_compression_details').html(response.results_html); | |
| 122 | + } else { | |
| 123 | + $('#wpo_gzip_compression_details').text(wpoptimize.error_unexpected_response); | |
| 124 | + } | |
| 125 | + | |
| 126 | + if (response.gzip_compression_control_panel) { | |
| 127 | + $('#wpo_gzip_control_panel').html(response.gzip_compression_control_panel); | |
| 128 | + } | |
| 129 | + }).fail(function() { | |
| 130 | + alert(wpoptimize.error_unexpected_response); | |
| 131 | + }).always(function() { | |
| 132 | + $('#wpo_gzip_control_panel .wpo_shade').addClass('hidden'); | |
| 133 | + $('#wpo_gzip_compression_details').show(); | |
| 134 | + btn.prop('disabled', false); | |
| 135 | + }); | |
| 136 | + }); | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Update gzip test results when tab is activated | |
| 140 | + */ | |
| 141 | + $('#wp-optimize-wrap').on('tab-change/wpo_cache/gzip', function() { | |
| 142 | + if (!$('#wp-optimize-nav-tab-wpo_cache-gzip').data('activated')) { | |
| 143 | + $('#wp-optimize-nav-tab-wpo_cache-gzip').data('activated', true); | |
| 144 | + $('#wpo_gzip_compression_test_btn').trigger('click'); | |
| 145 | + } | |
| 146 | + }); | |
| 147 | + | |
| 148 | + /** | |
| 114 | 149 | * Manually check gzip status |
| 115 | 150 | */ |
| 116 | - $('.wpo-refresh-gzip-status').on('click', function(e) { | |
| 151 | + $('#wp-optimize-nav-tab-wpo_cache-gzip-contents').on('click', '.wpo-refresh-gzip-status', function(e) { | |
| 117 | 152 | e.preventDefault(); |
| 118 | 153 | $link = $(this); |
| 119 | 154 | $link.addClass('loading'); |
| 120 | 155 | send_command('get_gzip_compression_status', null, function(response) { |
| @@ -151,12 +186,12 @@ | ||
| 151 | 186 | if (isNaN(browser_cache_expire_days)) browser_cache_expire_days = 0; |
| 152 | 187 | if (isNaN(browser_cache_expire_hours)) browser_cache_expire_hours = 0; |
| 153 | 188 | |
| 154 | 189 | if (browser_cache_expire_days < 0 || browser_cache_expire_hours < 0) { |
| 155 | - $('#wpo_browser_cache_error_message').text(wpoptimize.please_use_positive_integers).show(); | |
| 190 | + alert(wpoptimize.please_use_positive_integers); | |
| 156 | 191 | return false; |
| 157 | 192 | } else if (browser_cache_expire_hours > 23) { |
| 158 | - $('#wpo_browser_cache_error_message').text(wpoptimize.please_use_valid_values).show(); | |
| 193 | + alert(wpoptimize.please_use_valid_values); | |
| 159 | 194 | return false; |
| 160 | 195 | } else { |
| 161 | 196 | $('#wpo_browser_cache_error_message').hide(); |
| 162 | 197 | } |
| @@ -166,46 +201,48 @@ | ||
| 166 | 201 | browser_cache_expire_hours_el.val(browser_cache_expire_hours); |
| 167 | 202 | |
| 168 | 203 | loader.show(); |
| 169 | 204 | |
| 170 | - send_command('enable_browser_cache', {browser_cache_expire_days: browser_cache_expire_days, browser_cache_expire_hours: browser_cache_expire_hours}, function(response) { | |
| 171 | - var cache_status_message = $('#wpo_browser_cache_status'); | |
| 172 | - var cache_status_field = $('#wpo_enable_browser_cache'); | |
| 173 | - if (response) { | |
| 174 | - if (response.enabled) { | |
| 175 | - button.text(wpoptimize.update); | |
| 176 | - cache_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); | |
| 177 | - cache_status_field.val('true'); | |
| 178 | - } else { | |
| 179 | - button.text(wpoptimize.enable); | |
| 180 | - cache_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); | |
| 181 | - cache_status_field.val('false'); | |
| 182 | - } | |
| 205 | + send_command('enable_browser_cache', | |
| 206 | + {browser_cache_expire_days: browser_cache_expire_days, | |
| 207 | + browser_cache_expire_hours: browser_cache_expire_hours}, function(response) { | |
| 208 | + var cache_status_message = $('#wpo_browser_cache_status'); | |
| 209 | + var cache_status_field = $('#wpo_enable_browser_cache'); | |
| 210 | + if (response) { | |
| 211 | + if (response.enabled) { | |
| 212 | + button.text(wpoptimize.update); | |
| 213 | + cache_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); | |
| 214 | + cache_status_field.val('true'); | |
| 215 | + } else { | |
| 216 | + button.text(wpoptimize.enable); | |
| 217 | + cache_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); | |
| 218 | + cache_status_field.val('false'); | |
| 219 | + } | |
| 183 | 220 | |
| 184 | - if (response.message) { | |
| 185 | - $('#wpo_browser_cache_message').text(response.message).show(); | |
| 186 | - } else { | |
| 187 | - $('#wpo_browser_cache_message').hide(); | |
| 188 | - } | |
| 221 | + if (response.message) { | |
| 222 | + $('#wpo_browser_cache_message').text(response.message).show(); | |
| 223 | + } else { | |
| 224 | + $('#wpo_browser_cache_message').hide(); | |
| 225 | + } | |
| 189 | 226 | |
| 190 | - if (response.error_message) { | |
| 191 | - $('#wpo_browser_cache_error_message').text(response.error_message).show(); | |
| 192 | - } else { | |
| 193 | - $('#wpo_browser_cache_error_message').hide(); | |
| 194 | - } | |
| 227 | + if (response.error_message) { | |
| 228 | + $('#wpo_browser_cache_error_message').text(response.error_message).show(); | |
| 229 | + } else { | |
| 230 | + $('#wpo_browser_cache_error_message').hide(); | |
| 231 | + } | |
| 195 | 232 | |
| 196 | - if (response.output) { | |
| 197 | - $('#wpo_browser_cache_output').html(response.output).show(); | |
| 233 | + if (response.output) { | |
| 234 | + $('#wpo_browser_cache_output').html(response.output).show(); | |
| 235 | + } else { | |
| 236 | + $('#wpo_browser_cache_output').hide(); | |
| 237 | + } | |
| 238 | + | |
| 198 | 239 | } else { |
| 199 | - $('#wpo_browser_cache_output').hide(); | |
| 240 | + alert(wpoptimize.error_unexpected_response); | |
| 200 | 241 | } |
| 201 | 242 | |
| 202 | - } else { | |
| 203 | - alert(wpoptimize.error_unexpected_response); | |
| 204 | - } | |
| 205 | - | |
| 206 | - loader.hide(); | |
| 207 | - }).fail(function() { | |
| 243 | + loader.hide(); | |
| 244 | + }).fail(function() { | |
| 208 | 245 | alert(wpoptimize.error_unexpected_response); |
| 209 | 246 | loader.hide(); |
| 210 | 247 | }); |
| 211 | 248 | }); |
| @@ -263,9 +300,9 @@ | ||
| 263 | 300 | spinner = btn.next(), |
| 264 | 301 | success_icon = spinner.next(); |
| 265 | 302 | |
| 266 | 303 | spinner.show(); |
| 267 | - $.blockUI(); | |
| 304 | + block_ui(wpoptimize.saving); | |
| 268 | 305 | |
| 269 | 306 | send_command('save_cache_settings', { 'cache-settings': gather_cache_settings() }, function(response) { |
| 270 | 307 | |
| 271 | 308 | if (response.hasOwnProperty('js_trigger')) { |
| @@ -321,8 +358,10 @@ | ||
| 321 | 358 | $('#wp_optimize_run_minify_preload').prop('disabled', false); |
| 322 | 359 | $('#minify-preload').hide(); |
| 323 | 360 | } |
| 324 | 361 | |
| 362 | + $(document).trigger('wp-optimize/cache/toggle-status', {enabled: response.enabled}); | |
| 363 | + | |
| 325 | 364 | if (response.result) { |
| 326 | 365 | // If Result is true, show the success icon. |
| 327 | 366 | success_icon.show(); |
| 328 | 367 | setTimeout(function() { |
| @@ -391,11 +430,9 @@ | ||
| 391 | 430 | |
| 392 | 431 | if (is_running) { |
| 393 | 432 | btn.data('running', false); |
| 394 | 433 | |
| 395 | - while(agent_id = heartbeat_agents.shift()) { | |
| 396 | - heartbeat.cancel_agent(agent_id); | |
| 397 | - } | |
| 434 | + heartbeat.cancel_agents(heartbeat_agents); | |
| 398 | 435 | |
| 399 | 436 | send_command( |
| 400 | 437 | 'cancel_cache_preload', |
| 401 | 438 | null, |
| @@ -467,9 +504,10 @@ | ||
| 467 | 504 | */ |
| 468 | 505 | function run_update_cache_preload_status() { |
| 469 | 506 | var agent = heartbeat.add_agent({ |
| 470 | 507 | command: 'get_cache_preload_status', |
| 471 | - callback: update_cache_preload_status | |
| 508 | + callback: update_cache_preload_status, | |
| 509 | + _keep: false | |
| 472 | 510 | }); |
| 473 | 511 | |
| 474 | 512 | if (null !== agent) heartbeat_agents.push(agent); |
| 475 | 513 | } |
| @@ -500,7 +538,33 @@ | ||
| 500 | 538 | function update_cache_size_information(response) { |
| 501 | 539 | $('#wpo_current_cache_size_information').text(wpoptimize.current_cache_size + ' ' + response.size); |
| 502 | 540 | $('#wpo_current_cache_file_count').text(wpoptimize.number_of_files + ' ' + response.file_count); |
| 503 | 541 | } |
| 542 | + | |
| 543 | + | |
| 544 | + $('#wpo-auto-preload-after-purge').on('click', function() { | |
| 545 | + var clicked_btn = this; | |
| 546 | + var success_icon = $(this).closest('.wpo-fieldgroup__subgroup').find('.dashicons-yes'); | |
| 547 | + | |
| 548 | + clicked_btn.disabled = true; | |
| 549 | + | |
| 550 | + block_ui(wpoptimize.saving); | |
| 551 | + | |
| 552 | + send_command( | |
| 553 | + 'save_cache_auto_preload_option', | |
| 554 | + { enabled: !!clicked_btn.checked }, | |
| 555 | + function(response) { | |
| 556 | + clicked_btn.disabled = false; | |
| 557 | + $.unblockUI(); | |
| 558 | + if (response.success) { | |
| 559 | + success_icon.show().fadeOut(1000); | |
| 560 | + } else { | |
| 561 | + clicked_btn.checked = !clicked_btn.checked; | |
| 562 | + alert(response.message); | |
| 563 | + } | |
| 564 | + | |
| 565 | + } | |
| 566 | + ); | |
| 567 | + }); | |
| 504 | 568 | |
| 505 | 569 | wp_optimize.cache_settings = gather_cache_settings; |
| 506 | 570 | }; |