PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / License / License.php

License.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/License/License.php

502 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * License: License
4 *
5 * @package SimplePay
6 * @subpackage Core
7 * @copyright Copyright (c) 2022, Sandhills Development, LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 4.4.0
10 */
11
12 namespace SimplePay\Core\License;
13
14 use stdClass;
15
16 /**
17 * License class.
18 *
19 * @since 4.4.0
20 */
21 class License {
22
23 /**
24 * License key.
25 *
26 * Key is currently passed as a required argument but not currently used.
27 * In the future when admin_init doesn't automatically populate license
28 * data we will need to do so here.
29 *
30 * @see SimplePay\Pro\License_Management\check_license_still_valid()
31 *
32 * @since 4.4.0
33 * @var string|null
34 */
35 protected $key;
36
37 /**
38 * License customer email address.
39 *
40 * @since 4.4.0
41 * @var string|null
42 */
43 private $email;
44
45 /**
46 * License customer name.
47 *
48 * @since 4.4.0
49 * @var string|null
50 */
51 private $name;
52
53 /**
54 * License download/item ID.
55 *
56 * @since 4.4.0
57 * @var int|null
58 */
59 private $item_id;
60
61 /**
62 * License price ID.
63 *
64 * @since 4.4.0
65 * @var int|null
66 */
67 private $price_id;
68
69 /**
70 * License expiration date.
71 *
72 * 'unlimited' if the license does not expire. Date time otherwise.
73 *
74 * @since 4.4.0
75 * @var 'unlimited'|string|null
76 */
77 private $expiration;
78
79 /**
80 * Determines if the license is expiring at the end of the billing cycle.
81 *
82 * @since 4.4.6
83 * @var bool|null
84 */
85 private $is_expiring;
86
87 /**
88 * License creation date.
89 *
90 * @since 4.4.4
91 * @var string|null
92 */
93 private $date_created;
94
95 /**
96 * License status.
97 *
98 * @since 4.4.0
99 * @var 'empty'|'valid'|'expired'|'disabled'|'revoked'|'invalid'|'inactive'|'deactivated'|'failed'|'site_inactive'|'item_name_mismatch'|'invalid_item_id'|'no_activations_left'|null
100 */
101 private $status;
102
103 /**
104 * License.
105 *
106 * @since 4.4.0
107 *
108 * @param string $key License key.
109 */
110 public function __construct( $key ) {
111 $this->key = $key;
112 }
113
114 /**
115 * Returns the license's associated customer email address.
116 *
117 * @since 4.4.0
118 *
119 * @return string|null
120 */
121 public function get_customer_email() {
122 $data = $this->get_license_data();
123
124 if ( isset( $data->customer_email ) ) {
125 $this->email = $data->customer_email;
126 }
127
128 return $this->email;
129 }
130
131 /**
132 * Returns the license's associated customer name.
133 *
134 * @since 4.4.0
135 *
136 * @return string|null
137 */
138 public function get_customer_name() {
139 $data = $this->get_license_data();
140
141 if ( isset( $data->customer_name ) ) {
142 $this->name = $data->customer_name;
143 }
144
145 return $this->name;
146 }
147
148 /**
149 * Returns the license's item ID.
150 *
151 * @since 4.4.0
152 *
153 * @return int|null
154 */
155 public function get_item_id() {
156 $data = $this->get_license_data();
157
158 if ( isset( $data->item_id ) ) {
159 $this->item_id = (int) $data->item_id;
160 }
161
162 return $this->item_id;
163 }
164
165 /**
166 * Returns the license's price ID.
167 *
168 * @since 4.4.0
169 *
170 * @return int|null
171 */
172 public function get_price_id() {
173 $data = $this->get_license_data();
174
175 if ( isset( $data->price_id ) ) {
176 $this->price_id = (int) $data->price_id;
177 }
178
179 return $this->price_id;
180 }
181
182 /**
183 * Returns the license's expiration date.
184 *
185 * @since 4.4.0
186 *
187 * @return string|null
188 */
189 public function get_expiration() {
190 $data = $this->get_license_data();
191
192 if ( isset( $data->expires ) ) {
193 $this->expiration = $data->expires;
194 }
195
196 return $this->expiration;
197 }
198
199 /**
200 * Returns the license's creation date.
201 *
202 * @since 4.4.4
203 *
204 * @return null|string
205 */
206 public function get_date_created() {
207 $data = $this->get_license_data();
208
209 if ( isset( $data->date_created ) ) {
210 $this->date_created = $data->date_created;
211 }
212
213 return $this->date_created;
214 }
215
216 /**
217 * Returns the license's status.
218 *
219 * @since 4.4.0
220 *
221 * @return 'empty'|'valid'|'expired'|'disabled'|'revoked'|'invalid'|'inactive'|'deactivated'|'failed'|'site_inactive'|'item_name_mismatch'|'invalid_item_id'|'no_activations_left'|null
222 */
223 public function get_status() {
224 $data = $this->get_license_data();
225
226 if ( isset( $data->license ) ) {
227 $this->status = $data->license;
228 }
229
230 return $this->status;
231 }
232
233 /**
234 * Returns the license key.
235 *
236 * @since 4.4.3
237 * @since 4.4.6 Only returns a string.
238 * @return string
239 */
240 public function get_key() {
241 if ( true === $this->is_lite() ) {
242 return '';
243 }
244
245 if ( defined( 'SIMPLE_PAY_LICENSE_KEY' ) ) {
246 return SIMPLE_PAY_LICENSE_KEY;
247 }
248
249 /** @var string */
250 return get_option( 'simpay_license_key', '' );
251 }
252
253 /**
254 * Returns the current license level name.
255 *
256 * @since 4.4.0
257 *
258 * @return string 'lite'|'personal'|'plus'|'professional'|'ultimate'|'elite'
259 */
260 public function get_level() {
261 // Lite.
262 if ( $this->is_lite() ) {
263 return 'lite';
264 }
265
266 $price_id = $this->get_price_id();
267
268 // No price ID is found, assume Personal.
269 if ( null === $price_id ) {
270 return 'personal';
271 }
272
273 switch ( $price_id ) {
274 case '1':
275 return 'personal';
276 case '2':
277 return 'plus';
278 case '3':
279 return 'professional';
280 case '4':
281 return 'ultimate';
282 case '5':
283 return 'elite';
284 default:
285 return 'personal';
286 }
287 }
288
289 /**
290 * Determines if the current license is valid.
291 *
292 * @since 4.4.0
293 *
294 * @return bool
295 */
296 public function is_valid() {
297 return 'valid' === $this->get_status();
298 }
299
300 /**
301 * Determines if the current license is expiring at the "expiring" (billing cycle) date.
302 *
303 * @since 4.4.6
304 *
305 * @return bool|null
306 */
307 public function is_expiring() {
308 $data = $this->get_license_data();
309
310 if ( isset( $data->is_expiring ) ) {
311 $this->is_expiring = (bool) $data->is_expiring;
312 }
313
314 return $this->is_expiring;
315 }
316
317 /**
318 * Determines if the current license is in a grace period after expiration.
319 * If the license is valid or lifetime it is considered in the grace period.
320 *
321 * @since 4.4.6
322 *
323 * @return bool
324 */
325 public function is_in_grace_period() {
326 // License is valid, so technically we are in a grace period.
327 if ( $this->is_valid() ) {
328 return true;
329 }
330
331 $expired = 'expired' === $this->get_status();
332
333 // License is not expired, so we are technically in a grace period.
334 if ( ! $expired ) {
335 return true;
336 }
337
338 /** @var string $expiration */
339 $expiration = $this->get_expiration();
340
341 // License is lifetime, so we are technically in a grace period.
342 if ( 'lifetime' === $expiration ) {
343 return true;
344 }
345
346 $time_since_expiration = time() - strtotime( $expiration );
347
348 // Grace period is 14 days.
349 return $time_since_expiration < ( DAY_IN_SECONDS * 14 );
350 }
351
352 /**
353 * Determines if the current license has access to subscription functionality.
354 *
355 * @since 4.4.4
356 *
357 * @return bool
358 */
359 public function is_subscriptions_enabled() {
360 return true;
361 }
362
363 /**
364 * Determines if the current license has access to enhanced subscription functionality.
365 *
366 * @since 4.4.4
367 *
368 * @return bool
369 */
370 public function is_enhanced_subscriptions_enabled() {
371 // Not valid, so no subscriptions.
372 if ( false === $this->is_subscriptions_enabled() ) {
373 return false;
374 }
375
376 // Grandfather Plus or higher to all subscription features when purchased
377 // before March 30, 2022.
378 /** @var string $created */
379 $created = $this->get_date_created();
380
381 if (
382 $this->is_pro( 'plus', '=' ) &&
383 strtotime( $created ) < strtotime( '2022-03-30 23:23:59' )
384 ) {
385 return true;
386 }
387
388 // Available to Professional or higher.
389 return $this->is_pro( 'professional', '>=' );
390 }
391
392 /**
393 * Determines if the current license is Lite.
394 *
395 * @since 4.4.0
396 *
397 * @return bool
398 */
399 public function is_lite() {
400 $is_lite = ! file_exists(
401 trailingslashit( SIMPLE_PAY_INC ) . 'pro/class-simplepaypro.php'
402 );
403
404 /**
405 * Filters whether the current environment is Lite or not.
406 *
407 * @since 4.6.0
408 *
409 * @param bool $is_lite Whether the current environment is Lite or not.
410 */
411 $is_lite = apply_filters( 'simpay_is_lite', $is_lite );
412
413 return $is_lite;
414 }
415
416 /**
417 * Determines if the current license is a specific type of Pro license.
418 *
419 * @since 4.4.0
420 *
421 * @param string $tier License tier/level.
422 * @param string $comparison PHP comparison string.
423 * @return bool
424 */
425 public function is_pro( $tier = 'personal', $comparison = '>=' ) {
426 // Lite.
427 if ( $this->is_lite() ) {
428 return false;
429 }
430
431 $price_id = $this->get_price_id();
432
433 // No price ID is found, assume Personal.
434 if ( null === $price_id ) {
435 return false;
436 }
437
438 $price_id = (string) $price_id;
439
440 switch ( $tier ) {
441 case 'personal':
442 return version_compare( $price_id, '1', $comparison );
443 case 'plus':
444 return version_compare( $price_id, '2', $comparison );
445 case 'professional':
446 return version_compare( $price_id, '3', $comparison );
447 case 'ultimate':
448 return version_compare( $price_id, '4', $comparison );
449 case 'elite':
450 return version_compare( $price_id, '5', $comparison );
451 default:
452 return false;
453 }
454 }
455
456 /**
457 * Returns public license data.
458 *
459 * @since 4.4.6
460 *
461 * @return array<string, array<string, bool|string|null>|bool|string|null>
462 */
463 public function to_array() {
464 return array(
465 'key' => $this->get_key(),
466 'is_lite' => $this->is_lite(),
467 'level' => $this->get_level(),
468 'valid' => $this->is_valid(),
469 'status' => $this->get_status(),
470 'expires' => $this->get_expiration(),
471 'is_expiring' => $this->is_expiring(),
472 'created' => $this->get_date_created(),
473 'customer' => array(
474 'email' => $this->get_customer_email(),
475 'name' => $this->get_customer_name(),
476 ),
477 'features' => array(
478 'subscriptions' => $this->is_subscriptions_enabled(),
479 'enhanced_subscriptions' => $this->is_enhanced_subscriptions_enabled(),
480 ),
481 );
482 }
483
484 /**
485 * Returns the license data from the cache or remote response.
486 *
487 * @since 4.4.0
488 *
489 * @return object
490 */
491 private function get_license_data() {
492 $license_data = get_option( 'simpay_license_data', '' );
493
494 if ( empty( $license_data ) ) {
495 return new stdClass();
496 }
497
498 /** @var object $license_data */
499 return $license_data;
500 }
501 }
502