Completed
Push — release/2.1 ( c669cb...a27eea )
by Ravinder
995:32 queued 989:33
created

Give_License::__construct()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 32
nop 8
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
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
23
	 * to new Give add-ons.
24
	 *
25
	 * @since 1.0
26
	 */
27
	class Give_License {
28
29
		/**
30
		 * File
31
		 *
32
		 * @access private
33
		 * @since  1.0
34
		 *
35
		 * @var    string
36
		 */
37
		private $file;
38
39
		/**
40
		 * License
41
		 *
42
		 * @access private
43
		 * @since  1.0
44
		 *
45
		 * @var    string
46
		 */
47
		private $license;
48
49
		/**
50
		 * Item name
51
		 *
52
		 * @access private
53
		 * @since  1.0
54
		 *
55
		 * @var    string
56
		 */
57
		private $item_name;
58
59
		/**
60
		 * License Information object.
61
		 *
62
		 * @access private
63
		 * @since  1.7
64
		 *
65
		 * @var    object
66
		 */
67
		private $license_data;
68
69
		/**
70
		 * Item shortname
71
		 *
72
		 * @access private
73
		 * @since  1.0
74
		 *
75
		 * @var    string
76
		 */
77
		private $item_shortname;
78
79
		/**
80
		 * Version
81
		 *
82
		 * @access private
83
		 * @since  1.0
84
		 *
85
		 * @var    string
86
		 */
87
		private $version;
88
89
		/**
90
		 * Author
91
		 *
92
		 * @access private
93
		 * @since  1.0
94
		 *
95
		 * @var    string
96
		 */
97
		private $author;
98
99
		/**
100
		 * API URL
101
		 *
102
		 * @access private
103
		 * @since  1.0
104
		 *
105
		 * @var    string
106
		 */
107
		private $api_url = 'https://givewp.com/edd-sl-api/';
108
109
		/**
110
		 * Account URL
111
		 *
112
		 * @access private
113
		 * @since  1.7
114
		 *
115
		 * @var null|string
116
		 */
117
		private $account_url = 'https://givewp.com/my-account/';
118
119
		/**
120
		 * Checkout URL
121
		 *
122
		 * @access private
123
		 * @since  1.7
124
		 *
125
		 * @var null|string
126
		 */
127
		private $checkout_url = 'https://givewp.com/checkout/';
128
129
		/**
130
		 * Class Constructor
131
		 *
132
		 * Set up the Give License Class.
133
		 *
134
		 * @access public
135
		 * @since  1.0
136
		 *
137
		 * @param string $_file
138
		 * @param string $_item_name
139
		 * @param string $_version
140
		 * @param string $_author
141
		 * @param string $_optname
142
		 * @param string $_api_url
143
		 * @param string $_checkout_url
144
		 * @param string $_account_url
145
		 */
146
		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...
147
148
			$give_options = give_get_settings();
149
150
			$this->file             = $_file;
151
			$this->item_name        = $_item_name;
152
			$this->item_shortname   = self::get_short_name( $this->item_name );
153
			$this->version          = $_version;
154
			$this->license          = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : '';
155
			$this->license_data     = __give_get_active_license_info( $this->item_shortname );
0 ignored issues
show
Documentation Bug introduced by
It seems like __give_get_active_licens...($this->item_shortname) of type array is incompatible with the declared type object of property $license_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
156
			$this->author           = $_author;
157
			$this->api_url          = is_null( $_api_url ) ? $this->api_url : $_api_url;
158
			$this->checkout_url     = is_null( $_checkout_url ) ? $this->checkout_url : $_checkout_url;
159
			$this->account_url      = is_null( $_account_url ) ? $this->account_url : $_account_url;
160
			$this->auto_updater_obj = null;
161
162
			// Add Setting for Give Add-on activation status.
163
			$is_addon_activated = get_option( 'give_is_addon_activated' );
164
			if ( ! $is_addon_activated && is_object( $this ) && sizeof( $this ) > 0 ) {
165
				update_option( 'give_is_addon_activated', true );
166
				Give_Cache::set( 'give_cache_hide_license_notice_after_activation', true, DAY_IN_SECONDS );
167
			}
168
169
			// Setup hooks
170
			$this->includes();
171
			$this->hooks();
172
			$this->auto_updater();
173
		}
174
175
176
		/**
177
		 * Get plugin shortname
178
		 *
179
		 * @since  2.1.0
180
		 * @access public
181
		 *
182
		 * @param $plugin_name
183
		 *
184
		 * @return string
185
		 */
