Completed
Push — mehul/issue/1427 ( ad1d03 )
by Ravinder
757:24 queued 740:01
created

Give_License::activate_license()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 10
nop 0
dl 0
loc 56
rs 7.7155
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

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

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

Loading history...
146
147
			$give_options = give_get_settings();
148
149
			$this->file             = $_file;
150
			$this->item_name        = $_item_name;
151
			$this->item_shortname   = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
152
			$this->version          = $_version;
153
			$this->license          = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : '';
154
			$this->license_data     = get_option( $this->item_shortname . '_license_active' );
155
			$this->author           = $_author;
156
			$this->api_url          = is_null( $_api_url ) ? $this->api_url : $_api_url;
157
			$this->checkout_url     = is_null( $_checkout_url ) ? $this->checkout_url : $_checkout_url;
158
			$this->account_url      = is_null( $_account_url ) ? $this->account_url : $_account_url;
159
			$this->auto_updater_obj = null;
0 ignored issues
show
Bug introduced by
The property auto_updater_obj does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
160
161
			// Setup hooks
162
			$this->includes();
163
			$this->hooks();
164
			$this->auto_updater();
165
		}
166
167
		/**
168
		 * Includes
169
		 *
170
		 * Include the updater class.
171
		 *
172
		 * @access private
173
		 * @since  1.0
174
		 *
175
		 * @return void
176
		 */
177
		private function includes() {
178
179
			if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
180
				require_once 'admin/EDD_SL_Plugin_Updater.php';
181
			}
182
		}
183
184
		/**
185
		 * Hooks
186
		 *
187
		 * Setup license hooks.
188
		 *
189
		 * @access private
190
		 * @since  1.0
191
		 *
192
		 * @return void
193
		 */
194
		private function hooks() {
195
196
			// Register settings
197
			add_filter( 'give_settings_licenses', array( $this, 'settings' ), 1 );
198
199
			// Activate license key on settings save
200
			add_action( 'admin_init', array( $this, 'activate_license' ) );
201
202
			// Deactivate license key
203
			add_action( 'admin_init', array( $this, 'deactivate_license' ) );
204
205
			// Updater
206
			add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
207
			add_action( 'admin_notices', array( $this, 'notices' ) );
208
209
			// Check license weekly.
210
			add_action( 'give_weekly_scheduled_events', array( $this, 'weekly_license_check' ) );
211
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) );
212
213
			// Check subscription weekly.
214
			add_action( 'give_weekly_scheduled_events', array( $this, 'weekly_subscription_check' ) );
215
			add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) );
216
217
			// Show addon notice on plugin page.
218
			$plugin_name = explode( 'plugins/', $this->file );
219
			$plugin_name = end( $plugin_name );
220
			add_action( "after_plugin_row_{$plugin_name}", array( $this, 'plugin_page_notices' ), 10, 3 );
221
222
		}
223
224
225
		/**
226
		 * Auto Updater
227
		 *
228
		 * @access private
229
		 * @since  1.0
230
		 *
231
		 * @return void
232
		 */
233
		public function auto_updater() {
234
235
			// Setup the updater
236
			$this->auto_updater_obj = new EDD_SL_Plugin_Updater(
237
				$this->api_url,
238
				$this->file,
239
				array(
240
					'version'   => $this->version,
241
					'license'   => $this->license,
242
					'item_name' => $this->item_name,
243
					'author'    => $this->author,
244
				)
245
			);
246
		}
247
248
		/**
249
		 * License Settings
250
		 *
251
		 * Add license field to settings.
252
		 *
253
		 * @access public
254
		 * @since  1.0
255
		 *
256
		 * @param  array $settings License settings.
257
		 *
258
		 * @return array           License settings.
259
		 */
260
		public function settings( $settings ) {
261
262
			$give_license_settings = array(
263
				array(
264
					'name'    => $this->item_name,
265
					'id'      => $this->item_shortname . '_license_key',
266
					'desc'    => '',
267
					'type'    => 'license_key',
268
					'options' => array(
269
						'license'      => get_option( $this->item_shortname . '_license_active' ),
270
						'shortname'    => $this->item_shortname,
271
						'item_name'    => $this->item_name,
272
						'api_url'      => $this->api_url,
273
						'checkout_url' => $this->checkout_url,
274
						'account_url'  => $this->account_url,
275
					),
276
					'size'    => 'regular',
277
				),
278
			);
279
280
			return array_merge( $settings, $give_license_settings );
281
		}
