| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Script\Rest; |
| 4 |
|
| 5 |
use Cookiez\Modules\Script\Classes\Route_Base; |
| 6 |
use Cookiez\Modules\Script\Components\Script; |
| 7 |
|
| 8 |
use Throwable; |
| 9 |
use WP_REST_Response; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Get_Scripts |
| 17 |
* REST endpoint for retrieving all managed scripts |
| 18 |
*/ |
| 19 |
class Get_Scripts extends Route_Base { |
| 20 |
public string $path = ''; |
| 21 |
|
| 22 |
public function get_methods(): array { |
| 23 |
return [ 'GET' ]; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_name(): string { |
| 27 |
return 'get-scripts'; |
| 28 |
} |
| 29 |
|
| 30 |
public function GET(): WP_REST_Response { |
| 31 |
try { |
| 32 |
$error = $this->verify_capability(); |
| 33 |
|
| 34 |
if ( $error ) { |
| 35 |
return $error; |
| 36 |
} |
| 37 |
|
| 38 |
$scripts = ( new Script() )->list(); |
| 39 |
|
| 40 |
$formatted = array_map( fn( $dto ) => $dto->to_array(), $scripts ); |
| 41 |
|
| 42 |
return $this->respond_success_json( [ |
| 43 |
'scripts' => $formatted, |
| 44 |
] ); |
| 45 |
|
| 46 |
} catch ( Throwable $t ) { |
| 47 |
return $this->respond_error_json( [ |
| 48 |
'message' => $t->getMessage(), |
| 49 |
'code' => 'internal_server_error', |
| 50 |
], 500 ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|