| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Connect\Classes; |
| 4 |
|
| 5 |
use ElementorOne\Connect\Exceptions\ServiceException; |
| 6 |
use ElementorOne\Connect\Facade; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Service |
| 14 |
*/ |
| 15 |
class Service { |
| 16 |
|
| 17 |
/** |
| 18 |
* Facade instance |
| 19 |
* @var Facade |
| 20 |
*/ |
| 21 |
protected Facade $facade; |
| 22 |
|
| 23 |
/** |
| 24 |
* Service constructor |
| 25 |
* @param Facade $facade |
| 26 |
*/ |
| 27 |
public function __construct( Facade $facade ) { |
| 28 |
$this->facade = $facade; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get app config |
| 33 |
* @param string|null $key |
| 34 |
* @return array|string |
| 35 |
*/ |
| 36 |
protected function get_app_config( ?string $key = null ) { |
| 37 |
return $this->facade->get_config( $key ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Registers new client and returns client ID |
| 42 |
* |
| 43 |
* @return string |
| 44 |
* @throws ServiceException |
| 45 |
*/ |
| 46 |
public function register_client(): string { |
| 47 |
$data = $this->facade->data(); |
| 48 |
$utils = $this->facade->utils(); |
| 49 |
|
| 50 |
$client_data = $this->request( $utils->get_clients_url(), [ |
| 51 |
'method' => 'POST', |
| 52 |
'headers' => [ |
| 53 |
'Content-Type' => 'application/json', |
| 54 |
], |
| 55 |
'body' => wp_json_encode( [ |
| 56 |
'redirect_uri' => $utils->get_admin_url(), |
| 57 |
'app_type' => $this->get_app_config( 'app_type' ), |
| 58 |
] ), |
| 59 |
], 201 ); |
| 60 |
|
| 61 |
$client_id = $client_data['client_id'] ?? null; |
| 62 |
$client_secret = $client_data['client_secret'] ?? null; |
| 63 |
|
| 64 |
$data->set_client_id( $client_id ); |
| 65 |
$data->set_client_secret( $client_secret ); |
| 66 |
$data->set_home_url(); |
| 67 |
|
| 68 |
return $client_id; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get client |
| 73 |
* @return array |
| 74 |
* @throws ServiceException |
| 75 |
*/ |
| 76 |
public function get_client(): array { |
| 77 |
$data = $this->facade->data(); |
| 78 |
$utils = $this->facade->utils(); |
| 79 |
$client_id = $data->get_client_id(); |
| 80 |
|
| 81 |
if ( ! $client_id ) { |
| 82 |
throw new ServiceException( 'Missing client ID' ); |
| 83 |
} |
| 84 |
|
| 85 |
[ 'access_token' => $access_token ] = $this->get_token( GrantTypes::CLIENT_CREDENTIALS, null, false ); |
| 86 |
|
| 87 |
if ( ! $access_token ) { |
| 88 |
throw new ServiceException( 'Missing client token' ); |
| 89 |
} |
| 90 |
|
| 91 |
return $this->request( $utils->get_clients_url( $client_id ), [ |
| 92 |
'method' => 'GET', |
| 93 |
'headers' => [ |
| 94 |
'Content-Type' => 'application/json', |
| 95 |
'Authorization' => "Bearer {$access_token}", |
| 96 |
], |
| 97 |
], 200 ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Deactivate license |
| 102 |
* |
| 103 |
* @return void |
| 104 |
* @throws ServiceException |
| 105 |
*/ |
| 106 |
public function deactivate_license(): void { |
| 107 |
$data = $this->facade->data(); |
| 108 |
$utils = $this->facade->utils(); |
| 109 |
$client_id = $data->get_client_id(); |
| 110 |
|
| 111 |
if ( ! $client_id ) { |
| 112 |
throw new ServiceException( 'Missing client ID' ); |
| 113 |
} |
| 114 |
|
| 115 |
[ 'access_token' => $access_token ] = $this->renew_access_token( GrantTypes::CLIENT_CREDENTIALS ); |
| 116 |
|
| 117 |
if ( ! $access_token ) { |
| 118 |
throw new ServiceException( 'Missing access token' ); |
| 119 |
} |
| 120 |
|
| 121 |
// Trigger the before deactivate event for all apps |
| 122 |
do_action( 'elementor_one/before_deactivate', $this->facade ); |
| 123 |
|
| 124 |
// Trigger the before deactivate event for the app prefix |
| 125 |
do_action( 'elementor_one/' . $this->get_app_config( 'app_prefix' ) . '_before_deactivate', $this->facade ); |
| 126 |
|
| 127 |
$this->request( $utils->get_deactivation_url( $client_id ), [ |
| 128 |
'method' => 'DELETE', |
| 129 |
'headers' => [ |
| 130 |
'Authorization' => "Bearer {$access_token}", |
| 131 |
], |
| 132 |
], 204 ); |
| 133 |
|
| 134 |
$data->set_connect_mode_data( $data::ACCESS_TOKEN, null ); |
| 135 |
|
| 136 |
// Trigger the deactivated event for all apps |
| 137 |
do_action( 'elementor_one/deactivated', $this->facade ); |
| 138 |
|
| 139 |
// Trigger the deactivated event for the app prefix |
| 140 |
do_action( 'elementor_one/' . $this->get_app_config( 'app_prefix' ) . '_deactivated', $this->facade ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Disconnect session |
| 145 |
* @return void |
| 146 |
* @throws ServiceException |
| 147 |
*/ |
| 148 |
public function disconnect(): void { |
| 149 |
$data = $this->facade->data(); |
| 150 |
$utils = $this->facade->utils(); |
| 151 |
|
| 152 |
[ 'access_token' => $access_token ] = $this->renew_access_token( GrantTypes::REFRESH_TOKEN ); |
| 153 |
|
| 154 |
if ( ! $access_token ) { |
| 155 |
throw new ServiceException( 'Missing access token' ); |
| 156 |
} |
| 157 |
|
| 158 |
// Trigger the before disconnect event for all apps |
| 159 |
do_action( 'elementor_one/before_disconnect', $this->facade ); |
| 160 |
|
| 161 |
// Trigger the before disconnect event for the app prefix |
| 162 |
do_action( 'elementor_one/' . $this->get_app_config( 'app_prefix' ) . '_before_disconnect', $this->facade ); |
| 163 |
|
| 164 |
$this->request( $utils->get_sessions_url(), [ |
| 165 |
'method' => 'DELETE', |
| 166 |
'headers' => [ |
| 167 |
'Content-Type' => 'application/json', |
| 168 |
'Authorization' => "Bearer {$access_token}", |
| 169 |
], |
| 170 |
], 204 ); |
| 171 |
|
| 172 |
$data->clear_session(); |
| 173 |
|
| 174 |
// Trigger the disconnected event for all apps |
| 175 |
do_action( 'elementor_one/disconnected', $this->facade ); |
| 176 |
|
| 177 |
// Trigger the disconnected event for the app prefix |
| 178 |
do_action( 'elementor_one/' . $this->get_app_config( 'app_prefix' ) . '_disconnected', $this->facade ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get token |
| 183 |
* @param string $grant_type |
| 184 |
* @param string|null $credential |
| 185 |
* @param bool|null $update |
| 186 |
* @return array |
| 187 |
* @throws ServiceException |
| 188 |
*/ |
| 189 |
public function get_token( string $grant_type, ?string $credential = null, ?bool $update = true ): array { |
| 190 |
$data = $this->facade->data(); |
| 191 |
$utils = $this->facade->utils(); |
| 192 |
|
| 193 |
$client_id = $data->get_client_id(); |
| 194 |
$client_secret = $data->get_client_secret(); |
| 195 |
|
| 196 |
if ( empty( $client_id ) || empty( $client_secret ) ) { |
| 197 |
throw new ServiceException( 'Missing client ID or secret' ); |
| 198 |
} |
| 199 |
|
| 200 |
$body = [ |
| 201 |
'grant_type' => $grant_type, |
| 202 |
]; |
| 203 |
|
| 204 |
switch ( $grant_type ) { |
| 205 |
case GrantTypes::AUTHORIZATION_CODE: |
| 206 |
$body['code'] = $credential; |
| 207 |
$body['redirect_uri'] = $utils->get_admin_url(); |
| 208 |
break; |
| 209 |
case GrantTypes::REFRESH_TOKEN: |
| 210 |
$body['refresh_token'] = $credential; |
| 211 |
break; |
| 212 |
case GrantTypes::CLIENT_CREDENTIALS: |
| 213 |
break; |
| 214 |
default: |
| 215 |
throw new ServiceException( 'Invalid grant type' ); |
| 216 |
} |
| 217 |
|
| 218 |
$response = $this->request( $utils->get_token_url(), [ |
| 219 |
'method' => 'POST', |
| 220 |
'headers' => [ |
| 221 |
'Authorization' => 'Basic ' . base64_encode( "{$client_id}:{$client_secret}" ), |
| 222 |
], |
| 223 |
'body' => $body, |
| 224 |
] ); |
| 225 |
|
| 226 |
if ( $update ) { |
| 227 |
$data->set_connect_mode_data( $data::ACCESS_TOKEN, $response['access_token'] ?? null ); |
| 228 |
if ( GrantTypes::CLIENT_CREDENTIALS !== $grant_type ) { |
| 229 |
$data->set_connect_mode_data( $data::TOKEN_ID, $response['id_token'] ?? null ); |
| 230 |
$data->set_connect_mode_data( $data::REFRESH_TOKEN, $response['refresh_token'] ); |
| 231 |
$data->set_connect_mode_data( $data::USER_ACCESS_TOKEN, $response['access_token'] ?? null ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return $response; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Request |
| 240 |
* @param string $url |
| 241 |
* @param array $args |
| 242 |
* @param int $valid_response_code |
| 243 |
* @return array|null |
| 244 |
* @throws ServiceException |
| 245 |
*/ |
| 246 |
public function request( string $url, array $args, int $valid_response_code = 200 ): ?array { |
| 247 |
$args['timeout'] = 30; |
| 248 |
$args['headers'] = array_replace_recursive( [ |
| 249 |
'x-elementor-app-type' => $this->get_app_config( 'app_type' ), |
| 250 |
], $args['headers'] ?? [] ); |
| 251 |
|
| 252 |
$response = wp_remote_request( $url, $args ); |
| 253 |
|
| 254 |
if ( is_wp_error( $response ) ) { |
| 255 |
$this->facade->logger()->error( $response->get_error_message() ); |
| 256 |
|
| 257 |
throw new ServiceException( $response->get_error_message() ); |
| 258 |
} |
| 259 |
|
| 260 |
$response_body = wp_remote_retrieve_body( $response ); |
| 261 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 262 |
|
| 263 |
if ( $response_code !== $valid_response_code ) { |
| 264 |
$this->facade->logger()->error( 'Invalid status code ' . $response_code ); |
| 265 |
|
| 266 |
throw new ServiceException( $response_body, $response_code ); |
| 267 |
} |
| 268 |
|
| 269 |
return json_decode( $response_body, true ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Refresh token |
| 274 |
* @return array |
| 275 |
* @throws ServiceException |
| 276 |
*/ |
| 277 |
public function renew_access_token( string $grant_type = GrantTypes::CLIENT_CREDENTIALS ): array { |
| 278 |
$credential = null; |
| 279 |
if ( GrantTypes::REFRESH_TOKEN === $grant_type ) { |
| 280 |
$credential = $this->facade->data()->get_refresh_token(); |
| 281 |
} |
| 282 |
return $this->get_token( $grant_type, $credential, true ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Update redirect URI |
| 287 |
* @return void |
| 288 |
* @throws ServiceException |
| 289 |
*/ |
| 290 |
public function switch_domain(): void { |
| 291 |
$data = $this->facade->data(); |
| 292 |
$utils = $this->facade->utils(); |
| 293 |
$client_id = $data->get_client_id(); |
| 294 |
|
| 295 |
if ( ! $client_id ) { |
| 296 |
throw new ServiceException( 'Missing client ID' ); |
| 297 |
} |
| 298 |
|
| 299 |
[ 'access_token' => $access_token ] = $this->get_token( GrantTypes::CLIENT_CREDENTIALS, null, false ); |
| 300 |
|
| 301 |
if ( ! $access_token ) { |
| 302 |
throw new ServiceException( 'Missing client token' ); |
| 303 |
} |
| 304 |
|
| 305 |
$this->request( $utils->get_clients_patch_url( $client_id ), [ |
| 306 |
'method' => 'PATCH', |
| 307 |
'headers' => [ |
| 308 |
'Content-Type' => 'application/json', |
| 309 |
'Authorization' => "Bearer {$access_token}", |
| 310 |
], |
| 311 |
'body' => wp_json_encode( [ |
| 312 |
'redirect_uri' => $utils->get_admin_url(), |
| 313 |
] ), |
| 314 |
] ); |
| 315 |
|
| 316 |
$data->set_home_url(); |
| 317 |
|
| 318 |
// Trigger the switched domain event for all apps |
| 319 |
do_action( 'elementor_one/switched_domain', $this->facade ); |
| 320 |
|
| 321 |
// Trigger the switched domain event for the app prefix |
| 322 |
do_action( 'elementor_one/' . $this->get_app_config( 'app_prefix' ) . '_switched_domain', $this->facade ); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Get license info |
| 327 |
* @return array |
| 328 |
* @throws ServiceException |
| 329 |
*/ |
| 330 |
public function get_license_info(): array { |
| 331 |
$utils = $this->facade->utils(); |
| 332 |
|
| 333 |
[ 'access_token' => $access_token ] = $this->get_token( GrantTypes::CLIENT_CREDENTIALS, null, false ); |
| 334 |
|
| 335 |
if ( ! $access_token ) { |
| 336 |
throw new ServiceException( 'Missing access token' ); |
| 337 |
} |
| 338 |
|
| 339 |
return $this->request( $utils->get_license_info_url(), [ |
| 340 |
'method' => 'GET', |
| 341 |
'headers' => [ |
| 342 |
'Authorization' => "Bearer {$access_token}", |
| 343 |
], |
| 344 |
] ); |
| 345 |
} |
| 346 |
} |
| 347 |
|