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

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

166 lines 4.6 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_Minify_Load_Url_Task')) {
6 require_once(WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-load-url-task.php');
7 }
8
9 class WP_Optimize_Minify_Preloader extends WP_Optimize_Preloader {
10
11 protected $preload_type = 'minify';
12
13 protected $task_type = 'minify-load-url-task';
14
15 static protected $_instance = null;
16
17 /**
18 * WP_Optimize_Page_Cache_Preloader constructor.
19 */
20 public function __construct() {
21 parent::__construct();
22
23 add_filter('cron_schedules', array($this, 'cron_add_intervals'));
24 }
25
26 /**
27 * Check if minify is active.
28 *
29 * @return bool
30 */
31 public function is_option_active() {
32 if (!function_exists('wp_optimize_minify_config')) {
33 include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php';
34 }
35 return wp_optimize_minify_config()->get('enabled');
36 }
37
38 /**
39 * Add intervals to cron schedules.
40 *
41 * @param array $schedules
42 *
43 * @return array
44 */
45 public function cron_add_intervals($schedules) {
46 $interval = $this->get_continue_preload_cron_interval();
47 $schedules['wpo_minify_preload_continue_interval'] = array(
48 'interval' => $interval,
49 'display' => round($interval / 60, 1).' minutes'
50 );
51
52 return $schedules;
53 }
54
55 /**
56 * Create tasks (WP_Optimize_Load_Url_Task) for preload all urls from site.
57 *
58 * @param string $type The preload type (currently: scheduled, manual)
59 * @return void
60 */
61 public function create_tasks_for_preload_site_urls($type = 'manual') {
62 $urls = $this->get_site_urls();
63
64 if (!empty($urls)) {
65
66 $this->log(__('Minify: Creating tasks for preload site urls.', 'wp-optimize'));
67
68 foreach ($urls as $url) {
69 if (wpo_url_in_exceptions($url)) continue;
70
71 // this description is being used for internal purposes.
72 $description = 'Preload - '.$url;
73 $options = array('url' => $url, 'preload_type' => $type, 'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI));
74
75 WP_Optimize_Minify_Load_Url_Task::create_task($this->task_type, $description, $options, 'WP_Optimize_Minify_Load_Url_Task');
76 }
77
78 $this->log(__('Minify: Tasks for preload site urls created.', 'wp-optimize'));
79 }
80 }
81
82 /**
83 * Instance of WP_Optimize_Minify_Preloader.
84 *
85 * @return WP_Optimize_Minify_Preloader
86 */
87 public static function instance() {
88 if (empty(self::$_instance)) {
89 self::$_instance = new WP_Optimize_Minify_Preloader();
90 }
91
92 return self::$_instance;
93 }
94
95 /**
96 * Option disabled error message
97 *
98 * @return array
99 */
100 protected function get_option_disabled_error() {
101 return array(
102 'success' => false,
103 'error' => __('Minify is disabled.', 'wp-optimize')
104 );
105 }
106
107 /**
108 * Get preload already running error message
109 *
110 * @return array
111 */
112 protected function get_preload_already_running_error() {
113 return array(
114 'success' => false,
115 'error' => __('Probably minify preload is running already.', 'wp-optimize')
116 );
117 }
118
119 protected function get_preload_data() {
120 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
121 $cache_dir = $cache_path['cachedir'];
122 $minify_cache_data = array();
123 $minify_cache_data['size'] = esc_html(WP_Optimize_Minify_Cache_Functions::get_cachestats($cache_dir));
124 $minify_cache_data['total_size'] = esc_html(WP_Optimize_Minify_Cache_Functions::get_cachestats(WPO_CACHE_MIN_FILES_DIR));
125 return $minify_cache_data;
126 }
127
128 protected function get_preloading_message($minify_cache_data) {
129 return array(
130 'done' => false,
131 'message' => __('Loading URLs...', 'wp-optimize'),
132 'size' => WP_Optimize()->format_size($minify_cache_data['size']),
133 'total_size' => $minify_cache_data['total_size']
134 );
135 }
136
137 protected function get_last_preload_message($minify_cache_data, $last_preload_time_str) {
138 return array(
139 'done' => true,
140 'message' => sprintf(__('Last preload finished at %s', 'wp-optimize'), $last_preload_time_str),
141 'size' => WP_Optimize()->format_size($minify_cache_data['size']),
142 'total_size' => $minify_cache_data['total_size']
143 );
144 }
145
146 protected function get_preload_success_message($minify_cache_data) {
147 return array(
148 'done' => true,
149 'size' => WP_Optimize()->format_size($minify_cache_data['size']),
150 'total_size' => $minify_cache_data['total_size']
151 );
152 }
153
154 protected function get_preload_progress_message($minify_cache_data, $preloaded_message, $preload_resuming_in) {
155 return array(
156 'done' => false,
157 'message' => $preloaded_message,
158 'size' => WP_Optimize()->format_size($minify_cache_data['size']),
159 'total_size' => $minify_cache_data['total_size'],
160 'resume_in' => $preload_resuming_in
161 );
162 }
163 }
164
165 WP_Optimize_Minify_Preloader::instance();
166