| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimizer\Modules\Oauth\Components; |
| 4 |
|
| 5 |
use ImageOptimizer\Classes\Logger; |
| 6 |
use ImageOptimizer\Classes\Utils; |
| 7 |
use ImageOptimizer\Modules\Oauth\Classes\Data; |
| 8 |
use ImageOptimizer\Modules\Settings\Module as Settings_Module; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Connect |
| 16 |
*/ |
| 17 |
class Connect { |
| 18 |
const API_URL = 'https://my.elementor.com/api/connect/v1'; |
| 19 |
|
| 20 |
/** |
| 21 |
* is_connected |
| 22 |
* @return bool |
| 23 |
*/ |
| 24 |
public static function is_connected(): bool { |
| 25 |
return ! empty( Data::get_connect_data()['access_token'] ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* is_activated |
| 30 |
* @return bool |
| 31 |
*/ |
| 32 |
public static function is_activated(): bool { |
| 33 |
return ! empty( Data::get_activation_state() ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* maybe_handle_admin_connect_page |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public static function maybe_handle_admin_connect_page(): bool { |
| 41 |
if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'nonce_actionget_token' ) ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
$args = [ |
| 46 |
'page' => 'elementor-connect', |
| 47 |
'app' => 'library', |
| 48 |
'action' => 'get_token', |
| 49 |
'state' => Data::get_connect_state(), |
| 50 |
]; |
| 51 |
|
| 52 |
foreach ( $args as $key => $value ) { |
| 53 |
if ( ! isset( $_GET[ $key ] ) || $_GET[ $key ] !== $value ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! isset( $_GET['nonce'] ) || ! isset( $_GET['code'] ) ) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* handle_elementor_connect_admin |
| 67 |
*/ |
| 68 |
public function handle_elementor_connect_admin(): void { |
| 69 |
// validate args |
| 70 |
if ( ! self::maybe_handle_admin_connect_page() ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// validate nonce |
| 75 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'nonce_actionget_token' ) ) { |
| 76 |
wp_die( 'Nonce verification failed', 'image-optimizer' ); |
| 77 |
} |
| 78 |
|
| 79 |
$token_response = wp_remote_request( self::API_URL . '/get_token', [ |
| 80 |
'method' => 'POST', |
| 81 |
'body' => [ |
| 82 |
'app' => 'library', |
| 83 |
'grant_type' => 'authorization_code', |
| 84 |
'client_id' => Data::get_client_id(), |
| 85 |
'code' => sanitize_text_field( $_GET['code'] ), |
| 86 |
], |
| 87 |
] ); |
| 88 |
|
| 89 |
if ( is_wp_error( $token_response ) ) { |
| 90 |
wp_die( $token_response->get_error_message(), 'image-optimizer' ); |
| 91 |
} |
| 92 |
|
| 93 |
$data = json_decode( wp_remote_retrieve_body( $token_response ), true ); |
| 94 |
Data::set_connect_data( $data ); |
| 95 |
|
| 96 |
do_action( Checkpoint::ON_CONNECT ); |
| 97 |
|
| 98 |
// cleanup |
| 99 |
Data::delete_connect_state(); |
| 100 |
|
| 101 |
wp_redirect( add_query_arg( [ |
| 102 |
'page' => Settings_Module::SETTING_BASE_SLUG, |
| 103 |
'connected' => 'true', |
| 104 |
], admin_url( 'admin.php' ) ) ); |
| 105 |
die(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* disconnect |
| 110 |
*/ |
| 111 |
public static function disconnect() { |
| 112 |
$response = wp_remote_request( self::API_URL . '/disconnect', [ |
| 113 |
'method' => 'POST', |
| 114 |
'body' => [ |
| 115 |
'app' => 'library', |
| 116 |
'home_url' => trailingslashit( home_url() ), |
| 117 |
'client_id' => Data::get_client_id(), |
| 118 |
'access_token' => Data::get_access_token(), |
| 119 |
], |
| 120 |
] ); |
| 121 |
|
| 122 |
if ( is_wp_error( $response ) ) { |
| 123 |
wp_die( $response->get_error_message(), 'image-optimizer' ); |
| 124 |
} |
| 125 |
|
| 126 |
Data::reset(); |
| 127 |
do_action( Checkpoint::ON_DISCONNECT ); |
| 128 |
} |
| 129 |
|
| 130 |
public static function check_connect_status() { |
| 131 |
if ( ! self::is_connected() ) { |
| 132 |
Logger::log( Logger::LEVEL_INFO, 'Status check error. Reason: User is not connected' ); |
| 133 |
|
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
$response = Utils::get_api_client()->make_request( |
| 138 |
'POST', |
| 139 |
'status/check' |
| 140 |
); |
| 141 |
|
| 142 |
if ( is_wp_error( $response ) ) { |
| 143 |
Logger::log( |
| 144 |
Logger::LEVEL_ERROR, |
| 145 |
'Status check error. Reason: ' . $response->get_error_message() |
| 146 |
); |
| 147 |
|
| 148 |
return null; |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! isset( $response->status ) ) { |
| 152 |
Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' ); |
| 153 |
|
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
return $response; |
| 158 |
} |
| 159 |
|
| 160 |
public static function get_connect_route_url( $endpoint ): string { |
| 161 |
return rest_url( 'image-optimizer/v1/connect/' . $endpoint ); |
| 162 |
} |
| 163 |
|
| 164 |
public function __construct() { |
| 165 |
// handle connect if elementor is active |
| 166 |
add_action( 'load-elementor_page_elementor-connect', [ $this, 'handle_elementor_connect_admin' ], 9 ); |
| 167 |
// handle connect if elementor is not active |
| 168 |
add_action( '_admin_menu', [ $this, 'handle_elementor_connect_admin' ] ); |
| 169 |
} |
| 170 |
} |
| 171 |
|