| @@ -5,11 +5,17 @@ | ||
| 5 | 5 | /** |
| 6 | 6 | * All cache commands that are intended to be available for calling from any sort of control interface (e.g. wp-admin, UpdraftCentral) go in here. All public methods should either return the data to be returned, or a WP_Error with associated error code, message and error data. |
| 7 | 7 | */ |
| 8 | 8 | class WP_Optimize_Cache_Commands { |
| 9 | - | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * @var WP_Optimizer | |
| 12 | + */ | |
| 10 | 13 | private $optimizer; |
| 11 | - | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * @var WP_Optimize_Options | |
| 17 | + */ | |
| 12 | 18 | private $options; |
| 13 | 19 | |
| 14 | 20 | /** |
| 15 | 21 | * WP_Optimize_Cache_Commands constructor. |
| @@ -41,13 +47,14 @@ | ||
| 41 | 47 | } |
| 42 | 48 | |
| 43 | 49 | $enabled = false; |
| 44 | 50 | $disabled = false; |
| 45 | - $return = !empty($validation) ? $validation : array(); | |
| 51 | + $return = empty($validation) ? array() : $validation; | |
| 46 | 52 | $previous_settings = WPO_Cache_Config::instance()->get(); |
| 53 | + $settings_changed = false; | |
| 47 | 54 | |
| 48 | 55 | // Attempt to change current status if required |
| 49 | - if (isset($previous_settings['enable_page_caching']) && $previous_settings['enable_page_caching'] != $data['cache-settings']['enable_page_caching']) { | |
| 56 | + if (isset($previous_settings['enable_page_caching']) && (bool) $previous_settings['enable_page_caching'] !== (bool) $data['cache-settings']['enable_page_caching']) { | |
| 50 | 57 | // Disable cache. |
| 51 | 58 | if (empty($data['cache-settings']['enable_page_caching'])) { |
| 52 | 59 | $disabled = WPO_Page_Cache::instance()->disable(); |
| 53 | 60 | // Disabling failed |
| @@ -52,12 +59,23 @@ | ||
| 52 | 59 | $disabled = WPO_Page_Cache::instance()->disable(); |
| 53 | 60 | // Disabling failed |
| 54 | 61 | if ($disabled && is_wp_error($disabled)) { |
| 55 | 62 | // If disabling failed, we re-enable whatever was disabled, to make sure nothing breaks. |
| 56 | - if ($previous_settings['enable_page_caching']) WPO_Page_Cache::instance()->enable(true); | |
| 63 | + $cache_enable_result = null; | |
| 64 | + if ($previous_settings['enable_page_caching']) { | |
| 65 | + $cache_enable_result = WPO_Page_Cache::instance()->enable(true); | |
| 66 | + } | |
| 67 | + | |
| 68 | + $error_message = $disabled->get_error_message(); | |
| 69 | + | |
| 70 | + if (is_wp_error($cache_enable_result)) { | |
| 71 | + // translators: %s: The error message. | |
| 72 | + $error_message .= ' '. sprintf(__('Additionally, WP-Optimize failed to re-enable page caching after the failed disable attempt: %s', 'wp-optimize'), $cache_enable_result->get_error_message()); | |
| 73 | + } | |
| 74 | + | |
| 57 | 75 | $return['error'] = array( |
| 58 | 76 | 'code' => $disabled->get_error_code(), |
| 59 | - 'message' => $disabled->get_error_message() | |
| 77 | + 'message' => $error_message, | |
| 60 | 78 | ); |
| 61 | 79 | } elseif (WPO_Page_Cache::instance()->has_warnings()) { |
| 62 | 80 | $return['warnings_label'] = __('Page caching was disabled, but with some warnings:', 'wp-optimize'); |
| 63 | 81 | $return['warnings'] = WPO_Page_Cache::instance()->get_errors('warning'); |
| @@ -85,10 +103,9 @@ | ||
| 85 | 103 | } |
| 86 | 104 | // Override enabled setting value |
| 87 | 105 | $data['cache-settings']['enable_page_caching'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled)); |
| 88 | 106 | } else { |
| 89 | - $data['cache-settings']['enable_page_caching'] = $previous_settings['enable_page_caching']; | |
| 90 | - $enabled = $previous_settings['enable_page_caching']; | |
| 107 | + $enabled = isset($data['cache-settings']['enable_page_caching']) ? (bool) $data['cache-settings']['enable_page_caching'] : false; | |
| 91 | 108 | } |
| 92 | 109 | |
| 93 | 110 | $data['cache-settings']['use_webp_images'] = (bool) WP_Optimize()->get_options()->get_option('webp_conversion'); |
| 94 | 111 | |
| @@ -95,9 +112,14 @@ | ||
| 95 | 112 | $skip_if_no_file_yet = !$enabled || is_wp_error($enabled); |
| 96 | 113 | $save_settings_result = WPO_Cache_Config::instance()->update($data['cache-settings'], $skip_if_no_file_yet); |
| 97 | 114 | |
| 98 | 115 | if ($save_settings_result && !is_wp_error($save_settings_result)) { |
| 99 | - WP_Optimize_Page_Cache_Preloader::instance()->cache_settings_updated($data['cache-settings'], $previous_settings); | |
| 116 | + $settings_changed = !WP_Optimize_Utils::array_contains($data['cache-settings'], $previous_settings); | |
| 117 | + | |
| 118 | + if ($settings_changed) { | |
| 119 | + WP_Optimize_Page_Cache_Preloader::instance()->cache_settings_updated($data['cache-settings'], $previous_settings); | |
| 120 | + WPO_Page_Cache::instance()->cache_settings_updated($data['cache-settings']); | |
| 121 | + } | |
| 100 | 122 | $return['result'] = $save_settings_result; |
| 101 | 123 | } else { |
| 102 | 124 | // Saving the settings returned an error |
| 103 | 125 | if (is_wp_error($save_settings_result)) { |
| @@ -114,8 +136,39 @@ | ||
| 114 | 136 | } |
| 115 | 137 | |
| 116 | 138 | $return['enabled'] = ($enabled && !is_wp_error($enabled)) || ($previous_settings['enable_page_caching'] && is_wp_error($disabled)); |
| 117 | 139 | |
| 140 | + if ($enabled && !is_wp_error($enabled)) { | |
| 141 | + $options_needs_purge = array_flip(array( | |
| 142 | + 'enable_mobile_caching', | |
| 143 | + 'enable_user_caching', | |
| 144 | + 'enable_per_role_cache', | |
| 145 | + 'enable_user_specific_cache', | |
| 146 | + 'enable_rest_caching', | |
| 147 | + 'host_gravatars_locally', | |
| 148 | + 'cache_exception_urls', | |
| 149 | + 'cache_ignore_query_variables', | |
| 150 | + 'cache_exception_cookies', | |
| 151 | + 'cache_exception_conditional_tags', | |
| 152 | + 'cache_exception_browser_agents', | |
| 153 | + )); | |
| 154 | + $previous_settings = array_intersect_key($previous_settings, $options_needs_purge); | |
| 155 | + $current_settings = wp_parse_args($data['cache-settings'], WPO_Cache_Config::instance()->defaults); | |
| 156 | + $current_settings = array_intersect_key($current_settings, $options_needs_purge); | |
| 157 | + ksort($previous_settings); | |
| 158 | + ksort($current_settings); | |
| 159 | + if ($current_settings !== $previous_settings) { | |
| 160 | + $cache = WPO_Page_Cache::instance(); | |
| 161 | + $cache->file_log('Setting changed, purging cache'); | |
| 162 | + $cache->purge(); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + // $disable can either be boolean or WP_Error | |
| 166 | + if (!is_wp_error($disabled) && $disabled && $settings_changed) { | |
| 167 | + WPO_Page_Cache::instance()->prune_cache_logs(); | |
| 168 | + wp_clear_scheduled_hook('wpo_prune_cache_logs'); | |
| 169 | + } | |
| 170 | + | |
| 118 | 171 | return $return; |
| 119 | 172 | } |
| 120 | 173 | |
| 121 | 174 | /** |
| @@ -128,9 +181,11 @@ | ||
| 128 | 181 | |
| 129 | 182 | $status[] = WPO_Page_Cache::instance()->is_enabled() ? __('Caching is enabled', 'wp-optimize') : __('Caching is disabled', 'wp-optimize'); |
| 130 | 183 | |
| 131 | 184 | $preloader_status = WP_Optimize_Page_Cache_Preloader::instance()->get_status_info(); |
| 185 | + // translators: %s: The current cache size | |
| 132 | 186 | $status[] = sprintf(__('Current cache size: %s', 'wp-optimize'), $preloader_status['size']); |
| 187 | + // translators: %s: The number of files in the cache | |
| 133 | 188 | $status[] = sprintf(__('Number of files: %s', 'wp-optimize'), $preloader_status['file_count']); |
| 134 | 189 | |
| 135 | 190 | if (array_key_exists('message', $preloader_status)) $status[] = $preloader_status['message']; |
| 136 | 191 | |
| @@ -162,10 +217,12 @@ | ||
| 162 | 217 | * |
| 163 | 218 | * @return array |
| 164 | 219 | */ |
| 165 | 220 | public function purge_page_cache() { |
| 221 | + | |
| 222 | + $wpo_page_cache = WP_Optimize()->get_page_cache(); | |
| 166 | 223 | |
| 167 | - if (!WP_Optimize()->get_page_cache()->is_enabled()) { | |
| 224 | + if (!$wpo_page_cache->is_enabled()) { | |
| 168 | 225 | return array( |
| 169 | 226 | 'success' => false, |
| 170 | 227 | 'error' => __('Cache is not enabled.', 'wp-optimize'), |
| 171 | 228 | ); |
| @@ -170,9 +227,9 @@ | ||
| 170 | 227 | 'error' => __('Cache is not enabled.', 'wp-optimize'), |
| 171 | 228 | ); |
| 172 | 229 | } |
| 173 | 230 | |
| 174 | - if (!WP_Optimize()->get_page_cache()->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) { | |
| 231 | + if (!$wpo_page_cache->can_purge_cache() && !(defined('WP_CLI') && WP_CLI)) { | |
| 175 | 232 | return array( |
| 176 | 233 | 'success' => false, |
| 177 | 234 | 'message' => __('You do not have permission to purge the cache', 'wp-optimize'), |
| 178 | 235 | ); |
| @@ -178,8 +235,9 @@ | ||
| 178 | 235 | ); |
| 179 | 236 | } |
| 180 | 237 | |
| 181 | 238 | $purged = WP_Optimize()->get_page_cache()->purge(); |
| 239 | + if ($purged) WP_Optimize()->get_page_cache()->file_log("Full Cache Purge triggered by: " . __METHOD__); | |
| 182 | 240 | $cache_size = WP_Optimize()->get_page_cache()->get_cache_size(); |
| 183 | 241 | $wpo_page_cache_preloader = WP_Optimize_Page_Cache_Preloader::instance(); |
| 184 | 242 | |
| 185 | 243 | $response = array( |
| @@ -195,8 +253,11 @@ | ||
| 195 | 253 | $wpo_page_cache_preloader->reschedule_preload(); |
| 196 | 254 | |
| 197 | 255 | // run preloader. |
| 198 | 256 | $wpo_page_cache_preloader->run('scheduled', $response); |
| 257 | + } elseif ($wpo_page_cache->should_auto_preload_purged_contents()) { | |
| 258 | + // When auto preload is enabled but scheduled preload is disabled | |
| 259 | + $wpo_page_cache_preloader->run('manual', $response); | |
| 199 | 260 | } |
| 200 | 261 | |
| 201 | 262 | if ($response['success']) { |
| 202 | 263 | $response['message'] = __('Page cache purged successfully', 'wp-optimize'); |
| @@ -305,7 +366,24 @@ | ||
| 305 | 366 | * @param array $params Option value sent via AJAX |
| 306 | 367 | * @return array |
| 307 | 368 | */ |
| 308 | 369 | public function save_cache_auto_preload_option($params) { |
| 309 | - return array('success' => $this->options->update_option('auto_preload_purged_contents', 'true' == $params['enabled'] ? 'true' : 'false')); | |
| 370 | + $wpo_cache = WP_Optimize()->get_page_cache(); | |
| 371 | + $wpo_cache_options = $wpo_cache->config->get(); | |
| 372 | + $wpo_cache_options['auto_preload_purged_contents'] = 'true' === $params['enabled'] ? 1 : 0; | |
| 373 | + $update_status = $wpo_cache->config->update($wpo_cache_options); | |
| 374 | + | |
| 375 | + if (is_wp_error($update_status)) { | |
| 376 | + $result = array( | |
| 377 | + 'success' => false, | |
| 378 | + 'message' => $update_status->get_error_message(), | |
| 379 | + ); | |
| 380 | + } else { | |
| 381 | + $result = array( | |
| 382 | + 'success' => true, | |
| 383 | + 'message' => __('Auto preload settings have been updated successfully', 'wp-optimize'), | |
| 384 | + ); | |
| 385 | + } | |
| 386 | + | |
| 387 | + return $result; | |
| 310 | 388 | } |
| 311 | 389 | } |