PluginProbe
Property Hive / 2.2.6
Property Hive v2.2.6
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-licenses.php

class-ph-licenses.php in Property Hive 2.2.6, at includes/class-ph-licenses.php

1,036 lines 30.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * Licenses Controller
9 *
10 * Property Hive Licenses Class which handles the storing and checking of licenses
11 *
12 * @class PH_Licenses
13 * @version 1.0.0
14 * @package PropertyHive/Classes/Licenses
15 * @category Class
16 * @author PropertyHive
17 */
18 class PH_Licenses {
19
20 /** @var PH_Licenses The single instance of the class */
21 protected static $_instance = null;
22
23 private $pro_license_status = array();
24
25 /**
26 * Main PH_Licenses Instance.
27 *
28 * Ensures only one instance of PH_Licenses is loaded or can be loaded.
29 *
30 * @since 1.0.0
31 * @static
32 * @return PH_Licenses Main instance
33 */
34 public static function instance() {
35 if ( is_null( self::$_instance ) ) {
36 self::$_instance = new self();
37 }
38 return self::$_instance;
39 }
40
41 /**
42 * Cloning is forbidden.
43 *
44 * @since 1.0.0
45 */
46 public function __clone() {
47 _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'propertyhive' ), '1.0.0' );
48 }
49
50 /**
51 * Unserializing instances of this class is forbidden.
52 *
53 * @since 1.0.0
54 */
55 public function __wakeup() {
56 _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'propertyhive' ), '1.0.0' );
57 }
58
59 /**
60 * Constructor for the licenses class
61 *
62 */
63 public function __construct() {
64 add_action( 'propertyhive_check_licenses', array( $this, 'ph_check_licenses' ) );
65 add_filter( 'propertyhive_add_on_can_be_used', array( $this, 'ph_check_add_on_can_be_used' ), 10, 2 );
66 add_filter( 'propertyhive_add_on_can_be_updated', array( $this, 'ph_check_add_on_can_be_updated' ), 10, 2 );
67 }
68
69 public function ph_check_add_on_can_be_used( $can, $slug )
70 {
71 // Check add on wasn't active before
72 $pre_pro_add_ons = get_option( 'propertyhive_pre_pro_add_ons', array() );
73
74 if ( !empty($pre_pro_add_ons) )
75 {
76 foreach ( $pre_pro_add_ons as $pre_pro_add_on )
77 {
78 if ( isset($pre_pro_add_on['slug']) && $pre_pro_add_on['slug'] == $slug )
79 {
80 // Yes! This add on was being used pre pro so should still be able to be used without license checks
81 return true;
82 }
83 }
84 }
85
86 // Check we have the right kind of license key to operate this add on
87
88 $license_type = $this->get_license_type();
89
90 if ( $license_type == 'pro' )
91 {
92 if ( get_option('propertyhive_pro_license_key', '') != '' )
93 {
94 if ( !$this->is_valid_pro_license_key() )
95 {
96 // show warning
97 return false;
98 }
99
100 // Valid license. Check this plugin is valid for the purchased type of plan (i.e. map search can't be used with an 'import only' plan)
101 $feature = get_ph_pro_feature( $slug );
102 $product_id_and_package = PH()->license->get_pro_license_product_id_and_package();
103
104 if ( isset($product_id_and_package['success']) && $product_id_and_package['success'] === true )
105 {
106 if (
107 isset($feature['plans']) &&
108 isset($product_id_and_package['package']) &&
109 in_array($product_id_and_package['package'], $feature['plans'])
110 )
111 {
112
113 }
114 else
115 {
116 // show warning
117 return false;
118 }
119 }
120 }
121 }
122
123 return $can;
124 }
125
126 public function ph_check_add_on_can_be_updated( $can, $slug )
127 {
128 $license_type = $this->get_license_type();
129
130 if ( $license_type == 'old' )
131 {
132 $license = get_option( 'propertyhive_license_key_details', array() );
133
134 if ( !is_array( $license ) ) { $license = array(); }
135
136 if ( isset($license['active']) && $license['active'] == '1' && isset($license['expires_at']) && $license['expires_at'] != '' && strtotime($license['expires_at']) > time() )
137 {
138 return true;
139 }
140 }
141 elseif ( $license_type == 'pro' )
142 {
143 // get new license information
144 if ( get_option('propertyhive_pro_license_key', '') != '' )
145 {
146 if ( $this->is_valid_pro_license_key() )
147 {
148 // Valid license. Check this plugin is valid for the purchased type of plan (i.e. map search can't be used with an 'import only' plan)
149 $feature = get_ph_pro_feature( $slug );
150 $product_id_and_package = PH()->license->get_pro_license_product_id_and_package();
151
152 if ( isset($product_id_and_package['success']) && $product_id_and_package['success'] === true )
153 {
154 if (
155 isset($feature['plans']) &&
156 isset($product_id_and_package['package']) &&
157 in_array($product_id_and_package['package'], $feature['plans'])
158 )
159 {
160 return true;
161 }
162 }
163 }
164 }
165 }
166
167 return $can;
168 }
169
170 public function get_license_type()
171 {
172 $license_type = get_option( 'propertyhive_license_type', '' );
173
174 if ( $license_type == '' )
175 {
176 // We don't know. Let's work it out by seeing if they have a license key
177 $existing_license_key = get_option( 'propertyhive_license_key', '' );
178
179 if ( $existing_license_key != '' )
180 {
181 $license = PH()->license->get_current_license();
182
183 if ( isset($license['active']) && $license['active'] == '1' )
184 {
185 return 'old';
186 }
187 }
188 }
189
190 return $license_type; // pro / old
191 }
192
193 private function get_data_for_license_check()
194 {
195 global $wpdb;
196
197 $license_type = $this->get_license_type();
198
199 $data = array();
200 $data['license_key_type'] = $license_type;
201 $data['license_key'] = ( $license_type == 'old' ? get_option( 'propertyhive_license_key', '' ) : get_option( 'propertyhive_pro_license_key', '' ) );
202 $data['url'] = home_url();
203
204 if ( get_option( 'propertyhive_data_sharing' ) != 'no' )
205 {
206 // Not opted-out
207
208 // Retrieve current theme info
209 $theme_data = wp_get_theme();
210 $theme = $theme_data->Name . ' ' . $theme_data->Version;
211
212 $data['php_version'] = phpversion();
213 $data['ph_version'] = PH_VERSION;
214 $data['wp_version'] = get_bloginfo( 'version' );
215 $data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
216
217 $data['install_date'] = get_option('propertyhive_install_timestamp', '');
218 if ( $data['install_date'] != '' && $data['install_date'] != 0 )
219 {
220 $data['install_date'] = date("jS F Y", $data['install_date']);
221 }
222
223 $data['multisite'] = is_multisite();
224 $data['theme'] = $theme;
225 $data['email'] = get_bloginfo( 'admin_email' );
226
227 // Retrieve current plugin information
228 if ( !function_exists( 'get_plugins' ) ) {
229 include ABSPATH . '/wp-admin/includes/plugin.php';
230 }
231
232 $plugins = get_plugins(); // Fetch all plugins with details
233 $active_plugins = get_option('active_plugins', array());
234
235 $active_plugins_data = [];
236 $inactive_plugins_data = [];
237
238 // Loop through plugins to separate active and inactive along with version
239 foreach ($plugins as $plugin_path => $plugin_data)
240 {
241 $plugin_info = [
242 'name' => isset($plugin_data['Name']) ? $plugin_data['Name'] : '',
243 'version' => isset($plugin_data['Version']) ? $plugin_data['Version'] : '',
244 'path' => $plugin_path,
245 ];
246
247 if (in_array($plugin_path, $active_plugins))
248 {
249 $active_plugins_data[] = $plugin_info;
250 }
251 else
252 {
253 $inactive_plugins_data[] = $plugin_info;
254 }
255 }
256
257 $data['active_plugins'] = $active_plugins_data;
258 $data['inactive_plugins'] = $inactive_plugins_data;
259
260 $data['locale'] = get_locale();
261
262 // Import info
263 $property_import = get_option( 'propertyhive_property_import', array() );
264 if ( !is_array($property_import) )
265 {
266 $property_import = array();
267 }
268 $formats = array();
269 foreach ( $property_import as $import_id => $options )
270 {
271 if ( $options['running'] == '1' )
272 {
273 if ( $options['format'] == 'xml' )
274 {
275 $options['format'] .= ' (' . esc_url($options['xml_url']) . ')';
276 }
277 elseif ( $options['format'] == 'csv' )
278 {
279 $options['format'] .= ' (' . esc_url($options['csv_url']) . ')';
280 }
281 $formats[] = $options['format'];
282 }
283 }
284 $data['property_import_formats'] = $formats;
285
286 // Exports
287 $formats = array();
288
289 $exports = array(
290 'propertyhive_realtimefeed' => array(
291 'label' => 'RTDF',
292 'field' => 'send_property_api_url',
293 ),
294 'propertyhive_zooplarealtimefeed' => array(
295 'label' => 'Zoopla',
296 'field' => 'send_property_api_url',
297 ),
298 'propertyhive_blmexport' => array(
299 'label' => 'BLM',
300 'field' => 'ftp_host',
301 ),
302 );
303
304 foreach ( $exports as $option_name => $config )
305 {
306 $property_export = get_option( $option_name, array() );
307
308 if ( ! is_array( $property_export ) ) {
309 continue;
310 }
311
312 if ( ! isset( $property_export['portals'] ) ) {
313 continue;
314 }
315
316 if ( ! is_array( $property_export['portals'] ) ) {
317 continue;
318 }
319
320 $portals = isset( $property_export['portals'] ) && is_array( $property_export['portals'] )
321 ? $property_export['portals']
322 : array();
323
324 foreach ( $portals as $portal_id => $portal )
325 {
326 if ( ! is_array( $portal ) ) {
327 continue;
328 }
329
330 if ( ! is_array( $portal ) || ( $portal['mode'] ?? '' ) !== 'live' ) {
331 continue;
332 }
333
334 $destination = $portal[ $config['field'] ] ?? '';
335
336 $formats[] = sprintf(
337 '%s (%s)',
338 $config['label'],
339 $destination
340 );
341 }
342 }
343
344 $data['property_export_formats'] = $formats;
345
346 // Locrating
347 $locrating_settings = get_option( 'propertyhive_locrating', array() );
348 if ( !empty($locrating_settings) && isset($locrating_settings['enabled']) )
349 {
350 $data['locrating_enabled'] = ( $locrating_settings['enabled'] == 1 ? $locrating_settings['enabled'] : 0 );
351 }
352
353 // Search analytics
354 $table_name = $wpdb->prefix . 'ph_search_log';
355
356 $counts = $wpdb->get_row(
357 "SELECT
358 COALESCE(
359 SUM(searched_at >= UTC_TIMESTAMP() - INTERVAL 7 DAY),
360 0
361 ) AS last_7_days,
362 COALESCE(
363 SUM(searched_at >= UTC_TIMESTAMP() - INTERVAL 30 DAY),
364 0
365 ) AS last_30_days,
366 COUNT(*) AS last_90_days
367 FROM {$table_name}
368 WHERE searched_at >= UTC_TIMESTAMP() - INTERVAL 90 DAY",
369 ARRAY_A
370 );
371
372 if ( is_array($counts) )
373 {
374 $data['searches_last_7_days'] = (int) ( $counts['last_7_days'] ?? 0 );
375 $data['searches_last_30_days'] = (int) ( $counts['last_30_days'] ?? 0 );
376 $data['searches_last_90_days'] = (int) ( $counts['last_90_days'] ?? 0 );
377 }
378
379 // get counts
380 $post_types = array('office', 'property', 'contact', 'appraisal', 'viewing', 'offer', 'sale', 'tenancy', 'key_date');
381 foreach ( $post_types as $post_type )
382 {
383 $args = array(
384 'post_type' => $post_type,
385 'fields' => 'ids',
386 'nopaging' => TRUE,
387 'post_status' => 'publish'
388 );
389 if ( $post_type == 'property' )
390 {
391 $args['meta_query'] = array(
392 array(
393 'key' => '_on_market',
394 'value' => 'yes'
395 )
396 );
397 }
398
399 $post_query = new WP_Query( $args );
400
401 $data['post_type_count_' . $post_type] = $post_query->found_posts;
402
403 wp_reset_postdata();
404 }
405
406 $data = apply_filters( 'propertyhive_license_check_data', $data );
407 }
408
409 return $data;
410 }
411
412 /**
413 * Automated check for licenses
414 *
415 */
416 public function ph_check_licenses( $force_check = false )
417 {
418 // Do a maximum of once per week
419 $last_send = get_option( 'propertyhive_last_license_check', '' );
420 if( !$force_check && is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) ) {
421 return false;
422 }
423
424 update_option( 'propertyhive_last_license_check', time() );
425
426 // Start by removing what we already know about the license
427 update_option( 'propertyhive_license_key_details', '', 'no' );
428 update_option( 'propertyhive_license_key_error', '', 'no' );
429
430 $data = $this->get_data_for_license_check();
431
432 $request = wp_remote_post( 'http://license.wp-property-hive.com/check-license.php', array(
433 'method' => 'POST',
434 'timeout' => 20,
435 'redirection' => 5,
436 'httpversion' => '1.1',
437 'blocking' => true,
438 'body' => $data,
439 'user-agent' => 'PH/' . PH_VERSION . '; ' . get_bloginfo( 'url' )
440 ) );
441
442 if ( is_wp_error( $request ) )
443 {
444 update_option( 'propertyhive_license_key_details', array(), 'no' );
445 update_option( 'propertyhive_license_key_error', $request->get_error_message(), 'no' );
446 return false;
447 }
448
449 if ( isset($request['body']) && $request['body'] == '' )
450 {
451 update_option( 'propertyhive_license_key_details', array(), 'no' );
452 update_option( 'propertyhive_license_key_error', 'No response received when checking license', 'no' );
453 return false;
454 }
455
456 $body = @unserialize($request['body'], ['allowed_classes' => false]);
457 if ( $body !== FALSE && is_array($body) && !empty($body) )
458 {
459 update_option( 'propertyhive_license_key_details', $body, 'no' );
460 }
461 else
462 {
463 update_option( 'propertyhive_license_key_details', array(), 'no' );
464 update_option( 'propertyhive_license_key_error', 'Failed to process response data: ' . print_r($request['body'], true), 'no' );
465 }
466 }
467
468 public function get_current_license()
469 {
470 $license = get_option( 'propertyhive_license_key_details', array() );
471
472 if ( !is_array( $license ) ) { $license = array(); }
473
474 return $license;
475 }
476
477 public function is_valid_pro_license_key($force = false)
478 {
479 $license = $this->get_current_pro_license($force);
480 if ( isset($license['success']) && $license['success'] === true )
481 {
482 return true;
483 }
484
485 return false;
486 }
487
488 public function activate_pro_license_key()
489 {
490 $license = $this->get_pro_license_product_id_and_package(true);
491
492 if ( isset($license['success']) && $license['success'] === true )
493 {
494 $license_key = get_option( 'propertyhive_pro_license_key', '' );
495
496 $instance_id = get_option( 'propertyhive_pro_instance_id', '' );
497
498 if ( empty($instance_id) )
499 {
500 $instance_id = wp_generate_password( 12, false ); // disable specialchars
501 update_option('propertyhive_pro_instance_id', $instance_id );
502 }
503
504 $data = array();
505
506 // found API key. It's product ID will be stored in product_id
507 $url = 'https://wp-property-hive.com/?';
508 $url .= 'wc-api=wc-am-api&';
509 $url .= 'wc_am_action=activate&';
510 $url .= 'instance=' . $instance_id . '&';
511 $url .= 'object=' . parse_url( get_site_url(), PHP_URL_HOST ) . '&';
512 $url .= 'product_id=' . $license['product_id'] . '&';
513 $url .= 'api_key=' . $license_key;
514
515 $response = wp_remote_post( $url, array(
516 'body' => $data,
517 ) );
518
519 if ( is_wp_error($response) )
520 {
521 $return = array(
522 'success' => false,
523 'error' => __( 'Failed to activate license status', 'propertyhive' ) . ': ' . $response->get_error_message()
524 );
525 return $return;
526 }
527
528 if ( 200 !== wp_remote_retrieve_response_code( $response ) )
529 {
530 $return = array(
531 'success' => false,
532 'error' => sprintf(
533 /* translators: %s: HTTP header response code */
534 __( 'Received response code %s when activating license key status', 'propertyhive' ),
535 wp_remote_retrieve_response_code( $response )
536 )
537 );
538 return $return;
539 }
540
541 $result = $response['body'];
542
543 $body = json_decode($result, true);
544
545 if ( json_last_error() !== JSON_ERROR_NONE )
546 {
547 $return = array(
548 'success' => false,
549 'error' => __( 'Failed to decode response when activating license key status. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true )
550 );
551 return $return;
552 }
553
554 if ( isset($body['success']) )
555 {
556 if ( $body['success'] === true )
557 {
558 $return = array(
559 'success' => true,
560 );
561 return $return;
562 }
563 else
564 {
565 $return = array(
566 'success' => false,
567 'error' => __( 'Error when activating license key', 'propertyhive' ) . ': ' . $body['error']
568 );
569 return $return;
570 }
571 }
572 else
573 {
574 $return = array(
575 'success' => false,
576 'error' => __( 'Something went wrong when trying to activate license key', 'propertyhive' ) . ': ' . print_r($body, true)
577 );
578 return $return;
579 }
580 }
581 else
582 {
583 return $license;
584 }
585 }
586
587 public function deactivate_pro_license_key()
588 {
589 $license = $this->get_pro_license_product_id_and_package(true);
590
591 if ( isset($license['success']) && $license['success'] === true )
592 {
593 $license_key = get_option( 'propertyhive_pro_license_key', '' );
594
595 $instance_id = get_option( 'propertyhive_pro_instance_id', '' );
596
597 if ( empty($instance_id) )
598 {
599 $instance_id = wp_generate_password( 12, false ); // disable specialchars
600 update_option('propertyhive_pro_instance_id', $instance_id );
601 }
602
603 $data = array();
604
605 // found API key. It's product ID will be stored in product_id
606 $url = 'https://wp-property-hive.com/?';
607 $url .= 'wc-api=wc-am-api&';
608 $url .= 'wc_am_action=deactivate&';
609 $url .= 'instance=' . $instance_id . '&';
610 $url .= 'object=' . parse_url( get_site_url(), PHP_URL_HOST ) . '&';
611 $url .= 'product_id=' . $license['product_id'] . '&';
612 $url .= 'api_key=' . $license_key;
613
614 $response = wp_remote_post( $url, array(
615 'body' => $data,
616 ) );
617
618 if ( is_wp_error($response) )
619 {
620 $return = array(
621 'success' => false,
622 'error' => __( 'Failed to deactivate license status', 'propertyhive' ) . ': ' . $response->get_error_message()
623 );
624 return $return;
625 }
626
627 if ( 200 !== wp_remote_retrieve_response_code( $response ) )
628 {
629 $return = array(
630 'success' => false,
631 'error' => sprintf(
632 /* translators: %s: HTTP header response code */
633 __( 'Received response code %s when deactivating license key status', 'propertyhive' ),
634 wp_remote_retrieve_response_code( $response )
635 )
636 );
637 return $return;
638 }
639
640 $result = $response['body'];
641
642 $body = json_decode($result, true);
643
644 if ( json_last_error() !== JSON_ERROR_NONE )
645 {
646 $return = array(
647 'success' => false,
648 'error' => __( 'Failed to decode response when deactivating license key status. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true )
649 );
650 return $return;
651 }
652
653 if ( isset($body['success']) )
654 {
655 if ( $body['success'] === true )
656 {
657 $return = array(
658 'success' => true,
659 );
660 return $return;
661 }
662 else
663 {
664 $return = array(
665 'success' => false,
666 'error' => __( 'Error when deactivating license key', 'propertyhive' ) . ': ' . $body['error']
667 );
668 return $return;
669 }
670 }
671 else
672 {
673 $return = array(
674 'success' => false,
675 'error' => __( 'Something went wrong when trying to deactivate license key', 'propertyhive' ) . ': ' . print_r($body, true)
676 );
677 return $return;
678 }
679 }
680 else
681 {
682 return $license;
683 }
684 }
685
686 public function get_pro_license_product_id_and_package($force = false)
687 {
688 if ( $force !== true )
689 {
690 // Not forcing. Get from transient if possible
691 $pro_license_product_id_and_package = get_transient( 'ph_pro_license_product_id_and_package' );
692
693 if ( $pro_license_product_id_and_package !== false )
694 {
695 // return transient value
696 return $pro_license_product_id_and_package;
697 }
698 }
699
700 $license_key = get_option( 'propertyhive_pro_license_key', '' );
701
702 if ( empty($license_key) )
703 {
704 $return = array(
705 'success' => false,
706 'error' => __( 'No license key entered', 'propertyhive' )
707 );
708 set_transient( 'ph_pro_license_product_id_and_package', $return, HOUR_IN_SECONDS );
709 return $return;
710 }
711
712 $instance_id = get_option( 'propertyhive_pro_instance_id', '' );
713
714 if ( empty($instance_id) )
715 {
716 $instance_id = wp_generate_password( 12, false ); // disable specialchars
717 update_option('propertyhive_pro_instance_id', $instance_id );
718 }
719
720 $data = $this->get_data_for_license_check();
721
722 $url = 'https://wp-property-hive.com/?';
723 $url .= 'wc-api=wc-am-api&';
724 $url .= 'wc_am_action=product_list&';
725 $url .= 'instance=' . $instance_id . '&';
726 $url .= 'api_key=' . $license_key;
727
728 $response = wp_remote_post( $url, array(
729 'body' => $data,
730 ) );
731
732 if ( is_wp_error($response) )
733 {
734 $return = array(
735 'success' => false,
736 'error' => __( 'Failed to request license product list', 'propertyhive' ) . ': ' . $response->get_error_message()
737 );
738
739 $last_known = get_option('ph_pro_last_known_license_product_id_and_package', array());
740 if ( !empty($last_known) )
741 {
742 set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS );
743 return $last_known;
744 }
745
746 return $return;
747 }
748
749 if ( 200 !== wp_remote_retrieve_response_code( $response ) )
750 {
751 $return = array(
752 'success' => false,
753 'error' => sprintf(
754 /* translators: %s: HTTP header response code */
755 __( 'Received response code %s when requesting license key product list', 'propertyhive' ),
756 wp_remote_retrieve_response_code( $response )
757 )
758 );
759
760 $last_known = get_option('ph_pro_last_known_license_product_id_and_package', array());
761 if ( !empty($last_known) )
762 {
763 set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS );
764 return $last_known;
765 }
766
767 return $return;
768 }
769
770 $result = $response['body'];
771
772 $body = json_decode($result, true);
773
774 if ( json_last_error() !== JSON_ERROR_NONE )
775 {
776 $return = array(
777 'success' => false,
778 'error' => __( 'Failed to decode response when requesting license key product list. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true )
779 );
780
781 $last_known = get_option('ph_pro_last_known_license_product_id_and_package', array());
782 if ( !empty($last_known) )
783 {
784 set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS );
785 return $last_known;
786 }
787
788 return $return;
789 }
790
791 if ( isset($body['success']) )
792 {
793 if ( $body['success'] === true )
794 {
795 if ( isset($body['data']['product_list']['wc_subs_resources']) && !empty($body['data']['product_list']['wc_subs_resources']) )
796 {
797 $package = false;
798 $product_id = '';
799
800 foreach ( $body['data']['product_list']['wc_subs_resources'] as $resource )
801 {
802 if ( isset($resource['product_id']) )
803 {
804 if ( in_array($resource['product_id'], array(14492, 14493, 14494)) )
805 {
806 $package = 'import';
807 }
808 elseif ( in_array($resource['product_id'], array(14495, 14496, 14497)) )
809 {
810 $package = 'complete';
811 }
812 $product_id = $resource['product_id'];
813 }
814 }
815
816 if ( $package !== FALSE )
817 {
818 $return = array(
819 'success' => true,
820 'package' => $package,
821 'product_id' => $product_id
822 );
823 }
824 else
825 {
826 $return = array(
827 'success' => false,
828 'error' => __( 'API key exists but unable to establish the plan', 'propertyhive' )
829 );
830 }
831 }
832 else
833 {
834 $return = array(
835 'success' => false,
836 'error' => __( 'API key doesn\'t appear to belong to any orders', 'propertyhive' ) . ': ' . print_r($body, true)
837 );
838 }
839 }
840 else
841 {
842 $return = array(
843 'success' => false,
844 'error' => __( 'Error when requesting license key product list', 'propertyhive' ) . ': ' . $body['error']
845 );
846 }
847
848 set_transient( 'ph_pro_license_product_id_and_package', $return, HOUR_IN_SECONDS * 3 );
849 update_option( 'ph_pro_last_known_license_product_id_and_package', $return );
850 return $return;
851 }
852 else
853 {
854 $return = array(
855 'success' => false,
856 'error' => __( 'Something went wrong when requesting license key product list', 'propertyhive' ) . ': ' . print_r($body, true)
857 );
858
859 $last_known = get_option('ph_pro_last_known_license_product_id_and_package', array());
860 if ( !empty($last_known) )
861 {
862 set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS );
863 return $last_known;
864 }
865
866 return $return;
867 }
868 }
869
870 public function get_current_pro_license($force = false)
871 {
872 if ( $force !== true )
873 {
874 // Not forcing. Get from transient if possible
875 $pro_license_status = get_transient( 'ph_pro_license_status' );
876
877 if ( $pro_license_status !== false )
878 {
879 // return transient value
880 return $pro_license_status;
881 }
882 }
883
884 $product_id_and_package = $this->get_pro_license_product_id_and_package($force);
885
886 if ( !isset($product_id_and_package['success']) || ( isset($product_id_and_package['success']) && $product_id_and_package['success'] === false ) )
887 {
888 return $product_id_and_package;
889 }
890
891 $license_key = get_option( 'propertyhive_pro_license_key', '' );
892
893 if ( empty($license_key) )
894 {
895 $return = array(
896 'success' => false,
897 'error' => __( 'No license key entered', 'propertyhive' )
898 );
899 set_transient( 'ph_pro_license_status', $return, HOUR_IN_SECONDS );
900 return $return;
901 }
902
903 $instance_id = get_option( 'propertyhive_pro_instance_id', '' );
904
905 if ( empty($instance_id) )
906 {
907 $instance_id = wp_generate_password( 12, false ); // disable specialchars
908 update_option('propertyhive_pro_instance_id', $instance_id );
909 }
910
911 $data = $this->get_data_for_license_check();
912
913 $url = 'https://wp-property-hive.com/?';
914 $url .= 'wc-api=wc-am-api&';
915 $url .= 'wc_am_action=status&';
916 $url .= 'product_id=' . $product_id_and_package['product_id'] . '&';
917 $url .= 'instance=' . $instance_id . '&';
918 $url .= 'api_key=' . $license_key;
919
920 $response = wp_remote_post( $url, array(
921 'body' => $data,
922 ) );
923
924 if ( is_wp_error($response) )
925 {
926 // error for some reason. Return last known status
927 $previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' );
928 if ( !empty($previous_license_key_status) )
929 {
930 set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS );
931 return $previous_license_key_status;
932 }
933
934 $return = array(
935 'success' => false,
936 'error' => __( 'Failed to request license status', 'propertyhive' ) . ': ' . $response->get_error_message()
937 );
938 return $return;
939 }
940
941 if ( 200 !== wp_remote_retrieve_response_code( $response ) )
942 {
943 // error for some reason. Return last known status
944 $previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' );
945 if ( !empty($previous_license_key_status) )
946 {
947 set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS );
948 return $previous_license_key_status;
949 }
950
951 $return = array(
952 'success' => false,
953 'error' => sprintf(
954 /* translators: %s: HTTP header response code */
955 __( 'Received response code %s when requesting license key status', 'propertyhive' ),
956 wp_remote_retrieve_response_code( $response )
957 )
958 );
959
960 return $return;
961 }
962
963 $result = $response['body'];
964
965 $body = json_decode($result, true);
966
967 if ( json_last_error() !== JSON_ERROR_NONE )
968 {
969 $return = array(
970 'success' => false,
971 'error' => __( 'Failed to decode response when requesting license key status. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true )
972 );
973
974 // error for some reason. Return last known status
975 $previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' );
976 if ( !empty($previous_license_key_status) )
977 {
978 set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS );
979 return $previous_license_key_status;
980 }
981
982 return $return;
983 }
984
985 if ( isset($body['success']) )
986 {
987 if ( $body['success'] === true )
988 {
989 if ( isset($body['status_check']) && $body['status_check'] === 'active' )
990 {
991 $return = array(
992 'success' => true
993 );
994 }
995 else
996 {
997 $return = array(
998 'success' => false,
999 'error' => __( 'License key inactive', 'propertyhive' )
1000 );
1001 }
1002 }
1003 else
1004 {
1005 $return = array(
1006 'success' => false,
1007 'error' => __( 'Error when requesting license key status', 'propertyhive' ) . ': ' . $body['error']
1008 );
1009 }
1010
1011 update_option( 'propertyhive_pro_license_key_last_checked', time() );
1012 update_option( 'propertyhive_pro_license_key_status', $return );
1013
1014 set_transient( 'ph_pro_license_status', $return, HOUR_IN_SECONDS * 3 );
1015 return $return;
1016 }
1017 else
1018 {
1019 $return = array(
1020 'success' => false,
1021 'error' => __( 'Something went wrong when requesting license key status', 'propertyhive' ) . ': ' . print_r($body, true)
1022 );
1023
1024 // error for some reason. Return last known status
1025 $previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' );
1026 if ( !empty($previous_license_key_status) )
1027 {
1028 set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS );
1029 return $previous_license_key_status;
1030 }
1031
1032 return $return;
1033 }
1034 }
1035 }
1036