Completed
Push — issues/1649 ( e4c75d...047424 )
by Ravinder
26:31 queued 06:34
created

Give_License::deactivate_license()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 69
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 13
nop 0
dl 0
loc 69
rs 6.5437
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Give License handler
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/License
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
if ( ! class_exists( 'Give_License' ) ) :
18
19
	/**
20
	 * Give_License Class
21
	 *
22
	 * This class simplifies the process of adding license information to new Give add-ons.
23
	 *
24
	 * @since 1.0
25
	 */
26
	class Give_License {
27
28
		/**
29
		 * File
30
		 *
31
		 * @access private
32
		 * @since  1.0
33
		 *
34
		 * @var    string
35
		 */
36
		private $file;
37
38
		/**
39
		 * License
40
		 *
41
		 * @access private
42
		 * @since  1.0
43
		 *
44
		 * @var    string
45
		 */
46
		private $license;
47
48
		/**
49
		 * Item name
50
		 *
51
		 * @access private
52
		 * @since  1.0
53
		 *
54
		 * @var    string
55
		 */
56
		private $item_name;
57
58
		/**
59
		 * License Information object.
60
		 *
61
		 * @access private
62
		 * @since  1.7
63
		 *
64
		 * @var    object
65
		 */
66
		private $license_data;
67
68
		/**
69
		 * Item shortname
70
		 *
71
		 * @access private
72
		 * @since  1.0
73
		 *
74
		 * @var    string
75
		 */
76
		private $item_shortname;
77
78
		/**
79
		 * Version
80
		 *
81
		 * @access private
82
		 * @since  1.0
83
		 *
84
		 * @var    string
85
		 */
86
		private $version;
87
88
		/**
89
		 * Author
90
		 *
91
		 * @access private
92
		 * @since  1.0
93
		 *
94
		 * @var    string
95
		 */
96
		private $author;
97
98
		/**
99
		 * API URL
100
		 *
101
		 * @access private
102
		 * @since  1.0
103
		 *
104
		 * @var    string
105
		 */
106
		private $api_url = 'https://givewp.com/edd-sl-api/';
107
108
		/**
109
		 * Account URL
110
		 *
111
		 * @access private
112
		 * @since  1.7
113
		 *
114
		 * @var null|string
115
		 */
116
		private $account_url = 'https://givewp.com/my-account/';
117
118
		/**
119
		 * Ccheckout URL
120
		 *
121
		 * @access private
122
		 * @since  1.7
123
		 *
124
		 * @var null|string
125
		 */
126
		private $checkout_url = 'https://givewp.com/checkout/';
127
128
		/**
129
		 * Class Constructor
130
		 *
131
		 * Set up the Give License Class.
132
		 *
133
		 * @access public
134
		 * @since  1.0
135
		 *
136
		 * @param string $_file
137
		 * @param string $_item_name
138
		 * @param string $_version
139
		 * @param string $_author
140
		 * @param string $_optname
141
		 * @param string $_api_url
142
		 * @param string $_checkout_url
143
		 * @param string $_account_url
144
		 */
145
		public function __construct( $_file, $_item_name, $_version, $_author, $_optname = null, $_api_url = null, $_checkout_url = null, $_account_url = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $_optname is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
147
			$give_options = give_get_settings();
148
149
			$this->file           = $_file;
150
			$this->item_name      = $_item_name;
151
			$this->item_shortname = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
152
			$this->version        = $_version;
153
			$this->license        = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : '';
154
			$this->license_data   = get_option( $this->item_shortname . '_license_active' );
155
			$this->author         = $_author;
156
			$this->api_url        = is_null( $_api_url ) ? $this->api_url : $_api_url;
157
			$this->checkout_url   = is_null( $_checkout_url ) ? $this->checkout_url : $_checkout_url;
158
			$this->account_url    = is_null( $_account_url ) ? $this->account_url : $_account_url;
159
160
			// Setup hooks
161
			$this->includes();
162
			$this->hooks();
163
			$this->auto_updater();
164
		}
165
166
167
		/**
168
		 * Includes
169
		 *
170
		 * Include the updater class.
171
		 *
172
		 * @access private
173
		 * @since  1.0
174
		 *
175
		 * @return void
176
		 */
177
		private function includes() {
178
179
			if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
180
				require_once 'admin/EDD_SL_Plugin_Updater.php';
181
			}
182
		}
