PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.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
wp-optimize / cache / class-wp-optimize-page-cache-preloader.php

class-wp-optimize-page-cache-preloader.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.7.0, at cache/class-wp-optimize-page-cache-preloader.php

764 lines 22.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
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 }
8
9 class WP_Optimize_Page_Cache_Preloader extends WP_Optimize_Preloader {
10
11 protected $preload_type = 'page_cache';
12
13 protected $task_type = 'load-url-task';
14
15 static protected $_instance = null;
16
17 /**
18 * List of URLs for which a task will be created to preload them into cache
19 *
20 * @var array
21 */
22 private $url_preload_list = array();
23
24 /**
25 * WP_Optimize_Page_Cache_Preloader constructor.
26 */
27 public function __construct() {
28 parent::__construct();
29
30 add_action('wpo_page_cache_schedule_preload', array($this, 'run_scheduled_cache_preload'));
31 add_action('wpo_page_cache_run_preload', array($this, 'run_preload_cron_job'));
32 add_filter('wpo_preload_headers', array($this, 'preload_headers'));
33 }
34
35 /**
36 * Check if cache is active.
37 *
38 * @return bool
39 */
40 public function is_option_active() {
41 return WP_Optimize()->get_page_cache()->is_enabled();
42 }
43
44 /**
45 * Schedule or delete automatic preload action on cache settings update.
46 *
47 * @param array $new_settings The new settings
48 * @param array $previous_settings Settings before saving
49 */
50 public function cache_settings_updated($new_settings, $previous_settings) {
51 if (!$new_settings['enable_page_caching']) {
52 wp_clear_scheduled_hook('wpo_page_cache_schedule_preload');
53 wp_clear_scheduled_hook('wpo_page_cache_run_preload');
54 $this->delete_preload_continue_action();
55 return;
56 }
57
58 if (!empty($new_settings['enable_schedule_preload'])) {
59
60 $last_schedule_type = $previous_settings['preload_schedule_type'];
61
62 $last_use_nighttime = $this->options->get_option('page_cache_schedule_preload_nighttime');
63 // By default, we schedule preload events to the nighttime.
64 $use_nighttime = apply_filters('wpo_page_cache_schedule_preload_use_nighttime', 1);
65
66 $this->options->update_option('page_cache_schedule_preload_nighttime', $use_nighttime);
67
68 if (wp_next_scheduled('wpo_page_cache_schedule_preload')) {
69 // if already scheduled this schedule type
70 if ($new_settings['preload_schedule_type'] == $last_schedule_type && ($last_use_nighttime === $use_nighttime)) {
71 // If the schedule type is cache lifespan, check if the cache lifespan changed.
72 if ('wpo_use_cache_lifespan' == $new_settings['preload_schedule_type']) {
73 // 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']) {
75 return;
76 }
77 } else {
78 return;
79 }
80 }
81 // clear currently scheduled preload action.
82 wp_clear_scheduled_hook('wpo_page_cache_schedule_preload');
83 }
84
85 // schedule preload action.
86 $timestamp = time() + $this->get_schedule_interval($new_settings['preload_schedule_type']);
87
88 if ($use_nighttime) {
89 $timestamp = $this->make_nighttime($timestamp);
90 }
91
92 wp_schedule_event($timestamp, $new_settings['preload_schedule_type'], 'wpo_page_cache_schedule_preload');
93 } else {
94 wp_clear_scheduled_hook('wpo_page_cache_schedule_preload');
95 }
96
97 }
98
99 /**
100 * Clear active preload tasks, reschedule preload action.
101 */
102 public function reschedule_preload() {
103 // clear scheduled action.
104 if (wp_next_scheduled('wpo_page_cache_schedule_preload')) {
105 wp_clear_scheduled_hook('wpo_page_cache_schedule_preload');
106 }
107
108 // schedule preload action if need.
109 if ($this->is_scheduled_preload_enabled()) {
110 $preload_schedule_type = $this->get_cache_config('preload_schedule_type');
111 // By default, we schedule preload events to the nighttime.
112 $use_nighttime = apply_filters('wpo_page_cache_schedule_preload_use_nighttime', 1);
113
114 $timestamp = time() + $this->get_schedule_interval($preload_schedule_type);
115 if ($use_nighttime) {
116 $timestamp = $this->make_nighttime($timestamp);
117 }
118
119 wp_schedule_event($timestamp, $preload_schedule_type, 'wpo_page_cache_schedule_preload');
120 }
121 }
122
123 /**
124 * Check if scheduled preload enabled.
125 *
126 * @return bool
127 */
128 public function is_scheduled_preload_enabled() {
129 $enable_schedule_preload = $this->get_cache_config('enable_schedule_preload');
130 return !empty($enable_schedule_preload);
131 }
132
133 /**
134 * Check if we need run cache preload and run it.
135 */
136 public function run_scheduled_cache_preload() {
137
138 $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type');
139 if (!$schedule_type) return;
140
141 // Don't run preload if cache lifespan option enabled and cache not expired yet.
142 if ('wpo_use_cache_lifespan' == $schedule_type) {
143
144 /**
145 * Filters the allowed time difference between the cache exiry and the current time, in seconds.
146 * If the cache expires in less than $allowed_time_difference, preload. Otherwise leave it.
147 *
148 * @param integer $allowed_time_difference The time difference, in seconds (default is same as changed time limit)
149 */
150 $time_limit = (defined('WP_OPTIMIZE_SET_TIME_LIMIT') && WP_OPTIMIZE_SET_TIME_LIMIT > 15) ? WP_OPTIMIZE_SET_TIME_LIMIT : 1800;
151
152 $allowed_time_difference = apply_filters('wpo_preload_allowed_time_difference', $time_limit);
153 $page_cache_lifespan = WPO_Cache_Config::instance()->get_option('page_cache_length', 0);
154 $last_preload_time = $this->options->get_option('wpo_last_page_cache_preload', 0);
155 $time_since_last_preload = time() - $last_preload_time;
156 $minimum_time_to_next_schedule_preload = $page_cache_lifespan - $allowed_time_difference;
157 // Skip this if the last preload is not as old as the cache lifespan minus $allowed_time_difference
158 if ($page_cache_lifespan > 0 && $time_since_last_preload < $minimum_time_to_next_schedule_preload) return;
159 }
160
161 $this->run();
162 }
163
164 /**
165 * Runs manual cache preload and reschedules if scheduled preload is enabled
166 *
167 * @return void
168 */
169 public function run_preload_cron_job() {
170 $wpo_page_cache = WP_Optimize()->get_page_cache();
171
172 if ($this->is_scheduled_preload_enabled()) {
173 $this->cancel_preload();
174 $this->reschedule_preload();
175 $this->run('scheduled', null, true);
176 } elseif ($wpo_page_cache->should_auto_preload_purged_contents()) {
177 $this->run('manual', null, true);
178 }
179 }
180
181 /**
182 * Get cache config option value.
183 *
184 * @return mixed
185 */
186 public function get_cache_config($option) {
187 static $config = null;
188
189 if (null === $config) $config = WPO_Page_Cache::instance()->config->get();
190
191 if (is_array($config) && array_key_exists($option, $config)) {
192 return $config[$option];
193 }
194
195 return false;
196 }
197
198 /**
199 * Create tasks (WP_Optimize_Load_Url_Task) for preload all urls from site.
200 *
201 * @param string $type The preload type (currently: scheduled, manual)
202 * @return void
203 */
204 public function create_tasks_for_preload_site_urls($type) {
205 $urls = $this->get_site_urls();
206
207 if (!empty($urls)) {
208
209 $this->log(__('Creating tasks for preload site urls.', 'wp-optimize'));
210
211 foreach ($urls as $url) {
212 if (wpo_url_in_exceptions($url)) continue;
213
214 if ($this->url_is_already_cached($url, $type)) {
215 continue;
216 }
217
218 // this description is being used for internal purposes.
219 $description = 'Preload - '.$url;
220 $options = array('url' => $url, 'preload_type' => $type, 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI));
221
222 WP_Optimize_Load_Url_Task::create_task($this->task_type, $description, $options, 'WP_Optimize_Load_Url_Task');
223 }
224
225 $this->log(__('Tasks for preload site urls created.', 'wp-optimize'));
226 }
227 }
228
229 /**
230 * Preload mobile version from $url.
231 *
232 * @param string $url
233 *
234 * @return void
235 */
236 public function preload_mobile($url) {
237 static $is_mobile_caching_enabled;
238 if (!isset($is_mobile_caching_enabled)) {
239 $is_mobile_caching_enabled = $this->get_cache_config('enable_mobile_caching');
240 }
241
242 // Only run if option is active
243 if (!$is_mobile_caching_enabled) return;
244
245 $mobile_args = array(
246 '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',
248 'timeout' => 10,
249 'headers' => apply_filters('wpo_preload_headers', array()),
250 );
251
252 $mobile_args = apply_filters('wpo_page_cache_preloader_mobile_args', $mobile_args, $url);
253
254 $this->log('preload_mobile - ' . $url);
255
256 wp_remote_get($url, $mobile_args);
257 }
258
259 /**
260 * Preload amp version from $url.
261 *
262 * @param string $url
263 *
264 * @return void
265 */
266 public function preload_amp($url) {
267 if (!apply_filters('wpo_should_preload_amp', false, $url)) return;
268
269 $amp_args = array(
270 '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',
272 'timeout' => 10,
273 'headers' => array(
274 'X-WP-Optimize-Cache-Preload' => 'Yes',
275 ),
276 );
277
278 $url = untrailingslashit($url) . '/amp/';
279
280 $amp_args = apply_filters('wpo_page_cache_preloader_amp_args', $amp_args, $url);
281
282 $this->log('preload_amp - ' . $url);
283
284 wp_remote_get($url, $amp_args);
285 }
286
287 /**
288 * Check if sitemap exists then returns list of urls from sitemap file otherwise returns all posts urls.
289 *
290 * @return array
291 */
292 public function get_site_urls() {
293
294 $urls = $this->get_sitemap_urls();
295
296 if (!empty($urls)) {
297 $this->options->update_option('wpo_last_page_cache_preload_type', 'sitemap');
298 } else {
299 $urls = $this->get_post_urls();
300 $this->options->update_option('wpo_last_page_cache_preload_type', 'posts');
301 }
302
303 $this->log(sprintf(_n('%d url found.', '%d urls found.', count($urls), 'wp-optimize'), count($urls)));
304
305 /**
306 * Filter the URLs which will be preloaded
307 *
308 * @param array $urls
309 * @return array
310 */
311 return apply_filters('wpo_preload_get_site_urls', $urls);
312 }
313
314 /**
315 * Loads sitemap file and returns list of urls.
316 *
317 * @param string $sitemap_url
318 *
319 * @return array|bool
320 */
321 public function get_sitemap_urls($sitemap_url = '') {
322
323 $urls = array();
324
325 // if sitemap url is empty then use main sitemap file name.
326 $sitemap_url = ('' === $sitemap_url) ? site_url('/'.$this->get_sitemap_filename()) : $sitemap_url;
327
328 // if simplexml_load_string not available then we don't load sitemap.
329 if (!function_exists('simplexml_load_string')) {
330 return $urls;
331 }
332
333 // load sitemap file.
334 $response = wp_remote_get($sitemap_url, array('timeout' => 30));
335
336 // if we get error then
337 // sometimes returns error due to timeout
338 if (is_wp_error($response)) {
339 $response = file_get_contents($sitemap_url);
340
341 // if response is empty then try load from file.
342 if (empty($response) && '' == $sitemap_url) {
343 $sitemap_file = $this->get_local_sitemap_file();
344
345 if (is_file($sitemap_file)) {
346 $response = file_get_contents($sitemap_file);
347 }
348 }
349
350 if (empty($response)) return $urls;
351
352 $xml = @simplexml_load_string($response); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warnings when error found in XML data
353 } else {
354 // parse xml answer.
355 $xml = @simplexml_load_string(wp_remote_retrieve_body($response)); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warnings when error found in XML data
356 }
357
358 // xml file has not valid xml content then return false.
359 if (false === $xml) return false;
360
361 // if exists urls then return them.
362 if (isset($xml->url)) {
363 foreach ($xml->url as $element) {
364 if (!isset($element->loc)) continue;
365 $urls[] = (string) $element->loc;
366 }
367 } elseif (isset($xml->sitemap)) {
368 // if has links to other sitemap files then get urls from them.
369 foreach ($xml->sitemap as $element) {
370 if (!isset($element->loc)) continue;
371
372 $sitemap_urls = $this->get_sitemap_urls($element->loc);
373
374 if (is_array($sitemap_urls)) {
375 $urls = array_merge($urls, $sitemap_urls);
376 }
377 }
378 }
379
380 return $urls;
381 }
382
383 /**
384 * Get all posts of any post type and returns urls for them.
385 *
386 * @return array
387 */
388 public function get_post_urls() {
389 global $post;
390
391 $offset = 0;
392 $posts_per_page = 1000;
393 $urls = array();
394
395 $urls[] = site_url('/');
396
397 do {
398 $query = new WP_Query(array(
399 'post_type' => 'any',
400 'post_status' => 'publish',
401 'posts_per_page' => $posts_per_page,
402 'offset' => $offset,
403 'orderby' => 'ID',
404 'order' => 'ASC',
405 'cache_results' => false, // disable cache to avoid memory error.
406 ));
407
408 $posts_loaded = $query->post_count;
409
410 while ($query->have_posts()) {
411 $query->the_post();
412 $permalink = get_permalink();
413 $urls[] = $permalink;
414
415 // check page separators in the post content
416 preg_match_all('/\<\!--nextpage--\>/', $post->post_content, $matches);
417 // if there any separators add urls for each page
418 if (count($matches[0])) {
419 $prefix = strpos($permalink, '?') ? '&page=' : '';
420 for ($page = 0; $page < count($matches[0]); $page++) {
421 if ('' != $prefix) {
422 $urls[] = $permalink . $prefix . ($page+2);
423 } else {
424 $urls[] = trailingslashit($permalink) . ($page+2);
425 }
426 }
427 }
428 }
429
430 $offset += $posts_loaded;
431 } while ($posts_loaded > 0);
432
433 /**
434 * If domain mapping enabled then replace domains in urls.
435 */
436 if ($this->is_domain_mapping_enabled()) {
437 $blog_id = get_current_blog_id();
438
439 $mapped_domain = $this->get_mapped_domain($blog_id);
440 $blog_details = get_blog_details($blog_id);
441
442 if ($mapped_domain) {
443 foreach ($urls as $i => $url) {
444 $urls[$i] = preg_replace('/'.$blog_details->domain.'/i', $mapped_domain, $url, 1);
445 }
446 }
447 }
448
449 wp_reset_postdata();
450
451 return $urls;
452 }
453
454 /**
455 * Check if domain mapping enabled.
456 *
457 * @return bool
458 */
459 public function is_domain_mapping_enabled() {
460 // SUNRISE constant is defined with installation WordPress MU Domain Mapping plugin.
461 $enabled = is_multisite() && defined('SUNRISE') && 'on' == strtolower(SUNRISE);
462
463 /**
464 * Filters if Multisite Domain mapping is enabled.
465 * Currently, we can only detect if the WordPress MU Domain Mapping plugin is in use.
466 * Using the WP Core functionality should not require this, unless if the domain name is set somewhere else but in the site url option.
467 */
468 return apply_filters('wpo_is_domain_mapping_enabled', $enabled);
469 }
470
471 /**
472 * Return mapped domain by $blog_id.
473 *
474 * @param int $blog_id
475 *
476 * @return string
477 */
478 public function get_mapped_domain($blog_id) {
479 global $wpdb;
480
481 $domain = '';
482 $multisite_plugin_table_name = $wpdb->base_prefix.'domain_mapping';
483 // Check if table exists
484 if ($wpdb->get_var("SHOW TABLES LIKE '$multisite_plugin_table_name'") != $multisite_plugin_table_name) {
485 // 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);
487 if (!empty($row)) {
488 $domain = $row['domain'];
489 }
490 } else {
491 // When using the WP Core method, the site url option contains the mapped domain.
492 $domain = get_site_url($blog_id);
493 }
494
495 /**
496 * Filters the mapped domain name
497 *
498 * @param string $domain The domain name
499 * @param integer $blog_id The blog ID
500 */
501 return apply_filters('wpo_get_mapped_domain', $domain, $blog_id);
502 }
503
504 /**
505 * Actually add the URLs to be preloaded into cache during `shutdown` hook
506 *
507 * @return void
508 */
509 public function create_tasks_for_auto_preload_urls() {
510 foreach ($this->url_preload_list as $url) {
511 if (wpo_url_in_exceptions($url)) continue;
512
513 $description = 'Preload - '.$url;
514 $options = array('url' => $url, 'preload_type' => 'manual', 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI));
515
516 WP_Optimize_Load_Url_Task::create_task($this->task_type, $description, $options, 'WP_Optimize_Load_Url_Task');
517 }
518
519 $this->run('manual', null, true);
520 }
521
522 /**
523 * Prepare the URL list that will need to be tasked to be preloaded
524 *
525 * @param string $url The URL to be preloaded
526 * @return void
527 */
528 public function add_url_to_preload_list($url) {
529 $this->url_preload_list[] = $url;
530 }
531
532 /**
533 * Captures and logs any interesting messages
534 *
535 * @param String $message - the error message
536 * @param String $error_type - the error type
537 */
538 public function log($message, $error_type = 'info') {
539
540 if (isset($this->loggers)) {
541 foreach ($this->loggers as $logger) {
542 $logger->log($message, $error_type);
543 }
544 }
545 }
546
547 /**
548 * Instance of WP_Optimize_Page_Cache_Preloader.
549 *
550 * @return WP_Optimize_Page_Cache_Preloader
551 */
552 public static function instance() {
553 if (empty(self::$_instance)) {
554 self::$_instance = new WP_Optimize_Page_Cache_Preloader();
555 }
556
557 return self::$_instance;
558 }
559
560 /**
561 * Check if the URL is already cached, or needs to be preloaded
562 *
563 * @param string $url The preloaded url
564 * @param string $preload_type The preload type (manual | scheduled)
565 * @return boolean
566 */
567 private function url_is_already_cached($url, $preload_type) {
568 static $files = array();
569 $regenerate_count = 0;
570 $folder = trailingslashit(WPO_CACHE_FILES_DIR) . wpo_get_url_path($url);
571 // If the folder does not exist, consider the URL as cleared
572 if (!is_dir($folder)) return false;
573
574 if (empty($files)) {
575 // Check only the base files
576 $files[] = 'index.html';
577
578 if (WPO_Cache_Config::instance()->get_option('enable_mobile_caching')) {
579 $files[] = 'mobile.index.html';
580 }
581 $files = apply_filters('wpo_maybe_clear_files_list', $files);
582 }
583
584 foreach ($files as $file) {
585 $file_path = trailingslashit($folder).$file;
586 if (!file_exists($file_path)) {
587 // The file does not exist, count it as "deleted"
588 $regenerate_count++;
589 continue;
590 }
591
592 if ($this->should_regenerate_file($file_path, $preload_type)) {
593 // delefe the expired cache file
594 unlink($file_path);
595 $regenerate_count++;
596 }
597 }
598
599 // if 0 == $regenerate_count, nothing all the expected files exist, and none were deleted.
600 return 0 == $regenerate_count;
601 }
602
603 /**
604 * Determine if a file should be regenerated
605 *
606 * @param string $path The file to check
607 * @param string $preload_type The preload type (manual | scheduled)
608 *
609 * @return boolean
610 */
611 private function should_regenerate_file($path, $preload_type) {
612 // Store the variables, as they'll be used for each file and each file
613 static $is_preloader_scheduled = null;
614 static $lifespan = null;
615 static $schedule_type = null;
616 static $schedule_interval = null;
617 static $lifespan_expiry_threshold = null;
618 static $always_regenerate_file_if_preload_is_manual = null;
619 static $always_regenerate_file_if_preload_is_scheduled = null;
620 static $regenerate_file_when_no_expiry_date = null;
621
622 // Sets the variables once per request:
623 if (null === $is_preloader_scheduled) {
624 $is_preloader_scheduled = WPO_Cache_Config::instance()->get_option('enable_schedule_preload');
625 $schedule_type = WPO_Cache_Config::instance()->get_option('preload_schedule_type');
626 $lifespan = WPO_Cache_Config::instance()->get_option('page_cache_length');
627 $schedule_interval = $this->get_schedule_interval($schedule_type);
628
629 /**
630 * Expiry threshold: the current file will be considered stale if within the threshold. Default: 600s (10min)
631 */
632 $lifespan_expiry_threshold = apply_filters('wpo_lifespan_expiry_threshold', 600);
633
634 /**
635 * Filters if a cache should systematically be regenerated when running a manual preload. Default: false
636 */
637 $always_regenerate_file_if_preload_is_manual = apply_filters('wpo_always_regenerate_file_if_preload_is_manual', false);
638
639 /**
640 * Filters if a cache should systematically be regenerated when running a scheduled preload. Default: false
641 */
642 $always_regenerate_file_if_preload_is_scheduled = apply_filters('wpo_always_regenerate_file_if_preload_is_scheduled', false);
643
644 /**
645 * Filters if a cache should systematically be regenerated when running a preload and no schedule is set, and cache does not expire. Default: true
646 */
647 $regenerate_file_when_no_expiry_date = apply_filters('wpo_regenerate_file_when_no_expiry_date', true);
648 }
649
650 if (($always_regenerate_file_if_preload_is_manual && 'manual' == $preload_type) || ($always_regenerate_file_if_preload_is_scheduled && 'scheduled' == $preload_type)) {
651 $result = true;
652 } else {
653
654 $modified_time = (int) filemtime($path);
655
656 // cache lifespan is set.
657 if (0 != $lifespan) {
658 $expiry_time = $modified_time + $lifespan - $lifespan_expiry_threshold;
659 $result = time() > $expiry_time;
660 } elseif ($is_preloader_scheduled) {
661 $expiry_time = $modified_time + $schedule_interval - $lifespan_expiry_threshold;
662 $result = time() > $expiry_time;
663 } else {
664 $result = $regenerate_file_when_no_expiry_date;
665 }
666
667 }
668
669 return apply_filters('wpo_preloader_should_regenerate_file', $result, $path, $preload_type);
670 }
671
672 /**
673 * Add preloader headers
674 */
675 public function preload_headers($headers) {
676 $headers['X-WP-Optimize-Cache-Preload'] = 'Yes';
677 return $headers;
678 }
679
680 /**
681 * Option disabled error message
682 *
683 * @return array
684 */
685 protected function get_option_disabled_error() {
686 return array(
687 'success' => false,
688 'error' => __('Page cache is disabled.', 'wp-optimize')
689 );
690 }
691
692 /**
693 * Get preload already running error message
694 *
695 * @return array
696 */
697 protected function get_preload_already_running_error() {
698 return array(
699 'success' => false,
700 'error' => __('Probably page cache preload is running already.', 'wp-optimize')
701 );
702 }
703
704 protected function get_preload_data() {
705 return WP_Optimize()->get_page_cache()->get_cache_size();
706 }
707
708 protected function get_preloading_message($cache_size) {
709 return array(
710 'done' => false,
711 'message' => __('Loading URLs...', 'wp-optimize'),
712 'size' => WP_Optimize()->format_size($cache_size['size']),
713 'file_count' => $cache_size['file_count']
714 );
715 }
716
717 protected function get_last_preload_message($cache_size, $last_preload_time_str) {
718 return array(
719 'done' => true,
720 'message' => sprintf(__('Last preload finished at %s', 'wp-optimize'), $last_preload_time_str),
721 'size' => WP_Optimize()->format_size($cache_size['size']),
722 'file_count' => $cache_size['file_count']
723 );
724 }
725
726 protected function get_preload_success_message($cache_size) {
727 return array(
728 'done' => true,
729 'size' => WP_Optimize()->format_size($cache_size['size']),
730 'file_count' => $cache_size['file_count']
731 );
732 }
733
734 protected function get_preload_progress_message($cache_size, $preloaded_message, $preload_resuming_in) {
735 return array(
736 'done' => false,
737 'message' => $preloaded_message,
738 'size' => WP_Optimize()->format_size($cache_size['size']),
739 'file_count' => $cache_size['file_count'],
740 'resume_in' => $preload_resuming_in
741 );
742 }
743
744 /**
745 * Shifts the given timestamp to nighttime.
746 *
747 * @param int $time
748 *
749 * @return int
750 */
751 private function make_nighttime($time) {
752
753 $gmt_offset = WP_Optimize_Utils::get_gmt_offset();
754
755 // 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;
758
759 if ($nighttime < time()) $nighttime += 24 * 3600;
760
761 return $nighttime;
762 }
763 }
764