| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes\Image; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception; |
| 6 |
use WP_Post; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Image { |
| 13 |
public const SIZE_FULL = 'full'; |
| 14 |
private const SUPPORTED_MIME_TYPES = [ 'image/jpeg', 'image/png', 'image/webp', 'image/gif', 'image/avif' ]; |
| 15 |
|
| 16 |
// Used for error messages |
| 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' ]; |
| 20 |
|
| 21 |
protected int $image_id; |
| 22 |
protected $attachment_object; |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns attachment post id. |
| 26 |
* |
| 27 |
* @return int |
| 28 |
*/ |
| 29 |
public function get_id(): int { |
| 30 |
return $this->image_id; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns file URL for a specific image size. |
| 35 |
* |
| 36 |
* @param string $image_size Image size (e. g. 'full', 'thumbnail', etc) |
| 37 |
* @return string|null |
| 38 |
*/ |
| 39 |
public function get_url( string $image_size ): ?string { |
| 40 |
$image_data = wp_get_attachment_image_src( $this->image_id, $image_size ); |
| 41 |
|
| 42 |
if ( empty( $image_data ) ) { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
return $image_data[0]; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns absolute file path for a specific image size. |
| 51 |
* |
| 52 |
* @param string $image_size Image size (e. g. 'full', 'thumbnail', etc) |
| 53 |
* @return string|null |
| 54 |
*/ |
| 55 |
public function get_file_path( string $image_size ): ?string { |
| 56 |
if ( 'full' === $image_size ) { |
| 57 |
$path = get_attached_file( $this->image_id ); |
| 58 |
return $path ?? null; |
| 59 |
} |
| 60 |
|
| 61 |
$path_data = image_get_intermediate_size( $this->image_id, $image_size ); |
| 62 |
|
| 63 |
if ( empty( $path_data ) ) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
return sprintf( |
| 68 |
'%s/%s', |
| 69 |
wp_get_upload_dir()['basedir'], |
| 70 |
$path_data['path'] |
| 71 |
); |
| 72 |
} |
| 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 |
|
| 84 |
/** |
| 85 |
* Returns true if an image marked as optimized. |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function is_optimized(): bool { |
| 90 |
$meta = new Image_Meta( $this->image_id ); |
| 91 |
|
| 92 |
return $meta->get_status() === Image_Status::OPTIMIZED; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns true if an image can be restored from the backups. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function can_be_restored(): bool { |
| 101 |
$meta = new Image_Meta( $this->image_id ); |
| 102 |
|
| 103 |
return (bool) count( $meta->get_image_backup_paths() ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns image's mime type. |
| 108 |
* |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function get_mime_type(): string { |
| 112 |
return $this->attachment_object->post_mime_type; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns an original attachment WP_Post object. |
| 117 |
* |
| 118 |
* @return WP_Post |
| 119 |
*/ |
| 120 |
public function get_attachment_object(): WP_Post { |
| 121 |
return $this->attachment_object; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Updates WP_Post fields of the attachment. |
| 126 |
* |
| 127 |
* @return bool True if the post was updated successfully, false otherwise. |
| 128 |
*/ |
| 129 |
public function update_attachment( array $update_query ): bool { |
| 130 |
global $wpdb; |
| 131 |
|
| 132 |
// Must use $wpdb here as `wp_update_post()` doesn't allow to rewrite guid. |
| 133 |
$result = $wpdb->update( |
| 134 |
$wpdb->posts, |
| 135 |
$update_query, |
| 136 |
[ 'ID' => $this->image_id ] |
| 137 |
); |
| 138 |
|
| 139 |
if ( 0 !== $result ) { |
| 140 |
$this->attachment_object = get_post( $this->image_id ); |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the list of mime types supported by the plugin. |
| 149 |
* |
| 150 |
* @return string[] |
| 151 |
*/ |
| 152 |
public static function get_supported_mime_types(): array { |
| 153 |
return self::SUPPORTED_MIME_TYPES; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns the list of formats types supported by the plugin. |
| 158 |
* |
| 159 |
* @return string[] |
| 160 |
*/ |
| 161 |
public static function get_supported_formats(): array { |
| 162 |
return self::SUPPORTED_FORMATS; |
| 163 |
} |
| 164 |
|
| 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 |
/** |
| 184 |
* @throws Invalid_Image_Exception |
| 185 |
*/ |
| 186 |
public function __construct( int $image_id ) { |
| 187 |
$this->image_id = $image_id; |
| 188 |
$this->attachment_object = get_post( $image_id ); |
| 189 |
|
| 190 |
if ( ! $this->attachment_object ) { |
| 191 |
throw new Invalid_Image_Exception( "There is no entity with id '$image_id'" ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( ! wp_attachment_is_image( $this->attachment_object ) ) { |
| 195 |
throw new Invalid_Image_Exception( "Post '$image_id' is not an image" ); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|