183
184
		/**
185
		 * Hooks
186
		 *
187
		 * Setup license hooks.
188
		 *
189
		 * @access private
190
		 * @since  1.0
191
		 *
192
		 * @return void
193
		 */
194
		private function hooks() {
195
196
			// Register settings
197
			add_filter( 'give_settings_licenses', array( $this, 'settings' ), 1 );
198
199
			// Activate license key on settings save
200
			add_action( 'admin_init', array( $this, 'activate_license' ) );
201
202
			// Deactivate license key
203
			add_action( 'admin_init', array( $this, 'deactivate_license' ) );
204
205
			// Updater
206
			add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
207
			add_action( 'admin_notices', array( $this, 'notices' ) );
208
209
			// Check license weekly.
210
			add_action( 'give_weekly_scheduled_events', array( $this, 'weekly_license_check' ) );
211
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) );
212
213
			// Check subscription weekly.
214
			add_action( 'give_weekly_scheduled_events', array( $this, 'weekly_subscription_check' ) );
215
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) );
216
217
		}
218
219
220
		/**
221
		 * Auto Updater
222
		 *
223
		 * @access private
224
		 * @since  1.0
225
		 *
226
		 * @return void
227
		 */
228
		public function auto_updater() {
229
230
			// Setup the updater
231
			new EDD_SL_Plugin_Updater(
232
				$this->api_url,
233
				$this->file,
234
				array(
235
					'version'   => $this->version,
236
					'license'   => $this->license,
237
					'item_name' => $this->item_name,
238
					'author'    => $this->author,
239
				)
240
			);
241
		}
242
243
		/**
244
		 * License Settings
245
		 *
246
		 * Add license field to settings.
247
		 *
248
		 * @access public
249
		 * @since  1.0
250
		 *
251
		 * @param  array $settings License settings.
252
		 *
253
		 * @return array           License settings.
254
		 */
255
		public function settings( $settings ) {
256
257
			$give_license_settings = array(
258
				array(
259
					'name'    => $this->item_name,
260
					'id'      => "{$this->item_shortname}_license_key",
261
					'desc'    => '',
262
					'type'    => 'license_key',
263
					'options' => array(
264
						'license'       => $this->license_data,
265
						'shortname'     => $this->item_shortname,
266
						'item_name'     => $this->item_name,
267
						'api_url'       => $this->api_url,
268
						'checkout_url'  => $this->checkout_url,
269
						'account_url'   => $this->account_url,
270
					),
271
					'size'    => 'regular',
272
				),
273
			);
274
275
			return array_merge( $settings, $give_license_settings );
276
		}
277
278
		/**
279
		 * License Settings Content
280
		 *
281
		 * Add Some Content to the Licensing Settings.
282
		 *
283
		 * @access public
284
		 * @since  1.0
285
		 *
286
		 * @param  array $settings License settings content.
287
		 *
288
		 * @return array           License settings content.
289
		 */
290
		public function license_settings_content( $settings ) {
291
292
			$give_license_settings = array(
293
				array(
294
					'name' => __( 'Add-on Licenses', 'give' ),
295
					'desc' => '<hr>',
296
					'type' => 'give_title',
297
					'id'   => 'give_title',
298
				),
299
			);
300
301
			return array_merge( $settings, $give_license_settings );
302
		}
303
304
		/**
305
		 * Activate License
306
		 *
307
		 * Activate the license key.
308
		 *
309
		 * @access public
310
		 * @since  1.0
311
		 *
312
		 * @return void
313
		 */
