Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

Give_License::activate_license()   C

Complexity

Conditions 11
Paths 25

Size

Total Lines 88
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 39
nc 25
nop 0
dl 0
loc 88
ccs 0
cts 34
cp 0
crap 132
rs 5.2653
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
			$give_options = give_get_settings();
147
148
			$this->file           = $_file;
149
			$this->item_name      = $_item_name;
150
			$this->item_shortname = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
151
			$this->version        = $_version;
152
			$this->license        = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : '';
153
			$this->license_data   = get_option( $this->item_shortname . '_license_active' );
154
			$this->author         = $_author;
155
			$this->api_url        = is_null( $_api_url ) ? $this->api_url : $_api_url;
156
			$this->checkout_url   = is_null( $_checkout_url ) ? $this->checkout_url : $_checkout_url;
157
			$this->account_url    = is_null( $_account_url ) ? $this->account_url : $_account_url;
158
159
			// Setup hooks
160
			$this->includes();
161
			$this->hooks();
162
			//$this->auto_updater();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
163
		}
164
165
		/**
166
		 * Includes
167
		 *
168
		 * Include the updater class.
169
		 *
170
		 * @access private
171
		 * @since  1.0
172
		 *
173
		 * @return void
174
		 */
175
		private function includes() {
176
			if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
177
				require_once 'admin/EDD_SL_Plugin_Updater.php';
178
			}
179
		}
180
181
		/**
182
		 * Hooks
183
		 *
184
		 * Setup license hooks.
185
		 *
186
		 * @access private
187
		 * @since  1.0
188
		 *
189
		 * @return void
190
		 */
191
		private function hooks() {
192
193
			// Register settings
194
			add_filter( 'give_settings_licenses', array( $this, 'settings' ), 1 );
195
196
			// Activate license key on settings save
197
			add_action( 'admin_init', array( $this, 'activate_license' ) );
198
199
			// Deactivate license key
200
			add_action( 'admin_init', array( $this, 'deactivate_license' ) );
201
202
			// Updater
203
			add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
204
205
			add_action( 'admin_notices', array( $this, 'notices' ) );
206
207
			// Check license weekly.
208
			add_action( 'give_weekly_scheduled_events', array( $this, 'weekly_license_check' ) );
209
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) );
210
211
			// Check subscription weekly.
212
			add_action( 'give_weekly_scheduled_events', array( $this, 'weekly_subscription_check' ) );
213
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) );
214
		}
215
216
		/**
217
		 * Auto Updater
218
		 *
219
		 * @access private
220
		 * @since  1.0
221
		 *
222
		 * @return bool
223
		 */
224
		public function auto_updater() {
225
226
			if ( ! $this->is_valid_license() ) {
227
				return false;
228
			}
229
230
			// Setup the updater
231
			$give_updater = 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'       => get_option( $this->item_shortname . '_license_active' ),
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' => esc_html__( '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
323
				wp_die( esc_html__( 'Nonce verification failed.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
324
325
			}
326
327
			// Check if user have correct permissions.
328
			if ( ! current_user_can( 'manage_give_settings' ) ) {
329
				return;
330
			}
331
332
			// Allow third party addon developers to handle license activation.
333
			if( $this->__is_third_party_addon() ){
334
				do_action( 'give_activate_license', $this );
335
				return;
336
			}
337
338
			// Delete previous license setting if a empty license key submitted.
339
			if ( empty( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
340
				delete_option( $this->item_shortname . '_license_active' );
341
				return;
342
			}
343
344
			// Do not simultaneously activate any addon if user want to deactivate any addon.
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
353
			// Check if plugin previously installed.
354
			if ( $this->is_valid_license() ) {
355
				return;
356
			}
357
358
			// Get license key.
359
			$license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] );
360
361
			// Bailout.
362
			if( empty( $license ) ) {
363
				return;
364
			}
365
366
			// Delete previous license key from subscription if previously added.
367
			$this->__remove_license_key_from_subscriptions();
368
369
			// Data to send to the API
370
			$api_params = array(
371
				'edd_action' => 'activate_license', //never change from "edd_" to "give_"!
372
				'license'    => $license,
373
				'item_name'  => urlencode( $this->item_name ),
374
				'url'        => home_url()
375
			);
376
377
			// Call the API
378
			$response = wp_remote_post(
379
				$this->api_url,
380
				array(
381
					'timeout'   => 15,
382
					'sslverify' => false,
383
					'body'      => $api_params
384
				)
385
			);
386
387
			// Make sure there are no errors
388
			if ( is_wp_error( $response ) ) {
389
				return;
390
			}
391
392
			// Tell WordPress to look for updates
393
			set_site_transient( 'update_plugins', null );
394
395
			// Decode license data
396
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );
397
			update_option( $this->item_shortname . '_license_active', $license_data );
