| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimizer\Modules\Oauth\Rest; |
| 4 |
|
| 5 |
use ImageOptimizer\Classes\Utils; |
| 6 |
use ImageOptimizer\Modules\Oauth\Classes\Route_Base; |
| 7 |
use ImageOptimizer\Modules\Oauth\Components\Connect; |
| 8 |
use WP_REST_Request; |
| 9 |
|
| 10 |
class GetSubscriptions extends Route_Base { |
| 11 |
const NONCE_NAME = 'image-optimizer-get-subscription'; |
| 12 |
protected string $path = 'get-subscriptions'; |
| 13 |
|
| 14 |
public function get_name(): string { |
| 15 |
return 'get_subscriptions'; |
| 16 |
} |
| 17 |
|
| 18 |
public function get_methods(): array { |
| 19 |
return [ 'POST' ]; |
| 20 |
} |
| 21 |
|
| 22 |
public function POST( WP_REST_Request $request ) { |
| 23 |
$this->verify_nonce_and_capability( |
| 24 |
$request->get_param( self::NONCE_NAME ), |
| 25 |
self::NONCE_NAME |
| 26 |
); |
| 27 |
|
| 28 |
if ( ! Connect::is_connected() ) { |
| 29 |
return $this->respond_error_json( [ |
| 30 |
'message' => esc_html__( 'Please connect first', 'image-optimizer' ), |
| 31 |
'code' => 'forbidden', |
| 32 |
] ); |
| 33 |
} |
| 34 |
|
| 35 |
$response = Utils::get_api_client()->make_request( |
| 36 |
'POST', |
| 37 |
'activation/get-subscriptions' |
| 38 |
); |
| 39 |
|
| 40 |
if ( is_wp_error( $response ) ) { |
| 41 |
return $this->respond_error_json( [ |
| 42 |
'message' => $response->get_error_message(), |
| 43 |
'code' => 'internal_server_error', |
| 44 |
] ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! isset( $response->subscriptions ) ) { |
| 48 |
return $this->respond_error_json( [ |
| 49 |
'message' => esc_html__( 'Invalid response from server', 'image-optimizer' ), |
| 50 |
'code' => 'internal_server_error', |
| 51 |
] ); |
| 52 |
} |
| 53 |
|
| 54 |
return $this->respond_success_json( $response->subscriptions ); |
| 55 |
} |
| 56 |
} |
| 57 |
|