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