1: <?php
2: /**
3: * EPL License
4: *
5: * @package EPL
6: * @subpackage Classes/License
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.1
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: // phpcs:disable WordPress.Security.NonceVerification
18:
19: if ( ! class_exists( 'EPL_License' ) ) :
20:
21: /**
22: * License handler for Easy Property Listings
23: *
24: * This class should simplify the process of adding license information to new EPL extensions.
25: *
26: * @since 1.0
27: * @version 1.1
28: */
29: class EPL_License {
30: /**
31: * File
32: *
33: * @var string $file.
34: */
35: private $file;
36:
37: /**
38: * License
39: *
40: * @var string $file.
41: */
42: private $license;
43:
44: /**
45: * Item name
46: *
47: * @var string $file.
48: */
49: private $item_name;
50:
51: /**
52: * Shortname
53: *
54: * @var string $file.
55: */
56: private $item_shortname;
57:
58: /**
59: * Version
60: *
61: * @var string $file.
62: */
63: private $version;
64:
65: /**
66: * Author name
67: *
68: * @var string $file.
69: */
70: private $author;
71:
72: /**
73: * Item shortname
74: *
75: * @var string $file.
76: */
77: private $item_shortname_without_prefix;
78:
79: /**
80: * API URL
81: *
82: * @var string $file.
83: */
84: private $api_url = 'https://easypropertylistings.com.au/edd-sl-api/';
85:
86: /**
87: * Class constructor
88: *
89: * @global array $epl_options
90: * @param string $_file File.
91: * @param string $_item_name Item name.
92: * @param string $_version Version.
93: * @param string $_author Author name.
94: * @param string $_optname Options.
95: * @param string $_api_url API URL.
96: * @param string $_item_id Item ID.
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: * Allows for backwards compatibility with old license options,
130: * i.e. if the plugins had license key fields previously, the license
131: * handler will automatically pick these up and use those in lieu of the
132: * user having to reactive their license.
133: */
134: if ( ! empty( $_optname ) && isset( $epl_options[ $_optname ] ) && empty( $this->license ) ) {
135:
136: $this->license = trim( $epl_options[ $_optname ] );
137: }
138:
139: // Setup hooks.
140: $this->includes();
141: $this->hooks();
142: // phpcs:ignore // Test $this->auto_updater(); code.
143: // phpcs:ignore // Test $this->maybe_validate_license(); code.
144: }
145:
146: /**
147: * Include the updater class
148: *
149: * @access private
150: * @return void
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: * Setup hooks
160: *
161: * @access private
162: * @return void
163: */
164: private function hooks() {
165:
166: // Activate license key on settings save.
167: add_action( 'admin_init', array( $this, 'activate_license' ) );
168:
169: // Deactivate license key.
170: add_action( 'admin_init', array( $this, 'deactivate_license' ) );
171:
172: // Updater.
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: // Register plugins for beta support.
184: add_filter( 'epl_beta_enabled_extensions', array( $this, 'register_beta_support' ) );
185: }
186:
187: /**
188: * Auto updater
189: *
190: * @access private
191: * @global array $epl_options
192: * @return void
193: */
194: public function auto_updater() {
195:
196: $license = get_option( $this->item_shortname . '_license_active' );
197:
198: if ( 'valid' !== $license ) {
199: // dont check for updates on unvalid licensed.
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: // Setup the updater.
219: $edd_updater = new EPL_SL_Plugin_Updater(
220: $this->api_url,
221: $this->file,
222: $args
223: );
224: }
225:
226: /**
227: * Activate the license key
228: *
229: * @access public
230: * @return void
231: *
232: * @since 1.0.0
233: * @since 3.5.23 Fix: Transient set to null produced errors with other plugins when saving a license key.
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: // Don't activate a key when deactivating a different key.
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: // Data to send to the API.
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: // Call the API.
280: $response = wp_remote_get(
281: add_query_arg( $api_params, $this->api_url ),
282: array(
283: 'timeout' => 15,
284: // Previous 'body' => $api_params, Version.
285: 'sslverify' => false,
286: )
287: );
288:
289: // Make sure there are no errors.
290: if ( is_wp_error( $response ) ) {
291: return;
292: }
293:
294: // Tell WordPress to look for updates.
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: // Decode license data.
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: * Deactivate the license key
330: *
331: * @access public
332: * @return void
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: // Run on deactivate button press.
349: if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) { // Need to check this param.
350:
351: // Data to send to the API.
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: // Call the API.
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: // Make sure there are no errors.
369: if ( is_wp_error( $response ) ) {
370: return;
371: }
372:
373: // Decode the license data.
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: * Check if validating license
396: *
397: * @access private
398: *
399: * @since 3.5.3 Update to return local timestamp.
400: *
401: * @return void
402: */
403: public function maybe_validate_license() {
404:
405: // uncomment next two lines for testing.
406: // $this->validate_license(); | Testing.
407: // return; | Testing.
408: // Perform a request to validate the license.
409: // Only run every 24 hours.
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: * Validate license
431: *
432: * @since 3.5 Fix : Automatic conversion of false to array is deprecated
433: *
434: * @access private
435: * @return void
436: */
437: private function validate_license() {
438:
439: // Data to send to the API.
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: // Call the API.
448: $response = wp_remote_get(
449: add_query_arg( $api_params, $this->api_url ),
450: array(
451: 'timeout' => 15,
452: // Previous 'body' => $api_params, | code.
453: 'sslverify' => false,
454: )
455: );
456:
457: // Make sure there are no errors.
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: // Otherwise, our check has returned successfully. Set the transient and update our license type and flags.
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: * Outputs any notices generated by the class.
525: *
526: * @since 3.1.4
527: */
528: public function license_notices() {
529:
530: $option = get_option( $this->item_shortname . '_license_status' );
531:
532: // Only display notices to admin users.
533: if ( current_user_can( 'manage_options' ) ) {
534:
535: // If there is no license key, output nag about ensuring key is set for automatic updates.
536: if ( isset( $option['missing'] ) && $option['missing'] ) :
537: ?>
538: <div class="error">
539: <p>
540: <?php
541:
542: printf( // Translators: item name.
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: // If a key has expired, output nag about renewing the key.
554: if ( isset( $option['expired'] ) && $option['expired'] ) :
555: ?>
556: <div class="error">
557: <p>
558: <?php
559: // Translators: item name.
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: // If activation limit reached for the license.
568: if ( isset( $option['no_activations_left'] ) && $option['no_activations_left'] ) :
569: ?>
570: <div class="error">
571: <p>
572: <?php
573: // translators: item name.
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: // If a key is invalid, output nag about using another key.
582: if ( isset( $option['item_name_mismatch'] ) && $option['item_name_mismatch'] ) :
583: ?>
584: <div class="error">
585: <p>
586: <?php
587: // translators: item name.
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: * Admin notices for errors
600: *
601: * @access public
602: * @return void
603: * @since 3.5.21 Fix: License notice message sanitization adjusted to run after sprintf(). Thanks DAnn2012.
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: // translators: error.
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: * Displays message inline on plugin row that the license key is missing
654: *
655: * @param array $plugin_data Data.
656: * @param array $version_info Version info.
657: * @access public
658: * @since 2.5
659: * @return void
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 '&nbsp;<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: * Adds this plugin to the beta page
676: *
677: * @access public
678: *
679: * @param array $products Beta products.
680: *
681: * @return array
682: * @since 2.6.11
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; // End class_exists check.
692: