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