398
399
			// Check subscription for license key and store this to db (if any).
400
			$this->__single_subscription_check();
401
		}
402
403
		/**
404
		 * Deactivate License
405
		 *
406
		 * Deactivate the license key.
407
		 *
408
		 * @access public
409
		 * @since  1.0
410
		 *
411
		 * @return void
412
		 */
413
		public function deactivate_license() {
414
415
			if ( ! isset( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
416
				return;
417
			}
418
419
			if ( ! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' ) ) {
420
421
				wp_die( esc_html__( 'Nonce verification failed.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
422
423
			}
424
425
			if ( ! current_user_can( 'manage_give_settings' ) ) {
426
				return;
427
			}
428
429
			// Allow third party addon developers to handle license deactivation.
430
			if( $this->__is_third_party_addon() ){
431
				do_action( 'give_deactivate_license', $this );
432
				return;
433
			}
434
435
			// Run on deactivate button press
436
			if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
437
438
				// Data to send to the API
439
				$api_params = array(
440
					'edd_action' => 'deactivate_license', //never change from "edd_" to "give_"!
441
					'license'    => $this->license,
442
					'item_name'  => urlencode( $this->item_name ),
443
					'url'        => home_url()
444
				);
445
446
				// Call the API
447
				$response = wp_remote_post(
448
					$this->api_url,
449
					array(
450
						'timeout'   => 15,
451
						'sslverify' => false,
452
						'body'      => $api_params
453
					)
454
				);
455
456
				// Make sure there are no errors
457
				if ( is_wp_error( $response ) ) {
458
					return;
459
				}
460
461
				// Decode the license data
462
				$license_data = json_decode( wp_remote_retrieve_body( $response ) );
463
464
				// Remove license data.
465
				delete_option( $this->item_shortname . '_license_active' );
466
467
				// Remove license key from subscriptions if exist.
468
				$this->__remove_license_key_from_subscriptions();
469
			}
470
		}
471
472
		/**
473
		 * Check if license key is valid once per week.
474
		 *
475
		 * @access public
476
		 * @since  1.7
477
		 *
478
		 * @return bool|void
479
		 */
480
		public function weekly_license_check() {
481
482
			if( ! empty( $_POST['give_settings'] ) ) {
483
				// Don't fire when saving settings
484
				return false;
485
			}
486
487
			if( empty( $this->license ) ) {
488
				return false;
489
			}
490
491
			// Allow third party addon developers to handle their license check.
492
			if( $this->__is_third_party_addon() ){
493
				do_action( 'give_weekly_license_check', $this );
494
				return false;
495
			}
496
497
			// Data to send in our API request.
498
			$api_params = array(
499
				'edd_action'=> 'check_license',
500
				'license' 	=> $this->license,
501
				'item_name' => urlencode( $this->item_name ),
502
				'url'       => home_url()
503
			);
504
505
			// Call the API.
506
			$response = wp_remote_post(
507
				$this->api_url,
508
				array(
509
					'timeout'   => 15,
510
					'sslverify' => false,
511
					'body'      => $api_params
512
				)
513
			);
514
515
			// Make sure the response came back okay.
516
			if ( is_wp_error( $response ) ) {
517
				return false;
518
			}
519
520
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );
521
			update_option( $this->item_shortname . '_license_active', $license_data );
522
		}
523
524
		/**
525
		 * Check subscription validation once per week
526
		 *
527
		 * @access public
528
		 * @since  1.7
529
		 *
530
		 * @return bool|void
531
		 */
532
		public function weekly_subscription_check() {
533
534
			if( ! empty( $_POST['give_settings'] ) ) {
535
				// Don't fire when saving settings
536
				return false;
537
			}
538
539
			// Remove old subscription data.
540
			if( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp' , 1 ) ){
541
				delete_option( 'give_subscriptions' );
542
				update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp' , 1 ) ) );
543
			}
544
545
			if( empty( $this->license ) ) {
546
				return false;
547
			}
548
549
			// Allow third party addon developers to handle there subscription check.
550
			if( $this->__is_third_party_addon() ){
551
				do_action( 'give_weekly_subscription_check', $this );
552
				return false;
553
			}
554
555
			// Delete subscription notices show blocker.
556
			$this->__remove_license_notices_show_blocker();
557
558
			// Data to send in our API request.
559
			$api_params = array(
560
				// Do not get confuse with edd_action check_subscription.
561
				// By default edd software licensing api does not have api to check subscription.
562
				// This is custom feature to check subscriptions.
563
				'edd_action'=> 'check_subscription',
564
				'license' 	=> $this->license,
565
				'item_name' => urlencode( $this->item_name ),
566
				'url'       => home_url()
567
			);
568
569
			// Call the API
570
			$response = wp_remote_post(
571
				$this->api_url,
572
				array(
573
					'timeout'   => 15,
574
					'sslverify' => false,
575
					'body'      => $api_params
576
				)
577
			);
578
579
			// Make sure the response came back okay.
580
			if ( is_wp_error( $response ) ) {
581
				return false;
582
			}
583
584
			$subscription_data = json_decode( wp_remote_retrieve_body( $response ), true );
585
586
			if( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
587
				$subscriptions = get_option( 'give_subscriptions', array() );
588
589
				// Update subscription data only if subscription does not exist already.
590
				if( ! array_key_exists( $subscription_data['id'], $subscriptions ) ) {
591
					$subscriptions[ $subscription_data['id'] ] = $subscription_data;
592
					$subscriptions[ $subscription_data['id'] ]['licenses'] = array();
593
				}
594
595
				// Store licenses for subscription.
596
				if( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
597
					$subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
598
				}
599
600
				update_option( 'give_subscriptions', $subscriptions );
601
			}
602
		}
603
604
		/**
605
		 * Check if license key is part of subscription or not
606
		 *
607
		 * @access private
608
		 * @since  1.7
609
		 *
610
		 * @return bool|void
611
		 */
612
		private function __single_subscription_check() {
613
			// Do not fire if license key is not set.
614
			if ( ! isset( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
615
				return false;
616
			}
617
618
			if( empty( $this->license ) ) {
619
				return false;
620
			}
621
622
			// Data to send in our API request.
623
			$api_params = array(
624
				// Do not get confuse with edd_action check_subscription.
625
				// By default edd software licensing api does not have api to check subscription.
626
				// This is custom feature to check subscriptions.
627
				'edd_action'=> 'check_subscription',
628
				'license' 	=> $this->license,
629
				'item_name' => urlencode( $this->item_name ),
630
				'url'       => home_url()
631
			);
632
633
			// Call the API
634
			$response = wp_remote_post(
635
				$this->api_url,
636
				array(
637
					'timeout'   => 15,
638
					'sslverify' => false,
639
					'body'      => $api_params
640
				)
641
			);
642
643
			// Make sure the response came back okay.
644
			if ( is_wp_error( $response ) ) {
645
				return false;
646
			}
647
648
			$subscription_data = json_decode( wp_remote_retrieve_body( $response ), true );
649
650
			if( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
651
				$subscriptions = get_option( 'give_subscriptions', array() );
652
653
				// Update subscription data only if subscription does not exist already.
654
				if( ! array_key_exists( $subscription_data['id'], $subscriptions ) ) {
655
					$subscriptions[ $subscription_data['id'] ] = $subscription_data;
656
					$subscriptions[ $subscription_data['id'] ]['licenses'] = array();
657
				}
658
659
				// Store licenses for subscription.
660
				if( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
661
					$subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
662
				}
663
664
				update_option( 'give_subscriptions', $subscriptions );
665
			}
666
		}
667
668
		/**
669
		 * Admin notices for errors
670
		 *
671
		 * @access public
672
		 * @since  1.0
673
		 *
674
		 * @return void
675
		 */
676
		public function notices() {
677
			static $showed_invalid_message;
678
			static $showed_subscriptions_message;
679
			static $addon_license_key_in_subscriptions;
680
681
			// Set default value.
682
			$addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array();
683
684
			if( empty( $this->license ) ) {
685
				return;
686
			}
687
688
			if( ! current_user_can( 'manage_shop_settings' ) ) {
689
				return;
690
			}
691
692
			// Do not show licenses notices on license tab.
693
			if( ! empty( $_GET['tab'] ) && 'licenses' === $_GET['tab'] ) {
694
				return;
695
			}
696
697
			$messages = array();
698
699
			// Get subscriptions.
700
			$subscriptions = get_option( 'give_subscriptions' );
701
702
			// Show subscription messages.
703
			if( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) {
704
705
				foreach ( $subscriptions as $subscription ) {
706
					// Subscription expires timestamp.
707
					$subscription_expires = strtotime( $subscription['expires'] );
708
709
					// Start showing subscriptions message before one week of renewal date.
710
					if( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) {
711
						continue;
712
					}
713
714
					// Check if subscription message already exist in messages.
715
					if( array_key_exists( $subscription['id'], $messages ) ) {
716
						continue;
717
					}
718
719
					if( ( ! $this->__is_notice_dismissed( $subscription['id'] ) && 'active' !== $subscription['status'] ) ) {
720
721
						if( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) {// Check if license already expired.
722
							$messages[$subscription['id']] = sprintf(
723
								__( 'You Give addon 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' ),
724
								urldecode( $subscription['invoice_url'] ),
725
								$subscription['payment_id'],
726
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
727
								esc_url( add_query_arg( '_give_hide_license_notices_permanently', $subscription['id'], $_SERVER['REQUEST_URI'] ) )
728
							);
729
						}else{
730
							$messages[$subscription['id']] = sprintf(
731
								__( 'You Give addon 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' ),
732
								human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ),
733
								urldecode( $subscription['invoice_url'] ),
734
								$subscription['payment_id'],
735
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
736
								esc_url( add_query_arg( '_give_hide_license_notices_permanently', $subscription['id'], $_SERVER['REQUEST_URI'] ) )
737
							);
738
						}
739
					}
740
741
					// Stop validation for these licencse keys.
742
					$addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] );
743
				}
744
				$showed_subscriptions_message = true;
745
			}
