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-wp-optimize-page-cache-preloader.php +120 -36 3.5.0 → 4.7.0 View file →
@@ -6,13 +6,22 @@
6 6 require_once(WPO_PLUGIN_MAIN_PATH . 'cache/class-wpo-load-url-task.php');
7 7 }
8 8
9 9 class WP_Optimize_Page_Cache_Preloader extends WP_Optimize_Preloader {
10 -
10 +
11 + /**
12 + * @var string
13 + */
11 14 protected $preload_type = 'page_cache';
12 -
15 +
16 + /**
17 + * @var string
18 + */
13 19 protected $task_type = 'load-url-task';
14 -
20 +
21 + /**
22 + * @var null | WP_Optimize_Page_Cache_Preloader
23 + */
15 24 static protected $_instance = null;
16 25
17 26 /**
18 27 * List of URLs for which a task will be created to preload them into cache
@@ -27,8 +36,9 @@
27 36 public function __construct() {
28 37 parent::__construct();
29 38
30 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'));
31 41 add_filter('wpo_preload_headers', array($this, 'preload_headers'));
32 42 }
33 43
34 44 /**
@@ -48,8 +58,9 @@
48 58 */
49 59 public function cache_settings_updated($new_settings, $previous_settings) {
50 60 if (!$new_settings['enable_page_caching']) {
51 61 wp_clear_scheduled_hook('wpo_page_cache_schedule_preload');
62 + wp_clear_scheduled_hook('wpo_page_cache_run_preload');
52 63 $this->delete_preload_continue_action();
53 64 return;
54 65 }
55 66
@@ -56,9 +67,9 @@
56 67 if (!empty($new_settings['enable_schedule_preload'])) {
57 68
58 69 $last_schedule_type = $previous_settings['preload_schedule_type'];
59 70
60 - $last_use_nighttime = $this->options->get_option('page_cache_schedule_preload_nighttime');
71 + $last_use_nighttime = (int) $this->options->get_option('page_cache_schedule_preload_nighttime');
61 72 // By default, we schedule preload events to the nighttime.
62 73 $use_nighttime = apply_filters('wpo_page_cache_schedule_preload_use_nighttime', 1);
63 74
64 75 $this->options->update_option('page_cache_schedule_preload_nighttime', $use_nighttime);
@@ -64,13 +75,13 @@
64 75 $this->options->update_option('page_cache_schedule_preload_nighttime', $use_nighttime);
65 76
66 77 if (wp_next_scheduled('wpo_page_cache_schedule_preload')) {
67 78 // if already scheduled this schedule type
68 - if ($new_settings['preload_schedule_type'] == $last_schedule_type && ($last_use_nighttime === $use_nighttime)) {
79 + if ($new_settings['preload_schedule_type'] === $last_schedule_type && ($last_use_nighttime === $use_nighttime)) {
69 80 // If the schedule type is cache lifespan, check if the cache lifespan changed.
70 - if ('wpo_use_cache_lifespan' == $new_settings['preload_schedule_type']) {
81 + if ('wpo_use_cache_lifespan' === $new_settings['preload_schedule_type']) {
71 82 // Else, if the settings cache lifespan settings haven't changed, returns
72 - 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']) {
73 84 return;
74 85 }
75 86 } else {
76 87 return;
@@ -118,8 +129,19 @@
118 129 }
119 130 }
120 131
121 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 + /**
122 144 * Check if scheduled preload enabled.
123 145 *
124 146 * @return bool
125 147 */
@@ -136,9 +158,9 @@
136 158 $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type');
137 159 if (!$schedule_type) return;
138 160
139 161 // Don't run preload if cache lifespan option enabled and cache not expired yet.
140 - if ('wpo_use_cache_lifespan' == $schedule_type) {
162 + if ('wpo_use_cache_lifespan' === $schedule_type) {
141 163
142 164 /**
143 165 * Filters the allowed time difference between the cache exiry and the current time, in seconds.
144 166 * If the cache expires in less than $allowed_time_difference, preload. Otherwise leave it.
@@ -158,12 +180,30 @@
158 180
159 181 $this->run();
160 182 }
161 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();
162 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 +
163 201 /**
164 202 * Get cache config option value.
165 203 *
204 + * @param string $option
205 + *
166 206 * @return mixed
167 207 */
168 208 public function get_cache_config($option) {
169 209 static $config = null;
@@ -189,10 +229,12 @@
189 229 if (!empty($urls)) {
190 230
191 231 $this->log(__('Creating tasks for preload site urls.', 'wp-optimize'));
192 232
233 + $wpo_cache_specific_urls_only = wpo_cache_specific_urls_only();
234 +
193 235 foreach ($urls as $url) {
194 - if (wpo_url_in_exceptions($url)) continue;
236 + if (!$wpo_cache_specific_urls_only && wpo_url_in_exceptions($url)) continue;
195 237
196 238 if ($this->url_is_already_cached($url, $type)) {
197 239 continue;
198 240 }
@@ -225,9 +267,9 @@
225 267 if (!$is_mobile_caching_enabled) return;
226 268
227 269 $mobile_args = array(
228 270 'httpversion' => '1.1',
229 - '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'),
230 272 'timeout' => 10,
231 273 'headers' => apply_filters('wpo_preload_headers', array()),
232 274 );
233 275
@@ -249,9 +291,9 @@
249 291 if (!apply_filters('wpo_should_preload_amp', false, $url)) return;
250 292
251 293 $amp_args = array(
252 294 'httpversion' => '1.1',
253 - '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(),
254 296 'timeout' => 10,
255 297 'headers' => array(
256 298 'X-WP-Optimize-Cache-Preload' => 'Yes',
257 299 ),
@@ -281,8 +323,9 @@
281 323 $urls = $this->get_post_urls();
282 324 $this->options->update_option('wpo_last_page_cache_preload_type', 'posts');
283 325 }
284 326
327 + // translators: %d: Number of URLs found
285 328 $this->log(sprintf(_n('%d url found.', '%d urls found.', count($urls), 'wp-optimize'), count($urls)));
286 329
287 330 /**
288 331 * Filter the URLs which will be preloaded
@@ -289,9 +332,10 @@
289 332 *
290 333 * @param array $urls
291 334 * @return array
292 335 */
293 - 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;
294 338 }
295 339
296 340 /**
297 341 * Loads sitemap file and returns list of urls.
@@ -317,12 +361,12 @@
317 361
318 362 // if we get error then
319 363 // sometimes returns error due to timeout
320 364 if (is_wp_error($response)) {
321 - $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
322 366
323 367 // if response is empty then try load from file.
324 - if (empty($response) && '' == $sitemap_url) {
368 + if (empty($response)) {
325 369 $sitemap_file = $this->get_local_sitemap_file();
326 370
327 371 if (is_file($sitemap_file)) {
328 372 $response = file_get_contents($sitemap_file);
@@ -399,9 +443,9 @@
399 443 // if there any separators add urls for each page
400 444 if (count($matches[0])) {
401 445 $prefix = strpos($permalink, '?') ? '&page=' : '';
402 446 for ($page = 0; $page < count($matches[0]); $page++) {
403 - if ('' != $prefix) {
447 + if ('' !== $prefix) {
404 448 $urls[] = $permalink . $prefix . ($page+2);
405 449 } else {
406 450 $urls[] = trailingslashit($permalink) . ($page+2);
407 451 }
@@ -439,9 +483,9 @@
439 483 * @return bool
440 484 */
441 485 public function is_domain_mapping_enabled() {
442 486 // SUNRISE constant is defined with installation WordPress MU Domain Mapping plugin.
443 - $enabled = is_multisite() && defined('SUNRISE') && 'on' == strtolower(SUNRISE);
487 + $enabled = is_multisite() && defined('SUNRISE') && 'on' === strtolower(SUNRISE);
444 488
445 489 /**
446 490 * Filters if Multisite Domain mapping is enabled.
447 491 * Currently, we can only detect if the WordPress MU Domain Mapping plugin is in use.
@@ -462,11 +506,11 @@
462 506
463 507 $domain = '';
464 508 $multisite_plugin_table_name = $wpdb->base_prefix.'domain_mapping';
465 509 // Check if table exists
466 - 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) {
467 511 // This table created in WordPress MU Domain Mapping plugin.
468 - $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);
469 513 if (!empty($row)) {
470 514 $domain = $row['domain'];
471 515 }
472 516 } else {
@@ -488,11 +532,14 @@
488 532 *
489 533 * @return void
490 534 */
491 535 public function create_tasks_for_auto_preload_urls() {
536 + $wpo_cache_specific_urls_only = wpo_cache_specific_urls_only();
492 537 foreach ($this->url_preload_list as $url) {
538 + if (!$wpo_cache_specific_urls_only && wpo_url_in_exceptions($url)) continue;
539 +
493 540 $description = 'Preload - '.$url;
494 - $options = array('url' => $url, 'preload_type' => $this->task_type, 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI));
541 + $options = array('url' => $url, 'preload_type' => 'manual', 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI));
495 542
496 543 WP_Optimize_Load_Url_Task::create_task($this->task_type, $description, $options, 'WP_Optimize_Load_Url_Task');
497 544 }
498 545
@@ -511,10 +558,10 @@
511 558
512 559 /**
513 560 * Captures and logs any interesting messages
514 561 *
515 - * @param String $message - the error message
516 - * @param String $error_type - the error type
562 + * @param string $message - the error message
563 + * @param string $error_type - the error type
517 564 */
518 565 public function log($message, $error_type = 'info') {
519 566
520 567 if (isset($this->loggers)) {
@@ -569,16 +616,16 @@
569 616 continue;
570 617 }
571 618
572 619 if ($this->should_regenerate_file($file_path, $preload_type)) {
573 - // delefe the expired cache file
574 - unlink($file_path);
620 + // delete the expired cache file
621 + wp_delete_file($file_path);
575 622 $regenerate_count++;
576 623 }
577 624 }
578 625
579 626 // if 0 == $regenerate_count, nothing all the expected files exist, and none were deleted.
580 - return 0 == $regenerate_count;
627 + return 0 === $regenerate_count;
581 628 }
582 629
583 630 /**
584 631 * Determine if a file should be regenerated
@@ -602,9 +649,9 @@
602 649 // Sets the variables once per request:
603 650 if (null === $is_preloader_scheduled) {
604 651 $is_preloader_scheduled = WPO_Cache_Config::instance()->get_option('enable_schedule_preload');
605 652 $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type');
606 - $lifespan = WPO_Cache_Config::instance()->get_option('page_cache_length');
653 + $lifespan = (int) WPO_Cache_Config::instance()->get_option('page_cache_length');
607 654 $schedule_interval = $this->get_schedule_interval($schedule_type);
608 655
609 656 /**
610 657 * Expiry threshold: the current file will be considered stale if within the threshold. Default: 600s (10min)
@@ -626,9 +673,9 @@
626 673 */
627 674 $regenerate_file_when_no_expiry_date = apply_filters('wpo_regenerate_file_when_no_expiry_date', true);
628 675 }
629 676
630 - 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)) {
631 678 $result = true;
632 679 } else {
633 680
634 681 $modified_time = (int) filemtime($path);
@@ -633,9 +680,9 @@
633 680
634 681 $modified_time = (int) filemtime($path);
635 682
636 683 // cache lifespan is set.
637 - if (0 != $lifespan) {
684 + if (0 !== $lifespan) {
638 685 $expiry_time = $modified_time + $lifespan - $lifespan_expiry_threshold;
639 686 $result = time() > $expiry_time;
640 687 } elseif ($is_preloader_scheduled) {
641 688 $expiry_time = $modified_time + $schedule_interval - $lifespan_expiry_threshold;
@@ -650,8 +697,10 @@
650 697 }
651 698
652 699 /**
653 700 * Add preloader headers
701 + *
702 + * @param array $headers Preload headers
654 703 */
655 704 public function preload_headers($headers) {
656 705 $headers['X-WP-Optimize-Cache-Preload'] = 'Yes';
657 706 return $headers;
@@ -679,13 +728,25 @@
679 728 'success' => false,
680 729 'error' => __('Probably page cache preload is running already.', 'wp-optimize')
681 730 );
682 731 }
683 -
732 +
733 + /**
734 + * Returns preload data
735 + *
736 + * @return array
737 + */
684 738 protected function get_preload_data() {
685 739 return WP_Optimize()->get_page_cache()->get_cache_size();
686 740 }
687 -
741 +
742 + /**
743 + * Returns preloading message
744 + *
745 + * @param array $cache_size
746 + *
747 + * @return array
748 + */
688 749 protected function get_preloading_message($cache_size) {
689 750 return array(
690 751 'done' => false,
691 752 'message' => __('Loading URLs...', 'wp-optimize'),
@@ -692,18 +753,34 @@
692 753 'size' => WP_Optimize()->format_size($cache_size['size']),
693 754 'file_count' => $cache_size['file_count']
694 755 );
695 756 }
696 -
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 + */
697 766 protected function get_last_preload_message($cache_size, $last_preload_time_str) {
698 767 return array(
699 768 'done' => true,
769 + // translators: %s is the last preload time
700 770 'message' => sprintf(__('Last preload finished at %s', 'wp-optimize'), $last_preload_time_str),
701 771 'size' => WP_Optimize()->format_size($cache_size['size']),
702 772 'file_count' => $cache_size['file_count']
703 773 );
704 774 }
705 -
775 +
776 + /**
777 + * Returns preload success message
778 + *
779 + * @param array $cache_size
780 + *
781 + * @return array
782 + */
706 783 protected function get_preload_success_message($cache_size) {
707 784 return array(
708 785 'done' => true,
709 786 'size' => WP_Optimize()->format_size($cache_size['size']),
@@ -709,9 +786,18 @@
709 786 'size' => WP_Optimize()->format_size($cache_size['size']),
710 787 'file_count' => $cache_size['file_count']
711 788 );
712 789 }
713 -
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 + */
714 800 protected function get_preload_progress_message($cache_size, $preloaded_message, $preload_resuming_in) {
715 801 return array(
716 802 'done' => false,
717 803 'message' => $preloaded_message,
@@ -732,10 +818,10 @@
732 818
733 819 $gmt_offset = WP_Optimize_Utils::get_gmt_offset();
734 820
735 821 // Set time to random time from 1 a.m. to 4:59 a.m.
736 - $random_time = sprintf('%02d:%02d', rand(1, 4), rand(0, 59));
737 - $nighttime = strtotime(date("Y-m-d {$random_time}:00", $time)) - $gmt_offset;
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;
738 824
739 825 if ($nighttime < time()) $nighttime += 24 * 3600;
740 826
741 827 return $nighttime;
@@ -740,6 +826,4 @@
740 826
741 827 return $nighttime;
742 828 }
743 829 }
744 -
745 -WP_Optimize_Page_Cache_Preloader::instance();