| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Script\Rest; |
| 4 |
|
| 5 |
use Cookiez\Classes\Rest\Sanitizer; |
| 6 |
use Cookiez\Modules\Script\Classes\Route_Base; |
| 7 |
use Cookiez\Modules\Script\Components\Script; |
| 8 |
|
| 9 |
use RuntimeException; |
| 10 |
use Throwable; |
| 11 |
use WP_REST_Response; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Delete_Script |
| 19 |
* REST endpoint for deleting a managed script |
| 20 |
*/ |
| 21 |
class Delete_Script extends Route_Base { |
| 22 |
public string $path = '(?P<id>\d+)'; |
| 23 |
|
| 24 |
public function get_methods(): array { |
| 25 |
return [ 'DELETE' ]; |
| 26 |
} |
| 27 |
|
| 28 |
public function get_name(): string { |
| 29 |
return 'delete-script'; |
| 30 |
} |
| 31 |
|
| 32 |
protected function sanitize_fields(): array { |
| 33 |
return [ |
| 34 |
'id' => Sanitizer::absint(), |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public function DELETE(): WP_REST_Response { |
| 39 |
try { |
| 40 |
$error = $this->verify_capability(); |
| 41 |
|
| 42 |
if ( $error ) { |
| 43 |
return $error; |
| 44 |
} |
| 45 |
|
| 46 |
$script_id = (int) $this->params['id']; |
| 47 |
|
| 48 |
try { |
| 49 |
( new Script() )->delete( $script_id ); |
| 50 |
} catch ( RuntimeException $e ) { |
| 51 |
if ( 'script_not_found' === $e->getMessage() ) { |
| 52 |
return $this->respond_error_json( [ |
| 53 |
'message' => esc_html__( 'Script not found', 'cookiez' ), |
| 54 |
'code' => 'script_not_found', |
| 55 |
], 404 ); |
| 56 |
} |
| 57 |
|
| 58 |
throw $e; |
| 59 |
} |
| 60 |
|
| 61 |
return $this->respond_success_json( [ |
| 62 |
'message' => esc_html__( 'Script deleted successfully', 'cookiez' ), |
| 63 |
'script_id' => $script_id, |
| 64 |
] ); |
| 65 |
|
| 66 |
} catch ( Throwable $t ) { |
| 67 |
return $this->respond_error_json( [ |
| 68 |
'message' => $t->getMessage(), |
| 69 |
'code' => 'internal_server_error', |
| 70 |
], 500 ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|