746
747
			// Show non subscription addon messages.
748
			if( ! in_array( $this->license, $addon_license_key_in_subscriptions ) && ! $this->__is_notice_dismissed( 'general' ) && ! $this->is_valid_license() && empty( $showed_invalid_message ) ) {
749
750
				$messages['general'] = sprintf(
751
					__( 'You have invalid or expired license keys for Give Addon. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ),
752
					admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
753
				);
754
				$showed_invalid_message = true;
755
756
			}
757
758
			// Print messages.
759
			if( ! empty( $messages ) ) {
760
				foreach( $messages as $notice_id => $message ) {
761
					echo '<div class="notice notice-error is-dismissible give-license-notice" data-dismiss-notice-shortly="' . esc_url( add_query_arg( '_give_hide_license_notices_shortly', $notice_id, $_SERVER['REQUEST_URI'] ) ) . '">';
762
					echo '<p>' . $message . '</p>';
763
					echo '</div>';
764
				}
765
			}
766
		}
767
768
		/**
769
		 * Check if license is valid or not.
770
		 *
771
		 * @access public
772
		 * @since  1.7
773
		 *
774
		 * @return bool
775
		 */
776
		public function is_valid_license() {
777
			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 ) ) ) {
778
				return true;
779
			}
780
781
			return false;