314
		public function activate_license() {
315
			// Bailout: Check if license key set of not.
316
			if ( ! isset( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
317
				return;
318
			}
319
320
			// Security check.
321
			if ( ! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' ) ) {
322
				wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
323
			}
324
325
			// Check if user have correct permissions.
326
			if ( ! current_user_can( 'manage_give_settings' ) ) {
327
				return;
328
			}
329
330
			// Allow third party addon developers to handle license activation.
331
			if ( $this->__is_third_party_addon() ) {
332
				do_action( 'give_activate_license', $this );
333
334
				return;
335
			}
336
337
			// Delete previous license setting if a empty license key submitted.
338
			if ( empty( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
339
				delete_option( $this->item_shortname . '_license_active' );
340
341
				return;
342
			}
343
344
			// Do not simultaneously activate add-ons if the user want to deactivate a specific add-on.
345
			foreach ( $_POST as $key => $value ) {
346
				if ( false !== strpos( $key, 'license_key_deactivate' ) ) {
347
					// Don't activate a key when deactivating a different key
348
					return;
349
				}
350
			}
351
352
			// Check if plugin previously installed.
353
			if ( $this->is_valid_license() ) {
354
				return;
355
			}
356
357
			// Get license key.
358
			$license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] );
359
360
			// Bailout.
361
			if ( empty( $license ) ) {
362
				return;
363
			}
364
365
			// Delete previous license key from subscription if previously added.
366
			$this->__remove_license_key_from_subscriptions();
367
368
			// Data to send to the API
369
			$api_params = array(
370
				'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
371
				'license'    => $license,
372
				'item_name'  => urlencode( $this->item_name ),
373
				'url'        => home_url(),
374
			);
375
376
			// Call the API
377
			$response = wp_remote_post(
378
				$this->api_url,
379
				array(
380
					'timeout'   => 15,
381
					'sslverify' => false,
382
					'body'      => $api_params,
383
				)
384
			);
385
386
			// Make sure there are no errors
387
			if ( is_wp_error( $response ) ) {
388
				return;
389
			}
390
391
			// Decode license data
392
			$this->license_data = json_decode( wp_remote_retrieve_body( $response ) );
393
394
			// Ensure activated successfully.
395
			if( isset( $this->license_data->success ) ){
396
				// Tell WordPress to look for updates
397
				set_site_transient( 'update_plugins', null );
398
399
				// Add license key.
400
				give_update_option( "{$this->item_shortname}_license_key", $license );
401
402
403
				update_option( $this->item_shortname . '_license_active', $this->license_data );
404
405
				// Check subscription for license key and store this to db (if any).
406
				$this->__single_subscription_check();
407
			}
408
		}
409
410
		/**
411
		 * Deactivate License
412
		 *
413
		 * Deactivate the license key.
414
		 *
415
		 * @access public
416
		 * @since  1.0
417
		 *
418
		 * @return void
419
		 */
420
		public function deactivate_license() {
421
422
			if ( ! isset( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
423
				return;
424
			}
425
426
			if ( ! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' ) ) {
427
				wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
428
			}
429
430
			if ( ! current_user_can( 'manage_give_settings' ) ) {
431
				return;
432
			}
433
434
			// Allow third party add-on developers to handle license deactivation.
435
			if ( $this->__is_third_party_addon() ) {
436
				do_action( 'give_deactivate_license', $this );
437
438
				return;
439
			}
440
441
			// Run on deactivate button press
442
			if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
443
444
				// Data to send to the API
445
				$api_params = array(
446
					'edd_action' => 'deactivate_license', // never change from "edd_" to "give_"!
447
					'license'    => $this->license,
448
					'item_name'  => urlencode( $this->item_name ),
449
					'url'        => home_url(),
450
				);
451
452
				// Call the API
453
				$response = wp_remote_post(
454
					$this->api_url,
455
					array(
456
						'timeout'   => 15,
457
						'sslverify' => false,
458
						'body'      => $api_params,
459
					)
460
				);
461
462
				// Make sure there are no errors
463
				if ( is_wp_error( $response ) ) {
464
					return;
465
				}
466
467
				// Decode the license data
468
				$license_data = json_decode( wp_remote_retrieve_body( $response ) );
469
470
				// Unset license data.
471
				$this->license_data = '';
472
473
				// Remove licence key.
474
				give_delete_option( "{$this->item_shortname}_license_key" );
475
476
				// Ensure deactivated successfully.
477
				if ( isset( $license_data->success ) ) {
478
479
					// Remove license data.
480
					delete_option( $this->item_shortname . '_license_active' );
481
					give_delete_option( $this->item_shortname . '_license_key' );
482
483
					// Remove license key from subscriptions if exist.
484
					$this->__remove_license_key_from_subscriptions();
485
486
				}
487
			}
488
		}
489
490
		/**
491
		 * Check if license key is valid once per week.
492
		 *
493
		 * @access public
494
		 * @since  1.7
495
		 *
496
		 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
497
		 */