186
		public static function get_short_name( $plugin_name ) {
187
			$plugin_name = trim( str_replace( 'Give - ', '', $plugin_name ) );
188
			$plugin_name = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $plugin_name ) ) );
189
190
			return $plugin_name;
191
		}
192
193
		/**
194
		 * Includes
195
		 *
196
		 * Include the updater class.
197
		 *
198
		 * @access private
199
		 * @since  1.0
200
		 *
201
		 * @return void
202
		 */
203
		private function includes() {
204
205
			if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
206
				require_once 'admin/EDD_SL_Plugin_Updater.php';
207
			}
208
		}
209
210
		/**
211
		 * Hooks
212
		 *
213
		 * Setup license hooks.
214
		 *
215
		 * @access private
216
		 * @since  1.0
217
		 *
218
		 * @return void
219
		 */
220
		private function hooks() {
221
222
			// Register settings.
223
			add_filter( 'give_settings_licenses', array( $this, 'settings' ), 1 );
224
225
			// Activate license key on settings save.
226
			add_action( 'admin_init', array( $this, 'activate_license' ), 10 );
227
228
			// Deactivate license key.
229
			add_action( 'admin_init', array( $this, 'deactivate_license' ), 11 );
230
231
			// Updater.
232
			add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
233
			add_action( 'admin_notices', array( $this, 'notices' ) );
234
235
			// Check license weekly.
236
			Give_Cron::add_weekly_event( array( $this, 'weekly_license_check' ) );
237
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) );
238
239
			// Check subscription weekly.
240
			Give_Cron::add_weekly_event( array( $this, 'weekly_subscription_check' ) );
241
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) );
242
243
			// Show addon notice on plugin page.
244
			$plugin_name = explode( 'plugins/', $this->file );
245
			$plugin_name = end( $plugin_name );
246
			add_action( "after_plugin_row_{$plugin_name}", array( $this, 'plugin_page_notices' ), 10, 3 );
247
248
		}
249
250
251
		/**
252
		 * Auto Updater
253
		 *
254
		 * @access private
255
		 * @since  1.0
256
		 *
257
		 * @return void
258
		 */
259
		public function auto_updater() {
260
261
			// Setup the updater.
262
			$this->auto_updater_obj = new EDD_SL_Plugin_Updater(
263
				$this->api_url,
264
				$this->file,
265
				array(
266
					'version'   => $this->version,
267
					'license'   => $this->license,
268
					'item_name' => $this->item_name,
269
					'author'    => $this->author,
270
				)
271
			);
272
		}
273
274
		/**
275
		 * License Settings
276
		 *
277
		 * Add license field to settings.
278
		 *
279
		 * @access public
280
		 * @since  1.0
281
		 *
282
		 * @param  array $settings License settings.
283
		 *
284
		 * @return array           License settings.
285
		 */
286
		public function settings( $settings ) {
287
288
			$give_license_settings = array(
289
				array(
290
					'name'    => $this->item_name,
291
					'id'      => $this->item_shortname . '_license_key',
292
					'desc'    => '',
293
					'type'    => 'license_key',
294
					'options' => array(
295
						'license'      => get_option( $this->item_shortname . '_license_active' ),
296
						'shortname'    => $this->item_shortname,
297
						'item_name'    => $this->item_name,
298
						'api_url'      => $this->api_url,
299
						'checkout_url' => $this->checkout_url,
300
						'account_url'  => $this->account_url,
301
					),
302
					'size'    => 'regular',
303
				),
304
			);
305
306
			return array_merge( $settings, $give_license_settings );
307
		}
308
309
		/**
310
		 * License Settings Content
311
		 *
312
		 * Add Some Content to the Licensing Settings.
313
		 *
314
		 * @access public
315
		 * @since  1.0
316
		 *
317
		 * @param  array $settings License settings content.
318
		 *
319
		 * @return array           License settings content.
320
		 */
321
		public function license_settings_content( $settings ) {
322
323
			$give_license_settings = array(
324
				array(
325
					'name' => __( 'Add-on Licenses', 'give' ),
326
					'desc' => '<hr>',
327
					'type' => 'give_title',
328
					'id'   => 'give_title',
329
				),
330
			);
331
332
			return array_merge( $settings, $give_license_settings );
333
		}
334
335
		/**
336
		 * Activate License
337
		 *
338
		 * Activate the license key.
339
		 *
340
		 * @access public
341
		 * @since  1.0
342
		 *
343
		 * @return void
344
		 */
