| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimizer\Modules\Oauth\Rest; |
| 4 |
|
| 5 |
use ImageOptimizer\Modules\Oauth\Classes\{ |
| 6 |
Route_Base, |
| 7 |
Data |
| 8 |
}; |
| 9 |
use ImageOptimizer\Modules\Oauth\Components\Connect; |
| 10 |
use WP_REST_Request; |
| 11 |
|
| 12 |
class ConnectInit extends Route_Base { |
| 13 |
const NONCE_NAME = 'image-optimizer-connect'; |
| 14 |
protected string $path = 'init'; |
| 15 |
|
| 16 |
public function get_name(): string { |
| 17 |
return 'connect-init'; |
| 18 |
} |
| 19 |
|
| 20 |
public function get_methods(): array { |
| 21 |
return [ 'GET' ]; |
| 22 |
} |
| 23 |
|
| 24 |
public function get( WP_REST_Request $request ) { |
| 25 |
$this->verify_nonce_and_capability( |
| 26 |
$request->get_param( self::NONCE_NAME ), |
| 27 |
self::NONCE_NAME |
| 28 |
); |
| 29 |
|
| 30 |
if ( Connect::is_connected() ) { |
| 31 |
return $this->respond_error_json( [ |
| 32 |
'message' => esc_html__( 'You are already connected', 'image-optimizer' ), |
| 33 |
'code' => 'forbidden', |
| 34 |
] ); |
| 35 |
} |
| 36 |
|
| 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 |
); |
| 50 |
|
| 51 |
if ( is_wp_error( $response ) ) { |
| 52 |
return $this->respond_error_json( [ |
| 53 |
'message' => $response->get_error_message(), |
| 54 |
'code' => 'internal_server_error', |
| 55 |
] ); |
| 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 |
} |
| 89 |
} |
| 90 |
|