498
		public function weekly_license_check() {
499
500
			if ( ! empty( $_POST['give_settings'] ) ) {
501
				// Don't fire when saving settings
502
				return false;
503
			}
504
505
			if ( empty( $this->license ) ) {
506
				return false;
507
			}
508
509
			// Allow third party add-on developers to handle their license check.
510
			if ( $this->__is_third_party_addon() ) {
511
				do_action( 'give_weekly_license_check', $this );
512
513
				return false;
514
			}
515
516
			// Data to send in our API request.
517
			$api_params = array(
518
				'edd_action' => 'check_license',
519
				'license'    => $this->license,
520
				'item_name'  => urlencode( $this->item_name ),
521
				'url'        => home_url()
522
			);
523
524
			// Call the API.
525
			$response = wp_remote_post(
526
				$this->api_url,
527
				array(
528
					'timeout'   => 15,
529
					'sslverify' => false,
530
					'body'      => $api_params,
531
				)
532
			);
533
534
			// Make sure the response came back okay.
535
			if ( is_wp_error( $response ) ) {
536
				return false;
537
			}
538
539
			$this->license_data = json_decode( wp_remote_retrieve_body( $response ) );
540
			update_option( $this->item_shortname . '_license_active', $this->license_data );
541
		}
542
543
		/**
544
		 * Check subscription validation once per week
545
		 *
546
		 * @access public
547
		 * @since  1.7
548
		 *
549
		 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
550
		 */
551
		public function weekly_subscription_check() {
552
553
			if ( ! empty( $_POST['give_settings'] ) ) {
554
				// Don't fire when saving settings
555
				return false;
556
			}
557
558
			// Remove old subscription data.
559
			if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp' , 1 ) ) {
560
				delete_option( 'give_subscriptions' );
561
				update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) );
562
			}
563
564
			if ( empty( $this->license ) ) {
565
				return false;
566
			}
567
568
			// Allow third party add-on developers to handle their subscription check.
569
			if ( $this->__is_third_party_addon() ) {
570
				do_action( 'give_weekly_subscription_check', $this );
571
572
				return false;
573
			}
574
575
			// Delete subscription notices show blocker.
576
			$this->__remove_license_notices_show_blocker();
577
578
			// Data to send in our API request.
579
			$api_params = array(
580
				// Do not get confused with edd_action check_subscription.
581
				// By default edd software licensing api does not have api to check subscription.
582
				// This is a custom feature to check subscriptions.
583
				'edd_action' => 'check_subscription',
584
				'license'    => $this->license,
585
				'item_name'  => urlencode( $this->item_name ),
586
				'url'        => home_url()
587
			);
588
589
			// Call the API
590
			$response = wp_remote_post(
591
				$this->api_url,
592
				array(
593
					'timeout'   => 15,
594
					'sslverify' => false,
595
					'body'      => $api_params,
596
				)
597
			);
598
599
			// Make sure the response came back okay.
600
			if ( is_wp_error( $response ) ) {
601
				return false;
602
			}
603
604
			$subscription_data = json_decode( wp_remote_retrieve_body( $response ), true );
605
606
			if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
607
				$subscriptions = get_option( 'give_subscriptions', array() );
608
609
				// Update subscription data only if subscription does not exist already.
610
				if ( ! array_key_exists( $subscription_data['id'], $subscriptions ) ) {
611
					$subscriptions[ $subscription_data['id'] ]             = $subscription_data;
612
					$subscriptions[ $subscription_data['id'] ]['licenses'] = array();
613
				}
614
615
				// Store licenses for subscription.
616
				if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
617
					$subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
618
				}
619
620
				update_option( 'give_subscriptions', $subscriptions );
621
			}
622
		}
623
624
		/**
625
		 * Check if license key is part of subscription or not
626
		 *
627
		 * @access private
628
		 * @since  1.7
629
		 *
630
		 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
631
		 */
632
		private function __single_subscription_check() {
633
			// Do not fire if license key is not set.
634
			if ( ! isset( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
635
				return false;
636
			}
637
638
			if ( empty( $this->license ) ) {
639
				return false;
640
			}
641
642
			// Data to send in our API request.
643
			$api_params = array(
644
				// Do not get confused with edd_action check_subscription.
645
				// By default edd software licensing api does not have api to check subscription.
646
				// This is a custom feature to check subscriptions.
647
				'edd_action' => 'check_subscription',
648
				'license'    => $this->license,
649
				'item_name'  => urlencode( $this->item_name ),
650
				'url'        => home_url()
651
			);
652
653
			// Call the API
654
			$response = wp_remote_post(
655
				$this->api_url,
656
				array(
657
					'timeout'   => 15,
658
					'sslverify' => false,
659
					'body'      => $api_params,
660
				)
661
			);
662
663
			// Make sure the response came back okay.
664
			if ( is_wp_error( $response ) ) {
665
				return false;
666
			}
667
668
			$subscription_data = json_decode( wp_remote_retrieve_body( $response ), true );
669
670
			if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
671
				$subscriptions = get_option( 'give_subscriptions', array() );
672
673
				// Update subscription data only if subscription does not exist already.
674
				if ( ! array_key_exists( $subscription_data['id'], $subscriptions ) ) {
675
					$subscriptions[ $subscription_data['id'] ]             = $subscription_data;
676
677
					$subscriptions[ $subscription_data['id'] ]['licenses'] = array();
678
				}
679
680
				// Store licenses for subscription.
681
				if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
682
					$subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
683
				}
684
685
				update_option( 'give_subscriptions', $subscriptions );
686
			}