782
		}
783
784
		/**
785
		 * Check if license is valid or not.
786
		 *
787
		 * @access private
788
		 * @since  1.7
789
		 *
790
		 * @return bool
791
		 */
792
		private function __is_third_party_addon() {
793
			return ( false === strpos( $this->api_url, 'givewp.com/' ) );
794
		}
795
796
		/**
797
		 * Remove license key from subscription.
798
		 *
799
		 * This function mainly uses when admin user deactivate license key,
800
		 * then we do not need subscription information for that license key.
801
		 *
802
		 * @access private
803
		 * @since  1.7
804
		 *
805
		 * @return void|bool
806
		 */
807
		private function __remove_license_key_from_subscriptions(){
808
			$subscriptions = get_option( 'give_subscriptions', array() );
809
810
			// Bailout.
811
			if( empty( $this->license ) ) {
812
				return false;
813
			}
814
815
			if( ! empty( $subscriptions ) ) {
816
				foreach ( $subscriptions as $subscription_id => $subscription ) {
817
					$license_index = array_search( $this->license, $subscription['licenses'] );
818
					if( false !== $license_index ) {
819
						// Remove license key.
820
						unset( $subscriptions[ $subscription_id ]['licenses'][$license_index] );
821
822
						// Rearrange license keys.
823
						$subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] );
824
825
						// Update subscription information.
826
						update_option( 'give_subscriptions', $subscriptions );
827
						break;
828
					}
829
				}
