|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OptimoleWP\Offload; |
| 4 | + |
| 5 | +use Optml_Config; |
| 6 | +use Optml_Media_Offload; |
| 7 | +use WP_Error; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class ImageEditor |
| 11 | + * |
| 12 | + * Custom image editor implementation for Optimole offloaded images. |
| 13 | + * This class extends WP_Image_Editor to handle images that have been offloaded |
| 14 | + * to Optimole's servers, preventing local editing operations on remote images. |
| 15 | + * |
| 16 | + * @package OptimoleWP\Offload |
| 17 | + */ |
| 18 | +class ImageEditor extends \WP_Image_Editor { |
| 19 | + /** |
| 20 | + * Tests whether this editor can handle the given file. |
| 21 | + * |
| 22 | + * @param array $args Arguments to test the editor. |
| 23 | + * @return bool True if the file is an offloaded image, false otherwise. |
| 24 | + */ |
| 25 | + public static function test( $args = [] ) { |
| 26 | + if ( isset( $args['path'] ) && Optml_Media_Offload::is_uploaded_image( $args['path'] ) ) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Checks if this editor supports the given MIME type. |
| 35 | + * |
| 36 | + * @param string $mime_type The MIME type to check. |
| 37 | + * @return bool True if the MIME type is supported, false otherwise. |
| 38 | + */ |
| 39 | + public static function supports_mime_type( $mime_type ) { |
| 40 | + return in_array( $mime_type, Optml_Config::$image_extensions, true ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Saves the image to a file. |
| 45 | + * |
| 46 | + * For offloaded images, this operation is not supported and always returns false. |
| 47 | + * |
| 48 | + * @param string $destfilename Optional. The destination filename. |
| 49 | + * @param string $mime_type Optional. The MIME type of the image. |
| 50 | + * @return array|WP_Error { |
| 51 | + * Array on success or WP_Error if the file failed to save. |
| 52 | + * |
| 53 | + * @type string $path Path to the image file. |
| 54 | + * @type string $file Name of the image file. |
| 55 | + * @type int $width Image width. |
| 56 | + * @type int $height Image height. |
| 57 | + * @type string $mime-type The mime type of the image. |
| 58 | + * @type int $filesize File size of the image. |
| 59 | + * } |
| 60 | + */ |
| 61 | + public function save( $destfilename = null, $mime_type = null ) { |
| 62 | + return false; // @phpstan-ignore-line |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Loads the image file. |
| 67 | + * |
| 68 | + * @return bool True if the file is an offloaded image, false otherwise. |
| 69 | + */ |
| 70 | + public function load() { |
| 71 | + if ( isset( $this->file ) && Optml_Media_Offload::is_uploaded_image( $this->file ) ) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Resizes the image. |
| 79 | + * |
| 80 | + * For offloaded images, this operation is not performed locally and always returns true. |
| 81 | + * |
| 82 | + * @param int $max_w Maximum width. |
| 83 | + * @param int $max_h Maximum height. |
| 84 | + * @param bool $crop Optional. Whether to crop the image. Default false. |
| 85 | + * @return bool Always returns true for offloaded images. |
| 86 | + */ |
| 87 | + public function resize( $max_w, $max_h, $crop = false ) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Resizes the image to multiple sizes. |
| 93 | + * |
| 94 | + * For offloaded images, this operation is not performed locally and always returns an empty array. |
| 95 | + * |
| 96 | + * @param array $sizes Array of size arrays. Each size array must include width, height, crop. |
| 97 | + * @return array Empty array for offloaded images. |
| 98 | + */ |
| 99 | + public function multi_resize( $sizes ) { |
| 100 | + return []; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Crops the image. |
| 105 | + * |
| 106 | + * For offloaded images, this operation is not performed locally and always returns true. |
| 107 | + * |
| 108 | + * @param int $src_x The start x position to crop from. |
| 109 | + * @param int $src_y The start y position to crop from. |
| 110 | + * @param int $src_w The width to crop. |
| 111 | + * @param int $src_h The height to crop. |
| 112 | + * @param int $dst_w Optional. The destination width. |
| 113 | + * @param int $dst_h Optional. The destination height. |
| 114 | + * @param bool $src_abs Optional. If the source crop points are absolute. |
| 115 | + * @return bool Always returns true for offloaded images. |
| 116 | + */ |
| 117 | + public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Rotates the image. |
| 123 | + * |
| 124 | + * For offloaded images, this operation is not performed locally and always returns true. |
| 125 | + * |
| 126 | + * @param float $angle The angle of rotation. |
| 127 | + * @return bool Always returns true for offloaded images. |
| 128 | + */ |
| 129 | + public function rotate( $angle ) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Flips the image. |
| 135 | + * |
| 136 | + * For offloaded images, this operation is not performed locally and always returns true. |
| 137 | + * |
| 138 | + * @param bool $horz Whether to flip horizontally. |
| 139 | + * @param bool $vert Whether to flip vertically. |
| 140 | + * @return bool Always returns true for offloaded images. |
| 141 | + */ |
| 142 | + public function flip( $horz, $vert ) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Streams the image to the browser. |
| 148 | + * |
| 149 | + * For offloaded images, this redirects to the remote image URL. |
| 150 | + * |
| 151 | + * @param string $mime_type Optional. The MIME type of the image. |
| 152 | + */ |
| 153 | + public function stream( $mime_type = null ) { |
| 154 | + header( 'Location: ' . esc_url( $this->file ) ); |
| 155 | + return true; |
| 156 | + } |
| 157 | +} |
0 commit comments