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