687
		}
688
689
		/**
690
		 * Admin notices for errors
691
		 *
692
		 * @access public
693
		 * @since  1.0
694
		 *
695
		 * @return void
696
		 */
697
		public function notices() {
698
699
			if ( ! current_user_can( 'manage_give_settings' ) ) {
700
				return;
701
			}
702
703
			// Do not show licenses notices on license tab.
704
			if ( 'licenses' === give_get_current_setting_tab() ) {
705
				return;
706
			}
707
708
			static $showed_invalid_message;
709
			static $showed_subscriptions_message;
710
			static $addon_license_key_in_subscriptions;
711
712
			// Set default value.
713
			$addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array();
714
			$messages                           = array();
715
716
			if (
717
				empty( $this->license )
718
				&& ! $this->__is_notice_dismissed( 'general' )
719
				&& empty( $showed_invalid_message )
720
			) {
721
				$messages['general']    = sprintf(
722
					__( 'You have invalid or expired license keys for one or more Give Add-ons. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ),
723
					admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
724
				);
725
				$showed_invalid_message = true;
726
727
			}
728
729
			// Get subscriptions.
730
			$subscriptions = get_option( 'give_subscriptions' );
731
732
			// Show subscription messages.
733
			if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) {
734
735
				foreach ( $subscriptions as $subscription ) {
736
					// Subscription expires timestamp.
737
					$subscription_expires = strtotime( $subscription['expires'] );
738
739
					// Start showing subscriptions message before one week of renewal date.
740
					if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) {
741
						continue;
742
					}
743
744
					// Check if subscription message already exist in messages.
745
					if ( array_key_exists( $subscription['id'], $messages ) ) {
746
						continue;
747
					}
748
749
					if ( ( ! $this->__is_notice_dismissed( $subscription['id'] ) && 'active' !== $subscription['status'] ) ) {
750
751
						if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) {// Check if license already expired.
752
							$messages[ $subscription['id'] ] = sprintf(
753
								__( 'Your Give add-on license expired for payment <a href="%s" target="_blank">#%d</a>. <a href="%s" target="_blank">Click to renew an existing license</a> or <a href="%s">Click here if already renewed</a>.', 'give' ),
754
								urldecode( $subscription['invoice_url'] ),
755
								$subscription['payment_id'],
756
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
757
								esc_url( add_query_arg( '_give_hide_license_notices_permanently', $subscription['id'], $_SERVER['REQUEST_URI'] ) )
758
							);
759
						} else {
760
							$messages[ $subscription['id'] ] = sprintf(
761
								__( 'Your Give add-on license will expire in %s for payment <a href="%s" target="_blank">#%d</a>. <a href="%s" target="_blank">Click to renew an existing license</a> or <a href="%s">Click here if already renewed</a>.', 'give' ),
762
								human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ),
763
								urldecode( $subscription['invoice_url'] ),
764
								$subscription['payment_id'],
765
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
766
								esc_url( add_query_arg( '_give_hide_license_notices_permanently', $subscription['id'], $_SERVER['REQUEST_URI'] ) )
767
							);
768
						}
769
					}
770
771
					// Stop validation for these license keys.
772
					$addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] );
773
				}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
774
				$showed_subscriptions_message = true;
775
			}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
776
777
			// Show non subscription addon messages.
778
			if (
779
				! in_array( $this->license, $addon_license_key_in_subscriptions )
780
				&& ! $this->__is_notice_dismissed( 'general' )
781
				&& ! $this->is_valid_license()
782
				&& empty( $showed_invalid_message )
783
			) {
784
785
				$messages['general']    = sprintf(
786
					__( 'You have invalid or expired license keys for one or more Give Add-ons. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ),
787
					admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
788
				);
789
				$showed_invalid_message = true;
790
791
			}
792
793
			// Print messages.
794
			if ( ! empty( $messages ) ) {
795
				foreach ( $messages as $notice_id => $message ) {
796
797
					echo sprintf(
798
						'<div class="notice notice-error is-dismissible give-license-notice" data-dismiss-notice-shortly="%1$s"><p>%2$s</p></div>',
799
						esc_url( add_query_arg( '_give_hide_license_notices_shortly', $notice_id, $_SERVER['REQUEST_URI'] ) ),
800
						$message
801
					);
802
				}
803
			}
804
		}
