cdn
7 months ago
data_structure
7 months ago
activation.cls.php
7 months ago
admin-display.cls.php
7 months ago
admin-settings.cls.php
7 months ago
admin.cls.php
7 months ago
api.cls.php
7 months ago
avatar.cls.php
7 months ago
base.cls.php
7 months ago
cdn.cls.php
7 months ago
cloud.cls.php
7 months ago
conf.cls.php
7 months ago
control.cls.php
7 months ago
core.cls.php
7 months ago
crawler-map.cls.php
7 months ago
crawler.cls.php
7 months ago
css.cls.php
7 months ago
data.cls.php
7 months ago
data.upgrade.func.php
7 months ago
db-optm.cls.php
7 months ago
debug2.cls.php
7 months ago
doc.cls.php
7 months ago
error.cls.php
7 months ago
esi.cls.php
7 months ago
file.cls.php
7 months ago
gui.cls.php
7 months ago
health.cls.php
7 months ago
htaccess.cls.php
7 months ago
img-optm.cls.php
7 months ago
import.cls.php
7 months ago
import.preset.cls.php
7 months ago
lang.cls.php
7 months ago
localization.cls.php
7 months ago
media.cls.php
7 months ago
metabox.cls.php
7 months ago
object-cache-wp.cls.php
7 months ago
object-cache.cls.php
7 months ago
object.lib.php
7 months ago
optimize.cls.php
7 months ago
optimizer.cls.php
7 months ago
placeholder.cls.php
7 months ago
purge.cls.php
7 months ago
report.cls.php
7 months ago
rest.cls.php
7 months ago
root.cls.php
7 months ago
router.cls.php
7 months ago
str.cls.php
7 months ago
tag.cls.php
7 months ago
task.cls.php
7 months ago
tool.cls.php
7 months ago
ucss.cls.php
7 months ago
utility.cls.php
7 months ago
vary.cls.php
7 months ago
vpi.cls.php
7 months ago
task.cls.php
239 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | |
| 4 | /** |
| 5 | * The cron task class. |
| 6 | * |
| 7 | * @since 1.1.3 |
| 8 | */ |
| 9 | |
| 10 | namespace LiteSpeed; |
| 11 | |
| 12 | defined('WPINC') || exit(); |
| 13 | |
| 14 | class Task extends Root { |
| 15 | |
| 16 | const LOG_TAG = '⏰'; |
| 17 | private static $_triggers = array( |
| 18 | Base::O_IMG_OPTM_CRON => array( |
| 19 | 'name' => 'litespeed_task_imgoptm_pull', |
| 20 | 'hook' => 'LiteSpeed\Img_Optm::start_async_cron', |
| 21 | ), // always fetch immediately |
| 22 | Base::O_OPTM_CSS_ASYNC => array( |
| 23 | 'name' => 'litespeed_task_ccss', |
| 24 | 'hook' => 'LiteSpeed\CSS::cron_ccss', |
| 25 | ), |
| 26 | Base::O_OPTM_UCSS => array( |
| 27 | 'name' => 'litespeed_task_ucss', |
| 28 | 'hook' => 'LiteSpeed\UCSS::cron', |
| 29 | ), |
| 30 | Base::O_MEDIA_VPI_CRON => array( |
| 31 | 'name' => 'litespeed_task_vpi', |
| 32 | 'hook' => 'LiteSpeed\VPI::cron', |
| 33 | ), |
| 34 | Base::O_MEDIA_PLACEHOLDER_RESP_ASYNC => array( |
| 35 | 'name' => 'litespeed_task_lqip', |
| 36 | 'hook' => 'LiteSpeed\Placeholder::cron', |
| 37 | ), |
| 38 | Base::O_DISCUSS_AVATAR_CRON => array( |
| 39 | 'name' => 'litespeed_task_avatar', |
| 40 | 'hook' => 'LiteSpeed\Avatar::cron', |
| 41 | ), |
| 42 | Base::O_IMG_OPTM_AUTO => array( |
| 43 | 'name' => 'litespeed_task_imgoptm_req', |
| 44 | 'hook' => 'LiteSpeed\Img_Optm::cron_auto_request', |
| 45 | ), |
| 46 | Base::O_CRAWLER => array( |
| 47 | 'name' => 'litespeed_task_crawler', |
| 48 | 'hook' => 'LiteSpeed\Crawler::start_async_cron', |
| 49 | ), // Set crawler to last one to use above results |
| 50 | ); |
| 51 | |
| 52 | private static $_guest_options = array( Base::O_OPTM_CSS_ASYNC, Base::O_OPTM_UCSS, Base::O_MEDIA_VPI ); |
| 53 | |
| 54 | const FILTER_CRAWLER = 'litespeed_crawl_filter'; |
| 55 | const FILTER = 'litespeed_filter'; |
| 56 | |
| 57 | /** |
| 58 | * Keep all tasks in cron |
| 59 | * |
| 60 | * @since 3.0 |
| 61 | * @access public |
| 62 | */ |
| 63 | public function init() { |
| 64 | self::debug2('Init'); |
| 65 | add_filter('cron_schedules', array( $this, 'lscache_cron_filter' )); |
| 66 | |
| 67 | $guest_optm = $this->conf(Base::O_GUEST) && $this->conf(Base::O_GUEST_OPTM); |
| 68 | |
| 69 | foreach (self::$_triggers as $id => $trigger) { |
| 70 | if ($id == Base::O_IMG_OPTM_CRON) { |
| 71 | if (!Img_Optm::need_pull()) { |
| 72 | continue; |
| 73 | } |
| 74 | } elseif (!$this->conf($id)) { |
| 75 | if (!$guest_optm || !in_array($id, self::$_guest_options)) { |
| 76 | continue; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Special check for crawler |
| 81 | if ($id == Base::O_CRAWLER) { |
| 82 | if (!Router::can_crawl()) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | add_filter('cron_schedules', array( $this, 'lscache_cron_filter_crawler' )); |
| 87 | } |
| 88 | |
| 89 | if (!wp_next_scheduled($trigger['name'])) { |
| 90 | self::debug('Cron hook register [name] ' . $trigger['name']); |
| 91 | |
| 92 | wp_schedule_event(time(), $id == Base::O_CRAWLER ? self::FILTER_CRAWLER : self::FILTER, $trigger['name']); |
| 93 | } |
| 94 | |
| 95 | add_action($trigger['name'], $trigger['hook']); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Handle all async noabort requests |
| 101 | * |
| 102 | * @since 5.5 |
| 103 | */ |
| 104 | public static function async_litespeed_handler() { |
| 105 | $hash_data = self::get_option('async_call-hash', array()); |
| 106 | if (!$hash_data || !is_array($hash_data) || empty($hash_data['hash']) || empty($hash_data['ts'])) { |
| 107 | self::debug('async_litespeed_handler no hash data', $hash_data); |
| 108 | return; |
| 109 | } |
| 110 | if (time() - $hash_data['ts'] > 120 || empty($_GET['nonce']) || $_GET['nonce'] != $hash_data['hash']) { |
| 111 | self::debug('async_litespeed_handler nonce mismatch'); |
| 112 | return; |
| 113 | } |
| 114 | self::delete_option('async_call-hash'); |
| 115 | |
| 116 | $type = Router::verify_type(); |
| 117 | self::debug('type=' . $type); |
| 118 | |
| 119 | // Don't lock up other requests while processing |
| 120 | session_write_close(); |
| 121 | switch ($type) { |
| 122 | case 'crawler': |
| 123 | Crawler::async_handler(); |
| 124 | break; |
| 125 | case 'crawler_force': |
| 126 | Crawler::async_handler(true); |
| 127 | break; |
| 128 | case 'imgoptm': |
| 129 | Img_Optm::async_handler(); |
| 130 | break; |
| 131 | case 'imgoptm_force': |
| 132 | Img_Optm::async_handler(true); |
| 133 | break; |
| 134 | default: |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Async caller wrapper func |
| 140 | * |
| 141 | * @since 5.5 |
| 142 | */ |
| 143 | public static function async_call( $type ) { |
| 144 | $hash = Str::rrand(32); |
| 145 | self::update_option('async_call-hash', array( |
| 146 | 'hash' => $hash, |
| 147 | 'ts' => time(), |
| 148 | )); |
| 149 | $args = array( |
| 150 | 'timeout' => 0.01, |
| 151 | 'blocking' => false, |
| 152 | 'sslverify' => false, |
| 153 | // 'cookies' => $_COOKIE, |
| 154 | ); |
| 155 | $qs = array( |
| 156 | 'action' => 'async_litespeed', |
| 157 | 'nonce' => $hash, |
| 158 | Router::TYPE => $type, |
| 159 | ); |
| 160 | $url = add_query_arg($qs, admin_url('admin-ajax.php')); |
| 161 | self::debug('async call to ' . $url); |
| 162 | wp_safe_remote_post(esc_url_raw($url), $args); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Clean all potential existing crons |
| 167 | * |
| 168 | * @since 3.0 |
| 169 | * @access public |
| 170 | */ |
| 171 | public static function destroy() { |
| 172 | Utility::compatibility(); |
| 173 | array_map('wp_clear_scheduled_hook', array_column(self::$_triggers, 'name')); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Try to clean the crons if disabled |
| 178 | * |
| 179 | * @since 3.0 |
| 180 | * @access public |
| 181 | */ |
| 182 | public function try_clean( $id ) { |
| 183 | // Clean v2's leftover cron ( will remove in v3.1 ) |
| 184 | // foreach ( wp_get_ready_cron_jobs() as $hooks ) { |
| 185 | // foreach ( $hooks as $hook => $v ) { |
| 186 | // if ( strpos( $hook, 'litespeed_' ) === 0 && ( substr( $hook, -8 ) === '_trigger' || strpos( $hook, 'litespeed_task_' ) !== 0 ) ) { |
| 187 | // self::debug( 'Cron clear legacy [hook] ' . $hook ); |
| 188 | // wp_clear_scheduled_hook( $hook ); |
| 189 | // } |
| 190 | // } |
| 191 | // } |
| 192 | |
| 193 | if ($id && !empty(self::$_triggers[$id])) { |
| 194 | if (!$this->conf($id) || ($id == Base::O_CRAWLER && !Router::can_crawl())) { |
| 195 | self::debug('Cron clear [id] ' . $id . ' [hook] ' . self::$_triggers[$id]['name']); |
| 196 | wp_clear_scheduled_hook(self::$_triggers[$id]['name']); |
| 197 | } |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | self::debug('❌ Unknown cron [id] ' . $id); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Register cron interval imgoptm |
| 206 | * |
| 207 | * @since 1.6.1 |
| 208 | * @access public |
| 209 | */ |
| 210 | public function lscache_cron_filter( $schedules ) { |
| 211 | if (!array_key_exists(self::FILTER, $schedules)) { |
| 212 | $schedules[self::FILTER] = array( |
| 213 | 'interval' => 60, |
| 214 | 'display' => __('Every Minute', 'litespeed-cache'), |
| 215 | ); |
| 216 | } |
| 217 | return $schedules; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Register cron interval |
| 222 | * |
| 223 | * @since 1.1.0 |
| 224 | * @access public |
| 225 | */ |
| 226 | public function lscache_cron_filter_crawler( $schedules ) { |
| 227 | $CRAWLER_RUN_INTERVAL = defined('LITESPEED_CRAWLER_RUN_INTERVAL') ? constant('LITESPEED_CRAWLER_RUN_INTERVAL') : 600; |
| 228 | // $wp_schedules = wp_get_schedules(); |
| 229 | if (!array_key_exists(self::FILTER_CRAWLER, $schedules)) { |
| 230 | // self::debug('Crawler cron log: cron filter '.$interval.' added'); |
| 231 | $schedules[self::FILTER_CRAWLER] = array( |
| 232 | 'interval' => $CRAWLER_RUN_INTERVAL, |
| 233 | 'display' => __('LiteSpeed Crawler Cron', 'litespeed-cache'), |
| 234 | ); |
| 235 | } |
| 236 | return $schedules; |
| 237 | } |
| 238 | } |
| 239 |