← All changes
|
modules/optimization/components/single-optimization.php
+37
-15
1.0.1
→
1.7.7
View file →
| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace ImageOptimizer\Modules\Optimization\Components; | |
| 3 | +namespace ImageOptimization\Modules\Optimization\Components; | |
| 4 | 4 | |
| 5 | -use ImageOptimizer\Classes\Async_Operation\Async_Operation_Hook; | |
| 6 | -use ImageOptimizer\Classes\Image\{ | |
| 5 | +use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook; | |
| 6 | +use ImageOptimization\Classes\Image\{ | |
| 7 | 7 | Image, |
| 8 | 8 | Image_Meta, |
| 9 | 9 | Image_Optimization_Error_Type, |
| 10 | 10 | Image_Restore, |
| @@ -9,13 +9,19 @@ | ||
| 9 | 9 | Image_Optimization_Error_Type, |
| 10 | 10 | Image_Restore, |
| 11 | 11 | Image_Status |
| 12 | 12 | }; |
| 13 | -use ImageOptimizer\Classes\Logger; | |
| 14 | -use ImageOptimizer\Modules\Oauth\Classes\Exceptions\Quota_Exceeded_Error; | |
| 15 | -use ImageOptimizer\Modules\Optimization\Classes\Optimize_Image; | |
| 13 | +use ImageOptimization\Classes\Logger; | |
| 14 | +use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error; | |
| 15 | +use ImageOptimization\Modules\Connect\Classes\Exceptions\Connection_Error; | |
| 16 | +use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_File_Already_Exists_Error; | |
| 17 | +use ImageOptimization\Modules\Optimization\Classes\Optimize_Image; | |
| 16 | 18 | use Throwable; |
| 17 | 19 | |
| 20 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 21 | + exit; // Exit if accessed directly. | |
| 22 | +} | |
| 23 | + | |
| 18 | 24 | class Single_Optimization { |
| 19 | 25 | /** @async */ |
| 20 | 26 | public function optimize_single_image( int $image_id ) { |
| 21 | 27 | try { |
| @@ -29,15 +35,22 @@ | ||
| 29 | 35 | ( new Image_Meta( $image_id ) ) |
| 30 | 36 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 31 | 37 | ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED ) |
| 32 | 38 | ->save(); |
| 33 | - } catch ( Throwable $t ) { | |
| 34 | - Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() ); | |
| 35 | - | |
| 39 | + } catch ( Connection_Error $ce ) { | |
| 36 | 40 | ( new Image_Meta( $image_id ) ) |
| 37 | 41 | ->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 38 | - ->set_error_type( Image_Optimization_Error_Type::GENERIC ) | |
| 42 | + ->set_error_type( Image_Optimization_Error_Type::CONNECTION_ERROR ) | |
| 39 | 43 | ->save(); |
| 44 | + } catch ( Image_File_Already_Exists_Error $fe ) { | |
| 45 | + ( new Image_Meta( $image_id ) ) | |
| 46 | + ->set_status( Image_Status::OPTIMIZATION_FAILED ) | |
| 47 | + ->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) | |
| 48 | + ->save(); | |
| 49 | + } catch ( Throwable $t ) { | |
| 50 | + Logger::error( 'Single optimization error. Reason: ' . $t->getMessage() ); | |
| 51 | + | |
| 52 | + Retry::maybe_retry_optimization( $image_id ); | |
| 40 | 53 | } |
| 41 | 54 | } |
| 42 | 55 | |
| 43 | 56 | /** @async */ |
| @@ -51,8 +64,10 @@ | ||
| 51 | 64 | |
| 52 | 65 | $oi = new Optimize_Image( |
| 53 | 66 | $image_id, |
| 54 | 67 | 'manual', |
| 68 | + null, | |
| 69 | + true | |
| 55 | 70 | ); |
| 56 | 71 | |
| 57 | 72 | $oi->optimize(); |
| 58 | 73 | } catch ( Quota_Exceeded_Error $qe ) { |
| @@ -59,19 +74,26 @@ | ||
| 59 | 74 | ( new Image_Meta( $image_id ) ) |
| 60 | 75 | ->set_status( Image_Status::REOPTIMIZING_FAILED ) |
| 61 | 76 | ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED ) |
| 62 | 77 | ->save(); |
| 63 | - } catch ( Throwable $t ) { | |
| 64 | - Logger::log( Logger::LEVEL_ERROR, 'Reoptimizing error. Reason: ' . $t->getMessage() ); | |
| 65 | - | |
| 78 | + } catch ( Connection_Error $ce ) { | |
| 66 | 79 | ( new Image_Meta( $image_id ) ) |
| 80 | + ->set_status( Image_Status::OPTIMIZATION_FAILED ) | |
| 81 | + ->set_error_type( Image_Optimization_Error_Type::CONNECTION_ERROR ) | |
| 82 | + ->save(); | |
| 83 | + } catch ( Image_File_Already_Exists_Error $fe ) { | |
| 84 | + ( new Image_Meta( $image_id ) ) | |
| 67 | 85 | ->set_status( Image_Status::REOPTIMIZING_FAILED ) |
| 68 | - ->set_error_type( Image_Optimization_Error_Type::GENERIC ) | |
| 86 | + ->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) | |
| 69 | 87 | ->save(); |
| 88 | + } catch ( Throwable $t ) { | |
| 89 | + Logger::error( 'Single reoptimizing error. Reason: ' . $t->getMessage() ); | |
| 90 | + | |
| 91 | + Retry::maybe_retry_optimization( $image_id ); | |
| 70 | 92 | } |
| 71 | 93 | } |
| 72 | 94 | |
| 73 | 95 | public function __construct() { |
| 74 | - add_action( Async_Operation_Hook::OPTIMIZE_SINGLE, [ $this, 'optimize_single_image' ] ); | |
| 96 | + add_action( Async_Operation_Hook::OPTIMIZE_SINGLE, [ $this, 'optimize_single_image' ], 3 ); | |
| 75 | 97 | add_action( Async_Operation_Hook::REOPTIMIZE_SINGLE, [ $this, 'reoptimize_single_image' ] ); |
| 76 | 98 | } |
| 77 | 99 | } |