| 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’ 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’ 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 |
$license_type = $this->get_license_type(); |
| 196 |
|
| 197 |
$data = array(); |
| 198 |
$data['license_key_type'] = $license_type; |
| 199 |
$data['license_key'] = ( $license_type == 'old' ? get_option( 'propertyhive_license_key', '' ) : get_option( 'propertyhive_pro_license_key', '' ) ); |
| 200 |
$data['url'] = home_url(); |
| 201 |
|
| 202 |
if ( get_option( 'propertyhive_data_sharing' ) != 'no' ) |
| 203 |
{ |
| 204 |
// Not opted-out |
| 205 |
|
| 206 |
// Retrieve current theme info |
| 207 |
$theme_data = wp_get_theme(); |
| 208 |
$theme = $theme_data->Name . ' ' . $theme_data->Version; |
| 209 |
|
| 210 |
$data['php_version'] = phpversion(); |
| 211 |
$data['ph_version'] = PH_VERSION; |
| 212 |
$data['wp_version'] = get_bloginfo( 'version' ); |
| 213 |
$data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : ''; |
| 214 |
|
| 215 |
$data['install_date'] = get_option('propertyhive_install_timestamp', ''); |
| 216 |
if ( $data['install_date'] != '' && $data['install_date'] != 0 ) |
| 217 |
{ |
| 218 |
$data['install_date'] = date("jS F Y", $data['install_date']); |
| 219 |
} |
| 220 |
|
| 221 |
$data['multisite'] = is_multisite(); |
| 222 |
$data['theme'] = $theme; |
| 223 |
$data['email'] = get_bloginfo( 'admin_email' ); |
| 224 |
|
| 225 |
// Retrieve current plugin information |
| 226 |
if ( !function_exists( 'get_plugins' ) ) { |
| 227 |
include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 228 |
} |
| 229 |
|
| 230 |
$plugins = get_plugins(); // Fetch all plugins with details |
| 231 |
$active_plugins = get_option('active_plugins', array()); |
| 232 |
|
| 233 |
$active_plugins_data = []; |
| 234 |
$inactive_plugins_data = []; |
| 235 |
|
| 236 |
// Loop through plugins to separate active and inactive along with version |
| 237 |
foreach ($plugins as $plugin_path => $plugin_data) |
| 238 |
{ |
| 239 |
$plugin_info = [ |
| 240 |
'name' => isset($plugin_data['Name']) ? $plugin_data['Name'] : '', |
| 241 |
'version' => isset($plugin_data['Version']) ? $plugin_data['Version'] : '', |
| 242 |
'path' => $plugin_path, |
| 243 |
]; |
| 244 |
|
| 245 |
if (in_array($plugin_path, $active_plugins)) |
| 246 |
{ |
| 247 |
$active_plugins_data[] = $plugin_info; |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
$inactive_plugins_data[] = $plugin_info; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
$data['active_plugins'] = $active_plugins_data; |
| 256 |
$data['inactive_plugins'] = $inactive_plugins_data; |
| 257 |
|
| 258 |
$data['locale'] = get_locale(); |
| 259 |
|
| 260 |
$property_import = get_option( 'propertyhive_property_import', array() ); |
| 261 |
if ( !is_array($property_import) ) |
| 262 |
{ |
| 263 |
$property_import = array(); |
| 264 |
} |
| 265 |
$formats = array(); |
| 266 |
foreach ( $property_import as $import_id => $options ) |
| 267 |
{ |
| 268 |
if ( $options['running'] == '1' ) |
| 269 |
{ |
| 270 |
$formats[] = $options['format']; |
| 271 |
} |
| 272 |
} |
| 273 |
$data['property_import_formats'] = $formats; |
| 274 |
|
| 275 |
// get counts |
| 276 |
$post_types = array('office', 'property', 'contact', 'appraisal', 'viewing', 'offer', 'sale', 'tenancy', 'key_date'); |
| 277 |
foreach ( $post_types as $post_type ) |
| 278 |
{ |
| 279 |
$args = array( |
| 280 |
'post_type' => $post_type, |
| 281 |
'fields' => 'ids', |
| 282 |
'nopaging' => TRUE, |
| 283 |
'post_status' => 'publish' |
| 284 |
); |
| 285 |
if ( $post_type == 'property' ) |
| 286 |
{ |
| 287 |
$args['meta_query'] = array( |
| 288 |
array( |
| 289 |
'key' => '_on_market', |
| 290 |
'value' => 'yes' |
| 291 |
) |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
$post_query = new WP_Query( $args ); |
| 296 |
|
| 297 |
$data['post_type_count_' . $post_type] = $post_query->found_posts; |
| 298 |
|
| 299 |
wp_reset_postdata(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return $data; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Automated check for licenses |
| 308 |
* |
| 309 |
*/ |
| 310 |
public function ph_check_licenses( $force_check = false ) |
| 311 |
{ |
| 312 |
// Do a maximum of once per week |
| 313 |
$last_send = get_option( 'propertyhive_last_license_check', '' ); |
| 314 |
if( !$force_check && is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) ) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
update_option( 'propertyhive_last_license_check', time() ); |
| 319 |
|
| 320 |
// Start by removing what we already know about the license |
| 321 |
update_option( 'propertyhive_license_key_details', '', 'no' ); |
| 322 |
update_option( 'propertyhive_license_key_error', '', 'no' ); |
| 323 |
|
| 324 |
$data = $this->get_data_for_license_check(); |
| 325 |
|
| 326 |
$request = wp_remote_post( 'http://license.wp-property-hive.com/check-license.php', array( |
| 327 |
'method' => 'POST', |
| 328 |
'timeout' => 20, |
| 329 |
'redirection' => 5, |
| 330 |
'httpversion' => '1.1', |
| 331 |
'blocking' => true, |
| 332 |
'body' => $data, |
| 333 |
'user-agent' => 'PH/' . PH_VERSION . '; ' . get_bloginfo( 'url' ) |
| 334 |
) ); |
| 335 |
|
| 336 |
if ( is_wp_error( $request ) ) |
| 337 |
{ |
| 338 |
update_option( 'propertyhive_license_key_details', array(), 'no' ); |
| 339 |
update_option( 'propertyhive_license_key_error', $request->get_error_message(), 'no' ); |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
if ( isset($request['body']) && $request['body'] == '' ) |
| 344 |
{ |
| 345 |
update_option( 'propertyhive_license_key_details', array(), 'no' ); |
| 346 |
update_option( 'propertyhive_license_key_error', 'No response received when checking license', 'no' ); |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
$body = @unserialize($request['body'], ['allowed_classes' => false]); |
| 351 |
if ( $body !== FALSE && is_array($body) && !empty($body) ) |
| 352 |
{ |
| 353 |
update_option( 'propertyhive_license_key_details', $body, 'no' ); |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
update_option( 'propertyhive_license_key_details', array(), 'no' ); |
| 358 |
update_option( 'propertyhive_license_key_error', 'Failed to process response data: ' . print_r($request['body'], true), 'no' ); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
public function get_current_license() |
| 363 |
{ |
| 364 |
$license = get_option( 'propertyhive_license_key_details', array() ); |
| 365 |
|
| 366 |
if ( !is_array( $license ) ) { $license = array(); } |
| 367 |
|
| 368 |
return $license; |
| 369 |
} |
| 370 |
|
| 371 |
public function is_valid_pro_license_key($force = false) |
| 372 |
{ |
| 373 |
$license = $this->get_current_pro_license($force); |
| 374 |
if ( isset($license['success']) && $license['success'] === true ) |
| 375 |
{ |
| 376 |
return true; |
| 377 |
} |
| 378 |
|
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
public function activate_pro_license_key() |
| 383 |
{ |
| 384 |
$license = $this->get_pro_license_product_id_and_package(true); |
| 385 |
|
| 386 |
if ( isset($license['success']) && $license['success'] === true ) |
| 387 |
{ |
| 388 |
$license_key = get_option( 'propertyhive_pro_license_key', '' ); |
| 389 |
|
| 390 |
$instance_id = get_option( 'propertyhive_pro_instance_id', '' ); |
| 391 |
|
| 392 |
if ( empty($instance_id) ) |
| 393 |
{ |
| 394 |
$instance_id = wp_generate_password( 12, false ); // disable specialchars |
| 395 |
update_option('propertyhive_pro_instance_id', $instance_id ); |
| 396 |
} |
| 397 |
|
| 398 |
$data = array(); |
| 399 |
|
| 400 |
// found API key. It's product ID will be stored in product_id |
| 401 |
$url = 'https://wp-property-hive.com/?'; |
| 402 |
$url .= 'wc-api=wc-am-api&'; |
| 403 |
$url .= 'wc_am_action=activate&'; |
| 404 |
$url .= 'instance=' . $instance_id . '&'; |
| 405 |
$url .= 'object=' . parse_url( get_site_url(), PHP_URL_HOST ) . '&'; |
| 406 |
$url .= 'product_id=' . $license['product_id'] . '&'; |
| 407 |
$url .= 'api_key=' . $license_key; |
| 408 |
|
| 409 |
$response = wp_remote_post( $url, array( |
| 410 |
'body' => $data, |
| 411 |
) ); |
| 412 |
|
| 413 |
if ( is_wp_error($response) ) |
| 414 |
{ |
| 415 |
$return = array( |
| 416 |
'success' => false, |
| 417 |
'error' => __( 'Failed to activate license status', 'propertyhive' ) . ': ' . $response->get_error_message() |
| 418 |
); |
| 419 |
return $return; |
| 420 |
} |
| 421 |
|
| 422 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) |
| 423 |
{ |
| 424 |
$return = array( |
| 425 |
'success' => false, |
| 426 |
'error' => sprintf( |
| 427 |
/* translators: %s: HTTP header response code */ |
| 428 |
__( 'Received response code %s when activating license key status', 'propertyhive' ), |
| 429 |
wp_remote_retrieve_response_code( $response ) |
| 430 |
) |
| 431 |
); |
| 432 |
return $return; |
| 433 |
} |
| 434 |
|
| 435 |
$result = $response['body']; |
| 436 |
|
| 437 |
$body = json_decode($result, true); |
| 438 |
|
| 439 |
if ( json_last_error() !== JSON_ERROR_NONE ) |
| 440 |
{ |
| 441 |
$return = array( |
| 442 |
'success' => false, |
| 443 |
'error' => __( 'Failed to decode response when activating license key status. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true ) |
| 444 |
); |
| 445 |
return $return; |
| 446 |
} |
| 447 |
|
| 448 |
if ( isset($body['success']) ) |
| 449 |
{ |
| 450 |
if ( $body['success'] === true ) |
| 451 |
{ |
| 452 |
$return = array( |
| 453 |
'success' => true, |
| 454 |
); |
| 455 |
return $return; |
| 456 |
} |
| 457 |
else |
| 458 |
{ |
| 459 |
$return = array( |
| 460 |
'success' => false, |
| 461 |
'error' => __( 'Error when activating license key', 'propertyhive' ) . ': ' . $body['error'] |
| 462 |
); |
| 463 |
return $return; |
| 464 |
} |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
$return = array( |
| 469 |
'success' => false, |
| 470 |
'error' => __( 'Something went wrong when trying to activate license key', 'propertyhive' ) . ': ' . print_r($body, true) |
| 471 |
); |
| 472 |
return $return; |
| 473 |
} |
| 474 |
} |
| 475 |
else |
| 476 |
{ |
| 477 |
return $license; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
public function deactivate_pro_license_key() |
| 482 |
{ |
| 483 |
$license = $this->get_pro_license_product_id_and_package(true); |
| 484 |
|
| 485 |
if ( isset($license['success']) && $license['success'] === true ) |
| 486 |
{ |
| 487 |
$license_key = get_option( 'propertyhive_pro_license_key', '' ); |
| 488 |
|
| 489 |
$instance_id = get_option( 'propertyhive_pro_instance_id', '' ); |
| 490 |
|
| 491 |
if ( empty($instance_id) ) |
| 492 |
{ |
| 493 |
$instance_id = wp_generate_password( 12, false ); // disable specialchars |
| 494 |
update_option('propertyhive_pro_instance_id', $instance_id ); |
| 495 |
} |
| 496 |
|
| 497 |
$data = array(); |
| 498 |
|
| 499 |
// found API key. It's product ID will be stored in product_id |
| 500 |
$url = 'https://wp-property-hive.com/?'; |
| 501 |
$url .= 'wc-api=wc-am-api&'; |
| 502 |
$url .= 'wc_am_action=deactivate&'; |
| 503 |
$url .= 'instance=' . $instance_id . '&'; |
| 504 |
$url .= 'object=' . parse_url( get_site_url(), PHP_URL_HOST ) . '&'; |
| 505 |
$url .= 'product_id=' . $license['product_id'] . '&'; |
| 506 |
$url .= 'api_key=' . $license_key; |
| 507 |
|
| 508 |
$response = wp_remote_post( $url, array( |
| 509 |
'body' => $data, |
| 510 |
) ); |
| 511 |
|
| 512 |
if ( is_wp_error($response) ) |
| 513 |
{ |
| 514 |
$return = array( |
| 515 |
'success' => false, |
| 516 |
'error' => __( 'Failed to deactivate license status', 'propertyhive' ) . ': ' . $response->get_error_message() |
| 517 |
); |
| 518 |
return $return; |
| 519 |
} |
| 520 |
|
| 521 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) |
| 522 |
{ |
| 523 |
$return = array( |
| 524 |
'success' => false, |
| 525 |
'error' => sprintf( |
| 526 |
/* translators: %s: HTTP header response code */ |
| 527 |
__( 'Received response code %s when deactivating license key status', 'propertyhive' ), |
| 528 |
wp_remote_retrieve_response_code( $response ) |
| 529 |
) |
| 530 |
); |
| 531 |
return $return; |
| 532 |
} |
| 533 |
|
| 534 |
$result = $response['body']; |
| 535 |
|
| 536 |
$body = json_decode($result, true); |
| 537 |
|
| 538 |
if ( json_last_error() !== JSON_ERROR_NONE ) |
| 539 |
{ |
| 540 |
$return = array( |
| 541 |
'success' => false, |
| 542 |
'error' => __( 'Failed to decode response when deactivating license key status. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true ) |
| 543 |
); |
| 544 |
return $return; |
| 545 |
} |
| 546 |
|
| 547 |
if ( isset($body['success']) ) |
| 548 |
{ |
| 549 |
if ( $body['success'] === true ) |
| 550 |
{ |
| 551 |
$return = array( |
| 552 |
'success' => true, |
| 553 |
); |
| 554 |
return $return; |
| 555 |
} |
| 556 |
else |
| 557 |
{ |
| 558 |
$return = array( |
| 559 |
'success' => false, |
| 560 |
'error' => __( 'Error when deactivating license key', 'propertyhive' ) . ': ' . $body['error'] |
| 561 |
); |
| 562 |
return $return; |
| 563 |
} |
| 564 |
} |
| 565 |
else |
| 566 |
{ |
| 567 |
$return = array( |
| 568 |
'success' => false, |
| 569 |
'error' => __( 'Something went wrong when trying to deactivate license key', 'propertyhive' ) . ': ' . print_r($body, true) |
| 570 |
); |
| 571 |
return $return; |
| 572 |
} |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
return $license; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
public function get_pro_license_product_id_and_package($force = false) |
| 581 |
{ |
| 582 |
if ( $force !== true ) |
| 583 |
{ |
| 584 |
// Not forcing. Get from transient if possible |
| 585 |
$pro_license_product_id_and_package = get_transient( 'ph_pro_license_product_id_and_package' ); |
| 586 |
|
| 587 |
if ( $pro_license_product_id_and_package !== false ) |
| 588 |
{ |
| 589 |
// return transient value |
| 590 |
return $pro_license_product_id_and_package; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
$license_key = get_option( 'propertyhive_pro_license_key', '' ); |
| 595 |
|
| 596 |
if ( empty($license_key) ) |
| 597 |
{ |
| 598 |
$return = array( |
| 599 |
'success' => false, |
| 600 |
'error' => __( 'No license key entered', 'propertyhive' ) |
| 601 |
); |
| 602 |
set_transient( 'ph_pro_license_product_id_and_package', $return, HOUR_IN_SECONDS ); |
| 603 |
return $return; |
| 604 |
} |
| 605 |
|
| 606 |
$instance_id = get_option( 'propertyhive_pro_instance_id', '' ); |
| 607 |
|
| 608 |
if ( empty($instance_id) ) |
| 609 |
{ |
| 610 |
$instance_id = wp_generate_password( 12, false ); // disable specialchars |
| 611 |
update_option('propertyhive_pro_instance_id', $instance_id ); |
| 612 |
} |
| 613 |
|
| 614 |
$data = $this->get_data_for_license_check(); |
| 615 |
|
| 616 |
$url = 'https://wp-property-hive.com/?'; |
| 617 |
$url .= 'wc-api=wc-am-api&'; |
| 618 |
$url .= 'wc_am_action=product_list&'; |
| 619 |
$url .= 'instance=' . $instance_id . '&'; |
| 620 |
$url .= 'api_key=' . $license_key; |
| 621 |
|
| 622 |
$response = wp_remote_post( $url, array( |
| 623 |
'body' => $data, |
| 624 |
) ); |
| 625 |
|
| 626 |
if ( is_wp_error($response) ) |
| 627 |
{ |
| 628 |
$return = array( |
| 629 |
'success' => false, |
| 630 |
'error' => __( 'Failed to request license product list', 'propertyhive' ) . ': ' . $response->get_error_message() |
| 631 |
); |
| 632 |
|
| 633 |
$last_known = get_option('ph_pro_last_known_license_product_id_and_package', array()); |
| 634 |
if ( !empty($last_known) ) |
| 635 |
{ |
| 636 |
set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS ); |
| 637 |
return $last_known; |
| 638 |
} |
| 639 |
|
| 640 |
return $return; |
| 641 |
} |
| 642 |
|
| 643 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) |
| 644 |
{ |
| 645 |
$return = array( |
| 646 |
'success' => false, |
| 647 |
'error' => sprintf( |
| 648 |
/* translators: %s: HTTP header response code */ |
| 649 |
__( 'Received response code %s when requesting license key product list', 'propertyhive' ), |
| 650 |
wp_remote_retrieve_response_code( $response ) |
| 651 |
) |
| 652 |
); |
| 653 |
|
| 654 |
$last_known = get_option('ph_pro_last_known_license_product_id_and_package', array()); |
| 655 |
if ( !empty($last_known) ) |
| 656 |
{ |
| 657 |
set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS ); |
| 658 |
return $last_known; |
| 659 |
} |
| 660 |
|
| 661 |
return $return; |
| 662 |
} |
| 663 |
|
| 664 |
$result = $response['body']; |
| 665 |
|
| 666 |
$body = json_decode($result, true); |
| 667 |
|
| 668 |
if ( json_last_error() !== JSON_ERROR_NONE ) |
| 669 |
{ |
| 670 |
$return = array( |
| 671 |
'success' => false, |
| 672 |
'error' => __( 'Failed to decode response when requesting license key product list. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true ) |
| 673 |
); |
| 674 |
|
| 675 |
$last_known = get_option('ph_pro_last_known_license_product_id_and_package', array()); |
| 676 |
if ( !empty($last_known) ) |
| 677 |
{ |
| 678 |
set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS ); |
| 679 |
return $last_known; |
| 680 |
} |
| 681 |
|
| 682 |
return $return; |
| 683 |
} |
| 684 |
|
| 685 |
if ( isset($body['success']) ) |
| 686 |
{ |
| 687 |
if ( $body['success'] === true ) |
| 688 |
{ |
| 689 |
if ( isset($body['data']['product_list']['wc_subs_resources']) && !empty($body['data']['product_list']['wc_subs_resources']) ) |
| 690 |
{ |
| 691 |
$package = false; |
| 692 |
$product_id = ''; |
| 693 |
|
| 694 |
foreach ( $body['data']['product_list']['wc_subs_resources'] as $resource ) |
| 695 |
{ |
| 696 |
if ( isset($resource['product_id']) ) |
| 697 |
{ |
| 698 |
if ( in_array($resource['product_id'], array(14492, 14493, 14494)) ) |
| 699 |
{ |
| 700 |
$package = 'import'; |
| 701 |
} |
| 702 |
elseif ( in_array($resource['product_id'], array(14495, 14496, 14497)) ) |
| 703 |
{ |
| 704 |
$package = 'complete'; |
| 705 |
} |
| 706 |
$product_id = $resource['product_id']; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
if ( $package !== FALSE ) |
| 711 |
{ |
| 712 |
$return = array( |
| 713 |
'success' => true, |
| 714 |
'package' => $package, |
| 715 |
'product_id' => $product_id |
| 716 |
); |
| 717 |
} |
| 718 |
else |
| 719 |
{ |
| 720 |
$return = array( |
| 721 |
'success' => false, |
| 722 |
'error' => __( 'API key exists but unable to establish the plan', 'propertyhive' ) |
| 723 |
); |
| 724 |
} |
| 725 |
} |
| 726 |
else |
| 727 |
{ |
| 728 |
$return = array( |
| 729 |
'success' => false, |
| 730 |
'error' => __( 'API key doesn\'t appear to belong to any orders', 'propertyhive' ) . ': ' . print_r($body, true) |
| 731 |
); |
| 732 |
} |
| 733 |
} |
| 734 |
else |
| 735 |
{ |
| 736 |
$return = array( |
| 737 |
'success' => false, |
| 738 |
'error' => __( 'Error when requesting license key product list', 'propertyhive' ) . ': ' . $body['error'] |
| 739 |
); |
| 740 |
} |
| 741 |
|
| 742 |
set_transient( 'ph_pro_license_product_id_and_package', $return, HOUR_IN_SECONDS * 3 ); |
| 743 |
update_option( 'ph_pro_last_known_license_product_id_and_package', $return ); |
| 744 |
return $return; |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
$return = array( |
| 749 |
'success' => false, |
| 750 |
'error' => __( 'Something went wrong when requesting license key product list', 'propertyhive' ) . ': ' . print_r($body, true) |
| 751 |
); |
| 752 |
|
| 753 |
$last_known = get_option('ph_pro_last_known_license_product_id_and_package', array()); |
| 754 |
if ( !empty($last_known) ) |
| 755 |
{ |
| 756 |
set_transient( 'ph_pro_license_product_id_and_package', $last_known, HOUR_IN_SECONDS ); |
| 757 |
return $last_known; |
| 758 |
} |
| 759 |
|
| 760 |
return $return; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
public function get_current_pro_license($force = false) |
| 765 |
{ |
| 766 |
if ( $force !== true ) |
| 767 |
{ |
| 768 |
// Not forcing. Get from transient if possible |
| 769 |
$pro_license_status = get_transient( 'ph_pro_license_status' ); |
| 770 |
|
| 771 |
if ( $pro_license_status !== false ) |
| 772 |
{ |
| 773 |
// return transient value |
| 774 |
return $pro_license_status; |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
$product_id_and_package = $this->get_pro_license_product_id_and_package($force); |
| 779 |
|
| 780 |
if ( !isset($product_id_and_package['success']) || ( isset($product_id_and_package['success']) && $product_id_and_package['success'] === false ) ) |
| 781 |
{ |
| 782 |
return $product_id_and_package; |
| 783 |
} |
| 784 |
|
| 785 |
$license_key = get_option( 'propertyhive_pro_license_key', '' ); |
| 786 |
|
| 787 |
if ( empty($license_key) ) |
| 788 |
{ |
| 789 |
$return = array( |
| 790 |
'success' => false, |
| 791 |
'error' => __( 'No license key entered', 'propertyhive' ) |
| 792 |
); |
| 793 |
set_transient( 'ph_pro_license_status', $return, HOUR_IN_SECONDS ); |
| 794 |
return $return; |
| 795 |
} |
| 796 |
|
| 797 |
$instance_id = get_option( 'propertyhive_pro_instance_id', '' ); |
| 798 |
|
| 799 |
if ( empty($instance_id) ) |
| 800 |
{ |
| 801 |
$instance_id = wp_generate_password( 12, false ); // disable specialchars |
| 802 |
update_option('propertyhive_pro_instance_id', $instance_id ); |
| 803 |
} |
| 804 |
|
| 805 |
$data = $this->get_data_for_license_check(); |
| 806 |
|
| 807 |
$url = 'https://wp-property-hive.com/?'; |
| 808 |
$url .= 'wc-api=wc-am-api&'; |
| 809 |
$url .= 'wc_am_action=status&'; |
| 810 |
$url .= 'product_id=' . $product_id_and_package['product_id'] . '&'; |
| 811 |
$url .= 'instance=' . $instance_id . '&'; |
| 812 |
$url .= 'api_key=' . $license_key; |
| 813 |
|
| 814 |
$response = wp_remote_post( $url, array( |
| 815 |
'body' => $data, |
| 816 |
) ); |
| 817 |
|
| 818 |
if ( is_wp_error($response) ) |
| 819 |
{ |
| 820 |
// error for some reason. Return last known status |
| 821 |
$previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' ); |
| 822 |
if ( !empty($previous_license_key_status) ) |
| 823 |
{ |
| 824 |
set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS ); |
| 825 |
return $previous_license_key_status; |
| 826 |
} |
| 827 |
|
| 828 |
$return = array( |
| 829 |
'success' => false, |
| 830 |
'error' => __( 'Failed to request license status', 'propertyhive' ) . ': ' . $response->get_error_message() |
| 831 |
); |
| 832 |
return $return; |
| 833 |
} |
| 834 |
|
| 835 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) |
| 836 |
{ |
| 837 |
// error for some reason. Return last known status |
| 838 |
$previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' ); |
| 839 |
if ( !empty($previous_license_key_status) ) |
| 840 |
{ |
| 841 |
set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS ); |
| 842 |
return $previous_license_key_status; |
| 843 |
} |
| 844 |
|
| 845 |
$return = array( |
| 846 |
'success' => false, |
| 847 |
'error' => sprintf( |
| 848 |
/* translators: %s: HTTP header response code */ |
| 849 |
__( 'Received response code %s when requesting license key status', 'propertyhive' ), |
| 850 |
wp_remote_retrieve_response_code( $response ) |
| 851 |
) |
| 852 |
); |
| 853 |
|
| 854 |
return $return; |
| 855 |
} |
| 856 |
|
| 857 |
$result = $response['body']; |
| 858 |
|
| 859 |
$body = json_decode($result, true); |
| 860 |
|
| 861 |
if ( json_last_error() !== JSON_ERROR_NONE ) |
| 862 |
{ |
| 863 |
$return = array( |
| 864 |
'success' => false, |
| 865 |
'error' => __( 'Failed to decode response when requesting license key status. Please try again', 'propertyhive' ) . ': ' . print_r( $result, true ) |
| 866 |
); |
| 867 |
|
| 868 |
// error for some reason. Return last known status |
| 869 |
$previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' ); |
| 870 |
if ( !empty($previous_license_key_status) ) |
| 871 |
{ |
| 872 |
set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS ); |
| 873 |
return $previous_license_key_status; |
| 874 |
} |
| 875 |
|
| 876 |
return $return; |
| 877 |
} |
| 878 |
|
| 879 |
if ( isset($body['success']) ) |
| 880 |
{ |
| 881 |
if ( $body['success'] === true ) |
| 882 |
{ |
| 883 |
if ( isset($body['status_check']) && $body['status_check'] === 'active' ) |
| 884 |
{ |
| 885 |
$return = array( |
| 886 |
'success' => true |
| 887 |
); |
| 888 |
} |
| 889 |
else |
| 890 |
{ |
| 891 |
$return = array( |
| 892 |
'success' => false, |
| 893 |
'error' => __( 'License key inactive', 'propertyhive' ) |
| 894 |
); |
| 895 |
} |
| 896 |
} |
| 897 |
else |
| 898 |
{ |
| 899 |
$return = array( |
| 900 |
'success' => false, |
| 901 |
'error' => __( 'Error when requesting license key status', 'propertyhive' ) . ': ' . $body['error'] |
| 902 |
); |
| 903 |
} |
| 904 |
|
| 905 |
update_option( 'propertyhive_pro_license_key_last_checked', time() ); |
| 906 |
update_option( 'propertyhive_pro_license_key_status', $return ); |
| 907 |
|
| 908 |
set_transient( 'ph_pro_license_status', $return, HOUR_IN_SECONDS * 3 ); |
| 909 |
return $return; |
| 910 |
} |
| 911 |
else |
| 912 |
{ |
| 913 |
$return = array( |
| 914 |
'success' => false, |
| 915 |
'error' => __( 'Something went wrong when requesting license key status', 'propertyhive' ) . ': ' . print_r($body, true) |
| 916 |
); |
| 917 |
|
| 918 |
// error for some reason. Return last known status |
| 919 |
$previous_license_key_status = get_option( 'propertyhive_pro_license_key_status', '' ); |
| 920 |
if ( !empty($previous_license_key_status) ) |
| 921 |
{ |
| 922 |
set_transient( 'ph_pro_license_status', $previous_license_key_status, HOUR_IN_SECONDS ); |
| 923 |
return $previous_license_key_status; |
| 924 |
} |
| 925 |
|
| 926 |
return $return; |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|