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-single-image.php

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

71 lines 1.8 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\Image\Image;
6 use ImageOptimization\Modules\Oauth\Components\Connect;
7 use ImageOptimization\Modules\Optimization\Classes\{
8 Route_Base,
9 Single_Optimization,
10 Validate_Image,
11 };
12 use Throwable;
13 use WP_REST_Request;
14 use ImageOptimization\Plugin;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 class Optimize_Single_Image extends Route_Base {
21 const NONCE_NAME = 'image-optimization-optimize-image';
22 const IMAGE_ID_PARAM = 'imageId';
23
24 protected string $path = 'image';
25
26 public function get_name(): string {
27 return 'optimize-single-image';
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 $image_id = (int) $request->get_param( self::IMAGE_ID_PARAM );
49
50 if ( empty( $image_id ) ) {
51 return $this->respond_error_json([
52 'message' => esc_html__( 'Invalid image id', 'image-optimization' ),
53 'code' => 'bad_request',
54 ]);
55 }
56
57 try {
58 Validate_Image::is_valid( $image_id );
59
60 Single_Optimization::schedule_single_optimization( $image_id, $is_reoptimize );
61
62 return $this->respond_success_json();
63 } catch ( Throwable $t ) {
64 return $this->respond_error_json([
65 'message' => $t->getMessage(),
66 'code' => 'internal_server_error',
67 ]);
68 }
69 }
70 }
71