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