class-rio-folder-processing.php
5 months ago
class-rio-media-processing-avif.php
3 months ago
class-rio-media-processing-webp.php
3 months ago
class-rio-media-processing.php
3 months ago
class-rio-nextgen-processing.php
5 months ago
class-rio-processing.php
5 months ago
class-rio-media-processing-webp.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | use WBCR\Factory_Processing_759\WP_Background_Process; |
| 4 | |
| 5 | // Exit if accessed directly |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Класс для работы оптимизации в фоне |
| 12 | * |
| 13 | * @version 1.0 |
| 14 | */ |
| 15 | class WRIO_Media_Processing_Webp extends WRIO_Processing { |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $action = 'convert_process'; |
| 21 | |
| 22 | /** |
| 23 | * @var string Format type (webp or avif) |
| 24 | */ |
| 25 | protected $format = 'webp'; |
| 26 | |
| 27 | /** |
| 28 | * Constructor |
| 29 | * |
| 30 | * @param string $scope Processing scope |
| 31 | */ |
| 32 | public function __construct( $scope ) { |
| 33 | parent::__construct( $scope ); |
| 34 | |
| 35 | // Extract format from scope (e.g., 'media-library_webp' -> 'webp') |
| 36 | if ( $this->scope && strpos( $this->scope, '_' ) !== false ) { |
| 37 | $parts = explode( '_', $this->scope ); |
| 38 | $extracted_format = end( $parts ); |
| 39 | if ( in_array( $extracted_format, [ 'webp', 'avif' ], true ) ) { |
| 40 | $this->format = $extracted_format; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return int Count of pushed queue |
| 47 | */ |
| 48 | public function push_items() { |
| 49 | $attachment_ids = []; |
| 50 | if ( strpos( $this->scope, 'media-library_' ) === 0 ) { |
| 51 | $media_library = WRIO_Media_Library::get_instance(); |
| 52 | $attachment_ids = $media_library->getUnconvertedImages( $this->format ); |
| 53 | } |
| 54 | |
| 55 | foreach ( $attachment_ids as $attachment_id ) { |
| 56 | $this->push_to_queue( $attachment_id ); |
| 57 | } |
| 58 | |
| 59 | return $this->count_queue(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Метод оптимизирует изображения при выполнении задачи |
| 64 | * |
| 65 | * @param int $image |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | protected function task( $image ) { |
| 70 | if ( $image ) { |
| 71 | WRIO_Plugin::app()->logger->info( sprintf( 'Start convert attachment #%s to %s', $image, $this->format ) ); |
| 72 | $media_library = WRIO_Media_Library::get_instance(); |
| 73 | |
| 74 | try { |
| 75 | if ( strpos( $this->scope, 'media-library_' ) === 0 ) { |
| 76 | $media_library->webpConvertAttachment( $image, $this->format ); |
| 77 | } |
| 78 | } catch ( Throwable $throwable ) { |
| 79 | $wio_attachment = $media_library->getAttachment( $image ); |
| 80 | $wio_attachment->mark_conversion_failure( $throwable, $this->format, sprintf( '%s-conversion-background', $this->format ) ); |
| 81 | } |
| 82 | |
| 83 | WRIO_Plugin::app()->logger->info( sprintf( 'End convert attachment #%s to %s', $image, $this->format ) ); |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Fire after complete handle |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
| 94 | protected function handle_after_complete() { |
| 95 | WRIO_Plugin::app()->updatePopulateOption( "{$this->scope}_process_running", false ); |
| 96 | |
| 97 | WRIO_Plugin::app()->logger->info( |
| 98 | sprintf( '%s conversion background process completed for scope: %s', strtoupper( $this->format ), $this->scope ) |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 |