345
		public function activate_license() {
346
			// Bailout.
347
			if ( ! $this->__is_user_can_edit_license() ) {
348
				return;
349
			}
350
351
			// Allow third party addon developers to handle license activation.
352
			if ( $this->__is_third_party_addon() ) {
353
				do_action( 'give_activate_license', $this );
354
355
				return;
356
			}
357
358
			// Delete previous license setting if a empty license key submitted.
359
			if ( empty( $_POST["{$this->item_shortname}_license_key"] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
360
				$this->unset_license();
361
362
				return;
363
			}
364
365
			// Do not simultaneously activate add-ons if the user want to deactivate a specific add-on.
366
			if( $this->is_deactivating_license() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
367
				return;
368
			}
369
370
			// Check if plugin previously installed.
371
			if ( $this->is_valid_license() ) {
372
				return;
373
			}
374
375
			// Get license key.
376
			$this->license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
377
378
			// Delete previous license key from subscription if previously added.
379
			$this->__remove_license_key_from_subscriptions();
380
381
			// Make sure there are no api errors.
382
			if ( ! ( $license_data = $this->get_license_info( 'activate_license' ) ) ) {
383
				return;
384
			}
385
386
			// Make sure license is valid.
387
			// return because admin will want to activate license again.
388
			if ( ! $this->is_license( $license_data ) ) {
389
				// Add license key.
390
				give_update_option( "{$this->item_shortname}_license_key", $this->license );
391
392
				return;
393
			}
394
395
			// Tell WordPress to look for updates.
396
			set_site_transient( 'update_plugins', null );
397
398
			// Add license data.
399
			update_option( "{$this->item_shortname}_license_active", $license_data );
400
401
			// Add license key.
402
			give_update_option( "{$this->item_shortname}_license_key", $this->license );
403
404
			// Check subscription for license key and store this to db (if any).
405
			$this->__single_subscription_check();
406
		}
407
408
		/**
409
		 * Deactivate License
410
		 *
411
		 * Deactivate the license key.
412
		 *
413
		 * @access public
414
		 * @since  1.0
415
		 *
416
		 * @return void
417
		 */
418
		public function deactivate_license() {
419
			// Bailout.
420
			if ( ! $this->__is_user_can_edit_license() ) {
421
				return;
422
			}
423
424
			// Allow third party add-on developers to handle license deactivation.
425
			if ( $this->__is_third_party_addon() ) {
426
				do_action( 'give_deactivate_license', $this );
427
428
				return;
429
			}
430
431
			// Run on deactivate button press.
432
			if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
433
				$this->unset_license();
434
			}
435
		}
436
437
		/**
438
		 * Check if license key is valid once per week.
439
		 *
440
		 * @access public
441
		 * @since  1.7
442
		 *
443
		 * @return void
444
		 */
445
		public function weekly_license_check() {
446
447
			if (
448
				! empty( $_POST['give_settings'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
449
				empty( $this->license )
450
			) {
451
				return;
452
			}
453
454
			// Allow third party add-on developers to handle their license check.
455
			if ( $this->__is_third_party_addon() ) {
456
				do_action( 'give_weekly_license_check', $this );
457
458
				return;
459
			}
460
461
			// Make sure there are no api errors.
462
			if ( ! ( $license_data = $this->get_license_info( 'check_license' ) ) ) {
463
				return;
464
			}
465
466
			// Bailout.
467
			if ( ! $this->is_license( $license_data ) ) {
468
				return;
469
			}
470
471
			update_option( $this->item_shortname . '_license_active', $license_data );
472
473
			return;
474
		}
475
476
		/**
477
		 * Check subscription validation once per week
478
		 *
479
		 * @access public
480
		 * @since  1.7
481
		 *
482
		 * @return void
483
		 */
484
		public function weekly_subscription_check() {
485
			// Bailout.
486
			if (
487
				! empty( $_POST['give_settings'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
488
				empty( $this->license )
489
			) {
490
				return;
491
			}
492
493
			// Remove old subscription data.
494
			if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp', 1 ) ) {
495
				delete_option( 'give_subscriptions' );
496
				update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) );
497
			}
498
499
			// Allow third party add-on developers to handle their subscription check.
500
			if ( $this->__is_third_party_addon() ) {
501
				do_action( 'give_weekly_subscription_check', $this );
502
503
				return;
504
			}
505
506
			$this->__single_subscription_check();
507
		}
508
509
		/**
510
		 * Check if license key is part of subscription or not
511
		 *
512
		 * @access private
513
		 * @since  1.7
514
		 *
515
		 * @return void
516
		 */
517
		private function __single_subscription_check() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_License::__single_subscription_check" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
518
			if ( empty( $this->license ) ) {
519
				return;
520
			}
521
522
			/**
523
			 * Make sure there are no api errors.
524
			 *
525
			 * Do not get confused with edd_action check_subscription.
526
			 * By default edd software licensing api does not have api to check subscription.
527
			 * This is a custom feature to check subscriptions.
528
			 */
529
			$subscription_data = $this->get_license_info( 'check_subscription', true );
530
531
			if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
532
533
				$subscriptions = get_option( 'give_subscriptions', array() );
534
535
				// Update subscription data only if subscription does not exist already.
536
				$subscriptions[ $subscription_data['id'] ] = $subscription_data;
537
538
				// Initiate default set of license for subscription.
539
				if ( ! isset( $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
540
					$subscriptions[ $subscription_data['id'] ]['licenses'] = array();
541
				}
542
543
				// Store licenses for subscription.
544
				if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
545
					$subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
546
				}
547
548
				update_option( 'give_subscriptions', $subscriptions );
549
			}
