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