| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Connect\Components; |
| 4 |
|
| 5 |
use ElementorOne\Connect\Facade; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Class License |
| 13 |
*/ |
| 14 |
class License { |
| 15 |
|
| 16 |
/** |
| 17 |
* Status active |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
const STATUS_ACTIVE = 'ACTIVE'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Facade instance |
| 24 |
* @var Facade |
| 25 |
*/ |
| 26 |
protected Facade $facade; |
| 27 |
|
| 28 |
/** |
| 29 |
* Hook name |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $hook_name; |
| 33 |
|
| 34 |
/** |
| 35 |
* License constructor |
| 36 |
* @param Facade $facade |
| 37 |
*/ |
| 38 |
public function __construct( Facade $facade ) { |
| 39 |
$this->facade = $facade; |
| 40 |
|
| 41 |
$app_prefix = $this->facade->get_config( 'app_prefix' ); |
| 42 |
$this->hook_name = 'elementor_one/' . $app_prefix . '_license_info_hook'; |
| 43 |
|
| 44 |
$license_check_enabled = apply_filters( 'elementor_one/' . $app_prefix . '_license_check_enabled', true ); |
| 45 |
if ( ! $license_check_enabled ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $this->facade->utils()->is_connected() ) { |
| 50 |
add_action( 'admin_init', [ $this, 'schedule_license_check' ] ); |
| 51 |
add_action( $this->hook_name, [ $this, 'check_license' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
add_action( 'elementor_one/' . $app_prefix . '_connected', [ $this, 'on_connect' ] ); |
| 55 |
add_action( 'elementor_one/' . $app_prefix . '_deactivated', [ $this, 'on_deactivation' ] ); |
| 56 |
add_action( 'elementor_one/' . $app_prefix . '_disconnected', [ $this, 'on_deactivation' ] ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Check license |
| 61 |
* Runs at most once every 12 hours unless $force is true. |
| 62 |
* |
| 63 |
* @param bool $force If true, bypasses the 12-hour lock and runs the check. |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function check_license( bool $force = false ): void { |
| 67 |
if ( ! $force && get_transient( $this->get_lock_key() ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
try { |
| 72 |
$license_info = $this->facade->service()->get_license_info(); |
| 73 |
$license_is_valid = self::STATUS_ACTIVE === ( $license_info['status'] ?? null ); |
| 74 |
if ( ! $license_is_valid ) { |
| 75 |
$this->facade->service()->deactivate_license(); |
| 76 |
} |
| 77 |
} catch ( \Throwable $th ) { |
| 78 |
if ( in_array( $th->getCode(), [ \WP_Http::UNAUTHORIZED, \WP_Http::FORBIDDEN ], true ) ) { |
| 79 |
$this->facade->data()->clear_session(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
set_transient( $this->get_lock_key(), true, 12 * HOUR_IN_SECONDS ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get lock key |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
private function get_lock_key(): string { |
| 91 |
return 'elementor_one_' . $this->facade->get_config( 'app_prefix' ) . '_license_check_lock'; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Schedule license check |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function schedule_license_check(): void { |
| 99 |
if ( ! wp_next_scheduled( $this->hook_name ) ) { |
| 100 |
wp_schedule_event( time(), 'hourly', $this->hook_name ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* On connect |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function on_connect(): void { |
| 109 |
$this->schedule_license_check(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* On deactivation |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function on_deactivation(): void { |
| 117 |
wp_clear_scheduled_hook( $this->hook_name ); |
| 118 |
delete_transient( $this->get_lock_key() ); |
| 119 |
} |
| 120 |
} |
| 121 |
|