550
		}
551
552
		/**
553
		 * Admin notices for errors
554
		 *
555
		 * @access public
556
		 * @since  1.0
557
		 *
558
		 * @return void
559
		 */
560
		public function notices() {
561
562
			if ( ! current_user_can( 'manage_give_settings' ) ) {
563
				return;
564
			}
565
566
			// Do not show licenses notices on license tab.
567
			if ( 'licenses' === give_get_current_setting_tab() ) {
568
				return;
569
			}
570
571
			static $showed_invalid_message;
572
			static $showed_subscriptions_message;
573
			static $addon_license_key_in_subscriptions;
574
575
			// Set default value.
576
			$addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array();
577
			$messages                           = array();
578
579
			// Check whether admin has Give Add-on activated since 24 hours?
580
			$is_license_notice_hidden = Give_Cache::get( 'give_cache_hide_license_notice_after_activation' );
581
582
			// Display Invalid License notice, if its more than 24 hours since first Give Add-on activation.
583
			if (
584
				empty( $this->license )
585
				&& empty( $showed_invalid_message )
586
				&& ( false === $is_license_notice_hidden )
587
			) {
588
589
				Give()->notices->register_notice( array(
590
					'id'               => 'give-invalid-license',
591
					'type'             => 'error',
592
					'description'      => sprintf(
593
						__( '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' ),
594
						admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
595
					),
596
					'dismissible_type' => 'user',
597
					'dismiss_interval' => 'shortly',
598
				) );
599
600
				$showed_invalid_message = true;
601
602
			}
603
604
			// Get subscriptions.
605
			$subscriptions = get_option( 'give_subscriptions' );
606
607
			// Show subscription messages.
608
			if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) {
609
610
				foreach ( $subscriptions as $subscription ) {
611
					// Subscription expires timestamp.
612
					$subscription_expires = strtotime( $subscription['expires'] );
613
614
					// Start showing subscriptions message before one week of renewal date.
615
					if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) {
616
						continue;
617
					}
618
619
					// Check if subscription message already exist in messages.
620
					if ( array_key_exists( $subscription['id'], $messages ) ) {
621
						continue;
622
					}
623
624
					// Check if license already expired.
625
					if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) {
626
						Give()->notices->register_notice( array(
627
							'id'               => "give-expired-subscription-{$subscription['id']}",
628
							'type'             => 'error',
629
							'description'      => sprintf(
630
								__( 'Your Give add-on license expired for payment <a href="%1$s" target="_blank">#%2$d</a>. <a href="%3$s" target="_blank">Click to renew an existing license</a> or %4$s.', 'give' ),
631
								urldecode( $subscription['invoice_url'] ),
632
								$subscription['payment_id'],
633
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
634
								Give()->notices->get_dismiss_link( array(
635
									'title'            => __( 'Click here if already renewed', 'give' ),
636
									'dismissible_type' => 'user',
637
									'dismiss_interval' => 'permanent',
638
								) )
639
							),
640
							'dismissible_type' => 'user',
641
							'dismiss_interval' => 'shortly',
642
						) );
643
					} else {
644
						Give()->notices->register_notice( array(
645
							'id'               => "give-expires-subscription-{$subscription['id']}",
646
							'type'             => 'error',
647
							'description'      => sprintf(
648
								__( 'Your Give add-on license will expire in %1$s for payment <a href="%2$s" target="_blank">#%3$d</a>. <a href="%4$s" target="_blank">Click to renew an existing license</a> or %5$s.', 'give' ),
649
								human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ),
650
								urldecode( $subscription['invoice_url'] ),
651
								$subscription['payment_id'],
652
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
653
								Give()->notices->get_dismiss_link( array(
654
									'title'            => __( 'Click here if already renewed', 'give' ),
655
									'dismissible_type' => 'user',
656
									'dismiss_interval' => 'permanent',
657
								) )
658
							),
659
							'dismissible_type' => 'user',
660
							'dismiss_interval' => 'shortly',
661
						) );
