| 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 (bool) 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 |
/** |
| 120 |
* Returns preload data |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
protected function get_preload_data() { |
| 125 |
$cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 126 |
$cache_dir = $cache_path['cachedir']; |
| 127 |
$minify_cache_data = array(); |
| 128 |
$minify_cache_data['size'] = esc_html(WP_Optimize_Minify_Cache_Functions::get_cachestats($cache_dir)); |
| 129 |
$minify_cache_data['total_size'] = esc_html(WP_Optimize_Minify_Cache_Functions::get_cachestats(WPO_CACHE_MIN_FILES_DIR)); |
| 130 |
return $minify_cache_data; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns preloading message |
| 135 |
* |
| 136 |
* @param array $data |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
protected function get_preloading_message($data) { |
| 141 |
return array( |
| 142 |
'done' => false, |
| 143 |
'message' => __('Loading URLs...', 'wp-optimize'), |
| 144 |
'size' => WP_Optimize()->format_size($data['size']), |
| 145 |
'total_size' => $data['total_size'] |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns last preload message |
| 151 |
* |
| 152 |
* @param array $data |
| 153 |
* @param string $last_preload_time_str |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
protected function get_last_preload_message($data, $last_preload_time_str) { |
| 158 |
return array( |
| 159 |
'done' => true, |
| 160 |
// translators: %s: last preload time |
| 161 |
'message' => sprintf(__('Last preload finished at %s', 'wp-optimize'), $last_preload_time_str), |
| 162 |
'size' => WP_Optimize()->format_size($data['size']), |
| 163 |
'total_size' => $data['total_size'] |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns preload success message |
| 169 |
* |
| 170 |
* @param array $data |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
protected function get_preload_success_message($data) { |
| 175 |
return array( |
| 176 |
'done' => true, |
| 177 |
'size' => WP_Optimize()->format_size($data['size']), |
| 178 |
'total_size' => $data['total_size'] |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Returns preload progress message |
| 184 |
* |
| 185 |
* @param array $data |
| 186 |
* @param string $preloaded_message |
| 187 |
* @param int $preload_resuming_in |
| 188 |
* |
| 189 |
* @return array |
| 190 |
*/ |
| 191 |
protected function get_preload_progress_message($data, $preloaded_message, $preload_resuming_in) { |
| 192 |
return array( |
| 193 |
'done' => false, |
| 194 |
'message' => $preloaded_message, |
| 195 |
'size' => WP_Optimize()->format_size($data['size']), |
| 196 |
'total_size' => $data['total_size'], |
| 197 |
'resume_in' => $preload_resuming_in |
| 198 |
); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
WP_Optimize_Minify_Preloader::instance(); |
| 203 |
|