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 +100 -34 3.7.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
@@ -58,9 +67,9 @@
58 67 if (!empty($new_settings['enable_schedule_preload'])) {
59 68
60 69 $last_schedule_type = $previous_settings['preload_schedule_type'];
61 70
62 - $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');
63 72 // By default, we schedule preload events to the nighttime.
64 73 $use_nighttime = apply_filters('wpo_page_cache_schedule_preload_use_nighttime', 1);
65 74
66 75 $this->options->update_option('page_cache_schedule_preload_nighttime', $use_nighttime);
@@ -66,13 +75,13 @@
66 75 $this->options->update_option('page_cache_schedule_preload_nighttime', $use_nighttime);
67 76
68 77 if (wp_next_scheduled('wpo_page_cache_schedule_preload')) {
69 78 // if already scheduled this schedule type
70 - 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)) {
71 80 // If the schedule type is cache lifespan, check if the cache lifespan changed.
72 - if ('wpo_use_cache_lifespan' == $new_settings['preload_schedule_type']) {
81 + if ('wpo_use_cache_lifespan' === $new_settings['preload_schedule_type']) {
73 82 // Else, if the settings cache lifespan settings haven't changed, returns
74 - 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']) {
75 84 return;
76 85 }
77 86 } else {
78 87 return;
@@ -120,8 +129,19 @@
120 129 }
121 130 }
122 131
123 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 + /**
124 144 * Check if scheduled preload enabled.
125 145 *
126 146 * @return bool
127 147 */
@@ -138,9 +158,9 @@
138 158 $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type');
139 159 if (!$schedule_type) return;
140 160
141 161 // Don't run preload if cache lifespan option enabled and cache not expired yet.
142 - if ('wpo_use_cache_lifespan' == $schedule_type) {
162 + if ('wpo_use_cache_lifespan' === $schedule_type) {
143 163
144 164 /**
145 165 * Filters the allowed time difference between the cache exiry and the current time, in seconds.
146 166 * If the cache expires in less than $allowed_time_difference, preload. Otherwise leave it.
@@ -180,8 +200,10 @@
180 200
181 201 /**
182 202 * Get cache config option value.
183 203 *
204 + * @param string $option
205 + *
184 206 * @return mixed
185 207 */
186 208 public function get_cache_config($option) {
187 209 static $config = null;
@@ -207,10 +229,12 @@
207 229 if (!empty($urls)) {
208 230
209 231 $this->log(__('Creating tasks for preload site urls.', 'wp-optimize'));
210 232
233 + $wpo_cache_specific_urls_only = wpo_cache_specific_urls_only();
234 +
211 235 foreach ($urls as $url) {
212 - if (wpo_url_in_exceptions($url)) continue;
236 + if (!$wpo_cache_specific_urls_only && wpo_url_in_exceptions($url)) continue;
213 237
214 238 if ($this->url_is_already_cached($url, $type)) {
215 239 continue;
216 240 }
@@ -243,9 +267,9 @@
243 267 if (!$is_mobile_caching_enabled) return;
244 268
245 269 $mobile_args = array(
246 270 'httpversion' => '1.1',
247 - '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'),
248 272 'timeout' => 10,
249 273 'headers' => apply_filters('wpo_preload_headers', array()),
250 274 );
251 275
@@ -267,9 +291,9 @@
267 291 if (!apply_filters('wpo_should_preload_amp', false, $url)) return;
268 292
269 293 $amp_args = array(
270 294 'httpversion' => '1.1',
271 - '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(),
272 296 'timeout' => 10,
273 297 'headers' => array(
274 298 'X-WP-Optimize-Cache-Preload' => 'Yes',
275 299 ),
@@ -299,8 +323,9 @@
299 323 $urls = $this->get_post_urls();
300 324 $this->options->update_option('wpo_last_page_cache_preload_type', 'posts');
301 325 }
302 326
327 + // translators: %d: Number of URLs found
303 328 $this->log(sprintf(_n('%d url found.', '%d urls found.', count($urls), 'wp-optimize'), count($urls)));
304 329
305 330 /**
306 331 * Filter the URLs which will be preloaded
@@ -307,9 +332,10 @@
307 332 *
308 333 * @param array $urls
309 334 * @return array
310 335 */
311 - 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;
312 338 }
313 339
314 340 /**
315 341 * Loads sitemap file and returns list of urls.
@@ -335,12 +361,12 @@
335 361
336 362 // if we get error then
337 363 // sometimes returns error due to timeout
338 364 if (is_wp_error($response)) {
339 - $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
340 366
341 367 // if response is empty then try load from file.
342 - if (empty($response) && '' == $sitemap_url) {
368 + if (empty($response)) {
343 369 $sitemap_file = $this->get_local_sitemap_file();
344 370
345 371 if (is_file($sitemap_file)) {
346 372 $response = file_get_contents($sitemap_file);
@@ -417,9 +443,9 @@
417 443 // if there any separators add urls for each page
418 444 if (count($matches[0])) {
419 445 $prefix = strpos($permalink, '?') ? '&page=' : '';
420 446 for ($page = 0; $page < count($matches[0]); $page++) {
421 - if ('' != $prefix) {
447 + if ('' !== $prefix) {
422 448 $urls[] = $permalink . $prefix . ($page+2);
423 449 } else {
424 450 $urls[] = trailingslashit($permalink) . ($page+2);
425 451 }
@@ -457,9 +483,9 @@
457 483 * @return bool
458 484 */
459 485 public function is_domain_mapping_enabled() {
460 486 // SUNRISE constant is defined with installation WordPress MU Domain Mapping plugin.
461 - $enabled = is_multisite() && defined('SUNRISE') && 'on' == strtolower(SUNRISE);
487 + $enabled = is_multisite() && defined('SUNRISE') && 'on' === strtolower(SUNRISE);
462 488
463 489 /**
464 490 * Filters if Multisite Domain mapping is enabled.
465 491 * Currently, we can only detect if the WordPress MU Domain Mapping plugin is in use.
@@ -480,11 +506,11 @@
480 506
481 507 $domain = '';
482 508 $multisite_plugin_table_name = $wpdb->base_prefix.'domain_mapping';
483 509 // Check if table exists
484 - 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) {
485 511 // This table created in WordPress MU Domain Mapping plugin.
486 - $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);
487 513 if (!empty($row)) {
488 514 $domain = $row['domain'];
489 515 }
490 516 } else {
@@ -506,10 +532,11 @@
506 532 *
507 533 * @return void
508 534 */
509 535 public function create_tasks_for_auto_preload_urls() {
536 + $wpo_cache_specific_urls_only = wpo_cache_specific_urls_only();
510 537 foreach ($this->url_preload_list as $url) {
511 - if (wpo_url_in_exceptions($url)) continue;
538 + if (!$wpo_cache_specific_urls_only && wpo_url_in_exceptions($url)) continue;
512 539
513 540 $description = 'Preload - '.$url;
514 541 $options = array('url' => $url, 'preload_type' => 'manual', 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI));
515 542
@@ -531,10 +558,10 @@
531 558
532 559 /**
533 560 * Captures and logs any interesting messages
534 561 *
535 - * @param String $message - the error message
536 - * @param String $error_type - the error type
562 + * @param string $message - the error message
563 + * @param string $error_type - the error type
537 564 */
538 565 public function log($message, $error_type = 'info') {
539 566
540 567 if (isset($this->loggers)) {
@@ -589,16 +616,16 @@
589 616 continue;
590 617 }
591 618
592 619 if ($this->should_regenerate_file($file_path, $preload_type)) {
593 - // delefe the expired cache file
594 - unlink($file_path);
620 + // delete the expired cache file
621 + wp_delete_file($file_path);
595 622 $regenerate_count++;
596 623 }
597 624 }
598 625
599 626 // if 0 == $regenerate_count, nothing all the expected files exist, and none were deleted.
600 - return 0 == $regenerate_count;
627 + return 0 === $regenerate_count;
601 628 }
602 629
603 630 /**
604 631 * Determine if a file should be regenerated
@@ -622,9 +649,9 @@
622 649 // Sets the variables once per request:
623 650 if (null === $is_preloader_scheduled) {
624 651 $is_preloader_scheduled = WPO_Cache_Config::instance()->get_option('enable_schedule_preload');
625 652 $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type');
626 - $lifespan = WPO_Cache_Config::instance()->get_option('page_cache_length');
653 + $lifespan = (int) WPO_Cache_Config::instance()->get_option('page_cache_length');
627 654 $schedule_interval = $this->get_schedule_interval($schedule_type);
628 655
629 656 /**
630 657 * Expiry threshold: the current file will be considered stale if within the threshold. Default: 600s (10min)
@@ -646,9 +673,9 @@
646 673 */
647 674 $regenerate_file_when_no_expiry_date = apply_filters('wpo_regenerate_file_when_no_expiry_date', true);
648 675 }
649 676
650 - 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)) {
651 678 $result = true;
652 679 } else {
653 680
654 681 $modified_time = (int) filemtime($path);
@@ -653,9 +680,9 @@
653 680
654 681 $modified_time = (int) filemtime($path);
655 682
656 683 // cache lifespan is set.
657 - if (0 != $lifespan) {
684 + if (0 !== $lifespan) {
658 685 $expiry_time = $modified_time + $lifespan - $lifespan_expiry_threshold;
659 686 $result = time() > $expiry_time;
660 687 } elseif ($is_preloader_scheduled) {
661 688 $expiry_time = $modified_time + $schedule_interval - $lifespan_expiry_threshold;
@@ -670,8 +697,10 @@
670 697 }
671 698
672 699 /**
673 700 * Add preloader headers
701 + *
702 + * @param array $headers Preload headers
674 703 */
675 704 public function preload_headers($headers) {
676 705 $headers['X-WP-Optimize-Cache-Preload'] = 'Yes';
677 706 return $headers;
@@ -699,13 +728,25 @@
699 728 'success' => false,
700 729 'error' => __('Probably page cache preload is running already.', 'wp-optimize')
701 730 );
702 731 }
703 -
732 +
733 + /**
734 + * Returns preload data
735 + *
736 + * @return array
737 + */
704 738 protected function get_preload_data() {
705 739 return WP_Optimize()->get_page_cache()->get_cache_size();
706 740 }
707 -
741 +
742 + /**
743 + * Returns preloading message
744 + *
745 + * @param array $cache_size
746 + *
747 + * @return array
748 + */
708 749 protected function get_preloading_message($cache_size) {
709 750 return array(
710 751 'done' => false,
711 752 'message' => __('Loading URLs...', 'wp-optimize'),
@@ -712,18 +753,34 @@
712 753 'size' => WP_Optimize()->format_size($cache_size['size']),
713 754 'file_count' => $cache_size['file_count']
714 755 );
715 756 }
716 -
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 + */
717 766 protected function get_last_preload_message($cache_size, $last_preload_time_str) {
718 767 return array(
719 768 'done' => true,
769 + // translators: %s is the last preload time
720 770 'message' => sprintf(__('Last preload finished at %s', 'wp-optimize'), $last_preload_time_str),
721 771 'size' => WP_Optimize()->format_size($cache_size['size']),
722 772 'file_count' => $cache_size['file_count']
723 773 );
724 774 }
725 -
775 +
776 + /**
777 + * Returns preload success message
778 + *
779 + * @param array $cache_size
780 + *
781 + * @return array
782 + */
726 783 protected function get_preload_success_message($cache_size) {
727 784 return array(
728 785 'done' => true,
729 786 'size' => WP_Optimize()->format_size($cache_size['size']),
@@ -729,9 +786,18 @@
729 786 'size' => WP_Optimize()->format_size($cache_size['size']),
730 787 'file_count' => $cache_size['file_count']
731 788 );
732 789 }
733 -
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 + */
734 800 protected function get_preload_progress_message($cache_size, $preloaded_message, $preload_resuming_in) {
735 801 return array(
736 802 'done' => false,
737 803 'message' => $preloaded_message,
@@ -752,10 +818,10 @@
752 818
753 819 $gmt_offset = WP_Optimize_Utils::get_gmt_offset();
754 820
755 821 // Set time to random time from 1 a.m. to 4:59 a.m.
756 - $random_time = sprintf('%02d:%02d', rand(1, 4), rand(0, 59));
757 - $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;
758 824
759 825 if ($nighttime < time()) $nighttime += 24 * 3600;
760 826
761 827 return $nighttime;