plugin = $plugin; } /** * Register cron hooks. * * @return void */ public function register() { add_filter( 'cron_schedules', array( __CLASS__, 'custom_schedules' ) ); add_action( 'acelora_site_accelerator_cache_cleanup', array( $this, 'cache_cleanup' ) ); add_action( 'acelora_site_accelerator_scheduled_preload', array( $this, 'scheduled_preload' ) ); add_action( 'acelora_site_accelerator_database_cleanup', array( $this, 'database_cleanup' ) ); add_action( 'acelora_site_accelerator_one_click_optimization', array( $this, 'one_click' ) ); } /** * Add weekly and monthly schedules. * * @param array $schedules Schedules. * @return array */ public static function custom_schedules( $schedules ) { $schedules['weekly'] = array( 'interval' => WEEK_IN_SECONDS, 'display' => __( 'Once Weekly', 'acelora-site-accelerator' ), ); $schedules['monthly'] = array( 'interval' => 30 * DAY_IN_SECONDS, 'display' => __( 'Once Monthly', 'acelora-site-accelerator' ), ); return $schedules; } /** * Schedule events. * * @param Settings $settings Settings. * @return void */ public static function schedule_events( Settings $settings ) { if ( ! $settings->get( 'automation.enabled', 1 ) ) { return; } add_filter( 'cron_schedules', array( __CLASS__, 'custom_schedules' ) ); self::schedule_hook( 'acelora_site_accelerator_cache_cleanup', $settings->get( 'automation.cache_cleanup', 'daily' ) ); self::schedule_hook( 'acelora_site_accelerator_scheduled_preload', $settings->get( 'automation.preload', 'daily' ) ); self::schedule_hook( 'acelora_site_accelerator_database_cleanup', $settings->get( 'automation.database_optimization', 'weekly' ) ); self::schedule_hook( 'acelora_site_accelerator_one_click_optimization', $settings->get( 'automation.one_click', 'weekly' ) ); } /** * Reschedule all events after settings save. * * @param Settings $settings Settings. * @return void */ public static function reschedule_events( Settings $settings ) { wp_clear_scheduled_hook( 'acelora_site_accelerator_cache_cleanup' ); wp_clear_scheduled_hook( 'acelora_site_accelerator_scheduled_preload' ); wp_clear_scheduled_hook( 'acelora_site_accelerator_database_cleanup' ); wp_clear_scheduled_hook( 'acelora_site_accelerator_one_click_optimization' ); self::schedule_events( $settings ); } /** * Schedule a hook if needed. * * @param string $hook Hook name. * @param string $schedule Schedule name. * @return void */ private static function schedule_hook( $hook, $schedule ) { $schedule = in_array( $schedule, array( 'hourly', 'twicedaily', 'daily', 'weekly', 'monthly' ), true ) ? $schedule : 'daily'; if ( ! wp_next_scheduled( $hook ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, $schedule, $hook ); } } /** * Cleanup page cache and logs. * * @return void */ public function cache_cleanup() { $cache = $this->plugin->module( 'page_cache' ); if ( $cache && method_exists( $cache, 'cleanup_expired' ) ) { $cache->cleanup_expired(); } $this->plugin->logger()->prune(); } /** * Start scheduled preloading. * * @return void */ public function scheduled_preload() { $preloader = $this->plugin->module( 'preloader' ); if ( $preloader && method_exists( $preloader, 'start' ) ) { $preloader->start( false ); } } /** * Run scheduled database cleanup. * * @return void */ public function database_cleanup() { $database = $this->plugin->module( 'database' ); if ( $database && method_exists( $database, 'cleanup' ) ) { $database->cleanup(); } } /** * Run scheduled one-click optimization. * * @return void */ public function one_click() { $ajax = new Ajax( $this->plugin ); $ajax->run_full_optimization( false ); } }