PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
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.6.6, at modules/optimization/rest/optimize-bulk.php

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