| @@ -1,8 +1,11 @@ | ||
| 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; |
| 6 | + var heartbeat = WP_Optimize_Heartbeat(); | |
| 7 | + var heartbeat_agents = []; | |
| 5 | 8 | |
| 6 | 9 | var browser_cache_enable_btn = $('#wp_optimize_browser_cache_enable'), |
| 7 | 10 | purge_cache_btn = $('#wp-optimize-purge-cache'), |
| 8 | 11 | enable_page_caching_switch = $('#enable_page_caching'), |
| @@ -11,15 +14,19 @@ | ||
| 11 | 14 | /** |
| 12 | 15 | * Handle purge cache btn. |
| 13 | 16 | */ |
| 14 | 17 | purge_cache_btn.on('click', function() { |
| 18 | + | |
| 15 | 19 | var btn = $(this), |
| 16 | 20 | spinner = btn.next(), |
| 17 | 21 | success_icon = spinner.next(); |
| 18 | 22 | |
| 23 | + if (btn.prop('disabled')) return false; | |
| 24 | + btn.prop('disabled', true); | |
| 19 | 25 | spinner.show(); |
| 20 | 26 | |
| 21 | 27 | send_command('purge_page_cache', {}, function(response) { |
| 28 | + btn.prop('disabled', false); | |
| 22 | 29 | spinner.hide(); |
| 23 | 30 | success_icon.show(); |
| 24 | 31 | setTimeout(function() { |
| 25 | 32 | success_icon.fadeOut('slow', function() { |
| @@ -61,9 +68,9 @@ | ||
| 61 | 68 | |
| 62 | 69 | /** |
| 63 | 70 | * Handle Enable Gzip compression button click. |
| 64 | 71 | */ |
| 65 | - $('#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() { | |
| 66 | 73 | var button = $(this), |
| 67 | 74 | loader = button.next(); |
| 68 | 75 | |
| 69 | 76 | loader.show(); |
| @@ -68,20 +75,13 @@ | ||
| 68 | 75 | |
| 69 | 76 | loader.show(); |
| 70 | 77 | |
| 71 | 78 | send_command('enable_gzip_compression', {enable: button.data('enable')}, function(response) { |
| 72 | - var gzip_status_message = $('#wpo_gzip_compression_status'); | |
| 73 | 79 | if (response) { |
| 74 | - if (response.enabled) { | |
| 75 | - button.text(wpoptimize.disable); | |
| 76 | - button.data('enable', '0'); | |
| 77 | - gzip_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); | |
| 78 | - } else { | |
| 79 | - button.text(wpoptimize.enable); | |
| 80 | - button.data('enable', '1'); | |
| 81 | - gzip_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); | |
| 82 | - } | |
| 83 | 80 | |
| 81 | + $('#wpo_gzip_compression_test_btn').trigger('click'); | |
| 82 | + | |
| 83 | + // Handle error message | |
| 84 | 84 | if (response.message) { |
| 85 | 85 | $('#wpo_gzip_compression_error_message').text(response.message).show(); |
| 86 | 86 | } else { |
| 87 | 87 | $('#wpo_gzip_compression_error_message').hide(); |
| @@ -104,11 +104,52 @@ | ||
| 104 | 104 | }); |
| 105 | 105 | }); |
| 106 | 106 | |
| 107 | 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 | + /** | |
| 108 | 149 | * Manually check gzip status |
| 109 | 150 | */ |
| 110 | - $('.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) { | |
| 111 | 152 | e.preventDefault(); |
| 112 | 153 | $link = $(this); |
| 113 | 154 | $link.addClass('loading'); |
| 114 | 155 | send_command('get_gzip_compression_status', null, function(response) { |
| @@ -145,12 +186,12 @@ | ||
| 145 | 186 | if (isNaN(browser_cache_expire_days)) browser_cache_expire_days = 0; |
| 146 | 187 | if (isNaN(browser_cache_expire_hours)) browser_cache_expire_hours = 0; |
| 147 | 188 | |
| 148 | 189 | if (browser_cache_expire_days < 0 || browser_cache_expire_hours < 0) { |
| 149 | - $('#wpo_browser_cache_error_message').text(wpoptimize.please_use_positive_integers).show(); | |
| 190 | + alert(wpoptimize.please_use_positive_integers); | |
| 150 | 191 | return false; |
| 151 | 192 | } else if (browser_cache_expire_hours > 23) { |
| 152 | - $('#wpo_browser_cache_error_message').text(wpoptimize.please_use_valid_values).show(); | |
| 193 | + alert(wpoptimize.please_use_valid_values); | |
| 153 | 194 | return false; |
| 154 | 195 | } else { |
| 155 | 196 | $('#wpo_browser_cache_error_message').hide(); |
| 156 | 197 | } |
| @@ -160,43 +201,48 @@ | ||
| 160 | 201 | browser_cache_expire_hours_el.val(browser_cache_expire_hours); |
| 161 | 202 | |
| 162 | 203 | loader.show(); |
| 163 | 204 | |
| 164 | - send_command('enable_browser_cache', {browser_cache_expire_days: browser_cache_expire_days, browser_cache_expire_hours: browser_cache_expire_hours}, function(response) { | |
| 165 | - var cache_status_message = $('#wpo_browser_cache_status'); | |
| 166 | - if (response) { | |
| 167 | - if (response.enabled) { | |
| 168 | - button.text(wpoptimize.update); | |
| 169 | - cache_status_message.removeClass('wpo-disabled').addClass('wpo-enabled'); | |
| 170 | - } else { | |
| 171 | - button.text(wpoptimize.enable); | |
| 172 | - cache_status_message.addClass('wpo-disabled').removeClass('wpo-enabled'); | |
| 173 | - } | |
| 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 | + } | |
| 174 | 220 | |
| 175 | - if (response.message) { | |
| 176 | - $('#wpo_browser_cache_message').text(response.message).show(); | |
| 177 | - } else { | |
| 178 | - $('#wpo_browser_cache_message').hide(); | |
| 179 | - } | |
| 221 | + if (response.message) { | |
| 222 | + $('#wpo_browser_cache_message').text(response.message).show(); | |
| 223 | + } else { | |
| 224 | + $('#wpo_browser_cache_message').hide(); | |
| 225 | + } | |
| 180 | 226 | |
| 181 | - if (response.error_message) { | |
| 182 | - $('#wpo_browser_cache_error_message').text(response.error_message).show(); | |
| 183 | - } else { | |
| 184 | - $('#wpo_browser_cache_error_message').hide(); | |
| 185 | - } | |
| 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 | + } | |
| 186 | 232 | |
| 187 | - if (response.output) { | |
| 188 | - $('#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 | + | |
| 189 | 239 | } else { |
| 190 | - $('#wpo_browser_cache_output').hide(); | |
| 240 | + alert(wpoptimize.error_unexpected_response); | |
| 191 | 241 | } |
| 192 | 242 | |
| 193 | - } else { | |
| 194 | - alert(wpoptimize.error_unexpected_response); | |
| 195 | - } | |
| 196 | - | |
| 197 | - loader.hide(); | |
| 198 | - }).fail(function() { | |
| 243 | + loader.hide(); | |
| 244 | + }).fail(function() { | |
| 199 | 245 | alert(wpoptimize.error_unexpected_response); |
| 200 | 246 | loader.hide(); |
| 201 | 247 | }); |
| 202 | 248 | }); |
| @@ -254,9 +300,9 @@ | ||
| 254 | 300 | spinner = btn.next(), |
| 255 | 301 | success_icon = spinner.next(); |
| 256 | 302 | |
| 257 | 303 | spinner.show(); |
| 258 | - $.blockUI(); | |
| 304 | + block_ui(wpoptimize.saving); | |
| 259 | 305 | |
| 260 | 306 | send_command('save_cache_settings', { 'cache-settings': gather_cache_settings() }, function(response) { |
| 261 | 307 | |
| 262 | 308 | if (response.hasOwnProperty('js_trigger')) { |
| @@ -265,9 +311,9 @@ | ||
| 265 | 311 | |
| 266 | 312 | if (response.hasOwnProperty('error')) { |
| 267 | 313 | // show error |
| 268 | 314 | console.log(response.error); |
| 269 | - $('.wpo-error__enabling-cache').removeClass('wpo_hidden').find('p').text(response.error.message); | |
| 315 | + wp_optimize.notices.show_notice(response.error.code, response.error.message); | |
| 270 | 316 | } else { |
| 271 | 317 | $('.wpo-error__enabling-cache').addClass('wpo_hidden').find('p').text(''); |
| 272 | 318 | } |
| 273 | 319 | |
| @@ -298,9 +344,9 @@ | ||
| 298 | 344 | if (enable_page_caching_switch.is(':checked')) { |
| 299 | 345 | // show purge button |
| 300 | 346 | $('.purge-cache').show(); |
| 301 | 347 | // enable preload button |
| 302 | - $('#wp_optimize_run_cache_preload').removeProp('disabled'); | |
| 348 | + $('#wp_optimize_run_cache_preload').prop('disabled', false); | |
| 303 | 349 | // disable minify preload |
| 304 | 350 | $('#wp_optimize_run_minify_preload').prop('disabled', true); |
| 305 | 351 | $('#minify-preload').show(); |
| 306 | 352 | } else { |
| @@ -312,8 +358,10 @@ | ||
| 312 | 358 | $('#wp_optimize_run_minify_preload').prop('disabled', false); |
| 313 | 359 | $('#minify-preload').hide(); |
| 314 | 360 | } |
| 315 | 361 | |
| 362 | + $(document).trigger('wp-optimize/cache/toggle-status', {enabled: response.enabled}); | |
| 363 | + | |
| 316 | 364 | if (response.result) { |
| 317 | 365 | // If Result is true, show the success icon. |
| 318 | 366 | success_icon.show(); |
| 319 | 367 | setTimeout(function() { |
| @@ -359,9 +407,8 @@ | ||
| 359 | 407 | */ |
| 360 | 408 | |
| 361 | 409 | var run_cache_preload_btn = $('#wp_optimize_run_cache_preload'), |
| 362 | 410 | cache_preload_status_el = $('#wp_optimize_preload_cache_status'), |
| 363 | - check_status_interval = null, | |
| 364 | 411 | enable_schedule_preloading = $('#enable_schedule_preload'), |
| 365 | 412 | preloader_schedule_type_select = $('#preload_schedule_type'); |
| 366 | 413 | |
| 367 | 414 | enable_schedule_preloading.on('change', function() { |
| @@ -382,10 +429,11 @@ | ||
| 382 | 429 | btn.prop('disabled', true); |
| 383 | 430 | |
| 384 | 431 | if (is_running) { |
| 385 | 432 | btn.data('running', false); |
| 386 | - clearInterval(check_status_interval); | |
| 387 | - check_status_interval = null; | |
| 433 | + | |
| 434 | + heartbeat.cancel_agents(heartbeat_agents); | |
| 435 | + | |
| 388 | 436 | send_command( |
| 389 | 437 | 'cancel_cache_preload', |
| 390 | 438 | null, |
| 391 | 439 | function(response) { |
| @@ -414,9 +462,9 @@ | ||
| 414 | 462 | var resp = wpo_parse_json(response); |
| 415 | 463 | } catch (e) { |
| 416 | 464 | } |
| 417 | 465 | |
| 418 | - if (resp && resp.error) { | |
| 466 | + if (resp && resp.error && 'timeout' !== resp.statusText) { | |
| 419 | 467 | |
| 420 | 468 | var error_text = wpoptimize.error_unexpected_response; |
| 421 | 469 | |
| 422 | 470 | if (typeof resp.error != 'function') { |
| @@ -449,18 +497,20 @@ | ||
| 449 | 497 | run_update_cache_preload_status(); |
| 450 | 498 | } |
| 451 | 499 | |
| 452 | 500 | /** |
| 453 | - * Create interval action for update preloader status. | |
| 501 | + * Create heartbeat agent action for update preloader status. | |
| 454 | 502 | * |
| 455 | 503 | * @return void |
| 456 | 504 | */ |
| 457 | 505 | function run_update_cache_preload_status() { |
| 458 | - if (check_status_interval) return; | |
| 506 | + var agent = heartbeat.add_agent({ | |
| 507 | + command: 'get_cache_preload_status', | |
| 508 | + callback: update_cache_preload_status, | |
| 509 | + _keep: false | |
| 510 | + }); | |
| 459 | 511 | |
| 460 | - check_status_interval = setInterval(function() { | |
| 461 | - update_cache_preload_status(); | |
| 462 | - }, 5000); | |
| 512 | + if (null !== agent) heartbeat_agents.push(agent); | |
| 463 | 513 | } |
| 464 | 514 | |
| 465 | 515 | /** |
| 466 | 516 | * Update cache preload status ajax action. |
| @@ -466,22 +516,19 @@ | ||
| 466 | 516 | * Update cache preload status ajax action. |
| 467 | 517 | * |
| 468 | 518 | * @return void |
| 469 | 519 | */ |
| 470 | - function update_cache_preload_status() { | |
| 471 | - send_command('get_cache_preload_status', null, function(response) { | |
| 472 | - if (response.done) { | |
| 473 | - run_cache_preload_btn.val(wpoptimize.run_now); | |
| 474 | - run_cache_preload_btn.data('running', false); | |
| 475 | - clearInterval(check_status_interval); | |
| 476 | - check_status_interval = null; | |
| 477 | - } else { | |
| 478 | - run_cache_preload_btn.val(wpoptimize.cancel); | |
| 479 | - run_cache_preload_btn.data('running', true); | |
| 480 | - } | |
| 481 | - cache_preload_status_el.text(response.message); | |
| 482 | - update_cache_size_information(response); | |
| 483 | - }); | |
| 520 | + function update_cache_preload_status(response) { | |
| 521 | + if (response.done) { | |
| 522 | + run_cache_preload_btn.val(wpoptimize.run_now); | |
| 523 | + run_cache_preload_btn.data('running', false); | |
| 524 | + } else { | |
| 525 | + run_cache_preload_btn.val(wpoptimize.cancel); | |
| 526 | + run_cache_preload_btn.data('running', true); | |
| 527 | + run_update_cache_preload_status(); | |
| 528 | + } | |
| 529 | + cache_preload_status_el.text(response.message); | |
| 530 | + update_cache_size_information(response); | |
| 484 | 531 | } |
| 485 | 532 | |
| 486 | 533 | /** |
| 487 | 534 | * Run update information about cache size. |
| @@ -491,5 +538,33 @@ | ||
| 491 | 538 | function update_cache_size_information(response) { |
| 492 | 539 | $('#wpo_current_cache_size_information').text(wpoptimize.current_cache_size + ' ' + response.size); |
| 493 | 540 | $('#wpo_current_cache_file_count').text(wpoptimize.number_of_files + ' ' + response.file_count); |
| 494 | 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 | + }); | |
| 568 | + | |
| 569 | + wp_optimize.cache_settings = gather_cache_settings; | |
| 495 | 570 | }; |