| @@ -27,8 +27,9 @@ | ||
| 27 | 27 | Exceptions\Image_Optimization_Error, |
| 28 | 28 | Exceptions\Bulk_Token_Expired_Error, |
| 29 | 29 | Exceptions\Image_Already_Optimized_Error, |
| 30 | 30 | }; |
| 31 | +use ImageOptimization\Modules\Connect\Classes\Exceptions\Connection_Error; | |
| 31 | 32 | use ImageOptimization\Modules\Settings\Classes\Settings; |
| 32 | 33 | use Throwable; |
| 33 | 34 | |
| 34 | 35 | use ImageOptimization\Plugin; |
| @@ -43,9 +44,9 @@ | ||
| 43 | 44 | * |
| 44 | 45 | * This class is used by manual, bulk and on-upload optimization flows. |
| 45 | 46 | */ |
| 46 | 47 | class Optimize_Image { |
| 47 | - private const IMAGE_OPTIMIZE_ENDPOINT = 'image/optimize'; | |
| 48 | + public const IMAGE_OPTIMIZE_ENDPOINT = 'image/optimize'; | |
| 48 | 49 | |
| 49 | 50 | protected ?Image $image; |
| 50 | 51 | protected WP_Image_Meta $wp_meta; |
| 51 | 52 | protected string $initiator; |
| @@ -57,18 +58,15 @@ | ||
| 57 | 58 | protected Image_Conversion $image_conversion; |
| 58 | 59 | private bool $is_reoptimize; |
| 59 | 60 | |
| 60 | 61 | /** |
| 61 | - * @throws Quota_Exceeded_Error|Bulk_Token_Expired_Error|Image_File_Already_Exists_Error|Image_Optimization_Error | |
| 62 | + * @throws Quota_Exceeded_Error|Connection_Error|Bulk_Token_Expired_Error|Image_File_Already_Exists_Error|Image_Optimization_Error | |
| 62 | 63 | */ |
| 63 | 64 | public function optimize(): void { |
| 64 | 65 | $sizes_enabled = Settings::get( Settings::CUSTOM_SIZES_OPTION_NAME ); |
| 65 | 66 | $sizes_exist = $this->wp_meta->get_size_keys(); |
| 66 | 67 | |
| 67 | - Logger::log( | |
| 68 | - Logger::LEVEL_INFO, | |
| 69 | - "Start optimization of {$this->image->get_id()}" | |
| 70 | - ); | |
| 68 | + Logger::debug( "Start optimization of {$this->image->get_id()}" ); | |
| 71 | 69 | |
| 72 | 70 | foreach ( $sizes_exist as $size_exist ) { |
| 73 | 71 | // If some image sizes optimization is disabled in settings, we check if the current one is still enabled |
| 74 | 72 | if ( |
| @@ -87,23 +85,17 @@ | ||
| 87 | 85 | $image_meta = new Image_Meta( $this->image->get_id() ); |
| 88 | 86 | |
| 89 | 87 | // If the current size was already optimized -- ignore it. |
| 90 | 88 | if ( ! $this->is_reoptimize && in_array( $size_exist, $image_meta->get_optimized_sizes(), true ) ) { |
| 91 | - Logger::log( | |
| 92 | - Logger::LEVEL_INFO, | |
| 93 | - "Size `$size_exist` is already optimized" | |
| 94 | - ); | |
| 89 | + Logger::debug( "Size `$size_exist` is already optimized" ); | |
| 95 | 90 | |
| 96 | 91 | continue; |
| 97 | 92 | } |
| 98 | 93 | |
| 99 | 94 | if ( ! file_exists( $this->image->get_file_path( $size_exist ) ) ) { |
| 100 | - Logger::log( | |
| 101 | - Logger::LEVEL_ERROR, | |
| 102 | - "Can't access file for size `$size_exist`" | |
| 103 | - ); | |
| 95 | + Logger::debug( "Can't access file for size `$size_exist`" ); | |
| 104 | 96 | |
| 105 | - throw new Image_Optimization_Error( esc_html__( 'File is missing. Verify the upload', 'image-optimization' ) ); | |
| 97 | + continue; | |
| 106 | 98 | } |
| 107 | 99 | |
| 108 | 100 | $this->current_image_size = $size_exist; |
| 109 | 101 | $this->current_size_duplicates = $this->wp_meta->get_size_duplicates( $size_exist ); |
| @@ -121,12 +113,9 @@ | ||
| 121 | 113 | if ( ! $this->keep_backups ) { |
| 122 | 114 | Image_Backup::remove( $this->image->get_id() ); |
| 123 | 115 | } |
| 124 | 116 | |
| 125 | - Logger::log( | |
| 126 | - Logger::LEVEL_INFO, | |
| 127 | - "End optimization of {$this->image->get_id()}" | |
| 128 | - ); | |
| 117 | + Logger::debug( "End optimization of {$this->image->get_id()}" ); | |
| 129 | 118 | } |
| 130 | 119 | |
| 131 | 120 | private function optimize_current_size(): void { |
| 132 | 121 | try { |
| @@ -151,13 +140,13 @@ | ||
| 151 | 140 | ?? File_System::size( $this->image->get_file_path( $this->current_image_size ) ); |
| 152 | 141 | |
| 153 | 142 | $this->update_attachment_meta( $original_size, true ); |
| 154 | 143 | $this->update_attachment_post( true ); |
| 155 | - } catch ( Bulk_Token_Expired_Error | Quota_Exceeded_Error | Image_File_Already_Exists_Error $e ) { | |
| 144 | + } catch ( Bulk_Token_Expired_Error | Quota_Exceeded_Error | Connection_Error | Image_File_Already_Exists_Error $e ) { | |
| 156 | 145 | throw $e; |
| 157 | 146 | } catch ( Throwable $t ) { |
| 158 | 147 | // In case of anything else |
| 159 | - throw new Image_Optimization_Error( $t->getMessage() ); | |
| 148 | + throw new Image_Optimization_Error( esc_html( $t->getMessage() ) ); | |
| 160 | 149 | } |
| 161 | 150 | } |
| 162 | 151 | |
| 163 | 152 | private function send_file() { |
| @@ -203,12 +192,9 @@ | ||
| 203 | 192 | Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->update_usage_data( $response->stats ); |
| 204 | 193 | } |
| 205 | 194 | |
| 206 | 195 | if ( ! isset( $response->imageKey ) || $image_key !== $response->imageKey ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 207 | - Logger::log( | |
| 208 | - Logger::LEVEL_ERROR, | |
| 209 | - "Image key must be $image_key, instead got $response->imageKey" | |
| 210 | - ); | |
| 196 | + Logger::error( "Image key must be $image_key, instead got $response->imageKey" ); | |
| 211 | 197 | |
| 212 | 198 | throw new Image_Optimization_Error( esc_html__( 'Service response is incorrect', 'image-optimization' ) ); |
| 213 | 199 | } |
| 214 | 200 | |
| @@ -214,12 +200,9 @@ | ||
| 214 | 200 | |
| 215 | 201 | $received_file_hash = md5( base64_decode( $response->image, true ) ); |
| 216 | 202 | |
| 217 | 203 | if ( ! isset( $response->checksum ) || $received_file_hash !== $response->checksum ) { |
| 218 | - Logger::log( | |
| 219 | - Logger::LEVEL_ERROR, | |
| 220 | - "Image key must be $response->checksum, instead calculated $received_file_hash" | |
| 221 | - ); | |
| 204 | + Logger::error( "Image key must be $response->checksum, instead calculated $received_file_hash" ); | |
| 222 | 205 | |
| 223 | 206 | throw new Image_Optimization_Error( esc_html__( 'Service response is incorrect', 'image-optimization' ) ); |
| 224 | 207 | } |
| 225 | 208 | |