PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / trunk
Image Optimizer – Compress Images and Convert to WebP or AVIF vtrunk
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
← All changes | modules/oauth/rest/connect-init.php +25 -53 1.0.0 → trunk View file →
@@ -1,14 +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\{Route_Base, Data};
6 -use ImageOptimizer\Modules\Oauth\Components\Connect;
5 +use ImageOptimization\Modules\Oauth\{
6 + Classes\Route_Base,
7 + Components\Connect,
8 +};
9 +
10 +use Throwable;
7 11 use WP_REST_Request;
8 12
9 -class ConnectInit extends Route_Base {
10 - 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 +
11 20 protected string $path = 'init';
12 21
13 22 public function get_name(): string {
14 23 return 'connect-init';
@@ -18,69 +27,32 @@
18 27 return [ 'GET' ];
19 28 }
20 29
21 30 public function get( WP_REST_Request $request ) {
22 - $this->verify_nonce_and_capability(
31 + $error = $this->verify_nonce_and_capability(
23 32 $request->get_param( self::NONCE_NAME ),
24 33 self::NONCE_NAME
25 34 );
26 35
36 + if ( $error ) {
37 + return $error;
38 + }
39 +
27 40 if ( Connect::is_connected() ) {
28 41 return $this->respond_error_json( [
29 - 'message' => esc_html__( 'You are already connected', 'image-optimizer' ),
42 + 'message' => esc_html__( 'You are already connected', 'image-optimization' ),
30 43 'code' => 'forbidden',
31 44 ] );
32 45 }
33 46
34 - $response = wp_remote_request(
35 - Connect::API_URL . '/library/get_client_id',
36 - [
37 - 'method' => 'POST',
38 - 'body' => [
39 - 'local_id' => (int) get_current_user_id(),
40 - 'site_key' => Data::get_site_key(),
41 - 'app' => 'library',
42 - 'home_url' => trailingslashit( home_url() ),
43 - 'source' => 'image-optimizer'
44 - ]
45 - ]
46 - );
47 + try {
48 + $connect_url = Connect::initialize_connect();
47 49
48 - if ( is_wp_error( $response ) ) {
50 + return $this->respond_success_json( $connect_url );
51 + } catch ( Throwable $t ) {
49 52 return $this->respond_error_json( [
50 - 'message' => $response->get_error_message(),
53 + 'message' => $t->getMessage(),
51 54 'code' => 'internal_server_error',
52 55 ] );
53 56 }
54 -
55 - $data = json_decode( wp_remote_retrieve_body( $response ) );
56 -
57 - if ( ! isset( $data->client_id ) || ! isset( $data->auth_secret ) ) {
58 - return $this->respond_error_json( [
59 - 'message' => esc_html__( 'Invalid response from server' ),
60 - 'code' => 'internal_server_error',
61 - ] );
62 - }
63 -
64 - Data::set_client_data( $data->client_id, $data->auth_secret );
65 -
66 - return $this->respond_success_json(add_query_arg( [
67 - 'utm_source' => 'image-optimizer-panel',
68 - 'utm_campaign' => 'image-optimizer',
69 - 'utm_medium' => 'wp-dash',
70 - 'source' => 'generic',
71 - 'action' => 'authorize',
72 - 'response_type' => 'code',
73 - 'client_id' => $data->client_id,
74 - 'auth_secret' => $data->auth_secret,
75 - 'state' => Data::get_connect_state( true ),
76 - 'redirect_uri' => rawurlencode( add_query_arg( [
77 - 'page' => 'elementor-connect',
78 - 'app' => 'library',
79 - 'action' => 'get_token',
80 - 'nonce' => wp_create_nonce( 'nonce_action' . 'get_token' ),
81 - ], admin_url( 'admin.php' ) ) ),
82 - 'may_share_data' => 0,
83 - 'reconnect_nonce' => wp_create_nonce( 'nonce_action' . 'reconnect' ),
84 - ], self::SITE_URL . 'library' ));
85 57 }
86 58 }