activation.class.php
8 years ago
api.class.php
8 years ago
cdn.class.php
8 years ago
config.class.php
8 years ago
control.class.php
8 years ago
crawler-sitemap.class.php
8 years ago
crawler.class.php
8 years ago
data.class.php
8 years ago
esi.class.php
8 years ago
gui.class.php
8 years ago
import.class.php
8 years ago
litespeed-cache.class.php
8 years ago
litespeed.autoload.php
8 years ago
log.class.php
8 years ago
media.class.php
8 years ago
object.class.php
8 years ago
object.lib.php
8 years ago
optimize.class.php
8 years ago
optimizer.class.php
8 years ago
purge.class.php
8 years ago
router.class.php
8 years ago
tag.class.php
8 years ago
task.class.php
8 years ago
utility.class.php
8 years ago
vary.class.php
8 years ago
task.class.php
211 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The cron task class. |
| 5 | * |
| 6 | * @since 1.1.3 |
| 7 | * @since 1.5 Moved into /inc |
| 8 | * @package LiteSpeed_Cache |
| 9 | * @subpackage LiteSpeed_Cache/inc |
| 10 | * @author LiteSpeed Technologies <info@litespeedtech.com> |
| 11 | */ |
| 12 | class LiteSpeed_Cache_Task |
| 13 | { |
| 14 | private static $_instance ; |
| 15 | |
| 16 | const CRON_ACTION_HOOK_CRAWLER = 'litespeed_crawl_trigger' ; |
| 17 | const CRON_ACTION_HOOK_IMGOPTM = 'litespeed_imgoptm_trigger' ; |
| 18 | const CRON_FITLER_CRAWLER = 'litespeed_crawl_filter' ; |
| 19 | const CRON_FITLER_IMGOPTM = 'litespeed_imgoptm_filter' ; |
| 20 | |
| 21 | /** |
| 22 | * Init |
| 23 | * |
| 24 | * @since 1.6 |
| 25 | * @access private |
| 26 | */ |
| 27 | private function __construct() |
| 28 | { |
| 29 | LiteSpeed_Cache_Log::debug2( 'Task init' ) ; |
| 30 | |
| 31 | // Register crawler cron |
| 32 | if ( LiteSpeed_Cache::config( LiteSpeed_Cache_Config::CRWL_CRON_ACTIVE ) && LiteSpeed_Cache_Router::can_crawl() ) { |
| 33 | // keep cron intval filter |
| 34 | self::schedule_filter_crawler() ; |
| 35 | |
| 36 | // cron hook |
| 37 | add_action( self::CRON_ACTION_HOOK_CRAWLER, 'LiteSpeed_Cache_Crawler::crawl_data' ) ; |
| 38 | } |
| 39 | |
| 40 | // Register img optimization fetch ( always fetch immediately ) |
| 41 | if ( ! LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_MEDIA_IMG_OPTM_CRON_OFF ) && LiteSpeed_Cache_Media::check_need_pull() ) { |
| 42 | self::schedule_filter_imgoptm() ; |
| 43 | |
| 44 | add_action( self::CRON_ACTION_HOOK_IMGOPTM, 'LiteSpeed_Cache_Media::pull_optimized_img' ) ; |
| 45 | } |
| 46 | else { |
| 47 | // wp_clear_scheduled_hook( self::CRON_ACTION_HOOK_IMGOPTM ) ; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Enable/Disable cron task |
| 53 | * |
| 54 | * @since 1.1.0 |
| 55 | * @access public |
| 56 | */ |
| 57 | public static function enable() |
| 58 | { |
| 59 | $id = LiteSpeed_Cache_Config::CRWL_CRON_ACTIVE ; |
| 60 | |
| 61 | // get new setting |
| 62 | $is_enabled = ! LiteSpeed_Cache::config( $id ) ; |
| 63 | |
| 64 | // log |
| 65 | LiteSpeed_Cache_Log::debug( 'Crawler log: Crawler is ' . ( $is_enabled ? 'enabled' : 'disabled' ) ) ; |
| 66 | |
| 67 | // update config |
| 68 | LiteSpeed_Cache_Config::get_instance()->update_options( array( $id => $is_enabled ) ) ; |
| 69 | |
| 70 | self::update() ; |
| 71 | |
| 72 | echo json_encode( array( 'enable' => $is_enabled ) ) ; |
| 73 | wp_die() ; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Update cron status |
| 78 | * |
| 79 | * @since 1.1.0 |
| 80 | * @access public |
| 81 | * @param array $options The options to check if cron should be enabled |
| 82 | */ |
| 83 | public static function update( $options = false ) |
| 84 | { |
| 85 | $id = LiteSpeed_Cache_Config::CRWL_CRON_ACTIVE ; |
| 86 | if ( $options && isset( $options[ $id ] ) ) { |
| 87 | $is_active = $options[$id] ; |
| 88 | } |
| 89 | else { |
| 90 | $is_active = LiteSpeed_Cache::config( $id ) ; |
| 91 | } |
| 92 | |
| 93 | // If cron setting is on, check cache status |
| 94 | if ( $is_active ) { |
| 95 | if ( defined( 'LITESPEED_NEW_OFF' ) ) { |
| 96 | $is_active = false ; |
| 97 | } |
| 98 | elseif ( ! defined( 'LITESPEED_ON' ) && ! defined( 'LITESPEED_NEW_ON' ) ) { |
| 99 | $is_active = false ; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if ( ! $is_active ) { |
| 104 | self::clear() ; |
| 105 | } |
| 106 | |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Schedule cron img optimization |
| 111 | * |
| 112 | * @since 1.6.1 |
| 113 | * @access public |
| 114 | */ |
| 115 | public static function schedule_filter_imgoptm() |
| 116 | { |
| 117 | add_filter( 'cron_schedules', 'LiteSpeed_Cache_Task::lscache_cron_filter_imgoptm' ) ; |
| 118 | |
| 119 | // Schedule event here to see if it can lost again or not |
| 120 | if( ! wp_next_scheduled( self::CRON_ACTION_HOOK_IMGOPTM ) ) { |
| 121 | LiteSpeed_Cache_Log::debug( 'Cron log: ......img optimization cron hook register......' ) ; |
| 122 | wp_schedule_event( time(), self::CRON_FITLER_IMGOPTM, self::CRON_ACTION_HOOK_IMGOPTM ) ; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Schedule cron crawler |
| 128 | * |
| 129 | * @since 1.1.0 |
| 130 | * @access public |
| 131 | */ |
| 132 | public static function schedule_filter_crawler() |
| 133 | { |
| 134 | add_filter( 'cron_schedules', 'LiteSpeed_Cache_Task::lscache_cron_filter_crawler' ) ; |
| 135 | |
| 136 | // Schedule event here to see if it can lost again or not |
| 137 | if( ! wp_next_scheduled( self::CRON_ACTION_HOOK_CRAWLER ) ) { |
| 138 | LiteSpeed_Cache_Log::debug( 'Crawler cron log: ......cron hook register......' ) ; |
| 139 | wp_schedule_event( time(), self::CRON_FITLER_CRAWLER, self::CRON_ACTION_HOOK_CRAWLER ) ; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Register cron interval imgoptm |
| 145 | * |
| 146 | * @since 1.6.1 |
| 147 | * @access public |
| 148 | * @param array $schedules WP Hook |
| 149 | */ |
| 150 | public static function lscache_cron_filter_imgoptm( $schedules ) |
| 151 | { |
| 152 | if ( ! array_key_exists( self::CRON_FITLER_IMGOPTM, $schedules ) ) { |
| 153 | $schedules[ self::CRON_FITLER_IMGOPTM ] = array( |
| 154 | 'interval' => 60, |
| 155 | 'display' => __( 'LiteSpeed Cache Custom Cron ImgOptm', 'litespeed-cache' ), |
| 156 | ) ; |
| 157 | } |
| 158 | return $schedules ; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Register cron interval |
| 163 | * |
| 164 | * @since 1.1.0 |
| 165 | * @access public |
| 166 | * @param array $schedules WP Hook |
| 167 | */ |
| 168 | public static function lscache_cron_filter_crawler( $schedules ) |
| 169 | { |
| 170 | $interval = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::CRWL_RUN_INTERVAL ) ; |
| 171 | // $wp_schedules = wp_get_schedules() ; |
| 172 | if ( ! array_key_exists( self::CRON_FITLER_CRAWLER, $schedules ) ) { |
| 173 | // LiteSpeed_Cache_Log::debug('Crawler cron log: ......cron filter '.$interval.' added......') ; |
| 174 | $schedules[ self::CRON_FITLER_CRAWLER ] = array( |
| 175 | 'interval' => $interval, |
| 176 | 'display' => __( 'LiteSpeed Cache Custom Cron Crawler', 'litespeed-cache' ), |
| 177 | ) ; |
| 178 | } |
| 179 | return $schedules ; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Clear cron |
| 184 | * |
| 185 | * @since 1.1.0 |
| 186 | * @access public |
| 187 | */ |
| 188 | public static function clear() |
| 189 | { |
| 190 | LiteSpeed_Cache_Log::debug( 'Crawler cron log: ......cron hook cleared......' ) ; |
| 191 | wp_clear_scheduled_hook( self::CRON_ACTION_HOOK_CRAWLER ) ; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Get the current instance object. |
| 197 | * |
| 198 | * @since 1.6 |
| 199 | * @access public |
| 200 | * @return Current class instance. |
| 201 | */ |
| 202 | public static function get_instance() |
| 203 | { |
| 204 | if ( ! isset( self::$_instance ) ) { |
| 205 | self::$_instance = new self() ; |
| 206 | } |
| 207 | |
| 208 | return self::$_instance ; |
| 209 | } |
| 210 | |
| 211 | } |