Test Failed
Pull Request — master (#2482)
by Devin
05:37
created

Give_License::__construct()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

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
ccs 0
cts 7
cp 0
crap 72

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' ), 10 );
209
210
			// Deactivate license key.
211
			add_action( 'admin_init', array( $this, 'deactivate_license' ), 11 );
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
			// Delete previous license setting if a empty license key submitted.
341
			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...
342
				$this->unset_license();
343
344
				return;
345
			}
346
347
			// Do not simultaneously activate add-ons if the user want to deactivate a specific add-on.
348
			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...
349
				return;
350
			}
351
352
			// Check if plugin previously installed.
353
			if ( $this->is_valid_license() ) {
354
				return;
355
			}
356
357
			// Get license key.
358
			$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...
359
360
			// Delete previous license key from subscription if previously added.
361
			$this->__remove_license_key_from_subscriptions();
362
363
			// Make sure there are no api errors.
364
			if ( ! ( $license_data = $this->get_license_info( 'activate_license' ) ) ) {
365
				return;
366
			}
367
368
			// Make sure license is valid.
369
			// return because admin will want to activate license again.
370
			if ( ! $this->is_license( $license_data ) ) {
371
				// Add license key.
372
				give_update_option( "{$this->item_shortname}_license_key", $this->license );
373
374
				return;
375
			}
376
377
			// Tell WordPress to look for updates.
378
			set_site_transient( 'update_plugins', null );
379
380
			// Add license data.
381
			update_option( "{$this->item_shortname}_license_active", $license_data );
382
383
			// Add license key.
384
			give_update_option( "{$this->item_shortname}_license_key", $this->license );
385
386
			// Check subscription for license key and store this to db (if any).
387
			$this->__single_subscription_check();
388
		}
389
390
		/**
391
		 * Deactivate License
392
		 *
393
		 * Deactivate the license key.
394
		 *
395
		 * @access public
396
		 * @since  1.0
397
		 *
398
		 * @return void
399
		 */
400
		public function deactivate_license() {
401
			// Bailout.
402
			if ( ! $this->__is_user_can_edit_license() ) {
403
				return;
404
			}
405
406
			// Allow third party add-on developers to handle license deactivation.
407
			if ( $this->__is_third_party_addon() ) {
408
				do_action( 'give_deactivate_license', $this );
409
410
				return;
411
			}
412
413
			// Run on deactivate button press.
414
			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...
415
				$this->unset_license();
416
			}
417
		}
418
419
		/**
420
		 * Check if license key is valid once per week.
421
		 *
422
		 * @access public
423
		 * @since  1.7
424
		 *
425
		 * @return void
426
		 */
427
		public function weekly_license_check() {
428
429
			if (
430
				! empty( $_POST['give_settings'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
431
				empty( $this->license )
432
			) {
433
				return;
434
			}
435
436
			// Allow third party add-on developers to handle their license check.
437
			if ( $this->__is_third_party_addon() ) {
438
				do_action( 'give_weekly_license_check', $this );
439
440
				return;
441
			}
442
443
			// Make sure there are no api errors.
444
			if ( ! ( $license_data = $this->get_license_info( 'check_license' ) ) ) {
445
				return;
446
			}
447
448
			// Bailout.
449
			if ( ! $this->is_license( $license_data ) ) {
450
				return;
451
			}
452
453
			update_option( $this->item_shortname . '_license_active', $license_data );
454
455
			return;
456
		}
457
458
		/**
459
		 * Check subscription validation once per week
460
		 *
461
		 * @access public
462
		 * @since  1.7
463
		 *
464
		 * @return void
465
		 */
466
		public function weekly_subscription_check() {
467
			// Bailout.
468
			if (
469
				! empty( $_POST['give_settings'] ) ||
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
470
				empty( $this->license )
471
			) {
472
				return;
473
			}
474
475
			// Remove old subscription data.
476
			if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp', 1 ) ) {
477
				delete_option( 'give_subscriptions' );
478
				update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) );
479
			}
480
481
			// Allow third party add-on developers to handle their subscription check.