830
			}
831
		}
832
833
		/**
834
		 * Remove license notices show blocker.
835
		 *
836
		 * @access private
837
		 * @since  1.7
838
		 *
839
		 * @return void
840
		 */
841
		private function __remove_license_notices_show_blocker(){
842
			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...
843
844
			// Delete permanent notice blocker.
845
			$wpdb->query(
846
				$wpdb->prepare(
847
					"
848
					DELETE FROM $wpdb->usermeta
849
					WHERE meta_key
850
					LIKE '%%%s%%'
851
					",
852
					'_give_hide_license_notices_permanently'
853
				)
854
			);
855
856
			// Delete short notice blocker.
857
			$wpdb->query(
858
				$wpdb->prepare(
859
					"
860
					DELETE FROM $wpdb->options
861
					WHERE option_name
862
					LIKE '%%%s%%'
863
					",
864
					'__give_hide_license_notices_shortly_'
865
				)
866
			);
867
		}
868
869
		/**
870
		 * Check if notice dismissed by admin user or not.
871
		 *
872
		 * @access private
873
		 * @since  1.7
874
		 *
875
		 * @param  int $notice_id Notice ID.
876
		 *
877
		 * @return bool
878
		 */
879
		private function __is_notice_dismissed( $notice_id ){
880
			$current_user = wp_get_current_user();
881
			$is_notice_dismissed = false;
882
883
			// Ge is notice dismissed permanently.
884
			$already_dismiss_notices = ( $already_dismiss_notices = get_user_meta( $current_user->ID, '_give_hide_license_notices_permanently', true ) )
885
				? $already_dismiss_notices
886
				: array();
887
888
889
			if( in_array( $notice_id, $already_dismiss_notices ) || get_transient( "_give_hide_license_notices_shortly_{$current_user->ID}_{$notice_id}" ) ) {
890
				$is_notice_dismissed =  true;
891
			}
892
893
			return $is_notice_dismissed;
894
		}
895
	}
896
897
endif; // end class_exists check
898