| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Optimization\Classes; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Image\{ |
| 6 |
Exceptions\Invalid_Image_Exception, |
| 7 |
Image, |
| 8 |
WP_Image_Meta, |
| 9 |
}; |
| 10 |
use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error; |
| 11 |
use ImageOptimization\Classes\File_System\File_System; |
| 12 |
use ImageOptimization\Classes\File_Utils; |
| 13 |
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Validation_Error; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Validate_Image { |
| 20 |
public const MAX_FILE_SIZE = 10 * 1024 * 1024; |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns true if $image_id provided associated with an image that can be optimized. |
| 24 |
* |
| 25 |
* @param int $image_id Attachment id. |
| 26 |
* |
| 27 |
* @return true |
| 28 |
* @throws Image_Validation_Error |
| 29 |
* @throws Invalid_Image_Exception |
| 30 |
*/ |
| 31 |
public static function is_valid( int $image_id ): bool { |
| 32 |
$attachment_object = get_post( $image_id ); |
| 33 |
|
| 34 |
if ( ! $attachment_object ) { |
| 35 |
throw new Image_Validation_Error( |
| 36 |
__( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' ) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
if ( |
| 41 |
! wp_attachment_is_image( $attachment_object ) || |
| 42 |
! in_array( $attachment_object->post_mime_type, Image::get_supported_mime_types(), true ) |
| 43 |
) { |
| 44 |
throw new Image_Validation_Error( self::prepare_supported_formats_list_error() ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! file_exists( get_attached_file( $image_id ) ) ) { |
| 48 |
throw new Image_Validation_Error( |
| 49 |
esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
$wp_meta = new WP_Image_Meta( $image_id ); |
| 54 |
|
| 55 |
try { |
| 56 |
$image_size = $wp_meta->get_file_size( Image::SIZE_FULL ) |
| 57 |
?? File_System::size( ( new Image( $image_id ) )->get_file_path( Image::SIZE_FULL ) ); |
| 58 |
} catch ( File_System_Operation_Error $e ) { |
| 59 |
throw new Image_Validation_Error( |
| 60 |
esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
if ( $image_size > self::MAX_FILE_SIZE ) { |
| 65 |
throw new Image_Validation_Error( |
| 66 |
sprintf( |
| 67 |
__( 'File is too large. Max size is %s', 'image-optimization' ), |
| 68 |
File_Utils::format_file_size( self::MAX_FILE_SIZE, 0 ), |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Prepares the error message for the unsupported file formats. |
| 78 |
* |
| 79 |
* @return string The error message. |
| 80 |
*/ |
| 81 |
private static function prepare_supported_formats_list_error(): string { |
| 82 |
$formats = Image::get_supported_formats(); |
| 83 |
$last_item = strtoupper( array_pop( $formats ) ); |
| 84 |
|
| 85 |
$formats_list = join( ', ', array_map( 'strtoupper', $formats ) ); |
| 86 |
|
| 87 |
return sprintf( |
| 88 |
__( 'Wrong file format. Only %1$s, or %2$s are accepted', 'image-optimization' ), |
| 89 |
$formats_list, |
| 90 |
$last_item |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|