PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / vendor-prefixed / src / License / License.php

License.php in YayMail – WooCommerce Email Customizer 4.4.5, at vendor-prefixed/src/License/License.php

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