| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Optimization\Components; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Async_Operation\{ |
| 6 |
Async_Operation, |
| 7 |
Async_Operation_Hook, |
| 8 |
Async_Operation_Queue, |
| 9 |
}; |
| 10 |
use ImageOptimization\Classes\Image\{ |
| 11 |
Image_Meta, |
| 12 |
Image_Optimization_Error_Type, |
| 13 |
Image_Status |
| 14 |
}; |
| 15 |
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; |
| 20 |
use ImageOptimization\Modules\Settings\Classes\Settings; |
| 21 |
use Throwable; |
| 22 |
|
| 23 |
if ( ! defined( 'ABSPATH' ) ) { |
| 24 |
exit; // Exit if accessed directly. |
| 25 |
} |
| 26 |
|
| 27 |
class Upload_Optimization { |
| 28 |
public function handle_upload( int $attachment_id ) { |
| 29 |
if ( ! Settings::get( Settings::OPTIMIZE_ON_UPLOAD_OPTION_NAME ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
if ( ! Connect::is_connected() || ! Connect::is_activated() ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$attachment_post = get_post( $attachment_id ); |
| 38 |
|
| 39 |
if ( ! wp_attachment_is_image( $attachment_post ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$meta = new Image_Meta( $attachment_id ); |
| 44 |
|
| 45 |
try { |
| 46 |
$meta |
| 47 |
->set_status( Image_Status::OPTIMIZATION_IN_PROGRESS ) |
| 48 |
->save(); |
| 49 |
|
| 50 |
Async_Operation::create( |
| 51 |
Async_Operation_Hook::OPTIMIZE_ON_UPLOAD, |
| 52 |
[ 'attachment_id' => $attachment_id ], |
| 53 |
Async_Operation_Queue::OPTIMIZE |
| 54 |
); |
| 55 |
} catch ( Throwable $t ) { |
| 56 |
$meta |
| 57 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 58 |
->save(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** @async */ |
| 63 |
public function optimize_image_on_upload( int $image_id ) { |
| 64 |
try { |
| 65 |
$oi = new Optimize_Image( |
| 66 |
$image_id, |
| 67 |
'upload', |
| 68 |
); |
| 69 |
|
| 70 |
$oi->optimize(); |
| 71 |
} catch ( Quota_Exceeded_Error $qe ) { |
| 72 |
( new Image_Meta( $image_id ) ) |
| 73 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 74 |
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED ) |
| 75 |
->save(); |
| 76 |
} catch ( Image_File_Already_Exists_Error $fe ) { |
| 77 |
( new Image_Meta( $image_id ) ) |
| 78 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 79 |
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) |
| 80 |
->save(); |
| 81 |
} catch ( Throwable $t ) { |
| 82 |
Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() ); |
| 83 |
|
| 84 |
( new Image_Meta( $image_id ) ) |
| 85 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 86 |
->set_error_type( Image_Optimization_Error_Type::GENERIC ) |
| 87 |
->save(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function __construct() { |
| 92 |
add_action( 'add_attachment', [ $this, 'handle_upload' ] ); |
| 93 |
add_action( Async_Operation_Hook::OPTIMIZE_ON_UPLOAD, [ $this, 'optimize_image_on_upload' ] ); |
| 94 |
} |
| 95 |
} |
| 96 |
|