Test Failed
Push — issues/1944 ( 5c19d0...2ded50 )
by Ravinder
04:30
created

Give_License::__construct()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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