| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | if ( ! class_exists( 'EPL_License' ) ) : |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | class EPL_License { |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | private $file; |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | private $license; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | private $item_name; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | private $item_shortname; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | private $version; |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | private $author; |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | private $item_shortname_without_prefix; |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | private $api_url = 'https://easypropertylistings.com.au/edd-sl-api/'; |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public function __construct( $_file, $_item_name, $_version, $_author, $_optname = null, $_api_url = null, $_item_id = null ) { |
| 99: | |
| 100: | global $epl_options; |
| 101: | |
| 102: | $this->file = $_file; |
| 103: | $this->item_name = $_item_name; |
| 104: | |
| 105: | if ( is_numeric( $_item_id ) ) { |
| 106: | $this->item_id = absint( $_item_id ); |
| 107: | } |
| 108: | |
| 109: | $this->item_shortname_without_prefix = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) ); |
| 110: | $this->item_shortname = 'epl_' . $this->item_shortname_without_prefix; |
| 111: | |
| 112: | $this->version = $_version; |
| 113: | |
| 114: | $this->license = isset( $epl_options[ $this->item_shortname . '_license_key' ] ) ? trim( $epl_options[ $this->item_shortname . '_license_key' ] ) : ''; |
| 115: | |
| 116: | if ( empty( $this->license ) ) { |
| 117: | |
| 118: | $epl_license = get_option( 'epl_license' ); |
| 119: | if ( ! empty( $epl_license ) && isset( $epl_license[ $this->item_shortname_without_prefix ] ) ) { |
| 120: | |
| 121: | $this->license = $epl_license[ $this->item_shortname_without_prefix ]; |
| 122: | } |
| 123: | } |
| 124: | |
| 125: | $this->author = $_author; |
| 126: | $this->api_url = is_null( $_api_url ) ? $this->api_url : $_api_url; |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | if ( ! empty( $_optname ) && isset( $epl_options[ $_optname ] ) && empty( $this->license ) ) { |
| 135: | |
| 136: | $this->license = trim( $epl_options[ $_optname ] ); |
| 137: | } |
| 138: | |
| 139: | |
| 140: | $this->includes(); |
| 141: | $this->hooks(); |
| 142: | |
| 143: | |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | private function includes() { |
| 153: | if ( ! class_exists( 'EPL_SL_Plugin_Updater' ) ) { |
| 154: | require_once 'class-epl-sl-plugin-updater.php'; |
| 155: | } |
| 156: | } |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | private function hooks() { |
| 165: | |
| 166: | |
| 167: | add_action( 'admin_init', array( $this, 'activate_license' ) ); |
| 168: | |
| 169: | |
| 170: | add_action( 'admin_init', array( $this, 'deactivate_license' ) ); |
| 171: | |
| 172: | |
| 173: | add_action( 'admin_init', array( $this, 'auto_updater' ), 0 ); |
| 174: | |
| 175: | add_action( 'admin_init', array( $this, 'maybe_validate_license' ), 0 ); |
| 176: | |
| 177: | add_action( 'in_plugin_update_message-' . plugin_basename( $this->file ), array( $this, 'plugin_row_license_missing' ), 10, 2 ); |
| 178: | |
| 179: | add_action( 'admin_notices', array( $this, 'license_notices' ), 5 ); |
| 180: | |
| 181: | add_action( 'admin_notices', array( $this, 'notices' ), 20 ); |
| 182: | |
| 183: | |
| 184: | add_filter( 'epl_beta_enabled_extensions', array( $this, 'register_beta_support' ) ); |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | public function auto_updater() { |
| 195: | |
| 196: | $license = get_option( $this->item_shortname . '_license_active' ); |
| 197: | |
| 198: | if ( 'valid' !== $license ) { |
| 199: | |
| 200: | return; |
| 201: | } |
| 202: | |
| 203: | $betas = epl_get_option( 'enabled_betas', array() ); |
| 204: | |
| 205: | $args = array( |
| 206: | 'version' => $this->version, |
| 207: | 'license' => $this->license, |
| 208: | 'author' => $this->author, |
| 209: | 'beta' => function_exists( 'epl_extension_has_beta_support' ) && epl_extension_has_beta_support( $this->item_shortname ), |
| 210: | ); |
| 211: | |
| 212: | if ( ! empty( $this->item_id ) ) { |
| 213: | $args['item_id'] = $this->item_id; |
| 214: | } else { |
| 215: | $args['item_name'] = $this->item_name; |
| 216: | } |
| 217: | |
| 218: | |
| 219: | $edd_updater = new EPL_SL_Plugin_Updater( |
| 220: | $this->api_url, |
| 221: | $this->file, |
| 222: | $args |
| 223: | ); |
| 224: | } |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | public function activate_license() { |
| 236: | |
| 237: | if ( ! isset( $_REQUEST['action'] ) || 'epl_settings' !== $_REQUEST['action'] ) { |
| 238: | return; |
| 239: | } |
| 240: | |
| 241: | if ( ! isset( $_POST['epl_license'] ) ) { |
| 242: | return; |
| 243: | } |
| 244: | |
| 245: | if ( empty( $_POST['epl_license'][ $this->item_shortname ] ) ) { |
| 246: | |
| 247: | delete_option( $this->item_shortname . '_license_active' ); |
| 248: | |
| 249: | return; |
| 250: | } |
| 251: | |
| 252: | foreach ( $_POST as $key => $value ) { |
| 253: | if ( false !== strpos( $key, 'license_key_deactivate' ) ) { |
| 254: | |
| 255: | return; |
| 256: | } |
| 257: | } |
| 258: | |
| 259: | $license_active = get_option( $this->item_shortname . '_license_active' ); |
| 260: | |
| 261: | if ( 'valid' === $license_active ) { |
| 262: | return; |
| 263: | } |
| 264: | |
| 265: | $license = sanitize_text_field( wp_unslash( $_POST['epl_license'][ $this->item_shortname ] ) ); |
| 266: | |
| 267: | if ( empty( $license ) ) { |
| 268: | return; |
| 269: | } |
| 270: | |
| 271: | |
| 272: | $api_params = array( |
| 273: | 'edd_action' => 'activate_license', |
| 274: | 'license' => $license, |
| 275: | 'item_name' => rawurlencode( $this->item_name ), |
| 276: | 'url' => home_url(), |
| 277: | ); |
| 278: | |
| 279: | |
| 280: | $response = wp_remote_get( |
| 281: | add_query_arg( $api_params, $this->api_url ), |
| 282: | array( |
| 283: | 'timeout' => 15, |
| 284: | |
| 285: | 'sslverify' => false, |
| 286: | ) |
| 287: | ); |
| 288: | |
| 289: | |
| 290: | if ( is_wp_error( $response ) ) { |
| 291: | return; |
| 292: | } |
| 293: | |
| 294: | |
| 295: | $transient = get_site_transient( 'update_plugins' ); |
| 296: | |
| 297: | if ( ! is_object( $transient ) ) { |
| 298: | $transient = new stdClass(); |
| 299: | } |
| 300: | |
| 301: | $transient->response = isset( $transient->response ) && is_array( $transient->response ) |
| 302: | ? $transient->response |
| 303: | : array(); |
| 304: | |
| 305: | $transient->checked = isset( $transient->checked ) && is_array( $transient->checked ) |
| 306: | ? $transient->checked |
| 307: | : array(); |
| 308: | |
| 309: | set_site_transient( 'update_plugins', $transient ); |
| 310: | |
| 311: | |
| 312: | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 313: | update_option( $this->item_shortname . '_license_active', $license_data->license ); |
| 314: | |
| 315: | if ( ! (bool) $license_data->success ) { |
| 316: | set_transient( 'epl_license_error', $license_data, 1000 ); |
| 317: | } else { |
| 318: | delete_transient( 'epl_license_error' ); |
| 319: | $option = get_option( $this->item_shortname . '_license_status' ); |
| 320: | $option['expired'] = false; |
| 321: | $option['no_activations_left'] = false; |
| 322: | $option['item_name_mismatch'] = false; |
| 323: | $option['missing'] = false; |
| 324: | update_option( $this->item_shortname . '_license_status', $option ); |
| 325: | } |
| 326: | } |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | public function deactivate_license() { |
| 335: | |
| 336: | if ( ! isset( $_REQUEST['action'] ) || 'epl_settings' !== $_REQUEST['action'] ) { |
| 337: | return; |
| 338: | } |
| 339: | |
| 340: | if ( ! isset( $_POST['epl_license'] ) ) { |
| 341: | return; |
| 342: | } |
| 343: | |
| 344: | if ( ! isset( $_POST['epl_license'][ $this->item_shortname ] ) ) { |
| 345: | return; |
| 346: | } |
| 347: | |
| 348: | |
| 349: | if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) { |
| 350: | |
| 351: | |
| 352: | $api_params = array( |
| 353: | 'edd_action' => 'deactivate_license', |
| 354: | 'license' => $this->license, |
| 355: | 'item_name' => rawurlencode( $this->item_name ), |
| 356: | 'url' => home_url(), |
| 357: | ); |
| 358: | |
| 359: | |
| 360: | $response = wp_remote_get( |
| 361: | add_query_arg( $api_params, $this->api_url ), |
| 362: | array( |
| 363: | 'timeout' => 15, |
| 364: | 'sslverify' => false, |
| 365: | ) |
| 366: | ); |
| 367: | |
| 368: | |
| 369: | if ( is_wp_error( $response ) ) { |
| 370: | return; |
| 371: | } |
| 372: | |
| 373: | |
| 374: | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 375: | |
| 376: | if ( 'deactivated' === $license_data->license ) { |
| 377: | delete_option( $this->item_shortname . '_license_active' ); |
| 378: | } |
| 379: | |
| 380: | if ( ! (bool) $license_data->success ) { |
| 381: | set_transient( 'epl_license_error', $license_data, 1000 ); |
| 382: | } else { |
| 383: | delete_transient( 'epl_license_error' ); |
| 384: | $option = get_option( $this->item_shortname . '_license_status' ); |
| 385: | $option['expired'] = false; |
| 386: | $option['no_activations_left'] = false; |
| 387: | $option['item_name_mismatch'] = false; |
| 388: | $option['missing'] = false; |
| 389: | update_option( $this->item_shortname . '_license_status', $option ); |
| 390: | } |
| 391: | } |
| 392: | } |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | public function maybe_validate_license() { |
| 404: | |
| 405: | |
| 406: | |
| 407: | |
| 408: | |
| 409: | |
| 410: | $opt_key = 'epl_license_updates_' . $this->item_shortname; |
| 411: | |
| 412: | $timestamp = get_option( $opt_key ); |
| 413: | |
| 414: | if ( ! $timestamp ) { |
| 415: | $timestamp = strtotime( '+24 hours' ); |
| 416: | update_option( $opt_key, $timestamp ); |
| 417: | $this->validate_license(); |
| 418: | } else { |
| 419: | $current_timestamp = epl_get_local_timestamp(); |
| 420: | if ( $current_timestamp < $timestamp ) { |
| 421: | return; |
| 422: | } else { |
| 423: | update_option( $opt_key, strtotime( '+24 hours' ) ); |
| 424: | $this->validate_license(); |
| 425: | } |
| 426: | } |
| 427: | } |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | |
| 437: | private function validate_license() { |
| 438: | |
| 439: | |
| 440: | $api_params = array( |
| 441: | 'edd_action' => 'activate_license', |
| 442: | 'license' => $this->license, |
| 443: | 'item_name' => rawurlencode( $this->item_name ), |
| 444: | 'url' => home_url(), |
| 445: | ); |
| 446: | |
| 447: | |
| 448: | $response = wp_remote_get( |
| 449: | add_query_arg( $api_params, $this->api_url ), |
| 450: | array( |
| 451: | 'timeout' => 15, |
| 452: | |
| 453: | 'sslverify' => false, |
| 454: | ) |
| 455: | ); |
| 456: | |
| 457: | |
| 458: | if ( is_wp_error( $response ) ) { |
| 459: | set_transient( $this->item_shortname . '_license_active', false, 10 * MINUTE_IN_SECONDS ); |
| 460: | return; |
| 461: | } |
| 462: | |
| 463: | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 464: | |
| 465: | if ( isset( $license_data->error ) ) { |
| 466: | |
| 467: | $option = get_option( $this->item_shortname . '_license_status' ); |
| 468: | $option = false === $option ? array() : $option; |
| 469: | |
| 470: | switch ( $license_data->error ) { |
| 471: | |
| 472: | case 'missing': |
| 473: | set_transient( $this->item_shortname . '_license_active', false, DAY_IN_SECONDS ); |
| 474: | $option['expired'] = false; |
| 475: | $option['no_activations_left'] = false; |
| 476: | $option['item_name_mismatch'] = false; |
| 477: | $option['missing'] = true; |
| 478: | update_option( $this->item_shortname . '_license_status', $option ); |
| 479: | break; |
| 480: | |
| 481: | case 'expired': |
| 482: | set_transient( $this->item_shortname . '_license_active', false, DAY_IN_SECONDS ); |
| 483: | $option['expired'] = true; |
| 484: | $option['no_activations_left'] = false; |
| 485: | $option['item_name_mismatch'] = false; |
| 486: | $option['missing'] = false; |
| 487: | update_option( $this->item_shortname . '_license_status', $option ); |
| 488: | break; |
| 489: | |
| 490: | case 'no_activations_left': |
| 491: | set_transient( $this->item_shortname . '_license_active', false, DAY_IN_SECONDS ); |
| 492: | $option['expired'] = false; |
| 493: | $option['no_activations_left'] = true; |
| 494: | $option['item_name_mismatch'] = false; |
| 495: | $option['missing'] = false; |
| 496: | update_option( $this->item_shortname . '_license_status', $option ); |
| 497: | break; |
| 498: | |
| 499: | case 'item_name_mismatch': |
| 500: | set_transient( $this->item_shortname . '_license_active', false, DAY_IN_SECONDS ); |
| 501: | $option['expired'] = false; |
| 502: | $option['no_activations_left'] = false; |
| 503: | $option['item_name_mismatch'] = true; |
| 504: | $option['missing'] = false; |
| 505: | update_option( $this->item_shortname . '_license_status', $option ); |
| 506: | break; |
| 507: | |
| 508: | } |
| 509: | |
| 510: | return; |
| 511: | } |
| 512: | |
| 513: | |
| 514: | set_transient( '_epl_validate_license', true, DAY_IN_SECONDS ); |
| 515: | $option = get_option( $this->item_shortname . '_license_status' ); |
| 516: | $option['expired'] = false; |
| 517: | $option['no_activations_left'] = false; |
| 518: | $option['item_name_mismatch'] = false; |
| 519: | $option['missing'] = false; |
| 520: | update_option( $this->item_shortname . '_license_status', $option ); |
| 521: | } |
| 522: | |
| 523: | |
| 524: | |
| 525: | |
| 526: | |
| 527: | |
| 528: | public function license_notices() { |
| 529: | |
| 530: | $option = get_option( $this->item_shortname . '_license_status' ); |
| 531: | |
| 532: | |
| 533: | if ( current_user_can( 'manage_options' ) ) { |
| 534: | |
| 535: | |
| 536: | if ( isset( $option['missing'] ) && $option['missing'] ) : |
| 537: | ?> |
| 538: | <div class="error"> |
| 539: | <p> |
| 540: | <?php |
| 541: | |
| 542: | printf( |
| 543: | wp_kses_post( __( 'No valid license key has been entered, so automatic updates for %1$s have been turned off. <a href="%2$s">Please click here to enter your license key and begin receiving automatic updates.</a>', 'easy-property-listings' ) ), |
| 544: | esc_attr( $this->item_name ), |
| 545: | esc_url( add_query_arg( array( 'page' => 'epl-licenses' ), admin_url( 'admin.php' ) ) ) |
| 546: | ); |
| 547: | ?> |
| 548: | </p> |
| 549: | </div> |
| 550: | <?php |
| 551: | endif; |
| 552: | |
| 553: | |
| 554: | if ( isset( $option['expired'] ) && $option['expired'] ) : |
| 555: | ?> |
| 556: | <div class="error"> |
| 557: | <p> |
| 558: | <?php |
| 559: | |
| 560: | printf( wp_kses_post( __( 'Your license key for %1$s has expired. <a href="%2$s" target="_blank">Please click here to renew your license key and continue receiving automatic updates.</a>', 'easy-property-listings' ) ), esc_attr( $this->item_name ), 'https://easypropertylistings.com.au/your-account/' ); |
| 561: | ?> |
| 562: | </p> |
| 563: | </div> |
| 564: | <?php |
| 565: | endif; |
| 566: | |
| 567: | |
| 568: | if ( isset( $option['no_activations_left'] ) && $option['no_activations_left'] ) : |
| 569: | ?> |
| 570: | <div class="error"> |
| 571: | <p> |
| 572: | <?php |
| 573: | |
| 574: | printf( wp_kses_post( __( 'Your maximum usage limit for license key of %s has been reached. Please use a different key to continue receiving automatic updates.', 'easy-property-listings' ) ), esc_attr( $this->item_name ) ); |
| 575: | ?> |
| 576: | </p> |
| 577: | </div> |
| 578: | <?php |
| 579: | endif; |
| 580: | |
| 581: | |
| 582: | if ( isset( $option['item_name_mismatch'] ) && $option['item_name_mismatch'] ) : |
| 583: | ?> |
| 584: | <div class="error"> |
| 585: | <p> |
| 586: | <?php |
| 587: | |
| 588: | printf( wp_kses_post( __( 'The license you entered for %s does not belong to it. Please use a different key to continue receiving automatic updates.', 'easy-property-listings' ) ), esc_attr( $this->item_name ) ); |
| 589: | ?> |
| 590: | </p> |
| 591: | </div> |
| 592: | <?php |
| 593: | endif; |
| 594: | } |
| 595: | } |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | |
| 601: | |
| 602: | |
| 603: | |
| 604: | |
| 605: | public function notices() { |
| 606: | |
| 607: | if ( ! isset( $_GET['page'] ) || 'epl-licenses' !== sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) { |
| 608: | return; |
| 609: | } |
| 610: | |
| 611: | $license_error = get_transient( 'epl_license_error' ); |
| 612: | |
| 613: | if ( false === $license_error ) { |
| 614: | return; |
| 615: | } |
| 616: | |
| 617: | if ( ! empty( $license_error->error ) ) { |
| 618: | |
| 619: | switch ( $license_error->error ) { |
| 620: | |
| 621: | case 'item_name_mismatch': |
| 622: | $message = __( 'This license does not belong to the product you have entered it for.', 'easy-property-listings' ); |
| 623: | break; |
| 624: | |
| 625: | case 'no_activations_left': |
| 626: | $message = __( 'This license does not have any activations left', 'easy-property-listings' ); |
| 627: | break; |
| 628: | |
| 629: | case 'expired': |
| 630: | $message = __( 'This license key is expired. Please renew it.', 'easy-property-listings' ); |
| 631: | break; |
| 632: | |
| 633: | default: |
| 634: | |
| 635: | $message = wp_kses_post( sprintf( __( 'There was a problem activating your license key, please try again or contact support. Error code: %s', 'easy-property-listings' ), $license_error->error ) ); |
| 636: | break; |
| 637: | |
| 638: | } |
| 639: | } |
| 640: | |
| 641: | if ( ! empty( $message ) ) { |
| 642: | |
| 643: | echo '<div class="error">'; |
| 644: | echo '<p>' . wp_kses_post( $message ) . '</p>'; |
| 645: | echo '</div>'; |
| 646: | |
| 647: | } |
| 648: | |
| 649: | delete_transient( 'epl_license_error' ); |
| 650: | } |
| 651: | |
| 652: | |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | |
| 658: | |
| 659: | |
| 660: | |
| 661: | public function plugin_row_license_missing( $plugin_data, $version_info ) { |
| 662: | |
| 663: | static $showed_imissing_key_message; |
| 664: | |
| 665: | $license = get_option( $this->item_shortname . '_license_active' ); |
| 666: | |
| 667: | if ( ( 'valid' !== $license ) && empty( $showed_imissing_key_message[ $this->item_shortname ] ) ) { |
| 668: | |
| 669: | echo ' <strong><a href="' . esc_url( add_query_arg( array( 'page' => 'epl-licenses' ), admin_url( 'admin.php' ) ) ) . '">' . esc_html__( 'Enter valid license key for automatic updates.', 'easy-property-listings' ) . '</a></strong>'; |
| 670: | $showed_imissing_key_message[ $this->item_shortname ] = true; |
| 671: | } |
| 672: | } |
| 673: | |
| 674: | |
| 675: | |
| 676: | |
| 677: | |
| 678: | |
| 679: | |
| 680: | |
| 681: | |
| 682: | |
| 683: | |
| 684: | public function register_beta_support( $products ) { |
| 685: | $products[ $this->item_shortname ] = $this->item_name; |
| 686: | |
| 687: | return $products; |
| 688: | } |
| 689: | } |
| 690: | |
| 691: | endif; |
| 692: | |