← All changes
|
modules/optimization/components/upload-optimization.php
+31
-21
1.5.4
→
1.7.7
View file →
| @@ -6,19 +6,26 @@ | ||
| 6 | 6 | Async_Operation, |
| 7 | 7 | Async_Operation_Hook, |
| 8 | 8 | Async_Operation_Queue, |
| 9 | 9 | }; |
| 10 | + | |
| 10 | 11 | use ImageOptimization\Classes\Image\{ |
| 11 | - Image, | |
| 12 | + Exceptions\Invalid_Image_Exception, | |
| 12 | 13 | Image_Meta, |
| 13 | 14 | Image_Optimization_Error_Type, |
| 14 | - Image_Status | |
| 15 | + Image_Status, | |
| 15 | 16 | }; |
| 16 | 17 | |
| 18 | +use ImageOptimization\Modules\Optimization\Classes\{ | |
| 19 | + Exceptions\Image_File_Already_Exists_Error, | |
| 20 | + Exceptions\Image_Validation_Error, | |
| 21 | + Optimize_Image, | |
| 22 | + Validate_Image, | |
| 23 | +}; | |
| 24 | + | |
| 17 | 25 | use ImageOptimization\Classes\Logger; |
| 18 | 26 | use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error; |
| 19 | -use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_File_Already_Exists_Error; | |
| 20 | -use ImageOptimization\Modules\Optimization\Classes\Optimize_Image; | |
| 27 | +use ImageOptimization\Modules\Connect\Classes\Exceptions\Connection_Error; | |
| 21 | 28 | use ImageOptimization\Modules\Settings\Classes\Settings; |
| 22 | 29 | |
| 23 | 30 | use Throwable; |
| 24 | 31 | use ImageOptimization\Plugin; |
| @@ -27,11 +34,15 @@ | ||
| 27 | 34 | exit; // Exit if accessed directly. |
| 28 | 35 | } |
| 29 | 36 | |
| 30 | 37 | class Upload_Optimization { |
| 31 | - public function handle_upload( int $attachment_id ) { | |
| 38 | + public function handle_upload( array $metadata, int $attachment_id, string $context ) { | |
| 39 | + if ( 'create' !== $context ) { | |
| 40 | + return $metadata; | |
| 41 | + } | |
| 42 | + | |
| 32 | 43 | if ( ! Settings::get( Settings::OPTIMIZE_ON_UPLOAD_OPTION_NAME ) ) { |
| 33 | - return; | |
| 44 | + return $metadata; | |
| 34 | 45 | } |
| 35 | 46 | |
| 36 | 47 | // @var ImageOptimizer/Modules/ConnectManager/Module |
| 37 | 48 | $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| @@ -36,23 +47,15 @@ | ||
| 36 | 47 | // @var ImageOptimizer/Modules/ConnectManager/Module |
| 37 | 48 | $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| 38 | 49 | |
| 39 | 50 | if ( ! $module->connect_instance->is_connected() || ! $module->connect_instance->is_activated() ) { |
| 40 | - return; | |
| 51 | + return $metadata; | |
| 41 | 52 | } |
| 42 | 53 | |
| 43 | - $attachment_object = get_post( $attachment_id ); | |
| 44 | - | |
| 45 | - // TODO: Check how we can use Validate_Image::is_valid() here | |
| 46 | - if ( | |
| 47 | - ! wp_attachment_is_image( $attachment_object ) || | |
| 48 | - ! in_array( $attachment_object->post_mime_type, Image::get_supported_mime_types(), true ) || | |
| 49 | - ( | |
| 50 | - in_array( $attachment_object->post_mime_type, Image::get_mime_types_cannot_be_optimized(), true ) && | |
| 51 | - ! get_post_meta( $attachment_id, Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, true ) | |
| 52 | - ) | |
| 53 | - ) { | |
| 54 | - return; | |
| 54 | + try { | |
| 55 | + Validate_Image::is_valid( $attachment_id ); | |
| 56 | + } catch ( Invalid_Image_Exception | Image_Validation_Error $iie ) { | |
| 57 | + return $metadata; | |
| 55 | 58 | } |
| 56 | 59 | |
| 57 | 60 | $meta = new Image_Meta( $attachment_id ); |
| 58 | 61 | |
| @@ -70,8 +73,10 @@ | ||
| 70 | 73 | $meta |
| 71 | 74 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 72 | 75 | ->save(); |
| 73 | 76 | } |
| 77 | + | |
| 78 | + return $metadata; | |
| 74 | 79 | } |
| 75 | 80 | |
| 76 | 81 | /** @async */ |
| 77 | 82 | public function optimize_image_on_upload( int $image_id ) { |
| @@ -86,8 +91,13 @@ | ||
| 86 | 91 | ( new Image_Meta( $image_id ) ) |
| 87 | 92 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 88 | 93 | ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED ) |
| 89 | 94 | ->save(); |
| 95 | + } catch ( Connection_Error $ce ) { | |
| 96 | + ( new Image_Meta( $image_id ) ) | |
| 97 | + ->set_status( Image_Status::OPTIMIZATION_FAILED ) | |
| 98 | + ->set_error_type( Image_Optimization_Error_Type::CONNECTION_ERROR ) | |
| 99 | + ->save(); | |
| 90 | 100 | } catch ( Image_File_Already_Exists_Error $fe ) { |
| 91 | 101 | ( new Image_Meta( $image_id ) ) |
| 92 | 102 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 93 | 103 | ->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) |
| @@ -92,9 +102,9 @@ | ||
| 92 | 102 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 93 | 103 | ->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) |
| 94 | 104 | ->save(); |
| 95 | 105 | } catch ( Throwable $t ) { |
| 96 | - Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() ); | |
| 106 | + Logger::error( 'Upload optimization error. Reason: ' . $t->getMessage() ); | |
| 97 | 107 | |
| 98 | 108 | ( new Image_Meta( $image_id ) ) |
| 99 | 109 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 100 | 110 | ->set_error_type( Image_Optimization_Error_Type::GENERIC ) |
| @@ -102,8 +112,8 @@ | ||
| 102 | 112 | } |
| 103 | 113 | } |
| 104 | 114 | |
| 105 | 115 | public function __construct() { |
| 106 | - add_action( 'add_attachment', [ $this, 'handle_upload' ] ); | |
| 116 | + add_action( 'wp_generate_attachment_metadata', [ $this, 'handle_upload' ], 10, 3 ); | |
| 107 | 117 | add_action( Async_Operation_Hook::OPTIMIZE_ON_UPLOAD, [ $this, 'optimize_image_on_upload' ] ); |
| 108 | 118 | } |
| 109 | 119 | } |