PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / trunk
Image Optimizer – Compress Images and Convert to WebP or AVIF vtrunk
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
← All changes | modules/optimization/components/single-optimization.php +55 -14 1.0.0 → trunk View file →
@@ -1,18 +1,27 @@
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 + Image,
7 8 Image_Meta,
9 + Image_Optimization_Error_Type,
8 10 Image_Restore,
9 - Image_Status,
11 + Image_Status
10 12 };
11 -use ImageOptimizer\Classes\Logger;
12 -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;
13 18 use Throwable;
14 19
20 +if ( ! defined( 'ABSPATH' ) ) {
21 + exit; // Exit if accessed directly.
22 +}
23 +
15 24 class Single_Optimization {
16 25 /** @async */
17 26 public function optimize_single_image( int $image_id ) {
18 27 try {
@@ -21,14 +30,27 @@
21 30 'manual',
22 31 );
23 32
24 33 $oi->optimize();
25 - } catch ( Throwable $t ) {
26 - Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() );
27 -
34 + } catch ( Quota_Exceeded_Error $qe ) {
28 35 ( new Image_Meta( $image_id ) )
29 36 ->set_status( Image_Status::OPTIMIZATION_FAILED )
37 + ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
30 38 ->save();
39 + } catch ( Connection_Error $ce ) {
40 + ( new Image_Meta( $image_id ) )
41 + ->set_status( Image_Status::OPTIMIZATION_FAILED )
42 + ->set_error_type( Image_Optimization_Error_Type::CONNECTION_ERROR )
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 );
31 53 }
32 54 }
33 55
34 56 /** @async */
@@ -33,26 +55,45 @@
33 55
34 56 /** @async */
35 57 public function reoptimize_single_image( int $image_id ) {
36 58 try {
37 - Image_Restore::restore( $image_id, true );
59 + $image = new Image( $image_id );
38 60
61 + if ( $image->can_be_restored() ) {
62 + Image_Restore::restore( $image_id, true );
63 + }
64 +
39 65 $oi = new Optimize_Image(
40 66 $image_id,
41 67 'manual',
68 + null,
69 + true
42 70 );
43 71
44 72 $oi->optimize();
45 - } catch ( Throwable $t ) {
46 - Logger::log( Logger::LEVEL_ERROR, 'Reoptimizing error. Reason: ' . $t->getMessage() );
47 -
73 + } catch ( Quota_Exceeded_Error $qe ) {
48 74 ( new Image_Meta( $image_id ) )
49 75 ->set_status( Image_Status::REOPTIMIZING_FAILED )
76 + ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
50 77 ->save();
78 + } catch ( Connection_Error $ce ) {
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 ) )
85 + ->set_status( Image_Status::REOPTIMIZING_FAILED )
86 + ->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS )
87 + ->save();
88 + } catch ( Throwable $t ) {
89 + Logger::error( 'Single reoptimizing error. Reason: ' . $t->getMessage() );
90 +
91 + Retry::maybe_retry_optimization( $image_id );
51 92 }
52 93 }
53 94
54 95 public function __construct() {
55 - add_action( Async_Operation_Hook::OPTIMIZE_SINGLE, [ $this, 'optimize_single_image' ] );
96 + add_action( Async_Operation_Hook::OPTIMIZE_SINGLE, [ $this, 'optimize_single_image' ], 3 );
56 97 add_action( Async_Operation_Hook::REOPTIMIZE_SINGLE, [ $this, 'reoptimize_single_image' ] );
57 98 }
58 99 }