282
283
		/**
284
		 * License Settings Content
285
		 *
286
		 * Add Some Content to the Licensing Settings.
287
		 *
288
		 * @access public
289
		 * @since  1.0
290
		 *
291
		 * @param  array $settings License settings content.
292
		 *
293
		 * @return array           License settings content.
294
		 */
295
		public function license_settings_content( $settings ) {
296
297
			$give_license_settings = array(
298
				array(
299
					'name' => __( 'Add-on Licenses', 'give' ),
300
					'desc' => '<hr>',
301
					'type' => 'give_title',
302
					'id'   => 'give_title',
303
				),
304
			);
305
306
			return array_merge( $settings, $give_license_settings );
307
		}
308
309
		/**
310
		 * Activate License
311
		 *
312
		 * Activate the license key.
313
		 *
314
		 * @access public
315
		 * @since  1.0
316
		 *
317
		 * @return void
318
		 */
319
		public function activate_license() {
320
			// Bailout.
321
			if( ! $this->__is_user_can_edit_license() ) {
322
				return;
323
			}
324
325
			// Allow third party addon developers to handle license activation.
326
			if ( $this->__is_third_party_addon() ) {
327
				do_action( 'give_activate_license', $this );
328
329
				return;
330
			}
331
332
			// Delete previous license setting if a empty license key submitted.
333
			if ( empty( $_POST[ $this->item_shortname . '_license_key' ] ) ) {
334
				delete_option( $this->item_shortname . '_license_active' );
335
336
				return;
337
			}
338
339
			// Do not simultaneously activate add-ons if the user want to deactivate a specific add-on.
340
			foreach ( $_POST as $key => $value ) {
341
				if ( false !== strpos( $key, 'license_key_deactivate' ) ) {
342
					// Don't activate a key when deactivating a different key
343
					return;
344
				}
345
			}
346
347
			// Check if plugin previously installed.
348
			if ( $this->is_valid_license() ) {
349
				return;
350
			}
351
352
			// Get license key.
353
			$this->license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] );
354
355
			// Delete previous license key from subscription if previously added.
356
			$this->__remove_license_key_from_subscriptions();
357
358
			// Make sure there are no api errors
359
			if ( ! ( $license_data = $this->get_license_info( 'activate_license' ) ) ) {
360
				return;
361
			}
362
363
			// Tell WordPress to look for updates
364
			set_site_transient( 'update_plugins', null );
365
366
			// Add license data.
367
			update_option( $this->item_shortname . '_license_active', $license_data );
368
369
			// Add license key.
370
			give_update_option( "{$this->item_shortname}_license_key", $this->license );
371
372
			// Check subscription for license key and store this to db (if any).
373
			$this->__single_subscription_check();
374
		}
375
376
		/**
377
		 * Deactivate License
378
		 *
379
		 * Deactivate the license key.
380
		 *
381
		 * @access public
382
		 * @since  1.0
383
		 *
384
		 * @return void
385
		 */
386
		public function deactivate_license() {
387
			// Bailout.
388
			if( ! $this->__is_user_can_edit_license() ) {
389
				return;
390
			}
391
392
			// Allow third party add-on developers to handle license deactivation.
393
			if ( $this->__is_third_party_addon() ) {
394
				do_action( 'give_deactivate_license', $this );
395
396
				return;
397
			}
398
399
			// Run on deactivate button press
400
			if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
401
402
				// Make sure there are no api errors
403
				if ( ! ( $license_data = $this->get_license_info( 'deactivate_license' ) ) ) {
404
					return;
405
				}
406
407
				// Ensure deactivated successfully.
408
				if ( isset( $license_data->success ) ) {
409
410
					// Remove license data.
411
					delete_option( $this->item_shortname . '_license_active' );
412
413
					// Delete licence data.
414
					give_delete_option( $this->item_shortname . '_license_key' );
415
416
					// Remove license key from subscriptions if exist.
417
					$this->__remove_license_key_from_subscriptions();
418
419
				}
420
			}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