805
806
		/**
807
		 * Check if license is valid or not.
808
		 *
809
		 * @access public
810
		 * @since  1.7
811
		 *
812
		 * @return bool
813
		 */
814
		public function is_valid_license() {
815
			if ( apply_filters( 'give_is_valid_license', ( is_object( $this->license_data ) && ! empty( $this->license_data ) && property_exists( $this->license_data, 'license' ) && 'valid' === $this->license_data->license ) ) ) {
816
				return true;
817
			}
818
819
			return false;
820
		}
821
822
		/**
823
		 * Check if license is valid or not.
824
		 *
825
		 * @access private
826
		 * @since  1.7
827
		 *
828
		 * @return bool
829
		 */
830
		private function __is_third_party_addon() {
831
			return ( false === strpos( $this->api_url, 'givewp.com/' ) );
832
		}
833
834
		/**
835
		 * Remove license key from subscription.
836
		 *
837
		 * This function mainly uses when admin user deactivate license key,
838
		 * then we do not need subscription information for that license key.
839
		 *
840
		 * @access private
841
		 * @since  1.7
842
		 *
843
		 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
844
		 */
845
		private function __remove_license_key_from_subscriptions() {
846
			$subscriptions = get_option( 'give_subscriptions', array() );
847
848
			// Bailout.
849
			if ( empty( $this->license ) ) {
850
				return false;
851
			}
852
853
			if ( ! empty( $subscriptions ) ) {
854
				foreach ( $subscriptions as $subscription_id => $subscription ) {
855
					$license_index = array_search( $this->license, $subscription['licenses'] );
856
					if ( false !== $license_index ) {
857
						// Remove license key.
858
						unset( $subscriptions[ $subscription_id ]['licenses'][ $license_index ] );
859
860
						// Rearrange license keys.
861
						$subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] );
862
863
						// Update subscription information.
864
						update_option( 'give_subscriptions', $subscriptions );
865
						break;
866
					}
867
				}
868
			}
869
		}
870
871
		/**
872
		 * Remove license notices show blocker.
873
		 *
874
		 * @access private
875
		 * @since  1.7
876
		 *
877
		 * @return void
878
		 */
879
		private function __remove_license_notices_show_blocker() {
880
			global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
881
882
			// Delete permanent notice blocker.
883
			$wpdb->query(
884
				$wpdb->prepare(
885
					"
886
					DELETE FROM $wpdb->usermeta
887
					WHERE meta_key
888
					LIKE '%%%s%%'
889
					",
890
					'_give_hide_license_notices_permanently'
891
				)
892
			);
893
894
			// Delete short notice blocker.
895
			$wpdb->query(
896
				$wpdb->prepare(
897
					"
898
					DELETE FROM $wpdb->options
899
					WHERE option_name
900
					LIKE '%%%s%%'
901
					",
902
					'__give_hide_license_notices_shortly_'
903
				)
904
			);
905
		}
906
907
		/**
908
		 * Check if notice dismissed by admin user or not.
909
		 *
910
		 * @access private
911
		 * @since  1.7
912
		 *
913
		 * @param  int $notice_id Notice ID.
914
		 *
915
		 * @return bool
916
		 */
917
		private function __is_notice_dismissed( $notice_id ) {
918
			$current_user        = wp_get_current_user();
919
			$is_notice_dismissed = false;
920
921
			// Ge is notice dismissed permanently.
922
			$already_dismiss_notices = ( $already_dismiss_notices = get_user_meta( $current_user->ID, '_give_hide_license_notices_permanently', true ) )
923
				? $already_dismiss_notices
924
				: array();
925
926
			if (
927
				in_array( $notice_id, $already_dismiss_notices )
928
				|| Give_Cache::get( "_give_hide_license_notices_shortly_{$current_user->ID}_{$notice_id}", true )
929
			) {
930
				$is_notice_dismissed = true;
931
			}
932
933
			return $is_notice_dismissed;
934
		}
935
936
	}
937
938
endif; // end class_exists check
939