| @@ -35,9 +35,9 @@ | ||
| 35 | 35 | $attachment_object = get_post( $image_id ); |
| 36 | 36 | |
| 37 | 37 | if ( ! $attachment_object ) { |
| 38 | 38 | throw new Image_Validation_Error( |
| 39 | - __( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' ) | |
| 39 | + esc_html__( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' ) | |
| 40 | 40 | ); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | if ( ! wp_attachment_is_image( $attachment_object ) || |
| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | in_array( $attachment_object->post_mime_type, Image::get_mime_types_cannot_be_optimized(), true ) && |
| 47 | 47 | ! get_post_meta( $image_id, Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, true ) |
| 48 | 48 | ) |
| 49 | 49 | ) { |
| 50 | - 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() ) ); | |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | if ( ! file_exists( get_attached_file( $image_id ) ) ) { |
| 54 | 54 | throw new Image_Validation_Error( |
| @@ -55,13 +55,44 @@ | ||
| 55 | 55 | esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) |
| 56 | 56 | ); |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - $wp_meta = new WP_Image_Meta( $image_id ); | |
| 59 | + self::validate_file_size( $image_id ); | |
| 60 | 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 ) { | |
| 61 | 91 | try { |
| 62 | - $image_size = $wp_meta->get_file_size( Image::SIZE_FULL ) | |
| 63 | - ?? File_System::size( ( new Image( $image_id ) )->get_file_path( Image::SIZE_FULL ) ); | |
| 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 ) ); | |
| 64 | 95 | } catch ( File_System_Operation_Error $e ) { |
| 65 | 96 | throw new Image_Validation_Error( |
| 66 | 97 | esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) |
| 67 | 98 | ); |
| @@ -69,11 +100,13 @@ | ||
| 69 | 100 | |
| 70 | 101 | if ( $image_size > self::MAX_FILE_SIZE ) { |
| 71 | 102 | if ( ! self::are_big_files_supported() ) { |
| 72 | 103 | throw new Image_Validation_Error( |
| 73 | - sprintf( | |
| 74 | - __( 'File is too large. Max size is %s', 'image-optimization' ), | |
| 75 | - File_Utils::format_file_size( self::MAX_FILE_SIZE, 0 ), | |
| 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 | + ) | |
| 76 | 109 | ) |
| 77 | 110 | ); |
| 78 | 111 | } |
| 79 | 112 | |
| @@ -78,39 +111,26 @@ | ||
| 78 | 111 | } |
| 79 | 112 | |
| 80 | 113 | if ( $image_size > self::MAX_BIG_FILE_SIZE ) { |
| 81 | 114 | throw new Image_Validation_Error( |
| 82 | - sprintf( | |
| 83 | - __( 'File is too large. Max size is %s', 'image-optimization' ), | |
| 84 | - File_Utils::format_file_size( self::MAX_BIG_FILE_SIZE, 0 ), | |
| 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 | + ) | |
| 85 | 120 | ) |
| 86 | 121 | ); |
| 87 | 122 | } |
| 88 | 123 | } |
| 89 | - | |
| 90 | - return true; | |
| 91 | 124 | } |
| 92 | 125 | |
| 93 | 126 | /** |
| 94 | - * Prepares the error message for the unsupported file formats. | |
| 127 | + * Returns the max file size allowed by the current plan. | |
| 95 | 128 | * |
| 96 | - * @return string The error message. | |
| 129 | + * @return int File size in bytes. | |
| 97 | 130 | */ |
| 98 | - private static function prepare_supported_formats_list_error(): string { | |
| 99 | - $formats = Image::get_supported_formats(); | |
| 100 | - $formats = array_filter( | |
| 101 | - $formats, | |
| 102 | - fn ( $format ) => ! in_array( $format, Image::get_formats_cannot_be_optimized(), true ) | |
| 103 | - ); | |
| 104 | - $last_item = strtoupper( array_pop( $formats ) ); | |
| 105 | - | |
| 106 | - $formats_list = join( ', ', array_map( 'strtoupper', $formats ) ); | |
| 107 | - | |
| 108 | - return sprintf( | |
| 109 | - __( 'Unsupported file format. Only %1$s, or %2$s are supported', 'image-optimization' ), | |
| 110 | - $formats_list, | |
| 111 | - $last_item | |
| 112 | - ); | |
| 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; | |
| 113 | 133 | } |
| 114 | 134 | |
| 115 | 135 | public static function are_big_files_supported(): bool { |
| 116 | 136 | static $is_allowed = null; |
| @@ -116,9 +136,9 @@ | ||
| 116 | 136 | static $is_allowed = null; |
| 117 | 137 | $connect_manager = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| 118 | 138 | |
| 119 | 139 | if ( null === $is_allowed ) { |
| 120 | - if ( ! $connect_manager->connect_instance->is_connected() ) { | |
| 140 | + if ( ! $connect_manager->connect_instance->is_connected() ) { | |
| 121 | 141 | $is_allowed = false; |
| 122 | 142 | return $is_allowed; |
| 123 | 143 | } |
| 124 | 144 | |