482
			if ( $this->__is_third_party_addon() ) {
483
				do_action( 'give_weekly_subscription_check', $this );
484
485
				return;
486
			}
487
488
			$this->__single_subscription_check();
489
		}
490
491
		/**
492
		 * Check if license key is part of subscription or not
493
		 *
494
		 * @access private
495
		 * @since  1.7
496
		 *
497
		 * @return void
498
		 */
499
		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...
500
			if ( empty( $this->license ) ) {
501
				return;
502
			}
503
504
			/**
505
			 * Make sure there are no api errors.
506
			 *
507
			 * Do not get confused with edd_action check_subscription.
508
			 * By default edd software licensing api does not have api to check subscription.
509
			 * This is a custom feature to check subscriptions.
510
			 */
511
			$subscription_data = $this->get_license_info( 'check_subscription', true );
512
513
			if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
514
515
				$subscriptions = get_option( 'give_subscriptions', array() );
516
517
				// Update subscription data only if subscription does not exist already.
518
				$subscriptions[ $subscription_data['id'] ] = $subscription_data;
519
520
				// Initiate default set of license for subscription.
521
				if ( ! isset( $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
522
					$subscriptions[ $subscription_data['id'] ]['licenses'] = array();
523
				}
524
525
				// Store licenses for subscription.
526
				if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
527
					$subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
528
				}
529
530
				update_option( 'give_subscriptions', $subscriptions );
531
			}
532
		}
533
534
		/**
535
		 * Admin notices for errors
536
		 *
537
		 * @access public
538
		 * @since  1.0
539
		 *
540
		 * @return void
541
		 */
542
		public function notices() {
543
544
			if ( ! current_user_can( 'manage_give_settings' ) ) {
545
				return;
546
			}
547
548
			// Do not show licenses notices on license tab.
549
			if ( 'licenses' === give_get_current_setting_tab() ) {
550
				return;
551
			}
552
553
			static $showed_invalid_message;
554
			static $showed_subscriptions_message;
555
			static $addon_license_key_in_subscriptions;
556
557
			// Set default value.
558
			$addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array();
559
			$messages                           = array();
560
561
			// Check whether admin has Give Add-on activated since 24 hours?
562
			$is_license_notice_hidden = Give_Cache::get( 'give_cache_hide_license_notice_after_activation' );
563
564
			// Display Invalid License notice, if its more than 24 hours since first Give Add-on activation.
565
			if (
566
				empty( $this->license )
567
				&& empty( $showed_invalid_message )
568
				&& ( false === $is_license_notice_hidden )
569
			) {
570
571
				Give()->notices->register_notice( array(
572
					'id'               => 'give-invalid-license',
573
					'type'             => 'error',
574
					'description'      => sprintf(
575
						__( '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' ),
576
						admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
577
					),
578
					'dismissible_type' => 'user',
579
					'dismiss_interval' => 'shortly',
580
				) );
581
582
				$showed_invalid_message = true;
583
584
			}
585
586
			// Get subscriptions.
587
			$subscriptions = get_option( 'give_subscriptions' );
588
589
			// Show subscription messages.
590
			if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) {
591
592
				foreach ( $subscriptions as $subscription ) {
593
					// Subscription expires timestamp.
594
					$subscription_expires = strtotime( $subscription['expires'] );
595
596
					// Start showing subscriptions message before one week of renewal date.
597
					if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) {
598
						continue;
599
					}
600
601
					// Check if subscription message already exist in messages.
602
					if ( array_key_exists( $subscription['id'], $messages ) ) {
603
						continue;
604
					}
605
606
					// Check if license already expired.
607
					if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) {
608
						Give()->notices->register_notice( array(
609
							'id'               => "give-expired-subscription-{$subscription['id']}",
610
							'type'             => 'error',
611
							'description'      => sprintf(
612
								__( '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' ),
613
								urldecode( $subscription['invoice_url'] ),
614
								$subscription['payment_id'],
615
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
616
								Give()->notices->get_dismiss_link( array(
617
									'title'            => __( 'Click here if already renewed', 'give' ),
618
									'dismissible_type' => 'user',
619
									'dismiss_interval' => 'permanent',
620
								) )
621
							),
622
							'dismissible_type' => 'user',
623
							'dismiss_interval' => 'shortly',
624
						) );
625
					} else {
626
						Give()->notices->register_notice( array(
627
							'id'               => "give-expires-subscription-{$subscription['id']}",
628
							'type'             => 'error',
629
							'description'      => sprintf(
630
								__( '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' ),
631
								human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ),
632
								urldecode( $subscription['invoice_url'] ),
633
								$subscription['payment_id'],
634
								"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
635
								Give()->notices->get_dismiss_link( array(
636
									'title'            => __( 'Click here if already renewed', 'give' ),
637
									'dismissible_type' => 'user',
638
									'dismiss_interval' => 'permanent',
639
								) )
640
							),
641
							'dismissible_type' => 'user',
642
							'dismiss_interval' => 'shortly',
643
						) );
