| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Optimization\Rest; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Optimization\Classes\{ |
| 6 |
Bulk_Optimization_Controller, |
| 7 |
Route_Base, |
| 8 |
}; |
| 9 |
use ImageOptimization\Modules\Oauth\Components\Connect; |
| 10 |
use Throwable; |
| 11 |
use WP_REST_Request; |
| 12 |
use ImageOptimization\Plugin; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
class Cancel_Bulk_Optimization extends Route_Base { |
| 19 |
const NONCE_NAME = 'image-optimization-cancel-bulk-optimization'; |
| 20 |
|
| 21 |
protected string $path = 'bulk/cancel'; |
| 22 |
|
| 23 |
public function get_name(): string { |
| 24 |
return 'cancel-bulk-optimization'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_methods(): array { |
| 28 |
return [ 'POST' ]; |
| 29 |
} |
| 30 |
|
| 31 |
public function POST( WP_REST_Request $request ) { |
| 32 |
$this->verify_nonce_and_capability( |
| 33 |
$request->get_param( self::NONCE_NAME ), |
| 34 |
self::NONCE_NAME |
| 35 |
); |
| 36 |
|
| 37 |
if ( ! Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->is_activated() ) { |
| 38 |
return $this->respond_error_json([ |
| 39 |
'message' => esc_html__( 'Invalid activation', 'image-optimization' ), |
| 40 |
'code' => 'unauthorized', |
| 41 |
]); |
| 42 |
} |
| 43 |
|
| 44 |
try { |
| 45 |
$is_in_progress = Bulk_Optimization_Controller::is_optimization_in_progress(); |
| 46 |
|
| 47 |
if ( ! $is_in_progress ) { |
| 48 |
return $this->respond_error_json([ |
| 49 |
'message' => esc_html__( 'Bulk optimization is not in progress', 'image-optimization' ), |
| 50 |
'code' => 'forbidden', |
| 51 |
]); |
| 52 |
} |
| 53 |
|
| 54 |
Bulk_Optimization_Controller::delete_bulk_optimization(); |
| 55 |
|
| 56 |
return $this->respond_success_json(); |
| 57 |
} catch ( Throwable $t ) { |
| 58 |
return $this->respond_error_json([ |
| 59 |
'message' => $t->getMessage(), |
| 60 |
'code' => 'internal_server_error', |
| 61 |
]); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|