421
		}
422
423
		/**
424
		 * Check if license key is valid once per week.
425
		 *
426
		 * @access public
427
		 * @since  1.7
428
		 *
429
		 * @return void
430
		 */
431
		public function weekly_license_check() {
432
433
			if (
434
				! empty( $_POST['give_settings'] ) ||
435
				empty( $this->license )
436
			) {
437
				return;
438
			}
439
440
			// Allow third party add-on developers to handle their license check.
441
			if ( $this->__is_third_party_addon() ) {
442
				do_action( 'give_weekly_license_check', $this );
443
444
				return;
445
			}
446
447
			// Make sure there are no api errors
448
			if ( ! ( $license_data = $this->get_license_info( 'check_license' ) ) ) {
449
				return;
450
			}
451
452
			update_option( $this->item_shortname . '_license_active', $license_data );
453
454
			return;
455
		}
456
457
		/**
458
		 * Check subscription validation once per week
459
		 *
460
		 * @access public
461
		 * @since  1.7
462
		 *
463
		 * @return void
464
		 */
465
		public function weekly_subscription_check() {
466
			// Bailout.
467
			if (
468
				! empty( $_POST['give_settings'] ) ||
469
				empty( $this->license )
470
			) {
471
				return;
472
			}
473
474
			// Remove old subscription data.
475
			if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp', 1 ) ) {
476
				delete_option( 'give_subscriptions' );
477
				update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) );
478
			}
479
480
			// Allow third party add-on developers to handle their subscription check.
481
			if ( $this->__is_third_party_addon() ) {
482
				do_action( 'give_weekly_subscription_check', $this );
483
484
				return;
485
			}
486
487
			$this->__single_subscription_check();
488
		}
489
490
		/**
491
		 * Check if license key is part of subscription or not
492
		 *
493
		 * @access private
494
		 * @since  1.7
495
		 *
496
		 * @return void
497
		 */
498
		private function __single_subscription_check() {
499
			if ( empty( $this->license ) ) {
500
				return;
501
			}
502
503
			// Make sure there are no api errors
504
			// Do not get confused with edd_action check_subscription.
505
			// By default edd software licensing api does not have api to check subscription.
506
			// This is a custom feature to check subscriptions.
507
			if ( ! ( $subscription_data = $this->get_license_info( 'check_subscription' ) ) ) {
508
				return;
509
			}
510
511
			if ( ! empty( $subscription_data->success ) && absint( $subscription_data->success ) ) {
512
				$subscriptions = get_option( 'give_subscriptions', array() );
513
514
				// Update subscription data only if subscription does not exist already.
515
				if ( ! array_key_exists( $subscription_data->id, $subscriptions ) ) {
516
					$subscriptions[ $subscription_data->id ]             = $subscription_data;
517
					$subscriptions[ $subscription_data->id ]['licenses'] = array();
518
				}
519
520
				// Store licenses for subscription.
521
				if ( ! in_array( $this->license, $subscriptions[ $subscription_data->id ]['licenses'] ) ) {
522
					$subscriptions[ $subscription_data->id ]['licenses'][] = $this->license;
523
				}
524
525
				update_option( 'give_subscriptions', $subscriptions );
526
			}
527
		}
528
529
		/**
530
		 * Admin notices for errors
531
		 *
532
		 * @access public
533
		 * @since  1.0
534
		 *
535
		 * @return void
536
		 */
