PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.1.0
Image Optimization – Compress Images and Convert to WebP or AVIF v1.1.0
1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 All 32 releases
image-optimization / modules / optimization / rest / optimize-bulk.php

optimize-bulk.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.1.0, at modules/optimization/rest/optimize-bulk.php

99 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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