Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 62 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ImagesController | |
0.00% |
0 / 62 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| optimize_image | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Images\RestApi; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Performance\Images\ImageService; |
| 6 | use NewfoldLabs\WP\Module\Performance\Images\ImageSettings; |
| 7 | |
| 8 | /** |
| 9 | * Class ImagesController |
| 10 | * Provides REST API for single media item optimization. |
| 11 | */ |
| 12 | class ImagesController { |
| 13 | /** |
| 14 | * Dependency injection container. |
| 15 | * |
| 16 | * @var \NewfoldLabs\WP\Container\Container |
| 17 | */ |
| 18 | protected $container; |
| 19 | |
| 20 | /** |
| 21 | * The REST route namespace. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $namespace = 'newfold-performance/v1'; |
| 26 | |
| 27 | /** |
| 28 | * The REST route base. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $rest_base = '/images'; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | * |
| 37 | * @param \NewfoldLabs\WP\Container\Container $container Dependency injection container. |
| 38 | */ |
| 39 | public function __construct( $container ) { |
| 40 | $this->container = $container; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Registers API routes. |
| 45 | */ |
| 46 | public function register_routes() { |
| 47 | register_rest_route( |
| 48 | $this->namespace, |
| 49 | $this->rest_base . '/optimize', |
| 50 | array( |
| 51 | 'methods' => \WP_REST_Server::CREATABLE, |
| 52 | 'callback' => array( $this, 'optimize_image' ), |
| 53 | 'permission_callback' => function () { |
| 54 | return current_user_can( 'upload_files' ); |
| 55 | }, |
| 56 | 'args' => array( |
| 57 | 'media_id' => array( |
| 58 | 'required' => true, |
| 59 | 'type' => 'integer', |
| 60 | 'description' => __( 'The ID of the media item to optimize.', 'wp-module-performance' ), |
| 61 | 'validate_callback' => function ( $param ) { |
| 62 | return is_numeric( $param ) && $param > 0; |
| 63 | }, |
| 64 | ), |
| 65 | ), |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Optimizes a single media item. |
| 72 | * |
| 73 | * @param \WP_REST_Request $request The REST API request. |
| 74 | * @return \WP_REST_Response |
| 75 | */ |
| 76 | public function optimize_image( \WP_REST_Request $request ) { |
| 77 | $media_id = $request->get_param( 'media_id' ); |
| 78 | |
| 79 | $file_path = get_attached_file( $media_id ); |
| 80 | $image_url = wp_get_attachment_url( $media_id ); |
| 81 | |
| 82 | if ( empty( $file_path ) || empty( $image_url ) ) { |
| 83 | return new \WP_REST_Response( |
| 84 | array( |
| 85 | 'success' => false, |
| 86 | 'error' => __( 'Invalid media ID or media item not found.', 'wp-module-performance' ), |
| 87 | ), |
| 88 | 400 |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | $image_service = new ImageService( $this->container ); |
| 93 | $delete_original = ImageSettings::is_auto_delete_enabled(); |
| 94 | $optimized_result = $image_service->optimize_image( $image_url, $file_path ); |
| 95 | |
| 96 | if ( is_wp_error( $optimized_result ) ) { |
| 97 | return new \WP_REST_Response( |
| 98 | array( |
| 99 | 'success' => false, |
| 100 | 'error' => $optimized_result->get_error_message(), |
| 101 | ), |
| 102 | 500 |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | if ( $delete_original ) { |
| 107 | $response = $image_service->replace_original_with_webp( $media_id, $optimized_result ); |
| 108 | } else { |
| 109 | $response = $image_service->register_webp_as_new_media( $optimized_result ); |
| 110 | } |
| 111 | |
| 112 | if ( is_wp_error( $response ) ) { |
| 113 | return new \WP_REST_Response( |
| 114 | array( |
| 115 | 'success' => false, |
| 116 | 'error' => $response->get_error_message(), |
| 117 | ), |
| 118 | 500 |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | return new \WP_REST_Response( |
| 123 | array( |
| 124 | 'success' => true, |
| 125 | 'message' => __( 'Image successfully optimized.', 'wp-module-performance' ), |
| 126 | ), |
| 127 | 200 |
| 128 | ); |
| 129 | } |
| 130 | } |