PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 4.0.0
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v4.0.0
4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / data / controllers / class-key-controller.php
superb-blocks / src / data / controllers Last commit date
class-cache-controller.php 1 month ago class-css-controller.php 1 month ago class-domainshift-controller.php 1 month ago class-key-controller.php 1 month ago class-log-controller.php 1 month ago class-option-controller.php 1 month ago class-rest-controller.php 1 month ago
class-key-controller.php
205 lines
1 <?php
2
3 namespace SuperbAddons\Data\Controllers;
4
5 defined('ABSPATH') || exit();
6
7 use Exception;
8 use SuperbAddons\Data\Controllers\OptionController;
9 use SuperbAddons\Admin\Controllers\LicenseResolveController;
10 use SuperbAddons\Data\Utils\KeyException;
11 use SuperbAddons\Data\Utils\KeyType;
12 use SuperbAddons\Data\Utils\OptionException;
13
14 class KeyController
15 {
16 const ENDPOINT_BASE = 'addons-status/';
17
18 public static function RegisterKey($key, $is_registration = false)
19 {
20 try {
21 $is_valid = strlen($key) === 23 && preg_match('/^[A-Z0-9]{5}(-[A-Z0-9]{5}){3}$/', $key);
22 if (!$is_valid) {
23 throw new KeyException(esc_html__("Invalid License Key. Please check that the license key was entered correctly.", "superb-blocks"));
24 }
25
26 $option_controller = new OptionController();
27 $stamp = $option_controller->GetStamp();
28 $response = DomainShiftController::RemoteGet(self::ENDPOINT_BASE . 'keys?' . ($is_registration ? "registration=true" : "revalidate=true") . '&key=' . $key . '&dm=' . urlencode(home_url()) . '&stamp=' . absint($stamp));
29 $response_code = wp_remote_retrieve_response_code($response);
30 if (!is_array($response) || is_wp_error($response) || $response_code !== 200) {
31 if ($response_code === 404) {
32 throw new KeyException(esc_html__("License key could not be validated. Please check that the license key was entered correctly.", "superb-blocks"), true, $response_code);
33 } else {
34 throw new KeyException(esc_html__("Unable to validate license key. Please contact support for assistance.", "superb-blocks"), false, absint($response_code));
35 }
36 }
37
38 $data = json_decode($response['body']);
39 if (!isset($data->level) || !isset($data->active) || !$data->active || !isset($data->expired) || !isset($data->verification) || !$data->verification || !isset($data->verification->exceeded) || !isset($data->verification->verified) || !isset($data->verification->stamp)) {
40 throw new KeyException(esc_html__("License key not currently active. Please contact support for assistance.", "superb-blocks"));
41 }
42
43 if ($data->verification->exceeded) {
44 throw new KeyException(esc_html__("You have already used up all your domain activations for this license key.", "superb-blocks"));
45 }
46
47 if ((!$data->verification->verified || !$data->verification->stamp) && $data->active && !$data->expired) {
48 throw new KeyException(esc_html__("License key verification could not complete. Please contact support for assistance.", "superb-blocks"));
49 }
50
51 if ($data->expired && $data->level !== KeyType::STANDARD) {
52 throw new KeyException(esc_html__("License key has expired. Please renew your subscription to re-activate your license.", "superb-blocks"));
53 }
54
55 try {
56 $option_controller->UpdateKey($key, $data->verification->stamp);
57 self::UpdateKeyType($data->level, $data->active, $data->expired, $data->exceeded);
58 LicenseResolveController::ClearCooldown();
59 return array("type" => $data->level, "active" => $data->active, "expired" => $data->expired, "verified" => $data->verification->verified, "exceeded" => $data->exceeded);
60 } catch (OptionException $o_ex) {
61 self::RemoveKey($key, $data->verification->stamp);
62 throw new KeyException(esc_html__("Unable to store license in WordPress. If the problem persists, please contact support.", "superb-blocks"));
63 }
64 } catch (KeyException $k_ex) {
65 throw $k_ex;
66 } catch (Exception $ex) {
67 LogController::HandleException($ex);
68 throw new KeyException(esc_html__("Internal Error Occurred During License Key Registration", "superb-blocks"));
69 }
70 }
71
72 public static function RemoveKey($request_key = false, $request_stamp = false)
73 {
74 $option_controller = new OptionController();
75 try {
76 $stamp = $request_stamp ? $request_stamp : $option_controller->GetStamp();
77 $key = $request_key ? $request_key : $option_controller->GetKey();
78 $response = DomainShiftController::RemoteGet(self::ENDPOINT_BASE . 'keys/remove?key=' . $key . '&dm=' . urlencode(home_url()) . '&stamp=' . absint($stamp));
79 $response_code = wp_remote_retrieve_response_code($response);
80 if (!is_array($response) || is_wp_error($response) || $response_code !== 200) {
81 throw new Exception(esc_html__("License key removal record not received.", "superb-blocks"), absint($response_code));
82 }
83 } catch (Exception $ex) {
84 LogController::HandleException($ex);
85 }
86 LicenseResolveController::ClearCooldown();
87 return $option_controller->RemoveKey();
88 }
89
90 public static function GetUpdatedLicenseKeyInformation()
91 {
92 try {
93 $option_controller = new OptionController();
94 if ($option_controller->HasRegisteredKey()) {
95 $key = $option_controller->GetKey();
96 return KeyController::RegisterKey($key);
97 }
98 return array("type" => KeyType::FREE, "active" => true, "expired" => false, "verified" => true, "exceeded" => false);
99 } catch (KeyException $k_ex) {
100 throw $k_ex;
101 } catch (Exception $ex) {
102 LogController::HandleException($ex);
103 return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500));
104 }
105 }
106
107 public static function HasRegisteredKey()
108 {
109 $option_controller = new OptionController();
110 return $option_controller->HasRegisteredKey();
111 }
112
113 public static function HasValidStandardKey()
114 {
115 $option_controller = new OptionController();
116 $has_standard = $option_controller->HasStandardKey();
117 $is_active = $option_controller->KeyIsActive();
118 $is_verified = $option_controller->KeyIsVerified();
119 $is_exceeded = $option_controller->KeyIsExceeded();
120 return $has_standard && $is_active && $is_verified && !$is_exceeded;
121 }
122
123 public static function HasValidPremiumKey()
124 {
125 $option_controller = new OptionController();
126 $has_premium = $option_controller->HasPremiumKey();
127 $is_expired = $option_controller->KeyIsExpired();
128 $is_active = $option_controller->KeyIsActive();
129 $is_verified = $option_controller->KeyIsVerified();
130 $is_exceeded = $option_controller->KeyIsExceeded();
131 return $has_premium && !$is_expired && $is_active && $is_verified && !$is_exceeded;
132 }
133
134 public static function HasValidKey()
135 {
136 $option_controller = new OptionController();
137 $has_key = $option_controller->HasRegisteredKey();
138 if (!$has_key) {
139 return false;
140 }
141 $is_expired = !$option_controller->HasStandardKey() && $option_controller->KeyIsExpired();
142 $is_active = $option_controller->KeyIsActive();
143 $is_verified = $option_controller->KeyIsVerified();
144 $is_exceeded = $option_controller->KeyIsExceeded();
145 return !$is_expired && $is_active && $is_verified && !$is_exceeded;
146 }
147
148 public static function GetKeyTypeLabel($keytype)
149 {
150 switch ($keytype) {
151 case KeyType::PREMIUM:
152 return __("Premium License", "superb-blocks");
153 case KeyType::STANDARD:
154 return __("Theme License", "superb-blocks");
155 case KeyType::ADDONS:
156 return __("Addons License", "superb-blocks");
157 case KeyType::FREE_PLUS:
158 return __("Free+ License", "superb-blocks");
159 case KeyType::FREE:
160 default:
161 return __("Free License", "superb-blocks");
162 }
163 }
164
165 public static function GetCurrentKeyTypeLabel()
166 {
167 $option_controller = new OptionController();
168
169 if (!self::HasValidKey()) {
170 return self::GetKeyTypeLabel(KeyType::FREE);
171 }
172
173 return self::GetKeyTypeLabel($option_controller->GetKeyType());
174 }
175
176 public static function VerificationFailed()
177 {
178 $option_controller = new OptionController();
179 return $option_controller->SetKeyVerificationFailed();
180 }
181
182 public static function GetKeyStatus()
183 {
184 $option_controller = new OptionController();
185 return array("type" => $option_controller->GetKeyType(), "active" => $option_controller->KeyIsActive(), "expired" => $option_controller->KeyIsExpired(), "verified" => $option_controller->KeyIsVerified(), "exceeded" => $option_controller->KeyIsExceeded());
186 }
187
188 public static function UpdateKeyType($keytype, $active, $expired, $exceeded)
189 {
190 $option_controller = new OptionController();
191 switch ($keytype) {
192 case KeyType::PREMIUM:
193 case KeyType::STANDARD:
194 case KeyType::ADDONS:
195 case KeyType::FREE_PLUS:
196 // Accepted key types
197 break;
198 default:
199 $keytype = KeyType::FREE;
200 break;
201 }
202 return $option_controller->UpdateKeyType($keytype, $active, $expired, $exceeded);
203 }
204 }
205