models
4 months ago
processing
4 months ago
processors
4 months ago
class-rio-attachment.php
4 months ago
class-rio-backup.php
5 months ago
class-rio-bulk-optimization.php
5 months ago
class-rio-cron.php
6 months ago
class-rio-image-query.php
4 months ago
class-rio-image-statistic.php
4 months ago
class-rio-media-library.php
4 months ago
class-rio-multisite.php
6 months ago
class-rio-optimization-orchestrator.php
4 months ago
class-rio-optimization-tools.php
6 months ago
class-rio-views.php
4 months ago
class-wrio-license.php
6 months ago
class-wrio-premium-provider.php
6 months ago
class-wrio-support.php
6 months ago
index.php
6 months ago
class-rio-cron.php
270 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Класс для работы оптимизации по расписанию |
| 10 | * |
| 11 | * @version 1.0 |
| 12 | */ |
| 13 | class WRIO_Cron { |
| 14 | |
| 15 | /** |
| 16 | * Инициализация оптимизации по расписанию |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | $this->initHooks(); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Подключение � |
| 24 | уков |
| 25 | */ |
| 26 | public function initHooks() { |
| 27 | add_action( 'wrio/cron/optimization_process', [ $this, 'optimization_process' ], 10, 1 ); |
| 28 | add_action( 'wrio/cron/conversion_process', [ $this, 'conversion_process' ], 10, 1 ); |
| 29 | add_action( 'wrio/cron/avif_conversion_process', [ $this, 'avif_conversion_process' ], 10, 1 ); |
| 30 | add_filter( 'cron_schedules', [ $this, 'intervals' ], 100, 1 ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Кастомные интервалы выполнения cron задачи |
| 35 | * |
| 36 | * @param array $intervals Зарегистрированные интервалы |
| 37 | * |
| 38 | * @return array $intervals Новые интервалы |
| 39 | */ |
| 40 | public function intervals( $intervals ) { |
| 41 | $intervals['wio_1_min'] = [ |
| 42 | 'interval' => 60, |
| 43 | 'display' => __( '1 minute', 'robin-image-optimizer' ), |
| 44 | ]; |
| 45 | $intervals['wio_2_min'] = [ |
| 46 | 'interval' => 60 * 2, |
| 47 | // translators: %s is the number of minutes. |
| 48 | 'display' => sprintf( __( '%s minutes', 'robin-image-optimizer' ), '2' ), |
| 49 | ]; |
| 50 | $intervals['wio_5_min'] = [ |
| 51 | 'interval' => 60 * 5, |
| 52 | // translators: %s is the number of minutes. |
| 53 | 'display' => sprintf( __( '%s minutes', 'robin-image-optimizer' ), '5' ), |
| 54 | ]; |
| 55 | $intervals['wio_10_min'] = [ |
| 56 | 'interval' => 60 * 10, |
| 57 | // translators: %s is the number of minutes. |
| 58 | 'display' => sprintf( __( '%s minutes', 'robin-image-optimizer' ), '10' ), |
| 59 | ]; |
| 60 | $intervals['wio_30_min'] = [ |
| 61 | 'interval' => 60 * 30, |
| 62 | // translators: %s is the number of minutes. |
| 63 | 'display' => sprintf( __( '%s minutes', 'robin-image-optimizer' ), '30' ), |
| 64 | ]; |
| 65 | $intervals['wio_hourly'] = [ |
| 66 | 'interval' => 60 * 60, |
| 67 | // translators: %s is the number of minutes. |
| 68 | 'display' => sprintf( __( '%s minutes', 'robin-image-optimizer' ), '60' ), |
| 69 | ]; |
| 70 | $intervals['wio_daily'] = [ |
| 71 | 'interval' => 60 * 60 * 24, |
| 72 | 'display' => __( 'daily', 'robin-image-optimizer' ), |
| 73 | ]; |
| 74 | |
| 75 | return $intervals; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Запуск Cron задачи |
| 80 | */ |
| 81 | public static function start_single( $attachment_id ) { |
| 82 | wp_schedule_single_event( time() + 10, 'wrio/cron/optimization_process', [ $attachment_id ] ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Запуск Cron задачи |
| 87 | */ |
| 88 | public static function start( $type = 'optimization' ) { |
| 89 | $interval = WRIO_Plugin::app()->getPopulateOption( 'image_autooptimize_shedule_time', 'wio_5_min' ); |
| 90 | if ( ! wp_next_scheduled( "wrio/cron/{$type}_process" ) ) { |
| 91 | wp_schedule_event( time(), $interval, "wrio/cron/{$type}_process" ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Остановка Cron задачи |
| 97 | */ |
| 98 | public static function stop( $type = 'optimization' ) { |
| 99 | if ( wp_next_scheduled( "wrio/cron/{$type}_process" ) ) { |
| 100 | wp_clear_scheduled_hook( "wrio/cron/{$type}_process" ); |
| 101 | WRIO_Plugin::app()->updatePopulateOption( "{$type}_cron_running", false ); // останавливаем крон |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Метод оптимизирует изображения при выполнении cron задачи |
| 107 | */ |
| 108 | public function optimization_process( $attachment_id = 0 ) { |
| 109 | // Optimize single image via cron |
| 110 | if ( $attachment_id ) { |
| 111 | WRIO_Plugin::app()->logger->info( sprintf( 'START auto optimize cron job. Attachment: %s', $attachment_id ) ); |
| 112 | $media_library = WRIO_Media_Library::get_instance(); |
| 113 | $media_library->optimizeAttachment( $attachment_id ); |
| 114 | |
| 115 | // After optimization, also convert to WebP/AVIF if format conversion is enabled |
| 116 | if ( class_exists( 'WRIO_Format_Converter_Factory' ) && WRIO_Format_Converter_Factory::is_format_conversion_enabled() ) { |
| 117 | $formats = WRIO_Format_Converter_Factory::get_enabled_formats(); |
| 118 | foreach ( $formats as $format ) { |
| 119 | $media_library->webpConvertAttachment( $attachment_id, $format ); |
| 120 | WRIO_Plugin::app()->logger->info( sprintf( 'Auto converted attachment %s to %s', $attachment_id, $format ) ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | WRIO_Plugin::app()->logger->info( sprintf( 'END auto optimize cron job. Attachment: %s', $attachment_id ) ); |
| 125 | |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | $max_process_per_request = WRIO_Plugin::app()->getPopulateOption( 'image_autooptimize_items_number_per_interation', 3 ); |
| 130 | $cron_running_page = WRIO_Plugin::app()->getPopulateOption( 'cron_running', false ); |
| 131 | |
| 132 | if ( ! $cron_running_page ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | WRIO_Plugin::app()->logger->info( sprintf( 'Start cron job. Scope: %s', $cron_running_page ) ); |
| 137 | |
| 138 | if ( 'media-library' == $cron_running_page ) { |
| 139 | $media_library = WRIO_Media_Library::get_instance(); |
| 140 | $result = $media_library->processUnoptimizedImages( $max_process_per_request ); |
| 141 | } elseif ( 'nextgen' == $cron_running_page ) { |
| 142 | $nextgen_gallery = WRIO_Nextgen_Gallery::get_instance(); |
| 143 | $result = $nextgen_gallery->processUnoptimizedImages( $max_process_per_request ); |
| 144 | } elseif ( 'custom-folders' == $cron_running_page ) { |
| 145 | $cf = WRIO_Custom_Folders::get_instance(); |
| 146 | $result = $cf->processUnoptimizedImages( $max_process_per_request ); |
| 147 | } |
| 148 | |
| 149 | if ( is_wp_error( $result ) ) { |
| 150 | WRIO_Plugin::app()->logger->info( sprintf( 'Cron job failed. Error: %s', $result->get_error_message() ) ); |
| 151 | WRIO_Plugin::app()->deletePopulateOption( 'cron_running' ); |
| 152 | |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if ( $result['remain'] <= 0 ) { |
| 157 | WRIO_Plugin::app()->deletePopulateOption( 'cron_running' ); |
| 158 | } |
| 159 | |
| 160 | WRIO_Plugin::app()->logger->info( sprintf( 'End cron job. Scope: %s', $cron_running_page ) ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Метод оптимизирует изображения при выполнении cron задачи |
| 165 | */ |
| 166 | public function conversion_process( $attachment_id = 0 ) { |
| 167 | // Optimize single image via cron |
| 168 | if ( $attachment_id ) { |
| 169 | WRIO_Plugin::app()->logger->info( sprintf( 'START auto optimize cron job. Attachment: %s', $attachment_id ) ); |
| 170 | $media_library = WRIO_Media_Library::get_instance(); |
| 171 | $media_library->optimizeAttachment( $attachment_id ); |
| 172 | WRIO_Plugin::app()->logger->info( sprintf( 'END auto optimize cron job. Attachment: %s', $attachment_id ) ); |
| 173 | |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | $max_process_per_request = WRIO_Plugin::app()->getPopulateOption( 'image_autooptimize_items_number_per_interation', 3 ); |
| 178 | $cron_running_page = WRIO_Plugin::app()->getPopulateOption( 'conversion_cron_running', false ); |
| 179 | |
| 180 | if ( ! $cron_running_page ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | WRIO_Plugin::app()->logger->info( sprintf( 'Start cron job. Scope: %s', $cron_running_page ) ); |
| 185 | |
| 186 | if ( 'media-library' == $cron_running_page ) { |
| 187 | $media_library = WRIO_Media_Library::get_instance(); |
| 188 | $result = $media_library->webpUnoptimizedImages( $max_process_per_request ); |
| 189 | } |
| 190 | |
| 191 | if ( is_wp_error( $result ) ) { |
| 192 | WRIO_Plugin::app()->logger->info( sprintf( 'Cron job failed. Error: %s', $result->get_error_message() ) ); |
| 193 | WRIO_Plugin::app()->deletePopulateOption( 'conversion_cron_running' ); |
| 194 | |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if ( $result['remain'] <= 0 ) { |
| 199 | WRIO_Plugin::app()->deletePopulateOption( 'conversion_cron_running' ); |
| 200 | } |
| 201 | |
| 202 | WRIO_Plugin::app()->logger->info( sprintf( 'End cron job. Scope: %s', $cron_running_page ) ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * AVIF conversion cron process handler |
| 207 | * Метод конвертирует изображения в AVIF при выполнении cron задачи |
| 208 | * |
| 209 | * @param int $attachment_id Optional attachment ID for single image conversion |
| 210 | * |
| 211 | * @return void |
| 212 | */ |
| 213 | public function avif_conversion_process( $attachment_id = 0 ) { |
| 214 | // Convert single image via cron |
| 215 | if ( $attachment_id ) { |
| 216 | WRIO_Plugin::app()->logger->info( sprintf( 'START AVIF conversion cron job. Attachment: %s', $attachment_id ) ); |
| 217 | |
| 218 | if ( ! class_exists( 'WRIO_Format_Converter_Factory' ) || ! WRIO_Format_Converter_Factory::is_avif_enabled() ) { |
| 219 | WRIO_Plugin::app()->logger->warning( 'AVIF conversion cron triggered but AVIF is not enabled' ); |
| 220 | |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | $media_library = WRIO_Media_Library::get_instance(); |
| 225 | $media_library->webpConvertAttachment( $attachment_id, 'avif' ); |
| 226 | WRIO_Plugin::app()->logger->info( sprintf( 'END AVIF conversion cron job. Attachment: %s', $attachment_id ) ); |
| 227 | |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | $max_process_per_request = WRIO_Plugin::app()->getPopulateOption( 'image_autooptimize_items_number_per_interation', 3 ); |
| 232 | $cron_running_page = WRIO_Plugin::app()->getPopulateOption( 'avif_conversion_cron_running', false ); |
| 233 | |
| 234 | if ( ! $cron_running_page ) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | if ( ! class_exists( 'WRIO_Format_Converter_Factory' ) || ! WRIO_Format_Converter_Factory::is_avif_enabled() ) { |
| 239 | WRIO_Plugin::app()->logger->warning( 'AVIF conversion cron triggered but AVIF is not enabled' ); |
| 240 | WRIO_Plugin::app()->deletePopulateOption( 'avif_conversion_cron_running' ); |
| 241 | |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | WRIO_Plugin::app()->logger->info( sprintf( 'Start AVIF conversion cron job. Scope: %s', $cron_running_page ) ); |
| 246 | |
| 247 | $result = null; |
| 248 | if ( 'media-library' == $cron_running_page ) { |
| 249 | $media_library = WRIO_Media_Library::get_instance(); |
| 250 | $result = $media_library->webpUnoptimizedImages( $max_process_per_request, 'avif' ); |
| 251 | } |
| 252 | |
| 253 | if ( is_wp_error( $result ) ) { |
| 254 | WRIO_Plugin::app()->logger->error( sprintf( 'AVIF conversion cron job failed. Error: %s', $result->get_error_message() ) ); |
| 255 | WRIO_Plugin::app()->deletePopulateOption( 'avif_conversion_cron_running' ); |
| 256 | |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | if ( is_array( $result ) && isset( $result['remain'] ) && $result['remain'] <= 0 ) { |
| 261 | WRIO_Plugin::app()->logger->info( 'AVIF conversion cron job completed. All images converted.' ); |
| 262 | WRIO_Plugin::app()->deletePopulateOption( 'avif_conversion_cron_running' ); |
| 263 | } elseif ( is_array( $result ) && isset( $result['remain'] ) ) { |
| 264 | WRIO_Plugin::app()->logger->info( sprintf( 'AVIF conversion cron job: %d images remaining', $result['remain'] ) ); |
| 265 | } |
| 266 | |
| 267 | WRIO_Plugin::app()->logger->info( sprintf( 'End AVIF conversion cron job. Scope: %s', $cron_running_page ) ); |
| 268 | } |
| 269 | } |
| 270 |