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 +74 -30 2.0.42.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,13 +68,18 @@
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() ) {
81 79 try {
82 80 $this->activate_license( \CONTENT_CONTROL_LICENSE_KEY );
81 + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
83 82 } catch ( \Exception $e ) {
84 83 // Do nothing.
85 84 }
86 85 }
@@ -87,8 +86,10 @@
87 86 }
88 87
89 88 /**
90 89 * Schedule cron jobs.
90 + *
91 + * @return void
91 92 */
92 93 public function schedule_crons() {
93 94 if ( ! wp_next_scheduled( 'content_control_license_status_check' ) ) {
94 95 wp_schedule_event( time(), 'daily', 'content_control_license_status_check' );
@@ -97,9 +98,9 @@
97 98
98 99 /**
99 100 * Get license data.
100 101 *
101 - * @return array
102 + * @return array{key:string|null,status:array<string,mixed>|null}
102 103 */
103 104 public function get_license_data() {
104 105 if ( ! isset( $this->license_data ) ) {
105 106 $this->license_data = \get_option( self::OPTION_KEY, [
@@ -132,17 +133,23 @@
132 133 * @return string
133 134 */
134 135 public function get_license_key() {
135 136 $license_data = $this->get_license_data();
136 - return isset( $license_data['key'] ) ? $license_data['key'] : '';
137 + return ! empty( $license_data['key'] ) ? $license_data['key'] : '';
137 138 }
138 139
139 140 /**
140 141 * Get license status.
141 142 *
142 - * @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.
143 146 */
144 - public function get_license_status() {
147 + public function get_license_status( $refresh = false ) {
148 + if ( $refresh ) {
149 + $this->refresh_license_status();
150 + }
151 +
145 152 $license_data = $this->get_license_data();
146 153
147 154 $license_status = isset( $license_data['status'] ) ? $license_data['status'] : [];
148 155
@@ -149,11 +156,45 @@
149 156 return $license_status;
150 157 }
151 158
152 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 + /**
153 192 * Update license data.
154 193 *
155 - * @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
156 197 */
157 198 public function udpate_license_data( $license_data ) {
158 199 if ( \update_option( self::OPTION_KEY, $license_data ) ) {
159 200 $this->license_data = $license_data;
@@ -166,8 +207,10 @@
166 207 /**
167 208 * Update license key.
168 209 *
169 210 * @param string $key License key.
211 + *
212 + * @return void
170 213 */
171 214 public function update_license_key( $key ) {
172 215 $license_data = $this->get_license_data();
173 216
@@ -188,9 +231,11 @@
188 231
189 232 /**
190 233 * Update license status.
191 234 *
192 - * @param array $license_status License status data.
235 + * @param array<string,mixed> $license_status License status data.
236 + *
237 + * @return void
193 238 */
194 239 public function update_license_status( $license_status ) {
195 240 $license_data = $this->get_license_data();
196 241
@@ -217,9 +262,9 @@
217 262 * Get license expiration from license status data.
218 263 *
219 264 * @param bool $as_datetime Whether to return as DateTime object.
220 265 *
221 - * @return DateTime|false|null
266 + * @return \DateTime|false|null
222 267 */
223 268 public function get_license_expiration( $as_datetime = false ) {
224 269 $status = $this->get_license_status();
225 270
@@ -233,15 +278,15 @@
233 278 /**
234 279 * Fetch license status from remote server.
235 280 * This is a blocking request.
236 281 *
237 - * @return array
282 + * @return void
238 283 */
239 284 public function refresh_license_status() {
240 285 $key = $this->get_license_key();
241 286
242 287 if ( empty( $key ) ) {
243 - return [];
288 + return;
244 289 }
245 290
246 291 try {
247 292 $status = $this->check_license();
@@ -249,16 +294,14 @@
249 294 $status = [];
250 295 }
251 296
252 297 $this->update_license_status( $status );
253 -
254 - return $status;
255 298 }
256 299
257 300 /**
258 301 * Get license status from remote server.
259 302 *
260 - * @return array
303 + * @return array<string,mixed> License status data.
261 304 *
262 305 * @throws \Exception If there is an error.
263 306 */
264 307 private function check_license() {
@@ -298,9 +341,9 @@
298 341 * Activate license.
299 342 *
300 343 * @param string $key License key.
301 344 *
302 - * @return array License status data.
345 + * @return array<string,mixed> License status data.
303 346 *
304 347 * @throws \Exception If there is an error.
305 348 */
306 349 public function activate_license( $key = null ) {
@@ -313,9 +356,8 @@
313 356 $api_params = [
314 357 'edd_action' => 'activate_license',
315 358 'license' => $key,
316 359 'item_id' => self::ID,
317 - 'item_name' => rawurlencode( 'Content Control' ),
318 360 'url' => home_url(),
319 361 'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
320 362 ];
321 363
@@ -355,9 +397,9 @@
355 397
356 398 /**
357 399 * Deactivate license.
358 400 *
359 - * @return array License status data.
401 + * @return array<string,mixed> License status data.
360 402 *
361 403 * @throws \Exception If there is an error.
362 404 */
363 405 public function deactivate_license() {
@@ -400,9 +442,9 @@
400 442
401 443 /**
402 444 * Convert license error to human readable message.
403 445 *
404 - * @param array $license_status License status data.
446 + * @param array<string,mixed> $license_status License status data.
405 447 *
406 448 * @return string
407 449 */
408 450 public function get_license_error_message( $license_status ) {
@@ -450,8 +492,10 @@
450 492 }
451 493
452 494 /**
453 495 * Remove license.
496 + *
497 + * @return void
454 498 */
455 499 public function remove_license() {
456 500 try {
457 501 $deactivated = $this->deactivate_license();