| @@ -1,17 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace ImageOptimizer\Modules\Oauth\Rest; | |
| 3 | +namespace ImageOptimization\Modules\Oauth\Rest; | |
| 4 | 4 | |
| 5 | -use ImageOptimizer\Modules\Oauth\Classes\{ | |
| 6 | - Route_Base, | |
| 7 | - Data | |
| 5 | +use ImageOptimization\Modules\Oauth\{ | |
| 6 | + Classes\Route_Base, | |
| 7 | + Components\Connect, | |
| 8 | 8 | }; |
| 9 | -use ImageOptimizer\Modules\Oauth\Components\Connect; | |
| 9 | + | |
| 10 | +use Throwable; | |
| 10 | 11 | use WP_REST_Request; |
| 11 | 12 | |
| 12 | -class ConnectInit extends Route_Base { | |
| 13 | - const NONCE_NAME = 'image-optimizer-connect'; | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit; // Exit if accessed directly. | |
| 15 | +} | |
| 16 | + | |
| 17 | +class Connect_Init extends Route_Base { | |
| 18 | + const NONCE_NAME = 'image-optimization-connect'; | |
| 19 | + | |
| 14 | 20 | protected string $path = 'init'; |
| 15 | 21 | |
| 16 | 22 | public function get_name(): string { |
| 17 | 23 | return 'connect-init'; |
| @@ -21,69 +27,32 @@ | ||
| 21 | 27 | return [ 'GET' ]; |
| 22 | 28 | } |
| 23 | 29 | |
| 24 | 30 | public function get( WP_REST_Request $request ) { |
| 25 | - $this->verify_nonce_and_capability( | |
| 31 | + $error = $this->verify_nonce_and_capability( | |
| 26 | 32 | $request->get_param( self::NONCE_NAME ), |
| 27 | 33 | self::NONCE_NAME |
| 28 | 34 | ); |
| 29 | 35 | |
| 36 | + if ( $error ) { | |
| 37 | + return $error; | |
| 38 | + } | |
| 39 | + | |
| 30 | 40 | if ( Connect::is_connected() ) { |
| 31 | 41 | return $this->respond_error_json( [ |
| 32 | - 'message' => esc_html__( 'You are already connected', 'image-optimizer' ), | |
| 42 | + 'message' => esc_html__( 'You are already connected', 'image-optimization' ), | |
| 33 | 43 | 'code' => 'forbidden', |
| 34 | 44 | ] ); |
| 35 | 45 | } |
| 36 | 46 | |
| 37 | - $response = wp_remote_request( | |
| 38 | - Connect::API_URL . '/library/get_client_id', | |
| 39 | - [ | |
| 40 | - 'method' => 'POST', | |
| 41 | - 'body' => [ | |
| 42 | - 'local_id' => (int) get_current_user_id(), | |
| 43 | - 'site_key' => Data::get_site_key(), | |
| 44 | - 'app' => 'library', | |
| 45 | - 'home_url' => trailingslashit( home_url() ), | |
| 46 | - 'source' => 'image-optimizer' | |
| 47 | - ] | |
| 48 | - ] | |
| 49 | - ); | |
| 47 | + try { | |
| 48 | + $connect_url = Connect::initialize_connect(); | |
| 50 | 49 | |
| 51 | - if ( is_wp_error( $response ) ) { | |
| 50 | + return $this->respond_success_json( $connect_url ); | |
| 51 | + } catch ( Throwable $t ) { | |
| 52 | 52 | return $this->respond_error_json( [ |
| 53 | - 'message' => $response->get_error_message(), | |
| 53 | + 'message' => $t->getMessage(), | |
| 54 | 54 | 'code' => 'internal_server_error', |
| 55 | 55 | ] ); |
| 56 | 56 | } |
| 57 | - | |
| 58 | - $data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 59 | - | |
| 60 | - if ( ! isset( $data->client_id ) || ! isset( $data->auth_secret ) ) { | |
| 61 | - return $this->respond_error_json( [ | |
| 62 | - 'message' => esc_html__( 'Invalid response from server' ), | |
| 63 | - 'code' => 'internal_server_error', | |
| 64 | - ] ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - Data::set_client_data( $data->client_id, $data->auth_secret ); | |
| 68 | - | |
| 69 | - return $this->respond_success_json(add_query_arg( [ | |
| 70 | - 'utm_source' => 'image-optimizer-panel', | |
| 71 | - 'utm_campaign' => 'image-optimizer', | |
| 72 | - 'utm_medium' => 'wp-dash', | |
| 73 | - 'source' => 'generic', | |
| 74 | - 'action' => 'authorize', | |
| 75 | - 'response_type' => 'code', | |
| 76 | - 'client_id' => $data->client_id, | |
| 77 | - 'auth_secret' => $data->auth_secret, | |
| 78 | - 'state' => Data::get_connect_state( true ), | |
| 79 | - 'redirect_uri' => rawurlencode( add_query_arg( [ | |
| 80 | - 'page' => 'elementor-connect', | |
| 81 | - 'app' => 'library', | |
| 82 | - 'action' => 'get_token', | |
| 83 | - 'nonce' => wp_create_nonce( 'nonce_action' . 'get_token' ), | |
| 84 | - ], admin_url( 'admin.php' ) ) ), | |
| 85 | - 'may_share_data' => 0, | |
| 86 | - 'reconnect_nonce' => wp_create_nonce( 'nonce_action' . 'reconnect' ), | |
| 87 | - ], self::SITE_URL . 'library' )); | |
| 88 | 57 | } |
| 89 | 58 | } |