Complex classes like Give_License often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_License, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 ); |
||
| 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 | add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) ); |
||
| 251 | |||
| 252 | // Check subscription weekly. |
||
| 253 | Give_Cron::add_weekly_event( array( $this, 'weekly_subscription_check' ) ); |
||
| 254 | add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) ); |
||
| 255 | |||
| 256 | // Show addon notice on plugin page. |
||
| 257 | $plugin_name = explode( 'plugins/', $this->file ); |
||
| 258 | $plugin_name = end( $plugin_name ); |
||
| 259 | add_action( "after_plugin_row_{$plugin_name}", array( $this, 'plugin_page_notices' ), 10, 3 ); |
||
| 260 | |||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Auto Updater |
||
| 266 | * |
||
| 267 | * @access private |
||
| 268 | * @since 1.0 |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function auto_updater() { |
||
| 273 | |||
| 274 | // Setup the updater. |
||
| 275 | $this->auto_updater_obj = new EDD_SL_Plugin_Updater( |
||
| 276 | $this->api_url, |
||
| 277 | $this->file, |
||
| 278 | array( |
||
| 279 | 'version' => $this->version, |
||
| 280 | 'license' => $this->license, |
||
| 281 | 'item_name' => $this->item_name, |
||
| 282 | 'author' => $this->author, |
||
| 283 | ) |
||
| 284 | ); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * License Settings |
||
| 289 | * |
||
| 290 | * Add license field to settings. |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * @since 1.0 |
||
| 294 | * |
||
| 295 | * @param array $settings License settings. |
||
| 296 | * |
||
| 297 | * @return array License settings. |
||
| 298 | */ |
||
| 299 | public function settings( $settings ) { |
||
| 300 | |||
| 301 | $give_license_settings = array( |
||
| 302 | array( |
||
| 303 | 'name' => $this->item_name, |
||
| 304 | 'id' => $this->item_shortname . '_license_key', |
||
| 305 | 'desc' => '', |
||
| 306 | 'type' => 'license_key', |
||
| 307 | 'options' => array( |
||
| 308 | 'license' => get_option( $this->item_shortname . '_license_active' ), |
||
| 309 | 'shortname' => $this->item_shortname, |
||
| 310 | 'item_name' => $this->item_name, |
||
| 311 | 'api_url' => $this->api_url, |
||
| 312 | 'checkout_url' => $this->checkout_url, |
||
| 313 | 'account_url' => $this->account_url, |
||
| 314 | ), |
||
| 315 | 'size' => 'regular', |
||
| 316 | ), |
||
| 317 | ); |
||
| 318 | |||
| 319 | return array_merge( $settings, $give_license_settings ); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * License Settings Content |
||
| 324 | * |
||
| 325 | * Add Some Content to the Licensing Settings. |
||
| 326 | * |
||
| 327 | * @access public |
||
| 328 | * @since 1.0 |
||
| 329 | * |
||
| 330 | * @param array $settings License settings content. |
||
| 331 | * |
||
| 332 | * @return array License settings content. |
||
| 333 | */ |
||
| 334 | public function license_settings_content( $settings ) { |
||
| 335 | |||
| 336 | $give_license_settings = array( |
||
| 337 | array( |
||
| 338 | 'name' => __( 'Add-on Licenses', 'give' ), |
||
| 339 | 'desc' => '<hr>', |
||
| 340 | 'type' => 'give_title', |
||
| 341 | 'id' => 'give_title', |
||
| 342 | ), |
||
| 343 | ); |
||
| 344 | |||
| 345 | return array_merge( $settings, $give_license_settings ); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Activate License |
||
| 350 | * |
||
| 351 | * Activate the license key. |
||
| 352 | * |
||
| 353 | * @access public |
||
| 354 | * @since 1.0 |
||
| 355 | * |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | public function activate_license() { |
||
| 359 | // Bailout. |
||
| 360 | if ( ! $this->__is_user_can_edit_license() ) { |
||
| 361 | return; |
||
| 362 | } |
||
| 363 | |||
| 364 | // Allow third party addon developers to handle license activation. |
||
| 365 | if ( $this->__is_third_party_addon() ) { |
||
| 366 | do_action( 'give_activate_license', $this ); |
||
| 367 | |||
| 368 | return; |
||
| 369 | } |
||
| 370 | |||
| 371 | // Delete previous license setting if a empty license key submitted. |
||
| 372 | if ( empty( $_POST["{$this->item_shortname}_license_key"] ) ) { |
||
| 373 | $this->unset_license(); |
||
| 374 | |||
| 375 | return; |
||
| 376 | } |
||
| 377 | |||
| 378 | // Do not simultaneously activate add-ons if the user want to deactivate a specific add-on. |
||
| 379 | if( $this->is_deactivating_license() ) { |
||
| 380 | return; |
||
| 381 | } |
||
| 382 | |||
| 383 | // Check if plugin previously installed. |
||
| 384 | if ( $this->is_valid_license() ) { |
||
| 385 | return; |
||
| 386 | } |
||
| 387 | |||
| 388 | // Get license key. |
||
| 389 | $this->license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] ); |
||
| 390 | |||
| 391 | // Delete previous license key from subscription if previously added. |
||
| 392 | $this->__remove_license_key_from_subscriptions(); |
||
| 393 | |||
| 394 | // Make sure there are no api errors. |
||
| 395 | if ( ! ( $license_data = $this->get_license_info( 'activate_license' ) ) ) { |
||
| 396 | return; |
||
| 397 | } |
||
| 398 | |||
| 399 | // Make sure license is valid. |
||
| 400 | // return because admin will want to activate license again. |
||
| 401 | if ( ! $this->is_license( $license_data ) ) { |
||
| 402 | // Add license key. |
||
| 403 | give_update_option( "{$this->item_shortname}_license_key", $this->license ); |
||
| 404 | |||
| 405 | return; |
||
| 406 | } |
||
| 407 | |||
| 408 | // Tell WordPress to look for updates. |
||
| 409 | set_site_transient( 'update_plugins', null ); |
||
| 410 | |||
| 411 | // Add license data. |
||
| 412 | update_option( "{$this->item_shortname}_license_active", $license_data ); |
||
| 413 | |||
| 414 | // Add license key. |
||
| 415 | give_update_option( "{$this->item_shortname}_license_key", $this->license ); |
||
| 416 | |||
| 417 | // Check subscription for license key and store this to db (if any). |
||
| 418 | $this->__single_subscription_check(); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Deactivate License |
||
| 423 | * |
||
| 424 | * Deactivate the license key. |
||
| 425 | * |
||
| 426 | * @access public |
||
| 427 | * @since 1.0 |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | public function deactivate_license() { |
||
| 432 | // Bailout. |
||
| 433 | if ( ! $this->__is_user_can_edit_license() ) { |
||
| 434 | return; |
||
| 435 | } |
||
| 436 | |||
| 437 | // Allow third party add-on developers to handle license deactivation. |
||
| 438 | if ( $this->__is_third_party_addon() ) { |
||
| 439 | do_action( 'give_deactivate_license', $this ); |
||
| 440 | |||
| 441 | return; |
||
| 442 | } |
||
| 443 | |||
| 444 | // Run on deactivate button press. |
||
| 445 | if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) { |
||
| 446 | $this->unset_license(); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Check if license key is valid once per week. |
||
| 452 | * |
||
| 453 | * @access public |
||
| 454 | * @since 1.7 |
||
| 455 | * |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | public function weekly_license_check() { |
||
| 459 | |||
| 460 | if ( |
||
| 461 | ! empty( $_POST['give_settings'] ) || |
||
| 462 | empty( $this->license ) |
||
| 463 | ) { |
||
| 464 | return; |
||
| 465 | } |
||
| 466 | |||
| 467 | // Allow third party add-on developers to handle their license check. |
||
| 468 | if ( $this->__is_third_party_addon() ) { |
||
| 469 | do_action( 'give_weekly_license_check', $this ); |
||
| 470 | |||
| 471 | return; |
||
| 472 | } |
||
| 473 | |||
| 474 | // Make sure there are no api errors. |
||
| 475 | if ( ! ( $license_data = $this->get_license_info( 'check_license' ) ) ) { |
||
| 476 | return; |
||
| 477 | } |
||
| 478 | |||
| 479 | // Bailout. |
||
| 480 | if ( ! $this->is_license( $license_data ) ) { |
||
| 481 | return; |
||
| 482 | } |
||
| 483 | |||
| 484 | update_option( $this->item_shortname . '_license_active', $license_data ); |
||
| 485 | |||
| 486 | return; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Check subscription validation once per week |
||
| 491 | * |
||
| 492 | * @access public |
||
| 493 | * @since 1.7 |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function weekly_subscription_check() { |
||
| 498 | // Bailout. |
||
| 499 | if ( |
||
| 500 | ! empty( $_POST['give_settings'] ) || |
||
| 501 | empty( $this->license ) |
||
| 502 | ) { |
||
| 503 | return; |
||
| 504 | } |
||
| 505 | |||
| 506 | // Remove old subscription data. |
||
| 507 | if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp', 1 ) ) { |
||
| 508 | delete_option( 'give_subscriptions' ); |
||
| 509 | update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) ); |
||
| 510 | } |
||
| 511 | |||
| 512 | // Allow third party add-on developers to handle their subscription check. |
||
| 513 | if ( $this->__is_third_party_addon() ) { |
||
| 514 | do_action( 'give_weekly_subscription_check', $this ); |
||
| 515 | |||
| 516 | return; |
||
| 517 | } |
||
| 518 | |||
| 519 | $this->__single_subscription_check(); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Check if license key is part of subscription or not |
||
| 524 | * |
||
| 525 | * @access private |
||
| 526 | * @since 1.7 |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | private function __single_subscription_check() { |
||
| 531 | if ( empty( $this->license ) ) { |
||
| 532 | return; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Make sure there are no api errors. |
||
| 537 | * |
||
| 538 | * Do not get confused with edd_action check_subscription. |
||
| 539 | * By default edd software licensing api does not have api to check subscription. |
||
| 540 | * This is a custom feature to check subscriptions. |
||
| 541 | */ |
||
| 542 | $subscription_data = $this->get_license_info( 'check_subscription', true ); |
||
| 543 | |||
| 544 | if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) { |
||
| 545 | |||
| 546 | $subscriptions = get_option( 'give_subscriptions', array() ); |
||
| 547 | |||
| 548 | // Update subscription data only if subscription does not exist already. |
||
| 549 | $subscriptions[ $subscription_data['id'] ] = $subscription_data; |
||
| 550 | |||
| 551 | // Initiate default set of license for subscription. |
||
| 552 | if ( ! isset( $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) { |
||
| 553 | $subscriptions[ $subscription_data['id'] ]['licenses'] = array(); |
||
| 554 | } |
||
| 555 | |||
| 556 | // Store licenses for subscription. |
||
| 557 | if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) { |
||
| 558 | $subscriptions[ $subscription_data['id'] ]['licenses'][] = $this->license; |
||
| 559 | } |
||
| 560 | |||
| 561 | update_option( 'give_subscriptions', $subscriptions ); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Admin notices for errors |
||
| 567 | * |
||
| 568 | * @access public |
||
| 569 | * @since 1.0 |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | public function notices() { |
||
| 574 | |||
| 575 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
||
| 576 | return; |
||
| 577 | } |
||
| 578 | |||
| 579 | // Do not show licenses notices on license tab. |
||
| 580 | if ( 'licenses' === give_get_current_setting_tab() ) { |
||
| 581 | return; |
||
| 582 | } |
||
| 583 | |||
| 584 | static $showed_invalid_message; |
||
| 585 | static $showed_subscriptions_message; |
||
| 586 | static $addon_license_key_in_subscriptions; |
||
| 587 | |||
| 588 | // Set default value. |
||
| 589 | $addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array(); |
||
| 590 | $messages = array(); |
||
| 591 | |||
| 592 | // Check whether admin has Give Add-on activated since 24 hours? |
||
| 593 | $is_license_notice_hidden = Give_Cache::get( 'give_cache_hide_license_notice_after_activation' ); |
||
| 594 | |||
| 595 | // Display Invalid License notice, if its more than 24 hours since first Give Add-on activation. |
||
| 596 | if ( |
||
| 597 | empty( $this->license ) |
||
| 598 | && empty( $showed_invalid_message ) |
||
| 599 | && ( false === $is_license_notice_hidden ) |
||
| 600 | ) { |
||
| 601 | |||
| 602 | Give()->notices->register_notice( array( |
||
| 603 | 'id' => 'give-invalid-license', |
||
| 604 | 'type' => 'error', |
||
| 605 | 'description' => sprintf( |
||
| 606 | __( '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' ), |
||
| 607 | admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) |
||
| 608 | ), |
||
| 609 | 'dismissible_type' => 'user', |
||
| 610 | 'dismiss_interval' => 'shortly', |
||
| 611 | ) ); |
||
| 612 | |||
| 613 | $showed_invalid_message = true; |
||
| 614 | |||
| 615 | } |
||
| 616 | |||
| 617 | // Get subscriptions. |
||
| 618 | $subscriptions = get_option( 'give_subscriptions' ); |
||
| 619 | |||
| 620 | // Show subscription messages. |
||
| 621 | if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) { |
||
| 622 | |||
| 623 | foreach ( $subscriptions as $subscription ) { |
||
| 624 | // Subscription expires timestamp. |
||
| 625 | $subscription_expires = strtotime( $subscription['expires'] ); |
||
| 626 | |||
| 627 | // Start showing subscriptions message before one week of renewal date. |
||
| 628 | if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) { |
||
| 629 | continue; |
||
| 630 | } |
||
| 631 | |||
| 632 | // Check if subscription message already exist in messages. |
||
| 633 | if ( array_key_exists( $subscription['id'], $messages ) ) { |
||
| 634 | continue; |
||
| 635 | } |
||
| 636 | |||
| 637 | // Check if license already expired. |
||
| 638 | if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) { |
||
| 639 | Give()->notices->register_notice( array( |
||
| 640 | 'id' => "give-expired-subscription-{$subscription['id']}", |
||
| 641 | 'type' => 'error', |
||
| 642 | 'description' => sprintf( |
||
| 643 | __( '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' ), |
||
| 644 | urldecode( $subscription['invoice_url'] ), |
||
| 645 | $subscription['payment_id'], |
||
| 646 | "{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired", |
||
| 647 | Give()->notices->get_dismiss_link( array( |
||
| 648 | 'title' => __( 'Click here if already renewed', 'give' ), |
||
| 649 | 'dismissible_type' => 'user', |
||
| 650 | 'dismiss_interval' => 'permanent', |
||
| 651 | ) ) |
||
| 652 | ), |
||
| 653 | 'dismissible_type' => 'user', |
||
| 654 | 'dismiss_interval' => 'shortly', |
||
| 655 | ) ); |
||
| 656 | } else { |
||
| 657 | Give()->notices->register_notice( array( |
||
| 658 | 'id' => "give-expires-subscription-{$subscription['id']}", |
||
| 659 | 'type' => 'error', |
||
| 660 | 'description' => sprintf( |
||
| 661 | __( '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' ), |
||
| 662 | human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ), |
||
| 663 | urldecode( $subscription['invoice_url'] ), |
||
| 664 | $subscription['payment_id'], |
||
| 665 | "{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired", |
||
| 666 | Give()->notices->get_dismiss_link( array( |
||
| 667 | 'title' => __( 'Click here if already renewed', 'give' ), |
||
| 668 | 'dismissible_type' => 'user', |
||
| 669 | 'dismiss_interval' => 'permanent', |
||
| 670 | ) ) |
||
| 671 | ), |
||
| 672 | 'dismissible_type' => 'user', |
||
| 673 | 'dismiss_interval' => 'shortly', |
||
| 674 | ) ); |
||
| 675 | } |
||
| 676 | |||
| 677 | // Stop validation for these license keys. |
||
| 678 | $addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] ); |
||
| 679 | }// End foreach(). |
||
| 680 | $showed_subscriptions_message = true; |
||
| 681 | }// End if(). |
||
| 682 | |||
| 683 | // Show Non Subscription Give Add-on messages. |
||
| 684 | if ( |
||
| 685 | ! in_array( $this->license, $addon_license_key_in_subscriptions ) |
||
| 686 | && ! empty( $this->license ) |
||
| 687 | && empty( $showed_invalid_message ) |
||
| 688 | && ! $this->is_valid_license() |
||
| 689 | ) { |
||
| 690 | |||
| 691 | Give()->notices->register_notice( array( |
||
| 692 | 'id' => 'give-invalid-license', |
||
| 693 | 'type' => 'error', |
||
| 694 | 'description' => sprintf( |
||
| 695 | __( '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' ), |
||
| 696 | admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) |
||
| 697 | ), |
||
| 698 | 'dismissible_type' => 'user', |
||
| 699 | 'dismiss_interval' => 'shortly', |
||
| 700 | ) ); |
||
| 701 | |||
| 702 | $showed_invalid_message = true; |
||
| 703 | |||
| 704 | } |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Check if license is valid or not. |
||
| 709 | * |
||
| 710 | * @since 1.7 |
||
| 711 | * @access public |
||
| 712 | * |
||
| 713 | * @param null|object $licence_data |
||
| 714 | * |
||
| 715 | * @return bool |
||
| 716 | */ |
||
| 717 | public function is_valid_license( $licence_data = null ) { |
||
| 718 | $license_data = empty( $licence_data ) ? $this->license_data : $licence_data; |
||
| 719 | |||
| 720 | if ( apply_filters( 'give_is_valid_license', ( $this->is_license( $license_data ) && 'valid' === $license_data->license ) ) ) { |
||
| 721 | return true; |
||
| 722 | } |
||
| 723 | |||
| 724 | return false; |
||
| 725 | } |
||
| 726 | |||
| 727 | |||
| 728 | /** |
||
| 729 | * Check if license is license object of no. |
||
| 730 | * |
||
| 731 | * @since 1.7 |
||
| 732 | * @access public |
||
| 733 | * |
||
| 734 | * @param null|object $licence_data |
||
| 735 | * |
||
| 736 | * @return bool |
||
| 737 | */ |
||
| 738 | public function is_license( $licence_data = null ) { |
||
| 739 | $license_data = empty( $licence_data ) ? $this->license_data : $licence_data; |
||
| 740 | |||
| 741 | if ( apply_filters( 'give_is_license', ( is_object( $license_data ) && ! empty( $license_data ) && property_exists( $license_data, 'license' ) ) ) ) { |
||
| 742 | return true; |
||
| 743 | } |
||
| 744 | |||
| 745 | return false; |
||
| 746 | } |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Check if license is valid or not. |
||
| 750 | * |
||
| 751 | * @access private |
||
| 752 | * @since 1.7 |
||
| 753 | * |
||
| 754 | * @return bool |
||
| 755 | */ |
||
| 756 | private function __is_third_party_addon() { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Remove license key from subscription. |
||
| 762 | * |
||
| 763 | * This function mainly uses when admin user deactivate license key, |
||
| 764 | * then we do not need subscription information for that license key. |
||
| 765 | * |
||
| 766 | * @access private |
||
| 767 | * @since 1.7 |
||
| 768 | * |
||
| 769 | * @return bool |
||
| 770 | */ |
||
| 771 | private function __remove_license_key_from_subscriptions() { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @param $plugin_file |
||
| 799 | * @param $plugin_data |
||
| 800 | * @param $status |
||
| 801 | * |
||
| 802 | * @return bool |
||
| 803 | */ |
||
| 804 | public function plugin_page_notices( $plugin_file, $plugin_data, $status ) { |
||
| 805 | // Bailout. |
||
| 806 | if ( $this->is_valid_license() ) { |
||
| 807 | return false; |
||
| 808 | } |
||
| 809 | |||
| 810 | $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>'; |
||
| 811 | $message = $this->license_state_message(); |
||
| 817 | |||
| 818 | |||
| 819 | /** |
||
| 820 | * Get message related to license state. |
||
| 821 | * |
||
| 822 | * @since 1.8.7 |
||
| 823 | * @access public |
||
| 824 | * @return array |
||
| 825 | */ |
||
| 826 | public function license_state_message() { |
||
| 840 | |||
| 841 | |||
| 842 | /** |
||
| 843 | * Check if admin can edit license or not, |
||
| 844 | * |
||
| 845 | * @since 1.8.9 |
||
| 846 | * @access private |
||
| 847 | */ |
||
| 848 | private function __is_user_can_edit_license() { |
||
| 868 | |||
| 869 | |||
| 870 | /** |
||
| 871 | * Get license information. |
||
| 872 | * |
||
| 873 | * @since 1.8.9 |
||
| 874 | * @access public |
||
| 875 | * |
||
| 876 | * @param string $edd_action |
||
| 877 | * @param bool $response_in_array |
||
| 878 | * |
||
| 879 | * @return mixed |
||
| 880 | */ |
||
| 881 | public function get_license_info( $edd_action = '', $response_in_array = false ) { |
||
| 912 | |||
| 913 | |||
| 914 | /** |
||
| 915 | * Unset license |
||
| 916 | * |
||
| 917 | * @since 1.8.14 |
||
| 918 | * @access private |
||
| 919 | */ |
||
| 920 | private function unset_license() { |
||
| 933 | |||
| 934 | |||
| 935 | /** |
||
| 936 | * Check if deactivating any license key or not. |
||
| 937 | * |
||
| 938 | * @since 1.8.17 |
||
| 939 | * @access private |
||
| 940 | * |
||
| 941 | * @return bool |
||
| 942 | */ |
||
| 943 | private function is_deactivating_license() { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Return licensed addons info |
||
| 958 | * |
||
| 959 | * Note: note only for internal logic |
||
| 960 | * |
||
| 961 | * @since 2.1.4 |
||
| 962 | * |
||
| 963 | * @return array |
||
| 964 | */ |
||
| 965 | static function get_licensed_addons() { |
||
| 968 | |||
| 969 | } |
||
| 970 | |||
| 972 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.