PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
← All changes | classes/Plugin/License.php +83 -31 2.0.22.6.1 View file →
@@ -13,8 +13,11 @@
13 13
14 14 /**
15 15 * License management.
16 16 *
17 + * NOTE: For wordpress.org admins: This is only used if:
18 + * - The user explicitly entered a license key.
19 + *
17 20 * @package ContentControl
18 21 */
19 22 class License {
20 23
@@ -39,34 +42,25 @@
39 42 */
40 43 const OPTION_KEY = 'content_control_license';
41 44
42 45 /**
43 - * Container.
44 - *
45 - * @var Container
46 - */
47 - private $c;
48 -
49 - /**
50 46 * License data
51 47 *
52 - * @var array
48 + * @var array<string,mixed>|null
53 49 */
54 50 private $license_data;
55 51
56 52 /**
57 53 * Initialize license management.
58 - *
59 - * @param Container $c Container.
60 54 */
61 - public function __construct( $c ) {
62 - $this->c = $c;
63 -
55 + public function __construct() {
64 56 $this->register_hooks();
65 57 }
66 58
67 59 /**
68 60 * Register hooks.
61 + *
62 + * @return void
69 63 */
70 64 public function register_hooks() {
71 65 add_action( 'init', [ $this, 'autoregister' ] );
72 66 add_action( 'content_control_license_status_check', [ $this, 'refresh_license_status' ] );
@@ -74,17 +68,28 @@
74 68 }
75 69
76 70 /**
77 71 * Autoregister license.
72 + *
73 + * @return void
78 74 */
79 75 public function autoregister() {
80 - if ( defined( '\CONTENT_CONTROL_LICENSE_KEY' ) && ! empty( \CONTENT_CONTROL_LICENSE_KEY ) && '' === $this->get_license_key() ) {
76 + $key = defined( '\CONTENT_CONTROL_LICENSE_KEY' ) && '' !== \CONTENT_CONTROL_LICENSE_KEY ? \CONTENT_CONTROL_LICENSE_KEY : false;
77 +
78 + if ( $key && '' === $this->get_license_key() ) {
79 + try {
81 80 $this->activate_license( \CONTENT_CONTROL_LICENSE_KEY );
81 + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
82 + } catch ( \Exception $e ) {
83 + // Do nothing.
84 + }
82 85 }
83 86 }
84 87
85 88 /**
86 89 * Schedule cron jobs.
90 + *
91 + * @return void
87 92 */
88 93 public function schedule_crons() {
89 94 if ( ! wp_next_scheduled( 'content_control_license_status_check' ) ) {
90 95 wp_schedule_event( time(), 'daily', 'content_control_license_status_check' );
@@ -93,9 +98,9 @@
93 98
94 99 /**
95 100 * Get license data.
96 101 *
97 - * @return array
102 + * @return array{key:string|null,status:array<string,mixed>|null}
98 103 */
99 104 public function get_license_data() {
100 105 if ( ! isset( $this->license_data ) ) {
101 106 $this->license_data = \get_option( self::OPTION_KEY, [
@@ -128,17 +133,23 @@
128 133 * @return string
129 134 */
130 135 public function get_license_key() {
131 136 $license_data = $this->get_license_data();
132 - return isset( $license_data['key'] ) ? $license_data['key'] : '';
137 + return ! empty( $license_data['key'] ) ? $license_data['key'] : '';
133 138 }
134 139
135 140 /**
136 141 * Get license status.
137 142 *
138 - * @return array Array of license status data.
143 + * @param bool $refresh Whether to refresh license status.
144 + *
145 + * @return array<string,mixed> Array of license status data.
139 146 */
140 - public function get_license_status() {
147 + public function get_license_status( $refresh = false ) {
148 + if ( $refresh ) {
149 + $this->refresh_license_status();
150 + }
151 +
141 152 $license_data = $this->get_license_data();
142 153
143 154 $license_status = isset( $license_data['status'] ) ? $license_data['status'] : [];
144 155
@@ -145,11 +156,45 @@
145 156 return $license_status;
146 157 }
147 158
148 159 /**
160 + * Get license level.
161 + *
162 + * Only used in pro version.
163 + *
164 + * @return int Integer representing license level.
165 + */
166 + public function get_license_level() {
167 + $license_status = $this->get_license_status();
168 +
169 + if ( empty( $license_status ) ) {
170 + return -1;
171 + }
172 +
173 + $price_id = isset( $license_status['price_id'] ) ? $license_status['price_id'] : null;
174 +
175 + switch ( $price_id ) {
176 + default:
177 + return -1;
178 +
179 + case false:
180 + case 0:
181 + return 0;
182 +
183 + case 1:
184 + case 2:
185 + case 3:
186 + case 4:
187 + return absint( $price_id );
188 + }
189 + }
190 +
191 + /**
149 192 * Update license data.
150 193 *
151 - * @param array $license_data License data.
194 + * @param array{key:string|null,status:array<string,mixed>|null} $license_data License data.
195 + *
196 + * @return bool
152 197 */
153 198 public function udpate_license_data( $license_data ) {
154 199 if ( \update_option( self::OPTION_KEY, $license_data ) ) {
155 200 $this->license_data = $license_data;
@@ -162,8 +207,10 @@
162 207 /**
163 208 * Update license key.
164 209 *
165 210 * @param string $key License key.
211 + *
212 + * @return void
166 213 */
167 214 public function update_license_key( $key ) {
168 215 $license_data = $this->get_license_data();
169 216
@@ -184,9 +231,11 @@
184 231
185 232 /**
186 233 * Update license status.
187 234 *
188 - * @param array $license_status License status data.
235 + * @param array<string,mixed> $license_status License status data.
236 + *
237 + * @return void
189 238 */
190 239 public function update_license_status( $license_status ) {
191 240 $license_data = $this->get_license_data();
192 241
@@ -213,9 +262,9 @@
213 262 * Get license expiration from license status data.
214 263 *
215 264 * @param bool $as_datetime Whether to return as DateTime object.
216 265 *
217 - * @return DateTime|false|null
266 + * @return \DateTime|false|null
218 267 */
219 268 public function get_license_expiration( $as_datetime = false ) {
220 269 $status = $this->get_license_status();
221 270
@@ -229,15 +278,15 @@
229 278 /**
230 279 * Fetch license status from remote server.
231 280 * This is a blocking request.
232 281 *
233 - * @return array
282 + * @return void
234 283 */
235 284 public function refresh_license_status() {
236 285 $key = $this->get_license_key();
237 286
238 287 if ( empty( $key ) ) {
239 - return [];
288 + return;
240 289 }
241 290
242 291 try {
243 292 $status = $this->check_license();
@@ -245,16 +294,14 @@
245 294 $status = [];
246 295 }
247 296
248 297 $this->update_license_status( $status );
249 -
250 - return $status;
251 298 }
252 299
253 300 /**
254 301 * Get license status from remote server.
255 302 *
256 - * @return array
303 + * @return array<string,mixed> License status data.
257 304 *
258 305 * @throws \Exception If there is an error.
259 306 */
260 307 private function check_license() {
@@ -294,9 +341,9 @@
294 341 * Activate license.
295 342 *
296 343 * @param string $key License key.
297 344 *
298 - * @return array License status data.
345 + * @return array<string,mixed> License status data.
299 346 *
300 347 * @throws \Exception If there is an error.
301 348 */
302 349 public function activate_license( $key = null ) {
@@ -309,9 +356,8 @@
309 356 $api_params = [
310 357 'edd_action' => 'activate_license',
311 358 'license' => $key,
312 359 'item_id' => self::ID,
313 - 'item_name' => rawurlencode( 'Content Control' ),
314 360 'url' => home_url(),
315 361 'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
316 362 ];
317 363
@@ -351,9 +397,9 @@
351 397
352 398 /**
353 399 * Deactivate license.
354 400 *
355 - * @return array License status data.
401 + * @return array<string,mixed> License status data.
356 402 *
357 403 * @throws \Exception If there is an error.
358 404 */
359 405 public function deactivate_license() {
@@ -396,9 +442,9 @@
396 442
397 443 /**
398 444 * Convert license error to human readable message.
399 445 *
400 - * @param array $license_status License status data.
446 + * @param array<string,mixed> $license_status License status data.
401 447 *
402 448 * @return string
403 449 */
404 450 public function get_license_error_message( $license_status ) {
@@ -446,11 +492,17 @@
446 492 }
447 493
448 494 /**
449 495 * Remove license.
496 + *
497 + * @return void
450 498 */
451 499 public function remove_license() {
452 - $deactivated = $this->deactivate_license();
500 + try {
501 + $deactivated = $this->deactivate_license();
502 + } catch ( \Exception $e ) {
503 + $deactivated = [];
504 + }
453 505
454 506 if ( ! empty( $deactivated['license'] ) && 'active' !== $deactivated['license'] ) {
455 507 \delete_option( self::OPTION_KEY );
456 508 }