| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Optimization\Classes; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Image\{ |
| 6 |
Exceptions\Invalid_Image_Exception, |
| 7 |
Image, |
| 8 |
Image_Meta, |
| 9 |
WP_Image_Meta, |
| 10 |
}; |
| 11 |
use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error; |
| 12 |
use ImageOptimization\Classes\File_System\File_System; |
| 13 |
use ImageOptimization\Classes\File_Utils; |
| 14 |
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Validation_Error; |
| 15 |
use ImageOptimization\Plugin; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
class Validate_Image { |
| 22 |
public const MAX_FILE_SIZE = 10 * 1024 * 1024; |
| 23 |
public const MAX_BIG_FILE_SIZE = 25 * 1024 * 1024; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns true if $image_id provided associated with an image that can be optimized. |
| 27 |
* |
| 28 |
* @param int $image_id Attachment id. |
| 29 |
* |
| 30 |
* @return true |
| 31 |
* @throws Image_Validation_Error |
| 32 |
* @throws Invalid_Image_Exception |
| 33 |
*/ |
| 34 |
public static function is_valid( int $image_id ): bool { |
| 35 |
$attachment_object = get_post( $image_id ); |
| 36 |
|
| 37 |
if ( ! $attachment_object ) { |
| 38 |
throw new Image_Validation_Error( |
| 39 |
__( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' ) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! wp_attachment_is_image( $attachment_object ) || |
| 44 |
! in_array( $attachment_object->post_mime_type, Image::get_supported_mime_types(), true ) || |
| 45 |
( |
| 46 |
in_array( $attachment_object->post_mime_type, Image::get_mime_types_cannot_be_optimized(), true ) && |
| 47 |
! get_post_meta( $image_id, Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, true ) |
| 48 |
) |
| 49 |
) { |
| 50 |
throw new Image_Validation_Error( self::prepare_supported_formats_list_error() ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! file_exists( get_attached_file( $image_id ) ) ) { |
| 54 |
throw new Image_Validation_Error( |
| 55 |
esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
self::validate_file_size( $image_id ); |
| 60 |
|
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Prepares the error message for the unsupported file formats. |
| 66 |
* |
| 67 |
* @return string The error message. |
| 68 |
*/ |
| 69 |
private static function prepare_supported_formats_list_error(): string { |
| 70 |
$formats = Image::get_supported_formats(); |
| 71 |
$formats = array_filter( |
| 72 |
$formats, |
| 73 |
fn ( $format ) => ! in_array( $format, Image::get_formats_cannot_be_optimized(), true ) |
| 74 |
); |
| 75 |
$last_item = strtoupper( array_pop( $formats ) ); |
| 76 |
|
| 77 |
$formats_list = join( ', ', array_map( 'strtoupper', $formats ) ); |
| 78 |
|
| 79 |
return sprintf( |
| 80 |
__( 'Unsupported file format. Only %1$s, or %2$s are supported', 'image-optimization' ), |
| 81 |
$formats_list, |
| 82 |
$last_item |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @throws Invalid_Image_Exception |
| 88 |
* @throws Image_Validation_Error |
| 89 |
*/ |
| 90 |
public static function validate_file_size( int $image_id, string $image_size = Image::SIZE_FULL ) { |
| 91 |
try { |
| 92 |
$wp_meta = new WP_Image_Meta( $image_id ); |
| 93 |
$image_size = $wp_meta->get_file_size( $image_size ) |
| 94 |
?? File_System::size( ( new Image( $image_id ) )->get_file_path( $image_size ) ); |
| 95 |
} catch ( File_System_Operation_Error $e ) { |
| 96 |
throw new Image_Validation_Error( |
| 97 |
esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
if ( $image_size > self::MAX_FILE_SIZE ) { |
| 102 |
if ( ! self::are_big_files_supported() ) { |
| 103 |
throw new Image_Validation_Error( |
| 104 |
sprintf( |
| 105 |
__( 'File is too large. Max size is %s', 'image-optimization' ), |
| 106 |
File_Utils::format_file_size( self::MAX_FILE_SIZE, 0 ), |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
if ( $image_size > self::MAX_BIG_FILE_SIZE ) { |
| 112 |
throw new Image_Validation_Error( |
| 113 |
sprintf( |
| 114 |
__( 'File is too large. Max size is %s', 'image-optimization' ), |
| 115 |
File_Utils::format_file_size( self::MAX_BIG_FILE_SIZE, 0 ), |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the max file size allowed by the current plan. |
| 124 |
* |
| 125 |
* @return int File size in bytes. |
| 126 |
*/ |
| 127 |
public static function get_max_file_size(): int { |
| 128 |
return self::are_big_files_supported() ? self::MAX_BIG_FILE_SIZE : self::MAX_FILE_SIZE; |
| 129 |
} |
| 130 |
|
| 131 |
public static function are_big_files_supported(): bool { |
| 132 |
static $is_allowed = null; |
| 133 |
$connect_manager = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| 134 |
|
| 135 |
if ( null === $is_allowed ) { |
| 136 |
if ( ! $connect_manager->connect_instance->is_connected() ) { |
| 137 |
$is_allowed = false; |
| 138 |
return $is_allowed; |
| 139 |
} |
| 140 |
|
| 141 |
$plan_data = $connect_manager->connect_instance->get_connect_status(); |
| 142 |
|
| 143 |
if ( ! isset( $plan_data->subscription_plan->features->large_upload_allowed ) ) { |
| 144 |
$is_allowed = false; |
| 145 |
} else { |
| 146 |
$is_allowed = (bool) $plan_data->subscription_plan->features->large_upload_allowed; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $is_allowed; |
| 151 |
} |
| 152 |
} |
| 153 |
|