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