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