| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Stats\Rest; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Stats\Classes\{ |
| 6 |
Optimization_Stats, |
| 7 |
Route_Base, |
| 8 |
}; |
| 9 |
use Throwable; |
| 10 |
use WP_REST_Request; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
class Get_Optimization_Details extends Route_Base { |
| 17 |
protected string $path = 'optimization-details'; |
| 18 |
|
| 19 |
public function get_name(): string { |
| 20 |
return 'get-optimization-details'; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_methods(): array { |
| 24 |
return [ 'GET' ]; |
| 25 |
} |
| 26 |
|
| 27 |
public function GET( WP_REST_Request $request ) { |
| 28 |
$error = $this->verify_capability(); |
| 29 |
|
| 30 |
if ( $error ) { |
| 31 |
return $error; |
| 32 |
} |
| 33 |
|
| 34 |
try { |
| 35 |
$image_id = $request->get_param( 'image_id' ); |
| 36 |
|
| 37 |
if ( empty( $image_id ) ) { |
| 38 |
return $this->respond_error_json([ |
| 39 |
'message' => 'Image ID is required', |
| 40 |
'code' => 'bad_request', |
| 41 |
]); |
| 42 |
} |
| 43 |
|
| 44 |
return $this->respond_success_json( Optimization_Stats::get_optimization_details( $image_id ) ); |
| 45 |
} catch ( Throwable $t ) { |
| 46 |
return $this->respond_error_json([ |
| 47 |
'message' => $t->getMessage(), |
| 48 |
'code' => 'internal_server_error', |
| 49 |
]); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|