| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Oauth\Rest; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Oauth\{ |
| 6 |
Classes\Route_Base, |
| 7 |
Components\Connect, |
| 8 |
}; |
| 9 |
|
| 10 |
use Throwable; |
| 11 |
use WP_REST_Request; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
class Get_Subscriptions extends Route_Base { |
| 18 |
const NONCE_NAME = 'image-optimization-get-subscription'; |
| 19 |
|
| 20 |
protected string $path = 'get-subscriptions'; |
| 21 |
|
| 22 |
public function get_name(): string { |
| 23 |
return 'get_subscriptions'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_methods(): array { |
| 27 |
return [ 'POST' ]; |
| 28 |
} |
| 29 |
|
| 30 |
public function POST( WP_REST_Request $request ) { |
| 31 |
$this->verify_nonce_and_capability( |
| 32 |
$request->get_param( self::NONCE_NAME ), |
| 33 |
self::NONCE_NAME |
| 34 |
); |
| 35 |
|
| 36 |
if ( ! Connect::is_connected() ) { |
| 37 |
return $this->respond_error_json( [ |
| 38 |
'message' => esc_html__( 'Please connect first', 'image-optimization' ), |
| 39 |
'code' => 'forbidden', |
| 40 |
] ); |
| 41 |
} |
| 42 |
|
| 43 |
try { |
| 44 |
$subscriptions = Connect::get_subscriptions(); |
| 45 |
|
| 46 |
return $this->respond_success_json( $subscriptions ); |
| 47 |
} catch ( Throwable $t ) { |
| 48 |
return $this->respond_error_json( [ |
| 49 |
'message' => $t->getMessage(), |
| 50 |
'code' => 'internal_server_error', |
| 51 |
] ); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|