537
		public function notices() {
538
539
			if ( ! current_user_can( 'manage_give_settings' ) ) {
540
				return;
541
			}
542
543
			// Do not show licenses notices on license tab.
544
			if ( 'licenses' === give_get_current_setting_tab() ) {
545
				return;
546
			}
547
548
			static $showed_invalid_message;
549
			static $showed_subscriptions_message;
550
			static $addon_license_key_in_subscriptions;
551
552
			// Set default value.
553
			$addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array();
554
			$messages                           = array();
555
556
			if (
557
				empty( $this->license )
558
				&& empty( $showed_invalid_message )
559
			) {
560
561
				Give()->notices->register_notice( array(
562
					'id'               => 'give-invalid-license',
563
					'type'             => 'error',
564
					'description'      => sprintf(
565
						__( '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' ),
566
						admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
567
					),
568
					'dismissible_type' => 'user',
569
					'dismiss_interval' => 'shortly',
570
				) );
571
572
				$showed_invalid_message = true;
573
574
			}
575
576
			// Get subscriptions.
577
			$subscriptions = get_option( 'give_subscriptions' );
578
579
			// Show subscription messages.
580
			if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) {
581
582
				foreach ( $subscriptions as $subscription ) {
583
					// Subscription expires timestamp.
584
					$subscription_expires = strtotime( $subscription['expires'] );
585
586
					// Start showing subscriptions message before one week of renewal date.
587
					if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) {
588
						continue;
589
					}
590
591
					// Check if subscription message already exist in messages.
592
					if ( array_key_exists( $subscription['id'], $messages ) ) {
593
						continue;
594
					}
595
596
					if ( ( 'active' !== $subscription['status'] ) ) {
597
598
						// Check if license already expired.
599
						if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) {
600
							Give()->notices->register_notice( array(
601
								'id'               => "give-expired-subscription-{$subscription['id']}",
602
								'type'             => 'error',
603
								'description'      => sprintf(
604
									__( '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' ),
605
									urldecode( $subscription['invoice_url'] ),
606
									$subscription['payment_id'],
607
									"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
608
									Give()->notices->get_dismiss_link(array(
609
										'title' => __( 'Click here if already renewed', 'give' ),
610
										'dismissible_type'      => 'user',
611
										'dismiss_interval'      => 'permanent',
612
									))
613
								),
614
								'dismissible_type' => 'user',
615
								'dismiss_interval' => 'shortly',
616
							) );
617
						} else {
618
							Give()->notices->register_notice( array(
619
								'id'               => "give-expires-subscription-{$subscription['id']}",
620
								'type'             => 'error',
621
								'description'      => sprintf(
622
									__( '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' ),
623
									human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ),
624
									urldecode( $subscription['invoice_url'] ),
625
									$subscription['payment_id'],
626
									"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
627
									Give()->notices->get_dismiss_link(array(
628
										'title' => __( 'Click here if already renewed', 'give' ),
629
										'dismissible_type'      => 'user',
630
										'dismiss_interval'      => 'permanent',
631
									))
632
								),
633
								'dismissible_type' => 'user',
634
								'dismiss_interval' => 'shortly',
635
							) );
636
						}
637
					}
638
639
					// Stop validation for these license keys.
640
					$addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] );
641
				}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

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

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

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

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

Loading history...
644
645
			// Show non subscription addon messages.
646
			if (
647
				! in_array( $this->license, $addon_license_key_in_subscriptions )
648
				&& ! $this->is_valid_license()
649
				&& empty( $showed_invalid_message )
650
			) {
651
652
				Give()->notices->register_notice( array(
653
					'id'               => 'give-invalid-license',
654
					'type'             => 'error',
655
					'description'      => sprintf(
656
						__( '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' ),
657
						admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
658
					),
659
					'dismissible_type' => 'user',
660
					'dismiss_interval' => 'shortly',
661
				) );
662
663
				$showed_invalid_message = true;
664
665
			}
666
		}
667
668
		/**
669
		 * Check if license is valid or not.
670
		 *
671
		 * @access public
672
		 * @since  1.7
673
		 *
674
		 * @return bool
675
		 */
676
		public function is_valid_license() {
677
			if ( apply_filters( 'give_is_valid_license', ( is_object( $this->license_data ) && ! empty( $this->license_data ) && property_exists( $this->license_data, 'license' ) && 'valid' === $this->license_data->license ) ) ) {
678
				return true;
679
			}
680
681
			return false;
682
		}
683
684
		/**
685
		 * Check if license is valid or not.
686
		 *
687
		 * @access private
688
		 * @since  1.7
689
		 *
690
		 * @return bool
691
		 */
