| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMailScoped\YayCommerce\AdminShell\License; |
| 4 |
|
| 5 |
use YayMailScoped\YayCommerce\AdminShell\License\Contracts\LicenseConfigAdapter; |
| 6 |
\defined('ABSPATH') || exit; |
| 7 |
/** |
| 8 |
* License state model. |
| 9 |
* |
| 10 |
* Option keys are derived from the adapter slug — NEVER hardcoded. |
| 11 |
* Ported from YayMail Pro License.php — YAYMAIL_* constants replaced with adapter. |
| 12 |
* @internal |
| 13 |
*/ |
| 14 |
class License |
| 15 |
{ |
| 16 |
protected LicenseConfigAdapter $adapter; |
| 17 |
/** Cached license key (may be false if not set). */ |
| 18 |
protected $license_key = null; |
| 19 |
/** Cached license info array. */ |
| 20 |
protected $license_info = null; |
| 21 |
public function __construct(LicenseConfigAdapter $adapter) |
| 22 |
{ |
| 23 |
$this->adapter = $adapter; |
| 24 |
$this->license_key = $this->get_license_key(); |
| 25 |
$this->license_info = $this->get_license_info(); |
| 26 |
} |
| 27 |
public function update_license_info(array $license_info) : void |
| 28 |
{ |
| 29 |
unset($license_info['success']); |
| 30 |
update_option($this->adapter->get_plugin_slug() . '_license_info', $license_info, \false); |
| 31 |
$this->license_info = $license_info; |
| 32 |
} |
| 33 |
public function update_license_key(string $license_key) : void |
| 34 |
{ |
| 35 |
update_option($this->adapter->get_plugin_slug() . '_license_key', $license_key); |
| 36 |
$this->license_key = $license_key; |
| 37 |
} |
| 38 |
public function get_license_key() |
| 39 |
{ |
| 40 |
return get_option($this->adapter->get_plugin_slug() . '_license_key'); |
| 41 |
} |
| 42 |
public function get_license_info() : array |
| 43 |
{ |
| 44 |
$default = ['expires' => 'Not updated']; |
| 45 |
$info = get_option($this->adapter->get_plugin_slug() . '_license_info'); |
| 46 |
$info = \is_string($info) ? \json_decode($info, \true) : $info; |
| 47 |
return $info ?: $default; |
| 48 |
} |
| 49 |
public function remove_license_key() : void |
| 50 |
{ |
| 51 |
delete_option($this->adapter->get_plugin_slug() . '_license_key'); |
| 52 |
$this->license_key = null; |
| 53 |
} |
| 54 |
public function remove_license_info() : void |
| 55 |
{ |
| 56 |
delete_option($this->adapter->get_plugin_slug() . '_license_info'); |
| 57 |
$this->license_info = ['expires' => 'Not updated']; |
| 58 |
} |
| 59 |
public function activate(string $license_key) : array |
| 60 |
{ |
| 61 |
$activate_response = LicenseAPI::activate_license($this->adapter->get_store_url(), $this->adapter->get_item_id(), $license_key); |
| 62 |
if ($activate_response['success']) { |
| 63 |
$this->update_license_key($license_key); |
| 64 |
$this->update_license_info($activate_response); |
| 65 |
} |
| 66 |
LicenseHandler::remove_site_plugin_check($this->adapter); |
| 67 |
return $activate_response; |
| 68 |
} |
| 69 |
public function update() : array |
| 70 |
{ |
| 71 |
$license_key = $this->get_license_key(); |
| 72 |
$response = LicenseAPI::check_license($this->adapter->get_store_url(), $this->adapter->get_item_id(), $license_key); |
| 73 |
if ($response['success']) { |
| 74 |
$this->update_license_info($response); |
| 75 |
} elseif (empty($response['is_server_error'])) { |
| 76 |
$this->remove(); |
| 77 |
} |
| 78 |
LicenseHandler::remove_site_plugin_check($this->adapter); |
| 79 |
return $response; |
| 80 |
} |
| 81 |
public function remove() : void |
| 82 |
{ |
| 83 |
// Deactivate on EDD side to free the activation slot |
| 84 |
$license_key = $this->get_license_key(); |
| 85 |
if (!empty($license_key)) { |
| 86 |
LicenseAPI::deactivate_license($this->adapter->get_store_url(), $this->adapter->get_item_id(), $license_key); |
| 87 |
} |
| 88 |
LicenseHandler::remove_site_plugin_check($this->adapter); |
| 89 |
$this->remove_license_key(); |
| 90 |
$this->remove_license_info(); |
| 91 |
} |
| 92 |
public function is_active() : bool |
| 93 |
{ |
| 94 |
return (bool) $this->license_key; |
| 95 |
} |
| 96 |
public function is_expired() : bool |
| 97 |
{ |
| 98 |
if ($this->is_active()) { |
| 99 |
$license_info = $this->get_license_info(); |
| 100 |
$expires = $license_info['expires'] ?? null; |
| 101 |
if ($expires && 'lifetime' !== $expires && 'Not updated' !== $expires) { |
| 102 |
return \strtotime($expires) < \time(); |
| 103 |
} |
| 104 |
} |
| 105 |
return \false; |
| 106 |
} |
| 107 |
public function format_license_key(int $group = 8, string $separator = '-', int $hidden = 20) : string |
| 108 |
{ |
| 109 |
$key = (string) $this->license_key; |
| 110 |
$len = \strlen($key); |
| 111 |
if ($len <= $hidden) { |
| 112 |
return $key; |
| 113 |
} |
| 114 |
for ($i = $len - $hidden; $i < $len; $i++) { |
| 115 |
$key[$i] = '*'; |
| 116 |
} |
| 117 |
$formatted = ''; |
| 118 |
for ($i = 0; $i < $len; $i++) { |
| 119 |
$formatted .= $key[$i]; |
| 120 |
if (0 === ($i + 1) % $group && $i + 1 >= $group && $i !== $len - 1) { |
| 121 |
$formatted .= $separator; |
| 122 |
} |
| 123 |
} |
| 124 |
return $formatted; |
| 125 |
} |
| 126 |
public function get_renewal_url() : string |
| 127 |
{ |
| 128 |
return $this->adapter->get_store_url() . 'checkout/?edd_license_key=' . $this->license_key . '&download_id=' . $this->adapter->get_item_id(); |
| 129 |
} |
| 130 |
public function get_upgrade_url() : string |
| 131 |
{ |
| 132 |
$payment_id = $this->license_info['payment_id'] ?? ''; |
| 133 |
return $this->adapter->get_store_url() . 'checkout/purchase-history/?view=upgrades&action=manage_licenses&payment_id=' . $payment_id; |
| 134 |
} |
| 135 |
} |
| 136 |
|