adapter = $adapter; $this->license_key = $this->get_license_key(); $this->license_info = $this->get_license_info(); } public function update_license_info(array $license_info) : void { unset($license_info['success']); update_option($this->adapter->get_plugin_slug() . '_license_info', $license_info, \false); $this->license_info = $license_info; } public function update_license_key(string $license_key) : void { update_option($this->adapter->get_plugin_slug() . '_license_key', $license_key); $this->license_key = $license_key; } public function get_license_key() { return get_option($this->adapter->get_plugin_slug() . '_license_key'); } public function get_license_info() : array { $default = ['expires' => 'Not updated']; $info = get_option($this->adapter->get_plugin_slug() . '_license_info'); $info = \is_string($info) ? \json_decode($info, \true) : $info; return $info ?: $default; } public function remove_license_key() : void { delete_option($this->adapter->get_plugin_slug() . '_license_key'); $this->license_key = null; } public function remove_license_info() : void { delete_option($this->adapter->get_plugin_slug() . '_license_info'); $this->license_info = ['expires' => 'Not updated']; } public function activate(string $license_key) : array { $activate_response = LicenseAPI::activate_license($this->adapter->get_store_url(), $this->adapter->get_item_id(), $license_key); if ($activate_response['success']) { $this->update_license_key($license_key); $this->update_license_info($activate_response); } LicenseHandler::remove_site_plugin_check($this->adapter); return $activate_response; } public function update() : array { $license_key = $this->get_license_key(); $response = LicenseAPI::check_license($this->adapter->get_store_url(), $this->adapter->get_item_id(), $license_key); if ($response['success']) { $this->update_license_info($response); } elseif (empty($response['is_server_error'])) { $this->remove(); } LicenseHandler::remove_site_plugin_check($this->adapter); return $response; } public function remove() : void { // Deactivate on EDD side to free the activation slot $license_key = $this->get_license_key(); if (!empty($license_key)) { LicenseAPI::deactivate_license($this->adapter->get_store_url(), $this->adapter->get_item_id(), $license_key); } LicenseHandler::remove_site_plugin_check($this->adapter); $this->remove_license_key(); $this->remove_license_info(); } public function is_active() : bool { return (bool) $this->license_key; } public function is_expired() : bool { if ($this->is_active()) { $license_info = $this->get_license_info(); $expires = $license_info['expires'] ?? null; if ($expires && 'lifetime' !== $expires && 'Not updated' !== $expires) { return \strtotime($expires) < \time(); } } return \false; } public function format_license_key(int $group = 8, string $separator = '-', int $hidden = 20) : string { $key = (string) $this->license_key; $len = \strlen($key); if ($len <= $hidden) { return $key; } for ($i = $len - $hidden; $i < $len; $i++) { $key[$i] = '*'; } $formatted = ''; for ($i = 0; $i < $len; $i++) { $formatted .= $key[$i]; if (0 === ($i + 1) % $group && $i + 1 >= $group && $i !== $len - 1) { $formatted .= $separator; } } return $formatted; } public function get_renewal_url() : string { return $this->adapter->get_store_url() . 'checkout/?edd_license_key=' . $this->license_key . '&download_id=' . $this->adapter->get_item_id(); } public function get_upgrade_url() : string { $payment_id = $this->license_info['payment_id'] ?? ''; return $this->adapter->get_store_url() . 'checkout/purchase-history/?view=upgrades&action=manage_licenses&payment_id=' . $payment_id; } }