| 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\{ |
| 16 |
Classes\Exceptions\Bulk_Token_Expired_Error, |
| 17 |
Classes\Exceptions\Image_File_Already_Exists_Error, |
| 18 |
Classes\Optimize_Image, |
| 19 |
Classes\Bulk_Optimization_Controller, |
| 20 |
Components\Exceptions\Bulk_Optimization_Token_Not_Found_Error, |
| 21 |
}; |
| 22 |
|
| 23 |
use Throwable; |
| 24 |
|
| 25 |
if ( ! defined( 'ABSPATH' ) ) { |
| 26 |
exit; // Exit if accessed directly. |
| 27 |
} |
| 28 |
|
| 29 |
class Bulk_Optimization { |
| 30 |
const BULK_OPTIMIZATION_BASE_SLUG = 'image-optimization-bulk-optimization'; |
| 31 |
const BULK_OPTIMIZATION_CAPABILITY = 'manage_options'; |
| 32 |
|
| 33 |
public function render_app() { |
| 34 |
?> |
| 35 |
<!-- The hack required to wrap WP notifications --> |
| 36 |
<div class="wrap"> |
| 37 |
<h1 style="display: none;" role="presentation"></h1> |
| 38 |
</div> |
| 39 |
|
| 40 |
<div id="image-optimization-app"></div> |
| 41 |
<?php |
| 42 |
} |
| 43 |
|
| 44 |
public function register_page() { |
| 45 |
add_media_page( |
| 46 |
__( 'Bulk Optimization', 'image-optimization' ), |
| 47 |
__( 'Bulk Optimization', 'image-optimization' ), |
| 48 |
self::BULK_OPTIMIZATION_CAPABILITY, |
| 49 |
self::BULK_OPTIMIZATION_BASE_SLUG, |
| 50 |
[ $this, 'render_app' ], |
| 51 |
7 |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** @async */ |
| 56 |
public function optimize_bulk( int $image_id, string $operation_id ) { |
| 57 |
try { |
| 58 |
$bulk_token = Bulk_Optimization_Controller::get_bulk_operation_token( $operation_id ); |
| 59 |
|
| 60 |
$oi = new Optimize_Image( |
| 61 |
$image_id, |
| 62 |
'bulk', |
| 63 |
$bulk_token |
| 64 |
); |
| 65 |
|
| 66 |
$oi->optimize(); |
| 67 |
} catch ( Quota_Exceeded_Error $qe ) { |
| 68 |
( new Image_Meta( $image_id ) ) |
| 69 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 70 |
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED ) |
| 71 |
->save(); |
| 72 |
} catch ( Image_File_Already_Exists_Error $fe ) { |
| 73 |
( new Image_Meta( $image_id ) ) |
| 74 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 75 |
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) |
| 76 |
->save(); |
| 77 |
} catch ( Bulk_Token_Expired_Error |Bulk_Optimization_Token_Not_Found_Error $bte ) { |
| 78 |
( new Image_Meta( $image_id ) ) |
| 79 |
->set_status( Image_Status::NOT_OPTIMIZED ) |
| 80 |
->save(); |
| 81 |
|
| 82 |
Bulk_Optimization_Controller::reschedule_bulk_optimization(); |
| 83 |
} catch ( Throwable $t ) { |
| 84 |
Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() ); |
| 85 |
|
| 86 |
( new Image_Meta( $image_id ) ) |
| 87 |
->set_status( Image_Status::OPTIMIZATION_FAILED ) |
| 88 |
->set_error_type( Image_Optimization_Error_Type::GENERIC ) |
| 89 |
->save(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** @async */ |
| 94 |
public function reoptimize_bulk( int $image_id, string $operation_id ) { |
| 95 |
try { |
| 96 |
$image = new Image( $image_id ); |
| 97 |
|
| 98 |
if ( $image->can_be_restored() ) { |
| 99 |
Image_Restore::restore( $image_id, true ); |
| 100 |
} |
| 101 |
|
| 102 |
$bulk_token = Bulk_Optimization_Controller::get_bulk_operation_token( $operation_id ); |
| 103 |
|
| 104 |
$oi = new Optimize_Image( |
| 105 |
$image_id, |
| 106 |
'bulk', |
| 107 |
$bulk_token |
| 108 |
); |
| 109 |
|
| 110 |
$oi->optimize(); |
| 111 |
} catch ( Quota_Exceeded_Error $qe ) { |
| 112 |
( new Image_Meta( $image_id ) ) |
| 113 |
->set_status( Image_Status::REOPTIMIZING_FAILED ) |
| 114 |
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED ) |
| 115 |
->save(); |
| 116 |
} catch ( Image_File_Already_Exists_Error $fe ) { |
| 117 |
( new Image_Meta( $image_id ) ) |
| 118 |
->set_status( Image_Status::REOPTIMIZING_FAILED ) |
| 119 |
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS ) |
| 120 |
->save(); |
| 121 |
} catch ( Bulk_Token_Expired_Error |Bulk_Optimization_Token_Not_Found_Error $bte ) { |
| 122 |
( new Image_Meta( $image_id ) ) |
| 123 |
->set_status( Image_Status::NOT_OPTIMIZED ) |
| 124 |
->save(); |
| 125 |
|
| 126 |
Bulk_Optimization_Controller::reschedule_bulk_reoptimization(); |
| 127 |
} catch ( Throwable $t ) { |
| 128 |
Logger::log( Logger::LEVEL_ERROR, 'Reoptimization error. Reason: ' . $t->getMessage() ); |
| 129 |
|
| 130 |
( new Image_Meta( $image_id ) ) |
| 131 |
->set_status( Image_Status::REOPTIMIZING_FAILED ) |
| 132 |
->set_error_type( Image_Optimization_Error_Type::GENERIC ) |
| 133 |
->save(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public function __construct() { |
| 138 |
add_action( 'admin_menu', [ $this, 'register_page' ] ); |
| 139 |
|
| 140 |
add_action( Async_Operation_Hook::OPTIMIZE_BULK, [ $this, 'optimize_bulk' ], 10, 2 ); |
| 141 |
add_action( Async_Operation_Hook::REOPTIMIZE_BULK, [ $this, 'reoptimize_bulk' ], 10, 2 ); |
| 142 |
} |
| 143 |
} |
| 144 |
|