644
					}
645
646
					// Stop validation for these license keys.
647
					$addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] );
648
				}// End foreach().
649
				$showed_subscriptions_message = true;
650
			}// End if().
651
652
			// Show Non Subscription Give Add-on messages.
653
			if (
654
				! in_array( $this->license, $addon_license_key_in_subscriptions )
655
				&& ! empty( $this->license )
656
				&& empty( $showed_invalid_message )
657
				&& ! $this->is_valid_license()
658
			) {
659
660
				Give()->notices->register_notice( array(
661
					'id'               => 'give-invalid-license',
662
					'type'             => 'error',
663
					'description'      => sprintf(
664
						__( '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' ),
665
						admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
666
					),
667
					'dismissible_type' => 'user',
668
					'dismiss_interval' => 'shortly',
669
				) );
670
671
				$showed_invalid_message = true;
672
673
			}
674
		}
675
676
		/**
677
		 * Check if license is valid or not.
678
		 *
679
		 * @since  1.7
680
		 * @access public
681
		 *
682
		 * @param null|object $licence_data
683
		 *
684
		 * @return bool
685
		 */
686
		public function is_valid_license( $licence_data = null ) {
687
			$license_data = empty( $licence_data ) ? $this->license_data : $licence_data;
688
689
			if ( apply_filters( 'give_is_valid_license', ( $this->is_license( $license_data ) && 'valid' === $license_data->license ) ) ) {
690
				return true;
691
			}
692
693
			return false;
694
		}
695
696
697
		/**
698
		 * Check if license is license object of no.
699
		 *
700
		 * @since  1.7
701
		 * @access public
702
		 *
703
		 * @param null|object $licence_data
704
		 *
705
		 * @return bool
706
		 */
707
		public function is_license( $licence_data = null ) {
708
			$license_data = empty( $licence_data ) ? $this->license_data : $licence_data;
709
710
			if ( apply_filters( 'give_is_license', ( is_object( $license_data ) && ! empty( $license_data ) && property_exists( $license_data, 'license' ) ) ) ) {
711
				return true;
712
			}
713
714
			return false;
715
		}
716
717
		/**
718
		 * Check if license is valid or not.
719
		 *
720
		 * @access private
721
		 * @since  1.7
722
		 *
723
		 * @return bool
724
		 */
725
		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...
726
			return ( false === strpos( $this->api_url, 'givewp.com/' ) );
727
		}
728
729
		/**
730
		 * Remove license key from subscription.
731
		 *
732
		 * This function mainly uses when admin user deactivate license key,
733
		 * then we do not need subscription information for that license key.
734
		 *
735
		 * @access private
736
		 * @since  1.7
737
		 *
738
		 * @return bool
739
		 */
740
		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...
741
			$subscriptions = get_option( 'give_subscriptions', array() );
742
743
			// Bailout.
744
			if ( empty( $this->license ) ) {
745
				return false;
746
			}
747
748
			if ( ! empty( $subscriptions ) ) {
749
				foreach ( $subscriptions as $subscription_id => $subscription ) {
750
					$license_index = array_search( $this->license, $subscription['licenses'] );
751
					if ( false !== $license_index ) {
752
						// Remove license key.
753
						unset( $subscriptions[ $subscription_id ]['licenses'][ $license_index ] );
754
755
						// Rearrange license keys.
756
						$subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] );
757
758
						// Update subscription information.
759
						update_option( 'give_subscriptions', $subscriptions );
760
						break;
761
					}