662
					}
663
664
					// Stop validation for these license keys.
665
					$addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] );
666
				}// End foreach().
667
				$showed_subscriptions_message = true;
668
			}// End if().
669
670
			// Show Non Subscription Give Add-on messages.
671
			if (
672
				! in_array( $this->license, $addon_license_key_in_subscriptions )
673
				&& ! empty( $this->license )
674
				&& empty( $showed_invalid_message )
675
				&& ! $this->is_valid_license()
676
			) {
677
678
				Give()->notices->register_notice( array(
679
					'id'               => 'give-invalid-license',
680
					'type'             => 'error',
681
					'description'      => sprintf(
682
						__( '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' ),
683
						admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
684
					),
685
					'dismissible_type' => 'user',
686
					'dismiss_interval' => 'shortly',
687
				) );
688
689
				$showed_invalid_message = true;
690
691
			}
692
		}
693
694
		/**
695
		 * Check if license is valid or not.
696
		 *
697
		 * @since  1.7
698
		 * @access public
699
		 *
700
		 * @param null|object $licence_data
701
		 *
702
		 * @return bool
703
		 */
704
		public function is_valid_license( $licence_data = null ) {
705
			$license_data = empty( $licence_data ) ? $this->license_data : $licence_data;
706
707
			if ( apply_filters( 'give_is_valid_license', ( $this->is_license( $license_data ) && 'valid' === $license_data->license ) ) ) {
708
				return true;
709
			}
710
711
			return false;
712
		}
713
714
715
		/**
716
		 * Check if license is license object of no.
717
		 *
718
		 * @since  1.7
719
		 * @access public
720
		 *
721
		 * @param null|object $licence_data
722
		 *
723
		 * @return bool
724
		 */
725
		public function is_license( $licence_data = null ) {
726
			$license_data = empty( $licence_data ) ? $this->license_data : $licence_data;
727
728
			if ( apply_filters( 'give_is_license', ( is_object( $license_data ) && ! empty( $license_data ) && property_exists( $license_data, 'license' ) ) ) ) {
729
				return true;
730
			}
731
732
			return false;
733
		}
734
735
		/**
736
		 * Check if license is valid or not.
737
		 *
738
		 * @access private
739
		 * @since  1.7
740
		 *
741
		 * @return bool
742
		 */
743
		private function __is_third_party_addon() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_License::__is_third_party_addon" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
744
			return ( false === strpos( $this->api_url, 'givewp.com/' ) );
745
		}
746
747
		/**
748
		 * Remove license key from subscription.
749
		 *
750
		 * This function mainly uses when admin user deactivate license key,
751
		 * then we do not need subscription information for that license key.
752
		 *
753
		 * @access private
754
		 * @since  1.7
755
		 *
756
		 * @return bool
757
		 */
758
		private function __remove_license_key_from_subscriptions() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_License::__remove_license_key_from_subscriptions" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
759
			$subscriptions = get_option( 'give_subscriptions', array() );
760
761
			// Bailout.
762
			if ( empty( $this->license ) ) {
763
				return false;
764
			}
765
766
			if ( ! empty( $subscriptions ) ) {
767
				foreach ( $subscriptions as $subscription_id => $subscription ) {
768
					$license_index = array_search( $this->license, $subscription['licenses'] );
769
					if ( false !== $license_index ) {
770
						// Remove license key.
771
						unset( $subscriptions[ $subscription_id ]['licenses'][ $license_index ] );
772
773
						// Rearrange license keys.
774
						$subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] );
775
776
						// Update subscription information.
777
						update_option( 'give_subscriptions', $subscriptions );
778
						break;
779
					}
780
				}
781
			}
782
		}
783
784
		/**
785
		 * @param $plugin_file
786
		 * @param $plugin_data
787
		 * @param $status
788
		 *
789
		 * @return bool
790
		 */
