*/ private function initialize_and_start_processing() { // Check database. $this->table->maybe_trigger_recreate_table(); $this->table->maybe_upgrade(); // Check folders. $this->file_manager->create_dirs(); // Flush rewrite rules to make sure our REST API endpoint is available. flush_rewrite_rules(); // Run scanner. $this->run_rescan_process(); } /** * Plugin deactivation event. * * @return void */ public function deactivation() { $this->disable( true ); } /** * Disable image optimization. * * @param bool $is_deactivation Is plugin deactivation event. * @return void */ public function disable( $is_deactivation = false ) { // Update option. $this->options->disable( $is_deactivation ); // Stop process. $this->manager->interrupt( true ); // Clear cron jobs. wp_clear_scheduled_hook( 'rocket_image_optimization_run_rescan_process' ); wp_clear_scheduled_hook( 'rocket_image_optimization_run_queue_worker_process' ); } /** * Rescan process. * * @return void */ public function run_rescan_process() { // Start scanner. $this->manager->run_rescan_process(); } /** * Run queue worker process if not already running. * * @return void */ public function run_queue_worker_process() { // Start queue worker. $this->manager->run_queue_worker_process(); } /** * Add the image minification options to the WP Rocket options array. * * @param array $options WP Rocket options array. * * @return array * @since 3.12.6.1_1.1-1 */ public function add_options_first_time( $options ): array { $options = (array) $options; $options['awp_image_optimization'] = 0; $options['awp_image_optimization_unique_id'] = ''; return $options; } /** * Display notices. * * @return void */ public function display_notices() { $this->notices_handler->display_notices(); } /** * Restart the image minification. * * @return void * * @since 3.12.6.1_1.1-1 */ public function restart_image_minification() { $this->manager->interrupt(); if ( $this->options->is_image_optimization_enabled() ) { $this->manager->run_rescan_process(); $this->manager->run_queue_worker_process(); } } /** * Registers routes in the API. * * @return void * @since 3.12.6.1_1.1-1 */ public function register_routes() { $this->rest_api->register_process_notification_route(); } /** * Sanitizes options * * @param array $input Array of values submitted from the form. * @param AdminSettings $settings Settings class instance. * * @return array */ public function sanitize_options( $input, $settings ): array { // CL: In standalone mode, allow user to toggle the checkbox via UI. // In CLOS mode, preserve the current state (managed by CLI). if ( clsop_is_standalone() ) { // Standalone: respect user input. Preserve existing unique_id if any. if ( ! empty( $input['awp_image_optimization'] ) ) { $input['awp_image_optimization'] = 1; $input['awp_image_optimization_unique_id'] = $this->options->get_unique_id(); $input['cache_webp'] = 1; } } elseif ( $this->options->is_image_optimization_enabled() ) { // CLOS: force the current state to prevent UI changes. $input['awp_image_optimization_unique_id'] = $this->options->get_unique_id(); $input['awp_image_optimization'] = 1; $input['cache_webp'] = 1; } return $input; } /** * Handle options changed event - start rescan when image optimization is enabled via UI. * * @param array $value New options values. * @return void */ public function on_options_changed( $value ) { // Only for standalone mode. if ( ! function_exists( 'clsop_is_standalone' ) || ! clsop_is_standalone() ) { return; } // Check if image optimization was just enabled. if ( empty( $value['awp_image_optimization'] ) ) { return; } // Options are already saved, just initialize and start processing. $this->initialize_and_start_processing(); } /** * Complete event, clear all cache for new images. * * @return void */ public function complete() { try { // Default, GoDaddy, Siteground. rocket_clean_domain(); // AWP/Rocket Cdn. if ( 1 === $this->options->get_cdn() ) { do_action( 'rocketcdn_accelerate_wp_purge_cache' ); } // CloudFlare. do_action( 'rocket_purge_cloudflare' ); // WP Engine. if ( class_exists( 'WpeCommon' ) && function_exists( 'wpe_param' ) ) { wpe_param( 'purge-all' ); } } catch ( \Throwable $e ) { // phpcs:disable Generic.CodeAnalysis.EmptyStatement.DetectedCatch // Nothing. } } /** * Schedules cron jobs to run the file scanner and the queue worker. * * @return void */ public function schedule_cron_jobs() { $image_optimization_enabled = $this->options->is_image_optimization_enabled(); $events = [ 'rocket_image_optimization_run_rescan_process', 'rocket_image_optimization_run_queue_worker_process', ]; foreach ( $events as $event ) { $event_scheduled = wp_next_scheduled( $event ); if ( ! $image_optimization_enabled && $event_scheduled ) { wp_clear_scheduled_hook( $event ); continue; } if ( ! $image_optimization_enabled ) { continue; } if ( $event_scheduled ) { continue; } wp_schedule_event( time(), 'daily', $event ); } } /** * Adds the new attachment ID to local list that will be processed at the end of the current request. We can't add * the images to the queue here because the image subsizes are not yet generated. There is no suitable hook for us * to use when subsizes are generated. * * @param int $post_id Post ID. * * @since 3.12.6.1_1.1-1 */ public function process_new_attachment( $post_id ) { if ( ! $this->options->is_image_optimization_enabled() ) { return; } $mime_type = get_post_mime_type( $post_id ); if ( preg_match( '!^image/(gif|jpg|jpeg|png)$!', $mime_type ) ) { $this->new_attachments[] = $post_id; } } /** * Adds the new attachments to the queue. * * @since 3.12.6.1_1.1-1 */ public function submit_new_attachments_to_queue() { if ( ! empty( $this->new_attachments ) ) { $this->manager->add_attachments_to_queue( $this->new_attachments, 10 ); } } /** * Adds a file to the queue table. * * @param string $url file Url. * @param int $priority Job priority. * * @since 3.12.6.1_1.1-1 */ public function add_file_to_queue_table( $url, $priority ) { $this->manager->add_file_to_queue_table( $url, $priority ); } /** * Causes the image optimization process to be postponed by setting a dedicated transient. * * The queue worker process will check this transient next time it runs and act accordingly. * * @param array $args Array of arguments to be stored in the transient. * * @return void */ public function postpone( $args ) { $value = get_transient( 'rocket_image_optimization_process_postponed' ); if ( is_array( $value ) ) { $args = array_merge( $value, $args ); } else { $args['created_at'] = time(); $args['retries'] = 0; } set_transient( 'rocket_image_optimization_process_postponed', $args ); } /** * Causes the image optimization process to stop by setting a dedicated transient. * * The queue worker process will check this transient next time it runs and act accordingly. * * @param array $args Array of arguments to be stored in the transient. * * @return void */ public function stop( $args ) { $args['created_at'] = time(); set_transient( 'rocket_image_optimization_process_stopped_by_error', $args ); } /** * Add the cron schedule. * * @param array $schedules Array of current schedules. * * @return array */ public function add_wp_cron_schedule( $schedules ) { if ( isset( $schedules['every_minute'] ) ) { return $schedules; } $schedules['every_minute'] = [ 'interval' => 60, // in seconds. 'display' => __( 'Every minute', 'rocket' ), ]; return $schedules; } }