762
				}
763
			}
764
		}
765
766
		/**
767
		 * @param $plugin_file
768
		 * @param $plugin_data
769
		 * @param $status
770
		 *
771
		 * @return bool
772
		 */
773
		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...
774
			// Bailout.
775
			if ( $this->is_valid_license() ) {
776
				return false;
777
			}
778
779
			$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>';
780
			$message            = $this->license_state_message();
781
782
			if ( ! empty( $message['message'] ) ) {
783
				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...
784
			}
785
		}
786
787
788
		/**
789
		 * Get message related to license state.
790
		 *
791
		 * @since  1.8.7
792
		 * @access public
793
		 * @return array
794
		 */
795
		public function license_state_message() {
796
			$message_data = array();
797
798
			if ( ! $this->is_valid_license() ) {
799
800
				$message_data['message'] = sprintf(
801
					'Please <a href="%1$s">activate your license</a> to receive updates and support for the %2$s add-on.',
802
					esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) ),
803
					$this->item_name
804
				);
805
			}
806
807
			return $message_data;
808
		}
809
810
811
		/**
812
		 * Check if admin can edit license or not,
813
		 *
814
		 * @since  1.8.9
815
		 * @access private
816
		 */
817
		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...
818
			// Bailout.
819
			if (
820
				! Give_Admin_Settings::verify_nonce() ||
821
				! current_user_can( 'manage_give_settings' ) ||
822
				'licenses' !== give_get_current_setting_tab()
823
			) {
824
				return false;
825
			}
826
827
			// Security check.
828
			if (
829
				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...
830
				! 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...
831
			) {
832
				wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
833
			}
834
835
			return true;
836
		}
837
838
839
		/**
840
		 * Get license information.
841
		 *
842
		 * @since  1.8.9
843
		 * @access public
844
		 *
845
		 * @param string $edd_action
846
		 * @param bool   $response_in_array
847
		 *
848
		 * @return mixed
849
		 */
850
		public function get_license_info( $edd_action = '', $response_in_array = false ) {
851
852
			if ( empty( $edd_action ) ) {
853
				return false;
854
			}
855
856
			// Data to send to the API.
857
			$api_params = array(
858
				'edd_action' => $edd_action, // never change from "edd_" to "give_"!
859
				'license'    => $this->license,
860
				'item_name'  => urlencode( $this->item_name ),
861
				'url'        => home_url(),
862
			);
863
864
			// Call the API.
865
			$response = wp_remote_post(
866
				$this->api_url,
867
				array(
868
					'timeout'   => 15,
869
					'sslverify' => false,
870
					'body'      => $api_params,
871
				)
872
			);
873
874
			// Make sure there are no errors.
875
			if ( is_wp_error( $response ) ) {
876
				return false;
877
			}
878
879
			return json_decode( wp_remote_retrieve_body( $response ), $response_in_array );
880
		}
881
882
883
		/**
884
		 * Unset license
885
		 *
886
		 * @since  1.8.14
887
		 * @access private
888
		 */
889
		private function unset_license() {
890
891
			// Remove license key from subscriptions if exist.
892
			$this->__remove_license_key_from_subscriptions();
893
894
			// Remove license from database.
895
			delete_option( "{$this->item_shortname}_license_active" );
896
			give_delete_option( "{$this->item_shortname}_license_key" );
897
			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...
898
899
			// Unset license param.
900
			$this->license = '';
901
		}
902
903
904
		/**
905
		 * Check if deactivating any license key or not.
906
		 *
907
		 * @since  1.8.17
908
		 * @access private
909
		 *
910
		 * @return bool
911
		 */
912
		private function is_deactivating_license() {
913
			$status = false;
914
915
			foreach ( $_POST as $key => $value ) {
916
				if ( false !== strpos( $key, 'license_key_deactivate' ) ) {
917
					$status = true;
918
					break;
919
				}
920
			}
921
922
			return $status;
923
		}
924
	}
925
926
endif; // end class_exists check.
927