PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.1.2
GiveWP – Donation Plugin and Fundraising Platform v2.1.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / class-give-license-handler.php
give / includes Last commit date
admin 8 years ago api 8 years ago deprecated 8 years ago donors 8 years ago emails 8 years ago forms 8 years ago gateways 8 years ago libraries 8 years ago payments 8 years ago actions.php 8 years ago ajax-functions.php 8 years ago class-give-async-process.php 8 years ago class-give-background-updater.php 8 years ago class-give-cache.php 8 years ago class-give-cli-commands.php 8 years ago class-give-cron.php 8 years ago class-give-db-donor-meta.php 8 years ago class-give-db-donors.php 8 years ago class-give-db-form-meta.php 8 years ago class-give-db-logs-meta.php 8 years ago class-give-db-logs.php 8 years ago class-give-db-meta.php 8 years ago class-give-db-payment-meta.php 8 years ago class-give-db-sequential-ordering.php 8 years ago class-give-db.php 8 years ago class-give-donate-form.php 8 years ago class-give-donor.php 8 years ago class-give-email-access.php 8 years ago class-give-gravatars.php 8 years ago class-give-html-elements.php 8 years ago class-give-license-handler.php 8 years ago class-give-logging.php 8 years ago class-give-roles.php 8 years ago class-give-scripts.php 8 years ago class-give-session.php 8 years ago class-give-stats.php 8 years ago class-give-template-loader.php 8 years ago class-give-tooltips.php 8 years ago class-give-translation.php 8 years ago class-notices.php 8 years ago country-functions.php 8 years ago currency-functions.php 8 years ago error-tracking.php 8 years ago filters.php 8 years ago formatting.php 8 years ago import-functions.php 8 years ago install.php 8 years ago login-register.php 8 years ago misc-functions.php 8 years ago plugin-compatibility.php 8 years ago post-types.php 8 years ago price-functions.php 8 years ago process-donation.php 8 years ago shortcodes.php 8 years ago template-functions.php 8 years ago user-functions.php 8 years ago
class-give-license-handler.php
945 lines
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 ) {
147
148 $give_options = give_get_settings();
149
150 $this->file = $_file;
151 $this->item_name = $_item_name;
152 $this->item_shortname = self::get_short_name( $this->item_name );
153 $this->version = $_version;
154 $this->license = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : '';
155 $this->license_data = __give_get_active_license_info( $this->item_shortname );
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 ) ) {
165 update_option( 'give_is_addon_activated', true );
166 Give_Cache::set( 'give_cache_hide_license_notice_after_activation', true, DAY_IN_SECONDS );
167 }
168
169 // Setup hooks
170 $this->includes();
171 $this->hooks();
172 $this->auto_updater();
173 }
174
175
176 /**
177 * Get plugin shortname
178 *
179 * @since 2.1.0
180 * @access public
181 *
182 * @param $plugin_name
183 *
184 * @return string
185 */
186 public static function get_short_name( $plugin_name ) {
187 $plugin_name = trim( str_replace( 'Give - ', '', $plugin_name ) );
188 $plugin_name = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $plugin_name ) ) );
189
190 return $plugin_name;
191 }
192
193 /**
194 * Includes
195 *
196 * Include the updater class.
197 *
198 * @access private
199 * @since 1.0
200 *
201 * @return void
202 */
203 private function includes() {
204
205 if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
206 require_once 'admin/EDD_SL_Plugin_Updater.php';
207 }
208 }
209
210 /**
211 * Hooks
212 *
213 * Setup license hooks.
214 *
215 * @access private
216 * @since 1.0
217 *
218 * @return void
219 */
220 private function hooks() {
221
222 // Register settings.
223 add_filter( 'give_settings_licenses', array( $this, 'settings' ), 1 );
224
225 // Activate license key on settings save.
226 add_action( 'admin_init', array( $this, 'activate_license' ), 10 );
227
228 // Deactivate license key.
229 add_action( 'admin_init', array( $this, 'deactivate_license' ), 11 );
230
231 // Updater.
232 add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
233 add_action( 'admin_notices', array( $this, 'notices' ) );
234
235 // Check license weekly.
236 Give_Cron::add_weekly_event( array( $this, 'weekly_license_check' ) );
237 add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) );
238
239 // Check subscription weekly.
240 Give_Cron::add_weekly_event( array( $this, 'weekly_subscription_check' ) );
241 add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) );
242
243 // Show addon notice on plugin page.
244 $plugin_name = explode( 'plugins/', $this->file );
245 $plugin_name = end( $plugin_name );
246 add_action( "after_plugin_row_{$plugin_name}", array( $this, 'plugin_page_notices' ), 10, 3 );
247
248 }
249
250
251 /**
252 * Auto Updater
253 *
254 * @access private
255 * @since 1.0
256 *
257 * @return void
258 */
259 public function auto_updater() {
260
261 // Setup the updater.
262 $this->auto_updater_obj = new EDD_SL_Plugin_Updater(
263 $this->api_url,
264 $this->file,
265 array(
266 'version' => $this->version,
267 'license' => $this->license,
268 'item_name' => $this->item_name,
269 'author' => $this->author,
270 )
271 );
272 }
273
274 /**
275 * License Settings
276 *
277 * Add license field to settings.
278 *
279 * @access public
280 * @since 1.0
281 *
282 * @param array $settings License settings.
283 *
284 * @return array License settings.
285 */
286 public function settings( $settings ) {
287
288 $give_license_settings = array(
289 array(
290 'name' => $this->item_name,
291 'id' => $this->item_shortname . '_license_key',
292 'desc' => '',
293 'type' => 'license_key',
294 'options' => array(
295 'license' => get_option( $this->item_shortname . '_license_active' ),
296 'shortname' => $this->item_shortname,
297 'item_name' => $this->item_name,
298 'api_url' => $this->api_url,
299 'checkout_url' => $this->checkout_url,
300 'account_url' => $this->account_url,
301 ),
302 'size' => 'regular',
303 ),
304 );
305
306 return array_merge( $settings, $give_license_settings );
307 }
308
309 /**
310 * License Settings Content
311 *
312 * Add Some Content to the Licensing Settings.
313 *
314 * @access public
315 * @since 1.0
316 *
317 * @param array $settings License settings content.
318 *
319 * @return array License settings content.
320 */
321 public function license_settings_content( $settings ) {
322
323 $give_license_settings = array(
324 array(
325 'name' => __( 'Add-on Licenses', 'give' ),
326 'desc' => '<hr>',
327 'type' => 'give_title',
328 'id' => 'give_title',
329 ),
330 );
331
332 return array_merge( $settings, $give_license_settings );
333 }
334
335 /**
336 * Activate License
337 *
338 * Activate the license key.
339 *
340 * @access public
341 * @since 1.0
342 *
343 * @return void
344 */
345 public function activate_license() {
346 // Bailout.
347 if ( ! $this->__is_user_can_edit_license() ) {
348 return;
349 }
350
351 // Allow third party addon developers to handle license activation.
352 if ( $this->__is_third_party_addon() ) {
353 do_action( 'give_activate_license', $this );
354
355 return;
356 }
357
358 // Delete previous license setting if a empty license key submitted.
359 if ( empty( $_POST["{$this->item_shortname}_license_key"] ) ) {
360 $this->unset_license();
361
362 return;
363 }
364
365 // Do not simultaneously activate add-ons if the user want to deactivate a specific add-on.
366 if( $this->is_deactivating_license() ) {
367 return;
368 }
369
370 // Check if plugin previously installed.
371 if ( $this->is_valid_license() ) {
372 return;
373 }
374
375 // Get license key.
376 $this->license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] );
377
378 // Delete previous license key from subscription if previously added.
379 $this->__remove_license_key_from_subscriptions();
380
381 // Make sure there are no api errors.
382 if ( ! ( $license_data = $this->get_license_info( 'activate_license' ) ) ) {
383 return;
384 }
385
386 // Make sure license is valid.
387 // return because admin will want to activate license again.
388 if ( ! $this->is_license( $license_data ) ) {
389 // Add license key.
390 give_update_option( "{$this->item_shortname}_license_key", $this->license );
391
392 return;
393 }
394
395 // Tell WordPress to look for updates.
396 set_site_transient( 'update_plugins', null );
397
398 // Add license data.
399 update_option( "{$this->item_shortname}_license_active", $license_data );
400
401 // Add license key.
402 give_update_option( "{$this->item_shortname}_license_key", $this->license );
403
404 // Check subscription for license key and store this to db (if any).
405 $this->__single_subscription_check();
406 }
407
408 /**
409 * Deactivate License
410 *
411 * Deactivate the license key.
412 *
413 * @access public
414 * @since 1.0
415 *
416 * @return void
417 */
418 public function deactivate_license() {
419 // Bailout.
420 if ( ! $this->__is_user_can_edit_license() ) {
421 return;
422 }
423
424 // Allow third party add-on developers to handle license deactivation.
425 if ( $this->__is_third_party_addon() ) {
426 do_action( 'give_deactivate_license', $this );
427
428 return;
429 }
430
431 // Run on deactivate button press.
432 if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
433 $this->unset_license();
434 }
435 }
436
437 /**
438 * Check if license key is valid once per week.
439 *
440 * @access public
441 * @since 1.7
442 *
443 * @return void
444 */
445 public function weekly_license_check() {
446
447 if (
448 ! empty( $_POST['give_settings'] ) ||
449 empty( $this->license )
450 ) {
451 return;
452 }
453
454 // Allow third party add-on developers to handle their license check.
455 if ( $this->__is_third_party_addon() ) {
456 do_action( 'give_weekly_license_check', $this );
457
458 return;
459 }
460
461 // Make sure there are no api errors.
462 if ( ! ( $license_data = $this->get_license_info( 'check_license' ) ) ) {
463 return;
464 }
465
466 // Bailout.
467 if ( ! $this->is_license( $license_data ) ) {
468 return;
469 }
470
471 update_option( $this->item_shortname . '_license_active', $license_data );
472
473 return;
474 }
475
476 /**
477 * Check subscription validation once per week
478 *
479 * @access public
480 * @since 1.7
481 *
482 * @return void
483 */
484 public function weekly_subscription_check() {
485 // Bailout.
486 if (
487 ! empty( $_POST['give_settings'] ) ||
488 empty( $this->license )
489 ) {
490 return;
491 }
492
493 // Remove old subscription data.
494 if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp', 1 ) ) {
495 delete_option( 'give_subscriptions' );
496 update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) );
497 }
498
499 // Allow third party add-on developers to handle their subscription check.
500 if ( $this->__is_third_party_addon() ) {
501 do_action( 'give_weekly_subscription_check', $this );
502
503 return;
504 }
505
506 $this->__single_subscription_check();
507 }
508
509 /**
510 * Check if license key is part of subscription or not
511 *
512 * @access private
513 * @since 1.7
514 *
515 * @return void
516 */
517 private function __single_subscription_check() {
518 if ( empty( $this->license ) ) {
519 return;
520 }
521
522 /**
523 * Make sure there are no api errors.
524 *
525 * Do not get confused with edd_action check_subscription.
526 * By default edd software licensing api does not have api to check subscription.
527 * This is a custom feature to check subscriptions.
528 */
529 $subscription_data = $this->get_license_info( 'check_subscription', true );
530
531 if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) {
532
533 $subscriptions = get_option( 'give_subscriptions', array() );
534
535 // Update subscription data only if subscription does not exist already.
536 $subscriptions[ $subscription_data['id'] ] = $subscription_data;
537
538 // Initiate default set of license for subscription.
539 if ( ! isset( $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
540 $subscriptions[ $subscription_data['id'] ]['licenses'] = array();
541 }
542
543 // Store licenses for subscription.
544 if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) {
545 $subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license;
546 }
547
548 update_option( 'give_subscriptions', $subscriptions );
549 }
550 }
551
552 /**
553 * Admin notices for errors
554 *
555 * @access public
556 * @since 1.0
557 *
558 * @return void
559 */
560 public function notices() {
561
562 if ( ! current_user_can( 'manage_give_settings' ) ) {
563 return;
564 }
565
566 // Do not show licenses notices on license tab.
567 if ( 'licenses' === give_get_current_setting_tab() ) {
568 return;
569 }
570
571 static $showed_invalid_message;
572 static $showed_subscriptions_message;
573 static $addon_license_key_in_subscriptions;
574
575 // Set default value.
576 $addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array();
577 $messages = array();
578
579 // Check whether admin has Give Add-on activated since 24 hours?
580 $is_license_notice_hidden = Give_Cache::get( 'give_cache_hide_license_notice_after_activation' );
581
582 // Display Invalid License notice, if its more than 24 hours since first Give Add-on activation.
583 if (
584 empty( $this->license )
585 && empty( $showed_invalid_message )
586 && ( false === $is_license_notice_hidden )
587 ) {
588
589 Give()->notices->register_notice( array(
590 'id' => 'give-invalid-license',
591 'type' => 'error',
592 'description' => sprintf(
593 __( 'You have invalid or expired license keys for one or more Give Add-ons. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ),
594 admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
595 ),
596 'dismissible_type' => 'user',
597 'dismiss_interval' => 'shortly',
598 ) );
599
600 $showed_invalid_message = true;
601
602 }
603
604 // Get subscriptions.
605 $subscriptions = get_option( 'give_subscriptions' );
606
607 // Show subscription messages.
608 if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) {
609
610 foreach ( $subscriptions as $subscription ) {
611 // Subscription expires timestamp.
612 $subscription_expires = strtotime( $subscription['expires'] );
613
614 // Start showing subscriptions message before one week of renewal date.
615 if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) {
616 continue;
617 }
618
619 // Check if subscription message already exist in messages.
620 if ( array_key_exists( $subscription['id'], $messages ) ) {
621 continue;
622 }
623
624 // Check if license already expired.
625 if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) {
626 Give()->notices->register_notice( array(
627 'id' => "give-expired-subscription-{$subscription['id']}",
628 'type' => 'error',
629 'description' => sprintf(
630 __( 'Your Give add-on license expired for payment <a href="%1$s" target="_blank">#%2$d</a>. <a href="%3$s" target="_blank">Click to renew an existing license</a> or %4$s.', 'give' ),
631 urldecode( $subscription['invoice_url'] ),
632 $subscription['payment_id'],
633 "{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
634 Give()->notices->get_dismiss_link( array(
635 'title' => __( 'Click here if already renewed', 'give' ),
636 'dismissible_type' => 'user',
637 'dismiss_interval' => 'permanent',
638 ) )
639 ),
640 'dismissible_type' => 'user',
641 'dismiss_interval' => 'shortly',
642 ) );
643 } else {
644 Give()->notices->register_notice( array(
645 'id' => "give-expires-subscription-{$subscription['id']}",
646 'type' => 'error',
647 'description' => sprintf(
648 __( 'Your Give add-on license will expire in %1$s for payment <a href="%2$s" target="_blank">#%3$d</a>. <a href="%4$s" target="_blank">Click to renew an existing license</a> or %5$s.', 'give' ),
649 human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ),
650 urldecode( $subscription['invoice_url'] ),
651 $subscription['payment_id'],
652 "{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired",
653 Give()->notices->get_dismiss_link( array(
654 'title' => __( 'Click here if already renewed', 'give' ),
655 'dismissible_type' => 'user',
656 'dismiss_interval' => 'permanent',
657 ) )
658 ),
659 'dismissible_type' => 'user',
660 'dismiss_interval' => 'shortly',
661 ) );
662 }
663
664 // Stop validation for these license keys.
665 $addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] );
666 }// End foreach().
667 $showed_subscriptions_message = true;
668 }// End if().
669
670 // Show Non Subscription Give Add-on messages.
671 if (
672 ! in_array( $this->license, $addon_license_key_in_subscriptions )
673 && ! empty( $this->license )
674 && empty( $showed_invalid_message )
675 && ! $this->is_valid_license()
676 ) {
677
678 Give()->notices->register_notice( array(
679 'id' => 'give-invalid-license',
680 'type' => 'error',
681 'description' => sprintf(
682 __( 'You have invalid or expired license keys for one or more Give Add-ons. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ),
683 admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' )
684 ),
685 'dismissible_type' => 'user',
686 'dismiss_interval' => 'shortly',
687 ) );
688
689 $showed_invalid_message = true;
690
691 }
692 }
693
694 /**
695 * Check if license is valid or not.
696 *
697 * @since 1.7
698 * @access public
699 *
700 * @param null|object $licence_data
701 *
702 * @return bool
703 */
704 public function is_valid_license( $licence_data = null ) {
705 $license_data = empty( $licence_data ) ? $this->license_data : $licence_data;
706
707 if ( apply_filters( 'give_is_valid_license', ( $this->is_license( $license_data ) && 'valid' === $license_data->license ) ) ) {
708 return true;
709 }
710
711 return false;
712 }
713
714
715 /**
716 * Check if license is license object of no.
717 *
718 * @since 1.7
719 * @access public
720 *
721 * @param null|object $licence_data
722 *
723 * @return bool
724 */
725 public function is_license( $licence_data = null ) {
726 $license_data = empty( $licence_data ) ? $this->license_data : $licence_data;
727
728 if ( apply_filters( 'give_is_license', ( is_object( $license_data ) && ! empty( $license_data ) && property_exists( $license_data, 'license' ) ) ) ) {
729 return true;
730 }
731
732 return false;
733 }
734
735 /**
736 * Check if license is valid or not.
737 *
738 * @access private
739 * @since 1.7
740 *
741 * @return bool
742 */
743 private function __is_third_party_addon() {
744 return ( false === strpos( $this->api_url, 'givewp.com/' ) );
745 }
746
747 /**
748 * Remove license key from subscription.
749 *
750 * This function mainly uses when admin user deactivate license key,
751 * then we do not need subscription information for that license key.
752 *
753 * @access private
754 * @since 1.7
755 *
756 * @return bool
757 */
758 private function __remove_license_key_from_subscriptions() {
759 $subscriptions = get_option( 'give_subscriptions', array() );
760
761 // Bailout.
762 if ( empty( $this->license ) ) {
763 return false;
764 }
765
766 if ( ! empty( $subscriptions ) ) {
767 foreach ( $subscriptions as $subscription_id => $subscription ) {
768 $license_index = array_search( $this->license, $subscription['licenses'] );
769 if ( false !== $license_index ) {
770 // Remove license key.
771 unset( $subscriptions[ $subscription_id ]['licenses'][ $license_index ] );
772
773 // Rearrange license keys.
774 $subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] );
775
776 // Update subscription information.
777 update_option( 'give_subscriptions', $subscriptions );
778 break;
779 }
780 }
781 }
782 }
783
784 /**
785 * @param $plugin_file
786 * @param $plugin_data
787 * @param $status
788 *
789 * @return bool
790 */
791 public function plugin_page_notices( $plugin_file, $plugin_data, $status ) {
792 // Bailout.
793 if ( $this->is_valid_license() ) {
794 return false;
795 }
796
797 $update_notice_wrap = '<tr class="give-addon-notice-tr active"><td colspan="3" class="colspanchange"><div class="notice inline notice-warning notice-alt give-invalid-license"><p><span class="dashicons dashicons-info"></span> %s</p></div></td></tr>';
798 $message = $this->license_state_message();
799
800 if ( ! empty( $message['message'] ) ) {
801 echo sprintf( $update_notice_wrap, $message['message'] );
802 }
803 }
804
805
806 /**
807 * Get message related to license state.
808 *
809 * @since 1.8.7
810 * @access public
811 * @return array
812 */
813 public function license_state_message() {
814 $message_data = array();
815
816 if ( ! $this->is_valid_license() ) {
817
818 $message_data['message'] = sprintf(
819 'Please <a href="%1$s">activate your license</a> to receive updates and support for the %2$s add-on.',
820 esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) ),
821 $this->item_name
822 );
823 }
824
825 return $message_data;
826 }
827
828
829 /**
830 * Check if admin can edit license or not,
831 *
832 * @since 1.8.9
833 * @access private
834 */
835 private function __is_user_can_edit_license() {
836 // Bailout.
837 if (
838 ! Give_Admin_Settings::verify_nonce() ||
839 ! current_user_can( 'manage_give_settings' ) ||
840 'licenses' !== give_get_current_setting_tab()
841 ) {
842 return false;
843 }
844
845 // Security check.
846 if (
847 isset( $_POST[ $this->item_shortname . '_license_key-nonce' ] ) &&
848 ! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' )
849 ) {
850 wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
851 }
852
853 return true;
854 }
855
856
857 /**
858 * Get license information.
859 *
860 * @since 1.8.9
861 * @access public
862 *
863 * @param string $edd_action
864 * @param bool $response_in_array
865 *
866 * @return mixed
867 */
868 public function get_license_info( $edd_action = '', $response_in_array = false ) {
869
870 if ( empty( $edd_action ) ) {
871 return false;
872 }
873
874 // Data to send to the API.
875 $api_params = array(
876 'edd_action' => $edd_action, // never change from "edd_" to "give_"!
877 'license' => $this->license,
878 'item_name' => urlencode( $this->item_name ),
879 'url' => home_url(),
880 );
881
882 // Call the API.
883 $response = wp_remote_post(
884 $this->api_url,
885 array(
886 'timeout' => 15,
887 'sslverify' => false,
888 'body' => $api_params,
889 )
890 );
891
892 // Make sure there are no errors.
893 if ( is_wp_error( $response ) ) {
894 return false;
895 }
896
897 return json_decode( wp_remote_retrieve_body( $response ), $response_in_array );
898 }
899
900
901 /**
902 * Unset license
903 *
904 * @since 1.8.14
905 * @access private
906 */
907 private function unset_license() {
908
909 // Remove license key from subscriptions if exist.
910 $this->__remove_license_key_from_subscriptions();
911
912 // Remove license from database.
913 delete_option( "{$this->item_shortname}_license_active" );
914 give_delete_option( "{$this->item_shortname}_license_key" );
915 unset( $_POST["{$this->item_shortname}_license_key"] );
916
917 // Unset license param.
918 $this->license = '';
919 }
920
921
922 /**
923 * Check if deactivating any license key or not.
924 *
925 * @since 1.8.17
926 * @access private
927 *
928 * @return bool
929 */
930 private function is_deactivating_license() {
931 $status = false;
932
933 foreach ( $_POST as $key => $value ) {
934 if ( false !== strpos( $key, 'license_key_deactivate' ) ) {
935 $status = true;
936 break;
937 }
938 }
939
940 return $status;
941 }
942 }
943
944 endif; // end class_exists check.
945