692
		private function __is_third_party_addon() {
693
			return ( false === strpos( $this->api_url, 'givewp.com/' ) );
694
		}
695
696
		/**
697
		 * Remove license key from subscription.
698
		 *
699
		 * This function mainly uses when admin user deactivate license key,
700
		 * then we do not need subscription information for that license key.
701
		 *
702
		 * @access private
703
		 * @since  1.7
704
		 *
705
		 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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

Loading history...
706
		 */
707
		private function __remove_license_key_from_subscriptions() {
708
			$subscriptions = get_option( 'give_subscriptions', array() );
709
710
			// Bailout.
711
			if ( empty( $this->license ) ) {
712
				return false;
713
			}
714
715
			if ( ! empty( $subscriptions ) ) {
716
				foreach ( $subscriptions as $subscription_id => $subscription ) {
717
					$license_index = array_search( $this->license, $subscription['licenses'] );
718
					if ( false !== $license_index ) {
719
						// Remove license key.
720
						unset( $subscriptions[ $subscription_id ]['licenses'][ $license_index ] );
721
722
						// Rearrange license keys.
723
						$subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] );
724
725
						// Update subscription information.
726
						update_option( 'give_subscriptions', $subscriptions );
727
						break;
728
					}
729
				}
730
			}
731
		}
732
733
		/**
734
		 * @param $plugin_file
735
		 * @param $plugin_data
736
		 * @param $status
737
		 *
738
		 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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

Loading history...
739
		 */
740
		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...
741
			// Bailout.
742
			if ( $this->is_valid_license() ) {
743
				return false;
744
			}
745
746
			$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>';
747
			$message            = $this->license_state_message();
748
749
			if ( ! empty( $message['message'] ) ) {
750
				echo sprintf( $update_notice_wrap, $message['message'] );
751
			}
752
		}
753
754
755
		/**
756
		 * Get message related to license state.
757
		 *
758
		 * @since  1.8.7
759
		 * @access public
760
		 * @return array
761
		 */
762
		public function license_state_message() {
763
			$message_data = array();
764
765
			if ( ! $this->is_valid_license() ) {
766
767
				$message_data['message'] = sprintf(
768
					'Please <a href="%1$s">activate your license</a> to receive updates and support for the %2$s add-on.',
769
					esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) ),
770
					$this->item_name
771
				);
772
			}
773
774
			return $message_data;
775
		}
776
777
778
		/**
779
		 * Check if admin can edit license or not,
780
		 *
781
		 * @since 1.8.9
782
		 * @access private
783
		 */
784
		private function __is_user_can_edit_license(){
785
			// Bailout.
786
			if (
787
				empty( $_POST[ $this->item_shortname . '_license_key' ] ) ||
788
				! current_user_can( 'manage_give_settings' )
789
			) {
790
				return false;
791
			}
792
793
			// Security check.
794
			if ( ! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' ) ) {
795
				wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
796
			}
797
798
			return true;
799
		}
800
801
802
		/**
803
		 * Get license information.
804
		 *
805
		 * @since  1.8.9
806
		 * @access public
807
		 *
808
		 * @param string $edd_action
809
		 *
810
		 * @return mixed
811
		 */
812
		public function get_license_info( $edd_action = '' ) {
813
			if( empty( $edd_action ) ) {
814
				return false;
815
			}
816
817
			// Data to send to the API
818
			$api_params = array(
819
				'edd_action' => $edd_action, // never change from "edd_" to "give_"!
820
				'license'    => $this->license,
821
				'item_name'  => urlencode( $this->item_name ),
822
				'url'        => home_url(),
823
			);
824
825
			// Call the API
826
			$response = wp_remote_post(
827
				$this->api_url,
828
				array(
829
					'timeout'   => 15,
830
					'sslverify' => false,
831
					'body'      => $api_params,
832
				)
833
			);
834
835
			// Make sure there are no errors
836
			if ( is_wp_error( $response ) ) {
837
				return false;
838
			}
839
840
			return json_decode( wp_remote_retrieve_body( $response ) );
841
		}
842
	}
843
844
endif; // end class_exists check
845