791
		public function plugin_page_notices( $plugin_file, $plugin_data, $status ) {
0 ignored issues
show
Unused Code introduced by
The parameter $plugin_file 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...
Unused Code introduced by
The parameter $plugin_data 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...
Unused Code introduced by
The parameter $status 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...
792
			// Bailout.
793
			if ( $this->is_valid_license() ) {
794
				return false;
795
			}
796
797
			$update_notice_wrap = '<tr class="give-addon-notice-tr active"><td colspan="3" class="colspanchange"><div class="notice inline notice-warning notice-alt give-invalid-license"><p><span class="dashicons dashicons-info"></span> %s</p></div></td></tr>';
798
			$message            = $this->license_state_message();
799
800
			if ( ! empty( $message['message'] ) ) {
801
				echo sprintf( $update_notice_wrap, $message['message'] );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
802
			}
803
		}
804
805
806
		/**
807
		 * Get message related to license state.
808
		 *
809
		 * @since  1.8.7
810
		 * @access public
811
		 * @return array
812
		 */
813
		public function license_state_message() {
814
			$message_data = array();
815
816
			if ( ! $this->is_valid_license() ) {
817
818
				$message_data['message'] = sprintf(
819
					'Please <a href="%1$s">activate your license</a> to receive updates and support for the %2$s add-on.',
820
					esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) ),
821
					$this->item_name
822
				);
823
			}
824
825
			return $message_data;
826
		}
827
828
829
		/**
830
		 * Check if admin can edit license or not,
831
		 *
832
		 * @since  1.8.9
833
		 * @access private
834
		 */
835
		private function __is_user_can_edit_license() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_License::__is_user_can_edit_license" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
836
			// Bailout.
837
			if (
838
				! Give_Admin_Settings::verify_nonce() ||
839
				! current_user_can( 'manage_give_settings' ) ||
840
				'licenses' !== give_get_current_setting_tab()
841
			) {
842
				return false;
843
			}
844
845
			// Security check.
846
			if (
847
				isset( $_POST[ $this->item_shortname . '_license_key-nonce' ] ) &&
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
848
				! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' )
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
849
			) {
850
				wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
851
			}
852
853
			return true;
854
		}
855
856
857
		/**
858
		 * Get license information.
859
		 *
860
		 * @since  1.8.9
861
		 * @access public
862
		 *
863
		 * @param string $edd_action
864
		 * @param bool   $response_in_array
865
		 *
866
		 * @return mixed
867
		 */
868
		public function get_license_info( $edd_action = '', $response_in_array = false ) {
869
870
			if ( empty( $edd_action ) ) {
871
				return false;
872
			}
873
874
			// Data to send to the API.
875
			$api_params = array(
876
				'edd_action' => $edd_action, // never change from "edd_" to "give_"!
877
				'license'    => $this->license,
878
				'item_name'  => urlencode( $this->item_name ),
879
				'url'        => home_url(),
880
			);
881
882
			// Call the API.
883
			$response = wp_remote_post(
884
				$this->api_url,
885
				array(
886
					'timeout'   => 15,
887
					'sslverify' => false,
888
					'body'      => $api_params,
889
				)
890
			);
891
892
			// Make sure there are no errors.
893
			if ( is_wp_error( $response ) ) {
894
				return false;
895
			}
896
897
			return json_decode( wp_remote_retrieve_body( $response ), $response_in_array );
898
		}
899
900
901
		/**
902
		 * Unset license
903
		 *
904
		 * @since  1.8.14
905
		 * @access private
906
		 */
907
		private function unset_license() {
908
909
			// Remove license key from subscriptions if exist.
910
			$this->__remove_license_key_from_subscriptions();
911
912
			// Remove license from database.
913
			delete_option( "{$this->item_shortname}_license_active" );
914
			give_delete_option( "{$this->item_shortname}_license_key" );
915
			unset( $_POST["{$this->item_shortname}_license_key"] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
916
917
			// Unset license param.
918
			$this->license = '';
919
		}
920
921
922
		/**
923
		 * Check if deactivating any license key or not.
924
		 *
925
		 * @since  1.8.17
926
		 * @access private
927
		 *
928
		 * @return bool
929
		 */
930
		private function is_deactivating_license() {
931
			$status = false;
932
933
			foreach ( $_POST as $key => $value ) {
934
				if ( false !== strpos( $key, 'license_key_deactivate' ) ) {
935
					$status = true;
936
					break;
937
				}
938
			}
939
940
			return $status;
941
		}
942
	}
943
944
endif; // end class_exists check.
945