| @@ -1,16 +1,35 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 | 4 | |
| 5 | +if (!class_exists('WP_Optimize_Load_Url_Task')) { | |
| 6 | + require_once(WPO_PLUGIN_MAIN_PATH . 'cache/class-wpo-load-url-task.php'); | |
| 7 | +} | |
| 5 | 8 | |
| 6 | 9 | class WP_Optimize_Page_Cache_Preloader extends WP_Optimize_Preloader { |
| 7 | - | |
| 10 | + | |
| 11 | + /** | |
| 12 | + * @var string | |
| 13 | + */ | |
| 8 | 14 | protected $preload_type = 'page_cache'; |
| 9 | - | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * @var string | |
| 18 | + */ | |
| 10 | 19 | protected $task_type = 'load-url-task'; |
| 20 | + | |
| 21 | + /** | |
| 22 | + * @var null | WP_Optimize_Page_Cache_Preloader | |
| 23 | + */ | |
| 24 | + static protected $_instance = null; | |
| 11 | 25 | |
| 12 | - static protected $_instance = null; | |
| 26 | + /** | |
| 27 | + * List of URLs for which a task will be created to preload them into cache | |
| 28 | + * | |
| 29 | + * @var array | |
| 30 | + */ | |
| 31 | + private $url_preload_list = array(); | |
| 13 | 32 | |
| 14 | 33 | /** |
| 15 | 34 | * WP_Optimize_Page_Cache_Preloader constructor. |
| 16 | 35 | */ |
| @@ -16,10 +35,10 @@ | ||
| 16 | 35 | */ |
| 17 | 36 | public function __construct() { |
| 18 | 37 | parent::__construct(); |
| 19 | 38 | |
| 20 | - add_filter('cron_schedules', array($this, 'cron_add_intervals')); | |
| 21 | 39 | add_action('wpo_page_cache_schedule_preload', array($this, 'run_scheduled_cache_preload')); |
| 40 | + add_action('wpo_page_cache_run_preload', array($this, 'run_preload_cron_job')); | |
| 22 | 41 | add_filter('wpo_preload_headers', array($this, 'preload_headers')); |
| 23 | 42 | } |
| 24 | 43 | |
| 25 | 44 | /** |
| @@ -39,8 +58,9 @@ | ||
| 39 | 58 | */ |
| 40 | 59 | public function cache_settings_updated($new_settings, $previous_settings) { |
| 41 | 60 | if (!$new_settings['enable_page_caching']) { |
| 42 | 61 | wp_clear_scheduled_hook('wpo_page_cache_schedule_preload'); |
| 62 | + wp_clear_scheduled_hook('wpo_page_cache_run_preload'); | |
| 43 | 63 | $this->delete_preload_continue_action(); |
| 44 | 64 | return; |
| 45 | 65 | } |
| 46 | 66 | |
| @@ -47,15 +67,21 @@ | ||
| 47 | 67 | if (!empty($new_settings['enable_schedule_preload'])) { |
| 48 | 68 | |
| 49 | 69 | $last_schedule_type = $previous_settings['preload_schedule_type']; |
| 50 | 70 | |
| 71 | + $last_use_nighttime = (int) $this->options->get_option('page_cache_schedule_preload_nighttime'); | |
| 72 | + // By default, we schedule preload events to the nighttime. | |
| 73 | + $use_nighttime = apply_filters('wpo_page_cache_schedule_preload_use_nighttime', 1); | |
| 74 | + | |
| 75 | + $this->options->update_option('page_cache_schedule_preload_nighttime', $use_nighttime); | |
| 76 | + | |
| 51 | 77 | if (wp_next_scheduled('wpo_page_cache_schedule_preload')) { |
| 52 | 78 | // if already scheduled this schedule type |
| 53 | - if ($new_settings['preload_schedule_type'] == $last_schedule_type) { | |
| 79 | + if ($new_settings['preload_schedule_type'] === $last_schedule_type && ($last_use_nighttime === $use_nighttime)) { | |
| 54 | 80 | // If the schedule type is cache lifespan, check if the cache lifespan changed. |
| 55 | - if ('wpo_use_cache_lifespan' == $new_settings['preload_schedule_type']) { | |
| 81 | + if ('wpo_use_cache_lifespan' === $new_settings['preload_schedule_type']) { | |
| 56 | 82 | // Else, if the settings cache lifespan settings haven't changed, returns |
| 57 | - if ($new_settings['page_cache_length_value'] == $previous_settings['page_cache_length_value'] && $new_settings['page_cache_length_unit'] == $previous_settings['page_cache_length_unit']) { | |
| 83 | + if ($new_settings['page_cache_length_value'] === $previous_settings['page_cache_length_value'] && $new_settings['page_cache_length_unit'] === $previous_settings['page_cache_length_unit']) { | |
| 58 | 84 | return; |
| 59 | 85 | } |
| 60 | 86 | } else { |
| 61 | 87 | return; |
| @@ -63,10 +89,17 @@ | ||
| 63 | 89 | } |
| 64 | 90 | // clear currently scheduled preload action. |
| 65 | 91 | wp_clear_scheduled_hook('wpo_page_cache_schedule_preload'); |
| 66 | 92 | } |
| 93 | + | |
| 67 | 94 | // schedule preload action. |
| 68 | - wp_schedule_event((time() + $this->get_schedule_interval($new_settings['preload_schedule_type'])), $new_settings['preload_schedule_type'], 'wpo_page_cache_schedule_preload'); | |
| 95 | + $timestamp = time() + $this->get_schedule_interval($new_settings['preload_schedule_type']); | |
| 96 | + | |
| 97 | + if ($use_nighttime) { | |
| 98 | + $timestamp = $this->make_nighttime($timestamp); | |
| 99 | + } | |
| 100 | + | |
| 101 | + wp_schedule_event($timestamp, $new_settings['preload_schedule_type'], 'wpo_page_cache_schedule_preload'); | |
| 69 | 102 | } else { |
| 70 | 103 | wp_clear_scheduled_hook('wpo_page_cache_schedule_preload'); |
| 71 | 104 | } |
| 72 | 105 | |
| @@ -83,13 +116,32 @@ | ||
| 83 | 116 | |
| 84 | 117 | // schedule preload action if need. |
| 85 | 118 | if ($this->is_scheduled_preload_enabled()) { |
| 86 | 119 | $preload_schedule_type = $this->get_cache_config('preload_schedule_type'); |
| 87 | - wp_schedule_event(time() + $this->get_schedule_interval($preload_schedule_type), $preload_schedule_type, 'wpo_page_cache_schedule_preload'); | |
| 120 | + // By default, we schedule preload events to the nighttime. | |
| 121 | + $use_nighttime = apply_filters('wpo_page_cache_schedule_preload_use_nighttime', 1); | |
| 122 | + | |
| 123 | + $timestamp = time() + $this->get_schedule_interval($preload_schedule_type); | |
| 124 | + if ($use_nighttime) { | |
| 125 | + $timestamp = $this->make_nighttime($timestamp); | |
| 126 | + } | |
| 127 | + | |
| 128 | + wp_schedule_event($timestamp, $preload_schedule_type, 'wpo_page_cache_schedule_preload'); | |
| 88 | 129 | } |
| 89 | 130 | } |
| 90 | 131 | |
| 91 | 132 | /** |
| 133 | + * Reschedule preload action on plugin activation if page cache enabled. | |
| 134 | + * | |
| 135 | + * @return void | |
| 136 | + */ | |
| 137 | + public function maybe_reschedule_preload_on_activation() { | |
| 138 | + if ($this->get_cache_config('enable_page_caching')) { | |
| 139 | + $this->reschedule_preload(); | |
| 140 | + } | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 92 | 144 | * Check if scheduled preload enabled. |
| 93 | 145 | * |
| 94 | 146 | * @return bool |
| 95 | 147 | */ |
| @@ -98,31 +150,8 @@ | ||
| 98 | 150 | return !empty($enable_schedule_preload); |
| 99 | 151 | } |
| 100 | 152 | |
| 101 | 153 | /** |
| 102 | - * Add intervals to cron schedules. | |
| 103 | - * | |
| 104 | - * @param array $schedules | |
| 105 | - * | |
| 106 | - * @return array | |
| 107 | - */ | |
| 108 | - public function cron_add_intervals($schedules) { | |
| 109 | - $interval = $this->get_continue_preload_cron_interval(); | |
| 110 | - $schedules['wpo_page_cache_preload_continue_interval'] = array( | |
| 111 | - 'interval' => $interval, | |
| 112 | - 'display' => round($interval / 60, 1).' minutes' | |
| 113 | - ); | |
| 114 | - | |
| 115 | - $schedules['wpo_use_cache_lifespan'] = array( | |
| 116 | - 'interval' => WPO_Cache_Config::instance()->get_option('page_cache_length'), | |
| 117 | - 'display' => 'Same as cache lifespan: '.WPO_Cache_Config::instance()->get_option('page_cache_length_value').' '.WPO_Cache_Config::instance()->get_option('page_cache_length_unit') | |
| 118 | - ); | |
| 119 | - | |
| 120 | - return $schedules; | |
| 121 | - } | |
| 122 | - | |
| 123 | - | |
| 124 | - /** | |
| 125 | 154 | * Check if we need run cache preload and run it. |
| 126 | 155 | */ |
| 127 | 156 | public function run_scheduled_cache_preload() { |
| 128 | 157 | |
| @@ -129,17 +158,19 @@ | ||
| 129 | 158 | $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type'); |
| 130 | 159 | if (!$schedule_type) return; |
| 131 | 160 | |
| 132 | 161 | // Don't run preload if cache lifespan option enabled and cache not expired yet. |
| 133 | - if ('wpo_use_cache_lifespan' == $schedule_type) { | |
| 162 | + if ('wpo_use_cache_lifespan' === $schedule_type) { | |
| 134 | 163 | |
| 135 | 164 | /** |
| 136 | 165 | * Filters the allowed time difference between the cache exiry and the current time, in seconds. |
| 137 | 166 | * If the cache expires in less than $allowed_time_difference, preload. Otherwise leave it. |
| 138 | 167 | * |
| 139 | - * @param integer $allowed_time_difference The time difference, in seconds (default = 600) | |
| 168 | + * @param integer $allowed_time_difference The time difference, in seconds (default is same as changed time limit) | |
| 140 | 169 | */ |
| 141 | - $allowed_time_difference = apply_filters('wpo_preload_allowed_time_difference', 600); | |
| 170 | + $time_limit = (defined('WP_OPTIMIZE_SET_TIME_LIMIT') && WP_OPTIMIZE_SET_TIME_LIMIT > 15) ? WP_OPTIMIZE_SET_TIME_LIMIT : 1800; | |
| 171 | + | |
| 172 | + $allowed_time_difference = apply_filters('wpo_preload_allowed_time_difference', $time_limit); | |
| 142 | 173 | $page_cache_lifespan = WPO_Cache_Config::instance()->get_option('page_cache_length', 0); |
| 143 | 174 | $last_preload_time = $this->options->get_option('wpo_last_page_cache_preload', 0); |
| 144 | 175 | $time_since_last_preload = time() - $last_preload_time; |
| 145 | 176 | $minimum_time_to_next_schedule_preload = $page_cache_lifespan - $allowed_time_difference; |
| @@ -149,12 +180,30 @@ | ||
| 149 | 180 | |
| 150 | 181 | $this->run(); |
| 151 | 182 | } |
| 152 | 183 | |
| 184 | + /** | |
| 185 | + * Runs manual cache preload and reschedules if scheduled preload is enabled | |
| 186 | + * | |
| 187 | + * @return void | |
| 188 | + */ | |
| 189 | + public function run_preload_cron_job() { | |
| 190 | + $wpo_page_cache = WP_Optimize()->get_page_cache(); | |
| 153 | 191 | |
| 192 | + if ($this->is_scheduled_preload_enabled()) { | |
| 193 | + $this->cancel_preload(); | |
| 194 | + $this->reschedule_preload(); | |
| 195 | + $this->run('scheduled', null, true); | |
| 196 | + } elseif ($wpo_page_cache->should_auto_preload_purged_contents()) { | |
| 197 | + $this->run('manual', null, true); | |
| 198 | + } | |
| 199 | + } | |
| 200 | + | |
| 154 | 201 | /** |
| 155 | 202 | * Get cache config option value. |
| 156 | 203 | * |
| 204 | + * @param string $option | |
| 205 | + * | |
| 157 | 206 | * @return mixed |
| 158 | 207 | */ |
| 159 | 208 | public function get_cache_config($option) { |
| 160 | 209 | static $config = null; |
| @@ -180,10 +229,12 @@ | ||
| 180 | 229 | if (!empty($urls)) { |
| 181 | 230 | |
| 182 | 231 | $this->log(__('Creating tasks for preload site urls.', 'wp-optimize')); |
| 183 | 232 | |
| 233 | + $wpo_cache_specific_urls_only = wpo_cache_specific_urls_only(); | |
| 234 | + | |
| 184 | 235 | foreach ($urls as $url) { |
| 185 | - if (wpo_url_in_exceptions($url)) continue; | |
| 236 | + if (!$wpo_cache_specific_urls_only && wpo_url_in_exceptions($url)) continue; | |
| 186 | 237 | |
| 187 | 238 | if ($this->url_is_already_cached($url, $type)) { |
| 188 | 239 | continue; |
| 189 | 240 | } |
| @@ -191,11 +242,8 @@ | ||
| 191 | 242 | // this description is being used for internal purposes. |
| 192 | 243 | $description = 'Preload - '.$url; |
| 193 | 244 | $options = array('url' => $url, 'preload_type' => $type, 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI)); |
| 194 | 245 | |
| 195 | - if (!class_exists('WP_Optimize_Load_Url_Task')) { | |
| 196 | - require_once WPO_PLUGIN_MAIN_PATH . 'cache/class-wpo-load-url-task.php'; | |
| 197 | - } | |
| 198 | 246 | WP_Optimize_Load_Url_Task::create_task($this->task_type, $description, $options, 'WP_Optimize_Load_Url_Task'); |
| 199 | 247 | } |
| 200 | 248 | |
| 201 | 249 | $this->log(__('Tasks for preload site urls created.', 'wp-optimize')); |
| @@ -219,9 +267,9 @@ | ||
| 219 | 267 | if (!$is_mobile_caching_enabled) return; |
| 220 | 268 | |
| 221 | 269 | $mobile_args = array( |
| 222 | 270 | 'httpversion' => '1.1', |
| 223 | - 'user-agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1', | |
| 271 | + 'user-agent' => WP_Optimize_Utils::get_user_agent('mobile'), | |
| 224 | 272 | 'timeout' => 10, |
| 225 | 273 | 'headers' => apply_filters('wpo_preload_headers', array()), |
| 226 | 274 | ); |
| 227 | 275 | |
| @@ -243,9 +291,9 @@ | ||
| 243 | 291 | if (!apply_filters('wpo_should_preload_amp', false, $url)) return; |
| 244 | 292 | |
| 245 | 293 | $amp_args = array( |
| 246 | 294 | 'httpversion' => '1.1', |
| 247 | - 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36', | |
| 295 | + 'user-agent' => WP_Optimize_Utils::get_user_agent(), | |
| 248 | 296 | 'timeout' => 10, |
| 249 | 297 | 'headers' => array( |
| 250 | 298 | 'X-WP-Optimize-Cache-Preload' => 'Yes', |
| 251 | 299 | ), |
| @@ -275,8 +323,9 @@ | ||
| 275 | 323 | $urls = $this->get_post_urls(); |
| 276 | 324 | $this->options->update_option('wpo_last_page_cache_preload_type', 'posts'); |
| 277 | 325 | } |
| 278 | 326 | |
| 327 | + // translators: %d: Number of URLs found | |
| 279 | 328 | $this->log(sprintf(_n('%d url found.', '%d urls found.', count($urls), 'wp-optimize'), count($urls))); |
| 280 | 329 | |
| 281 | 330 | /** |
| 282 | 331 | * Filter the URLs which will be preloaded |
| @@ -283,9 +332,10 @@ | ||
| 283 | 332 | * |
| 284 | 333 | * @param array $urls |
| 285 | 334 | * @return array |
| 286 | 335 | */ |
| 287 | - return apply_filters('wpo_preload_get_site_urls', $urls); | |
| 336 | + $filtered_urls = apply_filters('wpo_preload_get_site_urls', $urls); | |
| 337 | + return is_array($filtered_urls) ? $filtered_urls : $urls; | |
| 288 | 338 | } |
| 289 | 339 | |
| 290 | 340 | /** |
| 291 | 341 | * Loads sitemap file and returns list of urls. |
| @@ -309,24 +359,27 @@ | ||
| 309 | 359 | // load sitemap file. |
| 310 | 360 | $response = wp_remote_get($sitemap_url, array('timeout' => 30)); |
| 311 | 361 | |
| 312 | 362 | // if we get error then |
| 363 | + // sometimes returns error due to timeout | |
| 313 | 364 | if (is_wp_error($response)) { |
| 314 | - $response = file_get_contents($sitemap_url); | |
| 365 | + $response = @file_get_contents($sitemap_url); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warnings when there is network error | |
| 315 | 366 | |
| 316 | 367 | // if response is empty then try load from file. |
| 317 | - if (empty($response) && '' == $sitemap_url) { | |
| 368 | + if (empty($response)) { | |
| 318 | 369 | $sitemap_file = $this->get_local_sitemap_file(); |
| 319 | 370 | |
| 320 | - $response = file_get_contents($sitemap_file); | |
| 371 | + if (is_file($sitemap_file)) { | |
| 372 | + $response = file_get_contents($sitemap_file); | |
| 373 | + } | |
| 321 | 374 | } |
| 322 | 375 | |
| 323 | 376 | if (empty($response)) return $urls; |
| 324 | 377 | |
| 325 | - $xml = @simplexml_load_string($response); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged | |
| 378 | + $xml = @simplexml_load_string($response); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warnings when error found in XML data | |
| 326 | 379 | } else { |
| 327 | 380 | // parse xml answer. |
| 328 | - $xml = @simplexml_load_string(wp_remote_retrieve_body($response)); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged | |
| 381 | + $xml = @simplexml_load_string(wp_remote_retrieve_body($response)); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warnings when error found in XML data | |
| 329 | 382 | } |
| 330 | 383 | |
| 331 | 384 | // xml file has not valid xml content then return false. |
| 332 | 385 | if (false === $xml) return false; |
| @@ -353,20 +406,8 @@ | ||
| 353 | 406 | return $urls; |
| 354 | 407 | } |
| 355 | 408 | |
| 356 | 409 | /** |
| 357 | - * Get the path to a local sitemap file | |
| 358 | - * | |
| 359 | - * @return string | |
| 360 | - */ | |
| 361 | - private function get_local_sitemap_file() { | |
| 362 | - if (!function_exists('get_home_path')) { | |
| 363 | - include_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 364 | - } | |
| 365 | - return trailingslashit(get_home_path()) . $this->get_sitemap_filename(); | |
| 366 | - } | |
| 367 | - | |
| 368 | - /** | |
| 369 | 410 | * Get all posts of any post type and returns urls for them. |
| 370 | 411 | * |
| 371 | 412 | * @return array |
| 372 | 413 | */ |
| @@ -402,9 +443,9 @@ | ||
| 402 | 443 | // if there any separators add urls for each page |
| 403 | 444 | if (count($matches[0])) { |
| 404 | 445 | $prefix = strpos($permalink, '?') ? '&page=' : ''; |
| 405 | 446 | for ($page = 0; $page < count($matches[0]); $page++) { |
| 406 | - if ('' != $prefix) { | |
| 447 | + if ('' !== $prefix) { | |
| 407 | 448 | $urls[] = $permalink . $prefix . ($page+2); |
| 408 | 449 | } else { |
| 409 | 450 | $urls[] = trailingslashit($permalink) . ($page+2); |
| 410 | 451 | } |
| @@ -442,9 +483,9 @@ | ||
| 442 | 483 | * @return bool |
| 443 | 484 | */ |
| 444 | 485 | public function is_domain_mapping_enabled() { |
| 445 | 486 | // SUNRISE constant is defined with installation WordPress MU Domain Mapping plugin. |
| 446 | - $enabled = is_multisite() && defined('SUNRISE') && 'on' == strtolower(SUNRISE); | |
| 487 | + $enabled = is_multisite() && defined('SUNRISE') && 'on' === strtolower(SUNRISE); | |
| 447 | 488 | |
| 448 | 489 | /** |
| 449 | 490 | * Filters if Multisite Domain mapping is enabled. |
| 450 | 491 | * Currently, we can only detect if the WordPress MU Domain Mapping plugin is in use. |
| @@ -465,11 +506,11 @@ | ||
| 465 | 506 | |
| 466 | 507 | $domain = ''; |
| 467 | 508 | $multisite_plugin_table_name = $wpdb->base_prefix.'domain_mapping'; |
| 468 | 509 | // Check if table exists |
| 469 | - if ($wpdb->get_var("SHOW TABLES LIKE '$multisite_plugin_table_name'") != $multisite_plugin_table_name) { | |
| 510 | + if ($wpdb->get_var("SHOW TABLES LIKE '" . esc_sql($multisite_plugin_table_name) . "'") !== $multisite_plugin_table_name) { | |
| 470 | 511 | // This table created in WordPress MU Domain Mapping plugin. |
| 471 | - $row = $wpdb->get_row("SELECT `domain` FROM {$multisite_plugin_table_name} WHERE `blog_id` = {$blog_id} AND `active` = 1", ARRAY_A); | |
| 512 | + $row = $wpdb->get_row($wpdb->prepare("SELECT `domain` FROM " .esc_sql($multisite_plugin_table_name) . " WHERE `blog_id` = %d AND `active` = %d", $blog_id, 1), ARRAY_A); | |
| 472 | 513 | if (!empty($row)) { |
| 473 | 514 | $domain = $row['domain']; |
| 474 | 515 | } |
| 475 | 516 | } else { |
| @@ -486,12 +527,41 @@ | ||
| 486 | 527 | return apply_filters('wpo_get_mapped_domain', $domain, $blog_id); |
| 487 | 528 | } |
| 488 | 529 | |
| 489 | 530 | /** |
| 531 | + * Actually add the URLs to be preloaded into cache during `shutdown` hook | |
| 532 | + * | |
| 533 | + * @return void | |
| 534 | + */ | |
| 535 | + public function create_tasks_for_auto_preload_urls() { | |
| 536 | + $wpo_cache_specific_urls_only = wpo_cache_specific_urls_only(); | |
| 537 | + foreach ($this->url_preload_list as $url) { | |
| 538 | + if (!$wpo_cache_specific_urls_only && wpo_url_in_exceptions($url)) continue; | |
| 539 | + | |
| 540 | + $description = 'Preload - '.$url; | |
| 541 | + $options = array('url' => $url, 'preload_type' => 'manual', 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI)); | |
| 542 | + | |
| 543 | + WP_Optimize_Load_Url_Task::create_task($this->task_type, $description, $options, 'WP_Optimize_Load_Url_Task'); | |
| 544 | + } | |
| 545 | + | |
| 546 | + $this->run('manual', null, true); | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * Prepare the URL list that will need to be tasked to be preloaded | |
| 551 | + * | |
| 552 | + * @param string $url The URL to be preloaded | |
| 553 | + * @return void | |
| 554 | + */ | |
| 555 | + public function add_url_to_preload_list($url) { | |
| 556 | + $this->url_preload_list[] = $url; | |
| 557 | + } | |
| 558 | + | |
| 559 | + /** | |
| 490 | 560 | * Captures and logs any interesting messages |
| 491 | 561 | * |
| 492 | - * @param String $message - the error message | |
| 493 | - * @param String $error_type - the error type | |
| 562 | + * @param string $message - the error message | |
| 563 | + * @param string $error_type - the error type | |
| 494 | 564 | */ |
| 495 | 565 | public function log($message, $error_type = 'info') { |
| 496 | 566 | |
| 497 | 567 | if (isset($this->loggers)) { |
| @@ -546,16 +616,16 @@ | ||
| 546 | 616 | continue; |
| 547 | 617 | } |
| 548 | 618 | |
| 549 | 619 | if ($this->should_regenerate_file($file_path, $preload_type)) { |
| 550 | - // delefe the expired cache file | |
| 551 | - unlink($file_path); | |
| 620 | + // delete the expired cache file | |
| 621 | + wp_delete_file($file_path); | |
| 552 | 622 | $regenerate_count++; |
| 553 | 623 | } |
| 554 | 624 | } |
| 555 | 625 | |
| 556 | 626 | // if 0 == $regenerate_count, nothing all the expected files exist, and none were deleted. |
| 557 | - return 0 == $regenerate_count; | |
| 627 | + return 0 === $regenerate_count; | |
| 558 | 628 | } |
| 559 | 629 | |
| 560 | 630 | /** |
| 561 | 631 | * Determine if a file should be regenerated |
| @@ -579,9 +649,9 @@ | ||
| 579 | 649 | // Sets the variables once per request: |
| 580 | 650 | if (null === $is_preloader_scheduled) { |
| 581 | 651 | $is_preloader_scheduled = WPO_Cache_Config::instance()->get_option('enable_schedule_preload'); |
| 582 | 652 | $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type'); |
| 583 | - $lifespan = WPO_Cache_Config::instance()->get_option('page_cache_length'); | |
| 653 | + $lifespan = (int) WPO_Cache_Config::instance()->get_option('page_cache_length'); | |
| 584 | 654 | $schedule_interval = $this->get_schedule_interval($schedule_type); |
| 585 | 655 | |
| 586 | 656 | /** |
| 587 | 657 | * Expiry threshold: the current file will be considered stale if within the threshold. Default: 600s (10min) |
| @@ -603,9 +673,9 @@ | ||
| 603 | 673 | */ |
| 604 | 674 | $regenerate_file_when_no_expiry_date = apply_filters('wpo_regenerate_file_when_no_expiry_date', true); |
| 605 | 675 | } |
| 606 | 676 | |
| 607 | - if (($always_regenerate_file_if_preload_is_manual && 'manual' == $preload_type) || ($always_regenerate_file_if_preload_is_scheduled && 'scheduled' == $preload_type)) { | |
| 677 | + if (($always_regenerate_file_if_preload_is_manual && 'manual' === $preload_type) || ($always_regenerate_file_if_preload_is_scheduled && 'scheduled' === $preload_type)) { | |
| 608 | 678 | $result = true; |
| 609 | 679 | } else { |
| 610 | 680 | |
| 611 | 681 | $modified_time = (int) filemtime($path); |
| @@ -610,9 +680,9 @@ | ||
| 610 | 680 | |
| 611 | 681 | $modified_time = (int) filemtime($path); |
| 612 | 682 | |
| 613 | 683 | // cache lifespan is set. |
| 614 | - if (0 != $lifespan) { | |
| 684 | + if (0 !== $lifespan) { | |
| 615 | 685 | $expiry_time = $modified_time + $lifespan - $lifespan_expiry_threshold; |
| 616 | 686 | $result = time() > $expiry_time; |
| 617 | 687 | } elseif ($is_preloader_scheduled) { |
| 618 | 688 | $expiry_time = $modified_time + $schedule_interval - $lifespan_expiry_threshold; |
| @@ -627,8 +697,10 @@ | ||
| 627 | 697 | } |
| 628 | 698 | |
| 629 | 699 | /** |
| 630 | 700 | * Add preloader headers |
| 701 | + * | |
| 702 | + * @param array $headers Preload headers | |
| 631 | 703 | */ |
| 632 | 704 | public function preload_headers($headers) { |
| 633 | 705 | $headers['X-WP-Optimize-Cache-Preload'] = 'Yes'; |
| 634 | 706 | return $headers; |
| @@ -656,13 +728,25 @@ | ||
| 656 | 728 | 'success' => false, |
| 657 | 729 | 'error' => __('Probably page cache preload is running already.', 'wp-optimize') |
| 658 | 730 | ); |
| 659 | 731 | } |
| 660 | - | |
| 732 | + | |
| 733 | + /** | |
| 734 | + * Returns preload data | |
| 735 | + * | |
| 736 | + * @return array | |
| 737 | + */ | |
| 661 | 738 | protected function get_preload_data() { |
| 662 | 739 | return WP_Optimize()->get_page_cache()->get_cache_size(); |
| 663 | 740 | } |
| 664 | - | |
| 741 | + | |
| 742 | + /** | |
| 743 | + * Returns preloading message | |
| 744 | + * | |
| 745 | + * @param array $cache_size | |
| 746 | + * | |
| 747 | + * @return array | |
| 748 | + */ | |
| 665 | 749 | protected function get_preloading_message($cache_size) { |
| 666 | 750 | return array( |
| 667 | 751 | 'done' => false, |
| 668 | 752 | 'message' => __('Loading URLs...', 'wp-optimize'), |
| @@ -669,18 +753,34 @@ | ||
| 669 | 753 | 'size' => WP_Optimize()->format_size($cache_size['size']), |
| 670 | 754 | 'file_count' => $cache_size['file_count'] |
| 671 | 755 | ); |
| 672 | 756 | } |
| 673 | - | |
| 757 | + | |
| 758 | + /** | |
| 759 | + * Returns last preload message | |
| 760 | + * | |
| 761 | + * @param array $cache_size | |
| 762 | + * @param string $last_preload_time_str | |
| 763 | + * | |
| 764 | + * @return array | |
| 765 | + */ | |
| 674 | 766 | protected function get_last_preload_message($cache_size, $last_preload_time_str) { |
| 675 | 767 | return array( |
| 676 | 768 | 'done' => true, |
| 769 | + // translators: %s is the last preload time | |
| 677 | 770 | 'message' => sprintf(__('Last preload finished at %s', 'wp-optimize'), $last_preload_time_str), |
| 678 | 771 | 'size' => WP_Optimize()->format_size($cache_size['size']), |
| 679 | 772 | 'file_count' => $cache_size['file_count'] |
| 680 | 773 | ); |
| 681 | 774 | } |
| 682 | - | |
| 775 | + | |
| 776 | + /** | |
| 777 | + * Returns preload success message | |
| 778 | + * | |
| 779 | + * @param array $cache_size | |
| 780 | + * | |
| 781 | + * @return array | |
| 782 | + */ | |
| 683 | 783 | protected function get_preload_success_message($cache_size) { |
| 684 | 784 | return array( |
| 685 | 785 | 'done' => true, |
| 686 | 786 | 'size' => WP_Optimize()->format_size($cache_size['size']), |
| @@ -686,9 +786,18 @@ | ||
| 686 | 786 | 'size' => WP_Optimize()->format_size($cache_size['size']), |
| 687 | 787 | 'file_count' => $cache_size['file_count'] |
| 688 | 788 | ); |
| 689 | 789 | } |
| 690 | - | |
| 790 | + | |
| 791 | + /** | |
| 792 | + * Returns preload progress message | |
| 793 | + * | |
| 794 | + * @param array $cache_size | |
| 795 | + * @param string $preloaded_message | |
| 796 | + * @param int $preload_resuming_in | |
| 797 | + * | |
| 798 | + * @return array | |
| 799 | + */ | |
| 691 | 800 | protected function get_preload_progress_message($cache_size, $preloaded_message, $preload_resuming_in) { |
| 692 | 801 | return array( |
| 693 | 802 | 'done' => false, |
| 694 | 803 | 'message' => $preloaded_message, |
| @@ -696,7 +805,25 @@ | ||
| 696 | 805 | 'file_count' => $cache_size['file_count'], |
| 697 | 806 | 'resume_in' => $preload_resuming_in |
| 698 | 807 | ); |
| 699 | 808 | } |
| 809 | + | |
| 810 | + /** | |
| 811 | + * Shifts the given timestamp to nighttime. | |
| 812 | + * | |
| 813 | + * @param int $time | |
| 814 | + * | |
| 815 | + * @return int | |
| 816 | + */ | |
| 817 | + private function make_nighttime($time) { | |
| 818 | + | |
| 819 | + $gmt_offset = WP_Optimize_Utils::get_gmt_offset(); | |
| 820 | + | |
| 821 | + // Set time to random time from 1 a.m. to 4:59 a.m. | |
| 822 | + $random_time = sprintf('%02d:%02d', wp_rand(1, 4), wp_rand(0, 59)); | |
| 823 | + $nighttime = strtotime(gmdate("Y-m-d {$random_time}:00", $time)) - $gmt_offset; | |
| 824 | + | |
| 825 | + if ($nighttime < time()) $nighttime += 24 * 3600; | |
| 826 | + | |
| 827 | + return $nighttime; | |
| 828 | + } | |
| 700 | 829 | } |
| 701 | - | |
| 702 | -WP_Optimize_Page_Cache_Preloader::instance(); | |