| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Admin\Services; |
| 4 |
|
| 5 |
use ElementorOne\Admin\Config; |
| 6 |
use ElementorOne\Admin\Exceptions\ClientException; |
| 7 |
use ElementorOne\Logger; |
| 8 |
use ElementorOne\Admin\Helpers\Utils; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Licenses |
| 16 |
*/ |
| 17 |
class Licenses { |
| 18 |
|
| 19 |
const USER_OPTION_ACTIVE_ONE_LICENSES = Config::APP_PREFIX . '_active_licenses'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Logger instance |
| 23 |
* @var Logger |
| 24 |
*/ |
| 25 |
private Logger $logger; |
| 26 |
|
| 27 |
/** |
| 28 |
* Instance |
| 29 |
* @var Licenses|null |
| 30 |
*/ |
| 31 |
private static ?Licenses $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Get instance |
| 35 |
* @return Licenses|null |
| 36 |
*/ |
| 37 |
public static function instance(): ?Licenses { |
| 38 |
if ( ! self::$instance ) { |
| 39 |
self::$instance = new self(); |
| 40 |
} |
| 41 |
return self::$instance; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor |
| 46 |
*/ |
| 47 |
private function __construct() { |
| 48 |
$this->logger = new Logger( self::class ); |
| 49 |
|
| 50 |
// If Elementor is not installed, return early. |
| 51 |
if ( ! class_exists( '\Elementor\Plugin' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// If One is already connected, return early. |
| 56 |
if ( Utils::get_one_connect()->utils()->is_connected() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// On load page to sync licenses after connect. |
| 61 |
add_action( 'load-elementor_page_elementor-connect', [ $this, 'on_load_page' ], 1 ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get licenses URL |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
public function get_licenses_url( string $app_type ): string { |
| 69 |
return add_query_arg( 'appType', $app_type, Client::get_client_base_url() . '/connect/api/v1/licenses' ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Sync user licenses. |
| 74 |
* @param bool $force |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function sync_user_licenses( bool $force = false ): void { |
| 78 |
if ( ! $force && false !== get_user_option( self::USER_OPTION_ACTIVE_ONE_LICENSES ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
try { |
| 83 |
$library_app = \Elementor\Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' ); |
| 84 |
$reflection_auth_headers = new \ReflectionMethod( $library_app->get_class_name(), 'generate_authentication_headers' ); |
| 85 |
|
| 86 |
if ( \PHP_VERSION_ID < 80100 ) { |
| 87 |
$reflection_auth_headers->setAccessible( true ); |
| 88 |
} |
| 89 |
|
| 90 |
$response = wp_remote_get( $this->get_licenses_url( Config::APP_TYPE ), [ |
| 91 |
'headers' => $reflection_auth_headers->invokeArgs( $library_app, [ 'one/licenses' ] ), |
| 92 |
'timeout' => 30, |
| 93 |
] ); |
| 94 |
|
| 95 |
if ( is_wp_error( $response ) ) { |
| 96 |
throw new ClientException( $response->get_error_message() ); |
| 97 |
} |
| 98 |
|
| 99 |
$response_body = wp_remote_retrieve_body( $response ); |
| 100 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 101 |
|
| 102 |
if ( ! empty( $response_body ) && null === json_decode( $response_body ) ) { |
| 103 |
throw new ClientException( esc_html( $response_body ), $response_code ); |
| 104 |
} |
| 105 |
|
| 106 |
if ( 200 !== $response_code ) { |
| 107 |
throw new ClientException( json_decode( $response_body )->message ?? $response_body, $response_code ); |
| 108 |
} |
| 109 |
|
| 110 |
$licenses = json_decode( $response_body, true ); |
| 111 |
update_user_option( get_current_user_id(), self::USER_OPTION_ACTIVE_ONE_LICENSES, $licenses ); |
| 112 |
} catch ( \Throwable $th ) { |
| 113 |
$this->logger->error( $th->getMessage() ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* On load page |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function on_load_page(): void { |
| 122 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if ( ! isset( $_GET['action'], $_GET['app'] ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$app = sanitize_text_field( wp_unslash( $_GET['app'] ) ); |
| 131 |
$action = sanitize_text_field( wp_unslash( $_GET['action'] ) ); |
| 132 |
|
| 133 |
if ( 'get_token' === $action && 'library' === $app ) { |
| 134 |
add_action( 'shutdown', [ $this, 'sync_user_licenses' ] ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|