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