robin-image-optimizer
/
libs
/
addons
/
includes
/
classes
/
format
/
class-format-converter-webp.php
class-format-converter-api.php
3 months ago
class-format-converter-avif.php
5 months ago
class-format-converter-factory.php
5 months ago
class-format-converter-webp.php
5 months ago
class-format-converter-webp.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * WebP format converter implementation. |
| 5 | * |
| 6 | * Converts images to WebP format using the Robin Image Optimizer API. |
| 7 | * |
| 8 | * @author Alexander Teshabaev <sasha.tesh@gmail.com> |
| 9 | */ |
| 10 | class WRIO_Format_Converter_WebP extends WRIO_Format_Converter_Api { |
| 11 | |
| 12 | /** |
| 13 | * Get the format name. |
| 14 | * |
| 15 | * @return string |
| 16 | */ |
| 17 | protected function get_format_name() { |
| 18 | return 'webp'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get the file extension for WebP format. |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | protected function get_file_extension() { |
| 27 | return '.webp'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Get the MIME type for WebP format. |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | protected function get_mime_type() { |
| 36 | return 'image/webp'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get API query parameters for WebP conversion. |
| 41 | * |
| 42 | * @param bool $quota Whether to include quota-related parameters. |
| 43 | * |
| 44 | * @return array Query parameters for the API request. |
| 45 | */ |
| 46 | protected function get_api_query_params( $quota ) { |
| 47 | $params = [ 'format' => 'webp' ]; |
| 48 | |
| 49 | if ( $quota ) { |
| 50 | $params['type'] = 'webp'; // Only add 'type' for quota check |
| 51 | } |
| 52 | |
| 53 | return $params; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Check if WebP format is available for free tier users. |
| 58 | * |
| 59 | * @return bool True - WebP is available for free users. |
| 60 | */ |
| 61 | protected function is_free_tier_supported() { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Static wrapper for get_save_path for backward compatibility. |
| 67 | * |
| 68 | * @param \RIO_Process_Queue $queue_model |
| 69 | * |
| 70 | * @return string |
| 71 | * @throws Exception |
| 72 | */ |
| 73 | public static function get_save_path_static( $queue_model ) { |
| 74 | $extra_data = $queue_model->get_extra_data(); |
| 75 | |
| 76 | if ( empty( $extra_data ) ) { |
| 77 | WRIO_Plugin::app()->logger->error( sprintf( 'Unable to get extra data for queue item #%s', $queue_model->get_id() ) ); |
| 78 | |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | $path = dirname( $extra_data->get_source_path() ); |
| 83 | |
| 84 | if ( ! file_exists( $path ) ) { |
| 85 | $message = sprintf( 'Failed to create directory %s with mode %s recursively', $path, 0755 ); |
| 86 | WRIO_Plugin::app()->logger->error( $message ); |
| 87 | throw new \Exception( $message ); |
| 88 | } |
| 89 | |
| 90 | return trailingslashit( $path ) . trim( wp_basename( $extra_data->get_source_path() ) ) . '.webp'; |
| 91 | } |
| 92 | } |
| 93 |