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 | cache/class-wpo-page-cache.php +72 -49 4.6.1 → 4.7.0 View file →
@@ -128,8 +128,9 @@
128 128 * Regenerate config file on cache flush.
129 129 */
130 130 add_action('wpo_cache_flush', array($this, 'update_cache_config'));
131 131 add_action('wpo_cache_flush', array($this, 'delete_cache_size_information'));
132 + add_action('wpo_cache_flush', array($this, 'update_cache_cleared_time'));
132 133 add_action('update_option_permalink_structure', array($this, 'update_option_permalink_structure'), 10, 3);
133 134
134 135 // Add purge cache link to admin bar.
135 136 add_filter('wpo_cache_admin_bar_menu_items', array($this, 'admin_bar_purge_cache'), 20, 1);
@@ -193,44 +194,37 @@
193 194 *
194 195 * This method checks cache rules to identify if caching is possible.
195 196 * If caching is not allowed, it adds appropriate HTTP headers and debug messages.
196 197 *
197 - * @return bool True if the page should be cached, false otherwise.
198 + * @return bool|array True if the page should be cached, false otherwise or not empty array with the reasons why the page isn't cached
198 199 */
199 200 public function should_cache_page() {
200 201
201 202 if (!$this->is_enabled()) return false;
202 203
203 - $no_cache_because = array();
204 + return wpo_can_serve_from_cache();
205 + }
204 206
205 - // Check serve cache rules, to identify if we need to cache the page.
206 - $can_serve_from_cache = wpo_can_serve_from_cache();
207 -
208 - if (false === $can_serve_from_cache) return false;
207 + /**
208 + * Maybe add information about why the page is not cached to the headers and footer output
209 + *
210 + * @param array $no_cache_because
211 + * @return void
212 + */
213 + public function maybe_add_no_cache_because_info($no_cache_because) {
214 + // Add http header
215 + if (!wp_doing_cron() && !empty($no_cache_because)) {
216 + $no_cache_because_message = join(", ", $no_cache_because);
217 + wpo_cache_add_nocache_http_header_with_send_headers_action($no_cache_because_message);
209 218
210 - if (is_array($can_serve_from_cache)) $no_cache_because = $can_serve_from_cache;
211 -
212 - if (!empty($no_cache_because)) {
213 - do_action('wpo_page_not_cached', $no_cache_because);
214 -
215 - // Add http header
216 - if (!wp_doing_cron()) {
217 - $no_cache_because_message = join(", ", $no_cache_because);
218 - wpo_cache_add_nocache_http_header_with_send_headers_action($no_cache_because_message);
219 -
220 - $not_cached_details = "";
221 -
222 - // Output the reason only when the user has turned on debugging
223 - if ((defined('WP_DEBUG') && WP_DEBUG) || !empty(TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('get', 'wpo_cache_debug'))) {
224 - $not_cached_details = "because: ".$no_cache_because_message;
225 - }
226 - wpo_cache_add_footer_output(sprintf("Page not served from cache %s", $not_cached_details));
219 + $not_cached_details = "";
220 +
221 + // Output the reason only when the user has turned on debugging
222 + if ((defined('WP_DEBUG') && WP_DEBUG) || !empty(TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('get', 'wpo_cache_debug'))) {
223 + $not_cached_details = "because: ".$no_cache_because_message;
227 224 }
228 -
229 - return false;
225 + wpo_cache_add_footer_output(sprintf("Page not served from cache %s", $not_cached_details));
230 226 }
231 -
232 - return true;
233 227 }
234 228
235 229 /**
236 230 * Handle cache settings update
@@ -304,9 +298,9 @@
304 298 * @return bool
305 299 */
306 300 public function can_purge_cache() {
307 301 if (!$this->is_enabled()) return false;
308 -
302 +
309 303 return WP_Optimize_Utils::current_user_can_purge_cache();
310 304 }
311 305
312 306 /**
@@ -380,14 +374,20 @@
380 374 }
381 375 // phpcs:enable
382 376
383 377 if (is_admin()) {
384 - add_action('admin_notices', function() use ($message, $type) {
378 + $admin_notices_hook = is_network_admin() ? 'network_admin_notices' : 'admin_notices';
379 +
380 + add_action($admin_notices_hook, function() use ($message, $type) {
385 381 $this->show_notice($message, $type);
386 382 });
383 +
384 + add_action('admin_head', array('WP_Optimize_Utils', 'script_to_remove_cache_purge_params_from_url'));
387 385 } else {
388 386 printf('<script>window.onload = function() {alert("%s");}</script>', esc_js($message));
387 + WP_Optimize_Utils::script_to_remove_cache_purge_params_from_url();
389 388 }
389 +
390 390 return;
391 391 }
392 392
393 393 if (!isset($_GET['_wpo_purge'])) return;
@@ -554,11 +554,12 @@
554 554
555 555 /**
556 556 * Disables page cache
557 557 *
558 + * @param bool $keep_option_enabled - Whether to keep the option `enable_page_caching` enabled or not. This is useful when we want to disable the cache temporarily, e.g. when deactivating the plugin, but we want to keep the option enabled so that it can be re-enabled when the plugin is reactivated.
558 559 * @return bool|WP_Error - true on success or WP_Error on failure.
559 560 */
560 - public function disable() {
561 + public function disable($keep_option_enabled = false) {
561 562 global $is_apache;
562 563
563 564 $ret = true;
564 565
@@ -600,9 +601,12 @@
600 601 wp_clear_scheduled_hook('wpo_purge_old_cache');
601 602
602 603 if (!is_wp_error($ret)) {
603 604 wp_clear_scheduled_hook('wpo_prune_cache_logs');
604 - $ret = $this->update_page_cache_enabled_state(false);
605 +
606 + if (!$keep_option_enabled) {
607 + $ret = $this->update_page_cache_enabled_state(false);
608 + }
605 609 }
606 610
607 611 if (!is_wp_error($ret)) {
608 612 do_action('wpo_page_cache_disabled');
@@ -627,8 +631,9 @@
627 631 return true;
628 632 }
629 633
630 634 $cache_config['enable_page_caching'] = $enabled;
635 +
631 636 return $config->update($cache_config, true);
632 637 }
633 638
634 639 /**
@@ -733,36 +738,40 @@
733 738 return true;
734 739 }
735 740
736 741 /**
737 - * Check if cache is enabled and working
742 + * Checks whether page caching is turned on in settings.
738 743 *
739 - * @return bool - true on success, false otherwise
744 + * @return bool true if the `enable_page_caching` option is set & environment is ready to cache, false otherwise.
740 745 */
741 746 public function is_enabled() {
747 + return $this->config->get_option('enable_page_caching') && $this->is_environment_functional();
748 + }
742 749
743 - if (!$this->config->get_option('enable_page_caching')) return false;
750 + /**
751 + * Checks whether the cache delivery chain is actually intact and able to serve cached pages.
752 + *
753 + * Verifies the WP_CACHE and WPO_ADVANCED_CACHE constants are defined and true, and that the cache config file exists on disk.
754 + *
755 + * @return bool True if the delivery chain is functional, false otherwise.
756 + */
757 + private function is_environment_functional() {
758 + $is_cli_or_ajax = (defined('WP_CLI') && WP_CLI) || (defined('DOING_AJAX') && DOING_AJAX);
744 759
745 - if (!defined('WP_CACHE') || !WP_CACHE) {
746 - if ((!(defined('WP_CLI') && WP_CLI)) && (!defined('DOING_AJAX') || !DOING_AJAX)) {
747 - $this->log("WP_CACHE constant is not present in wp-config.php");
748 - return false;
749 - }
760 + if (!$is_cli_or_ajax && (!defined('WP_CACHE') || !WP_CACHE)) {
761 + $this->file_log("WP_CACHE constant is not present in wp-config.php");
762 + return false;
750 763 }
751 764
752 - if (!defined('WPO_ADVANCED_CACHE') || !WPO_ADVANCED_CACHE) {
753 - if ((!(defined('WP_CLI') && WP_CLI)) && (!defined('DOING_AJAX') || !DOING_AJAX)) {
754 - $this->log("WPO_ADVANCED_CACHE constant is not present in advanced-cache.php");
755 - return false;
756 - }
765 + if (!$is_cli_or_ajax && (!defined('WPO_ADVANCED_CACHE') || !WPO_ADVANCED_CACHE)) {
766 + $this->file_log("WPO_ADVANCED_CACHE constant is not present in advanced-cache.php");
767 + return false;
757 768 }
758 769
759 770 $config_file = WPO_CACHE_CONFIG_DIR . '/'.$this->config->get_cache_config_filename();
760 - if (!file_exists($config_file)) {
761 - if ((!(defined('WP_CLI') && WP_CLI)) && (!defined('DOING_AJAX') || !DOING_AJAX)) {
762 - $this->log("Cache config file $config_file is not present");
763 - return false;
764 - }
771 + if (!$is_cli_or_ajax && !file_exists($config_file)) {
772 + $this->file_log("Cache config file $config_file is not present");
773 + return false;
765 774 }
766 775
767 776 return true;
768 777 }
@@ -1030,8 +1039,13 @@
1030 1039 if (!$config_path) {
1031 1040 return false;
1032 1041 }
1033 1042
1043 + // Bail early if wp-config.php is not writable, to avoid a PHP warning.
1044 + if (!wp_is_writable($config_path)) {
1045 + return false;
1046 + }
1047 +
1034 1048 $config_file_string = file_get_contents($config_path);
1035 1049
1036 1050 // Config file is empty. Maybe couldn't read it?
1037 1051 if (empty($config_file_string)) {
@@ -1157,8 +1171,17 @@
1157 1171 public function delete_cache_size_information() {
1158 1172 //not removing delete_transient so it clear this key from option table
1159 1173 delete_transient('wpo_get_cache_size');
1160 1174 delete_site_transient('wpo_get_cache_size');
1175 + }
1176 +
1177 + /**
1178 + * Update cache cleared time
1179 + */
1180 + public function update_cache_cleared_time(): void {
1181 + $config = $this->config->get();
1182 + $config['last_cleared'] = time();
1183 + $this->config->update($config);
1161 1184 }
1162 1185
1163 1186 /**
1164 1187 * Get current cache size.