| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace ImageOptimizer\Classes\Image; | |
| 3 | +namespace ImageOptimization\Classes\Image; | |
| 4 | 4 | |
| 5 | -use ImageOptimizer\Classes\Image\Exceptions\Invalid_Image_Exception; | |
| 5 | +use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception; | |
| 6 | 6 | use WP_Post; |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | 9 | exit; // Exit if accessed directly. |
| @@ -10,12 +10,14 @@ | ||
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | class Image { |
| 13 | 13 | public const SIZE_FULL = 'full'; |
| 14 | - private const SUPPORTED_MIME_TYPES = [ 'image/jpeg', 'image/png', 'image/webp', 'image/gif' ]; | |
| 14 | + private const SUPPORTED_MIME_TYPES = [ 'image/jpeg', 'image/png', 'image/webp', 'image/gif', 'image/avif' ]; | |
| 15 | 15 | |
| 16 | 16 | // Used for error messages |
| 17 | - private const SUPPORTED_FORMATS = [ 'jpeg', 'png', 'webp', 'gif' ]; | |
| 17 | + private const SUPPORTED_FORMATS = [ 'jpeg', 'png', 'webp', 'gif', 'avif' ]; | |
| 18 | + private const MIME_TYPES_CANNOT_BE_OPTIMIZED = [ 'image/avif' ]; | |
| 19 | + private const FORMATS_CANNOT_BE_OPTIMIZED = [ 'avif' ]; | |
| 18 | 20 | |
| 19 | 21 | protected int $image_id; |
| 20 | 22 | protected $attachment_object; |
| 21 | 23 | |
| @@ -68,8 +70,18 @@ | ||
| 68 | 70 | $path_data['path'] |
| 69 | 71 | ); |
| 70 | 72 | } |
| 71 | 73 | |
| 74 | + public function file_exists( string $image_size ): bool { | |
| 75 | + $path = $this->get_file_path( $image_size ); | |
| 76 | + | |
| 77 | + if ( ! $path ) { | |
| 78 | + return false; | |
| 79 | + } | |
| 80 | + | |
| 81 | + return file_exists( $path ); | |
| 82 | + } | |
| 83 | + | |
| 72 | 84 | /** |
| 73 | 85 | * Returns true if an image marked as optimized. |
| 74 | 86 | * |
| 75 | 87 | * @return bool |
| @@ -150,8 +162,26 @@ | ||
| 150 | 162 | return self::SUPPORTED_FORMATS; |
| 151 | 163 | } |
| 152 | 164 | |
| 153 | 165 | /** |
| 166 | + * Returns the list of mime types that are supported by the plugin, but cannot be optimized | |
| 167 | + * | |
| 168 | + * @return string[] | |
| 169 | + */ | |
| 170 | + public static function get_mime_types_cannot_be_optimized(): array { | |
| 171 | + return self::MIME_TYPES_CANNOT_BE_OPTIMIZED; | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Returns the list of formats that are supported by the plugin, but cannot be optimized | |
| 176 | + * | |
| 177 | + * @return string[] | |
| 178 | + */ | |
| 179 | + public static function get_formats_cannot_be_optimized(): array { | |
| 180 | + return self::FORMATS_CANNOT_BE_OPTIMIZED; | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 154 | 184 | * @throws Invalid_Image_Exception |
| 155 | 185 | */ |
| 156 | 186 | public function __construct( int $image_id ) { |
| 157 | 187 | $this->image_id = $image_id; |
| @@ -157,12 +187,12 @@ | ||
| 157 | 187 | $this->image_id = $image_id; |
| 158 | 188 | $this->attachment_object = get_post( $image_id ); |
| 159 | 189 | |
| 160 | 190 | if ( ! $this->attachment_object ) { |
| 161 | - throw new Invalid_Image_Exception( "There is no entity with id '$image_id'" ); | |
| 191 | + throw new Invalid_Image_Exception( esc_html( "There is no entity with id '$image_id'" ) ); | |
| 162 | 192 | } |
| 163 | 193 | |
| 164 | 194 | if ( ! wp_attachment_is_image( $this->attachment_object ) ) { |
| 165 | - throw new Invalid_Image_Exception( "Post '$image_id' is not an image" ); | |
| 195 | + throw new Invalid_Image_Exception( esc_html( "Post '$image_id' is not an image" ) ); | |
| 166 | 196 | } |
| 167 | 197 | } |
| 168 | 198 | } |