| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Optimization\Rest; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Async_Operation\Exceptions\Async_Operation_Exception; |
| 6 |
use ImageOptimization\Modules\Optimization\Classes\{ |
| 7 |
Bulk_Optimization_Controller, |
| 8 |
Route_Base, |
| 9 |
}; |
| 10 |
use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception; |
| 11 |
use ImageOptimization\Modules\Oauth\Classes\Exceptions\Quota_Exceeded_Error; |
| 12 |
use ImageOptimization\Modules\Oauth\Components\Connect; |
| 13 |
use Throwable; |
| 14 |
use WP_REST_Request; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
} |
| 19 |
|
| 20 |
class Optimize_Bulk extends Route_Base { |
| 21 |
const NONCE_NAME = 'image-optimization-optimize-bulk'; |
| 22 |
|
| 23 |
protected string $path = 'bulk'; |
| 24 |
|
| 25 |
public function get_name(): string { |
| 26 |
return 'optimize-bulk'; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_methods(): array { |
| 30 |
return [ 'POST' ]; |
| 31 |
} |
| 32 |
|
| 33 |
public function POST( WP_REST_Request $request ) { |
| 34 |
$this->verify_nonce_and_capability( |
| 35 |
$request->get_param( self::NONCE_NAME ), |
| 36 |
self::NONCE_NAME |
| 37 |
); |
| 38 |
|
| 39 |
if ( ! Connect::is_activated() ) { |
| 40 |
return $this->respond_error_json([ |
| 41 |
'message' => esc_html__( 'Invalid activation', 'image-optimization' ), |
| 42 |
'code' => 'unauthorized', |
| 43 |
]); |
| 44 |
} |
| 45 |
|
| 46 |
$is_reoptimize = (bool) $request->get_param( 'reoptimize' ); |
| 47 |
|
| 48 |
try { |
| 49 |
if ( $is_reoptimize ) { |
| 50 |
return $this->handle_bulk_reoptimization(); |
| 51 |
} else { |
| 52 |
return $this->handle_bulk_optimization(); |
| 53 |
} |
| 54 |
} catch ( Throwable $t ) { |
| 55 |
return $this->respond_error_json([ |
| 56 |
'message' => $t->getMessage(), |
| 57 |
'code' => 'internal_server_error', |
| 58 |
]); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @return \WP_Error|\WP_REST_Response |
| 64 |
* @throws Async_Operation_Exception|Invalid_Image_Exception|Quota_Exceeded_Error |
| 65 |
*/ |
| 66 |
private function handle_bulk_optimization() { |
| 67 |
$is_in_progress = Bulk_Optimization_Controller::is_optimization_in_progress(); |
| 68 |
|
| 69 |
if ( $is_in_progress ) { |
| 70 |
return $this->respond_error_json( [ |
| 71 |
'message' => esc_html__( 'Bulk optimization is already in progress', 'image-optimization' ), |
| 72 |
'code' => 'forbidden', |
| 73 |
] ); |
| 74 |
} |
| 75 |
|
| 76 |
Bulk_Optimization_Controller::find_images_and_schedule_optimization(); |
| 77 |
|
| 78 |
return $this->respond_success_json(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @throws Async_Operation_Exception|Quota_Exceeded_Error |
| 83 |
*/ |
| 84 |
private function handle_bulk_reoptimization() { |
| 85 |
$is_in_progress = Bulk_Optimization_Controller::is_reoptimization_in_progress(); |
| 86 |
|
| 87 |
if ( $is_in_progress ) { |
| 88 |
return $this->respond_error_json( [ |
| 89 |
'message' => esc_html__( 'Bulk re-optimization is already in progress', 'image-optimization' ), |
| 90 |
'code' => 'forbidden', |
| 91 |
] ); |
| 92 |
} |
| 93 |
|
| 94 |
Bulk_Optimization_Controller::find_optimized_images_and_schedule_reoptimization(); |
| 95 |
|
| 96 |
return $this->respond_success_json(); |
| 97 |
} |
| 98 |
} |
| 99 |
|