| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* PropertyHive countries |
| 9 |
* |
| 10 |
* The PropertyHive countries class stores country data. |
| 11 |
* |
| 12 |
* @class PH_Countries |
| 13 |
* @version 1.0.0 |
| 14 |
* @package PropertyHive/Classes |
| 15 |
* @category Class |
| 16 |
* @author PropertyHive |
| 17 |
*/ |
| 18 |
class PH_Countries { |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
|
| 22 |
add_action( 'template_redirect', array( $this, 'ph_check_currency_change' ) ); |
| 23 |
|
| 24 |
add_action( 'propertyhive_update_currency_exchange_rates', array( $this, 'ph_update_currency_exchange_rates' ) ); |
| 25 |
|
| 26 |
add_filter( 'propertyhive_search_form_fields_after', array( $this, 'ensure_currency_value_set' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Auto-load in-accessible properties on demand. |
| 31 |
* @param mixed $key |
| 32 |
* @return mixed |
| 33 |
*/ |
| 34 |
public function __get( $key ) { |
| 35 |
if ( 'countries' == $key ) { |
| 36 |
return $this->get_countries(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function ensure_currency_value_set( $form_controls ) |
| 41 |
{ |
| 42 |
if ( isset($form_controls['currency']) ) |
| 43 |
{ |
| 44 |
if ( isset($_GET['currency']) && $_GET['currency'] != '' ) |
| 45 |
{ |
| 46 |
|
| 47 |
} |
| 48 |
elseif ( isset($_COOKIE['propertyhive_currency']) && $_COOKIE['propertyhive_currency'] != '' ) |
| 49 |
{ |
| 50 |
$currency = @json_decode(html_entity_decode($_COOKIE['propertyhive_currency']), TRUE); |
| 51 |
if ( !empty($currency) && isset($currency['currency_code']) && array_key_exists(ph_clean($currency['currency_code']), $form_controls['currency']['options']) ) |
| 52 |
{ |
| 53 |
$form_controls['currency']['value'] = $currency['currency_code']; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $form_controls; |
| 59 |
} |
| 60 |
|
| 61 |
public function ph_check_currency_change() |
| 62 |
{ |
| 63 |
if ( is_post_type_archive('property') && isset($_GET['currency']) ) |
| 64 |
{ |
| 65 |
if ( $_GET['currency'] == '' ) |
| 66 |
{ |
| 67 |
// Set to blank to reset back to properties entered currency |
| 68 |
unset( $_COOKIE['propertyhive_currency'] ); |
| 69 |
setcookie( 'propertyhive_currency', '', time() - ( 15 * 60 ) ); |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
$currency = $this->get_currency( sanitize_text_field($_GET['currency']) ); |
| 74 |
if ( $currency === FALSE ) |
| 75 |
{ |
| 76 |
$default_country = get_option( 'propertyhive_default_country', 'GB' ); |
| 77 |
$default_country = $this->get_country( $default_country ); |
| 78 |
|
| 79 |
$currency = $this->get_currency( (isset($default_country['currency_code'])) ? $default_country['currency_code'] : 'GBP' ); |
| 80 |
} |
| 81 |
|
| 82 |
$currency['exchange_rate'] = 1; |
| 83 |
$exchange_rates = get_option( 'propertyhive_currency_exchange_rates', array() ); |
| 84 |
if ( isset($exchange_rates[$_GET['currency']]) ) |
| 85 |
{ |
| 86 |
$currency['exchange_rate'] = $exchange_rates[sanitize_text_field($_GET['currency'])]; |
| 87 |
} |
| 88 |
|
| 89 |
ph_setcookie( 'propertyhive_currency', htmlentities(json_encode($currency)), time() + (30 * DAY_IN_SECONDS), is_ssl() ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function get_country( $country_code ) { |
| 94 |
|
| 95 |
$countries = $this->get_countries(); |
| 96 |
|
| 97 |
if ( isset($countries[$country_code]) ) |
| 98 |
{ |
| 99 |
return $countries[$country_code]; |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
public function get_currency( $currency_code ) { |
| 106 |
|
| 107 |
$countries = $this->get_countries(); |
| 108 |
|
| 109 |
foreach ( $countries as $country ) |
| 110 |
{ |
| 111 |
if ( $country['currency_code'] == $currency_code ) |
| 112 |
{ |
| 113 |
$currency_symbol = apply_filters( 'propertyhive_currency_symbol', $country['currency_symbol'], $currency_code); |
| 114 |
$currency_prefix = apply_filters( 'propertyhive_currency_prefix', $country['currency_prefix'], $currency_code); |
| 115 |
|
| 116 |
return array( |
| 117 |
'currency_code' => $currency_code, |
| 118 |
'currency_symbol' => $currency_symbol, |
| 119 |
'currency_prefix' => $currency_prefix |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get all countries. |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
private function get_countries() { |
| 132 |
$countries = array( |
| 133 |
'AR' => array( |
| 134 |
'name' => 'Argentina', |
| 135 |
'currency_code' => 'ARS', |
| 136 |
'currency_symbol' => '$', |
| 137 |
'currency_prefix' => true |
| 138 |
), |
| 139 |
'AU' => array( |
| 140 |
'name' => 'Australia', |
| 141 |
'currency_code' => 'AUD', |
| 142 |
'currency_symbol' => '$', |
| 143 |
'currency_prefix' => true |
| 144 |
), |
| 145 |
'AT' => array( |
| 146 |
'name' => 'Austria', |
| 147 |
'currency_code' => 'EUR', |
| 148 |
'currency_symbol' => '€', |
| 149 |
'currency_prefix' => false |
| 150 |
), |
| 151 |
'BB' => array( |
| 152 |
'name' => 'Barbados', |
| 153 |
'currency_code' => 'BBD', |
| 154 |
'currency_symbol' => '$', |
| 155 |
'currency_prefix' => true |
| 156 |
), |
| 157 |
'BE' => array( |
| 158 |
'name' => 'Belgium', |
| 159 |
'currency_code' => 'EUR', |
| 160 |
'currency_symbol' => '€', |
| 161 |
'currency_prefix' => false |
| 162 |
), |
| 163 |
'BR' => array( |
| 164 |
'name' => 'Brazil', |
| 165 |
'currency_code' => 'BRL', |
| 166 |
'currency_symbol' => 'R$', |
| 167 |
'currency_prefix' => true |
| 168 |
), |
| 169 |
'BG' => array( |
| 170 |
'name' => 'Bulgaria', |
| 171 |
'currency_code' => 'BGN', |
| 172 |
'currency_symbol' => 'лв', |
| 173 |
'currency_prefix' => false |
| 174 |
), |
| 175 |
'CA' => array( |
| 176 |
'name' => 'Canada', |
| 177 |
'currency_code' => 'CAD', |
| 178 |
'currency_symbol' => '$', |
| 179 |
'currency_prefix' => true |
| 180 |
), |
| 181 |
'CN' => array( |
| 182 |
'name' => 'China', |
| 183 |
'currency_code' => 'CNY', |
| 184 |
'currency_symbol' => '¥', |
| 185 |
'currency_prefix' => true |
| 186 |
), |
| 187 |
'CO' => array( |
| 188 |
'name' => 'Colombia', |
| 189 |
'currency_code' => 'COP', |
| 190 |
'currency_symbol' => '$', |
| 191 |
'currency_prefix' => true |
| 192 |
), |
| 193 |
'HR' => array( |
| 194 |
'name' => 'Croatia', |
| 195 |
'currency_code' => 'EUR', |
| 196 |
'currency_symbol' => '€', |
| 197 |
'currency_prefix' => false |
| 198 |
), |
| 199 |
'CY' => array( |
| 200 |
'name' => 'Cyprus', |
| 201 |
'currency_code' => 'EUR', |
| 202 |
'currency_symbol' => '€', |
| 203 |
'currency_prefix' => false |
| 204 |
), |
| 205 |
'CZ' => array( |
| 206 |
'name' => 'Czech Republic', |
| 207 |
'currency_code' => 'CZK', |
| 208 |
'currency_symbol' => 'Kč', |
| 209 |
'currency_prefix' => false |
| 210 |
), |
| 211 |
'DK' => array( |
| 212 |
'name' => 'Denmark', |
| 213 |
'currency_code' => 'DKK', |
| 214 |
'currency_symbol' => 'kr', |
| 215 |
'currency_prefix' => false |
| 216 |
), |
| 217 |
'EG' => array( |
| 218 |
'name' => 'Egypt', |
| 219 |
'currency_code' => 'EGP', |
| 220 |
'currency_symbol' => 'E£', |
| 221 |
'currency_prefix' => true |
| 222 |
), |
| 223 |
'FI' => array( |
| 224 |
'name' => 'Finland', |
| 225 |
'currency_code' => 'EUR', |
| 226 |
'currency_symbol' => '€', |
| 227 |
'currency_prefix' => false |
| 228 |
), |
| 229 |
'FR' => array( |
| 230 |
'name' => 'France', |
| 231 |
'currency_code' => 'EUR', |
| 232 |
'currency_symbol' => '€', |
| 233 |
'currency_prefix' => false |
| 234 |
), |
| 235 |
'DE' => array( |
| 236 |
'name' => 'Germany', |
| 237 |
'currency_code' => 'EUR', |
| 238 |
'currency_symbol' => '€', |
| 239 |
'currency_prefix' => false |
| 240 |
), |
| 241 |
'GI' => array( |
| 242 |
'name' => 'Gibraltar', |
| 243 |
'currency_code' => 'GBP', |
| 244 |
'currency_symbol' => '£', |
| 245 |
'currency_prefix' => true |
| 246 |
), |
| 247 |
'GR' => array( |
| 248 |
'name' => 'Greece', |
| 249 |
'currency_code' => 'EUR', |
| 250 |
'currency_symbol' => '€', |
| 251 |
'currency_prefix' => false |
| 252 |
), |
| 253 |
'HK' => array( |
| 254 |
'name' => 'Hong Kong', |
| 255 |
'currency_code' => 'HKD', |
| 256 |
'currency_symbol' => '$', |
| 257 |
'currency_prefix' => true |
| 258 |
), |
| 259 |
'IN' => array( |
| 260 |
'name' => 'India', |
| 261 |
'currency_code' => 'INR', |
| 262 |
'currency_symbol' => '₹', |
| 263 |
'currency_prefix' => true |
| 264 |
), |
| 265 |
'ID' => array( |
| 266 |
'name' => 'Indonesia', |
| 267 |
'currency_code' => 'IDR', |
| 268 |
'currency_symbol' => 'Rp', |
| 269 |
'currency_prefix' => true |
| 270 |
), |
| 271 |
'IE' => array( |
| 272 |
'name' => 'Ireland', |
| 273 |
'currency_code' => 'EUR', |
| 274 |
'currency_symbol' => '€', |
| 275 |
'currency_prefix' => false |
| 276 |
), |
| 277 |
'IT' => array( |
| 278 |
'name' => 'Italy', |
| 279 |
'currency_code' => 'EUR', |
| 280 |
'currency_symbol' => '€', |
| 281 |
'currency_prefix' => false |
| 282 |
), |
| 283 |
'JM' => array( |
| 284 |
'name' => 'Jamaica', |
| 285 |
'currency_code' => 'JMD', |
| 286 |
'currency_symbol' => '$', |
| 287 |
'currency_prefix' => true |
| 288 |
), |
| 289 |
'JP' => array( |
| 290 |
'name' => 'Japan', |
| 291 |
'currency_code' => 'JPY', |
| 292 |
'currency_symbol' => '¥', |
| 293 |
'currency_prefix' => true |
| 294 |
), |
| 295 |
'KE' => array( |
| 296 |
'name' => 'Kenya', |
| 297 |
'currency_code' => 'KES', |
| 298 |
'currency_symbol' => 'KSh ', |
| 299 |
'currency_prefix' => true |
| 300 |
), |
| 301 |
'LU' => array( |
| 302 |
'name' => 'Luxembourg', |
| 303 |
'currency_code' => 'EUR', |
| 304 |
'currency_symbol' => '€', |
| 305 |
'currency_prefix' => false |
| 306 |
), |
| 307 |
'MY' => array( |
| 308 |
'name' => 'Malaysia', |
| 309 |
'currency_code' => 'MYR', |
| 310 |
'currency_symbol' => 'RM', |
| 311 |
'currency_prefix' => true |
| 312 |
), |
| 313 |
'MT' => array( |
| 314 |
'name' => 'Malta', |
| 315 |
'currency_code' => 'EUR', |
| 316 |
'currency_symbol' => '€', |
| 317 |
'currency_prefix' => false |
| 318 |
), |
| 319 |
'MU' => array( |
| 320 |
'name' => 'Mauritius', |
| 321 |
'currency_code' => 'MUR', |
| 322 |
'currency_symbol' => 'Rs', |
| 323 |
'currency_prefix' => false |
| 324 |
), |
| 325 |
'MX' => array( |
| 326 |
'name' => 'Mexico', |
| 327 |
'currency_code' => 'MXN', |
| 328 |
'currency_symbol' => '$', |
| 329 |
'currency_prefix' => true |
| 330 |
), |
| 331 |
'MA' => array( |
| 332 |
'name' => 'Morocco', |
| 333 |
'currency_code' => 'MAD', |
| 334 |
'currency_symbol' => 'د.� |
| 335 |
.', |
| 336 |
'currency_prefix' => false |
| 337 |
), |
| 338 |
'NL' => array( |
| 339 |
'name' => 'Netherlands', |
| 340 |
'currency_code' => 'EUR', |
| 341 |
'currency_symbol' => '€', |
| 342 |
'currency_prefix' => false |
| 343 |
), |
| 344 |
'NZ' => array( |
| 345 |
'name' => 'New Zealand', |
| 346 |
'currency_code' => 'NZD', |
| 347 |
'currency_symbol' => '$', |
| 348 |
'currency_prefix' => true |
| 349 |
), |
| 350 |
'NO' => array( |
| 351 |
'name' => 'Norway', |
| 352 |
'currency_code' => 'NOK', |
| 353 |
'currency_symbol' => 'kr', |
| 354 |
'currency_prefix' => false |
| 355 |
), |
| 356 |
'PK' => array( |
| 357 |
'name' => 'Pakistan', |
| 358 |
'currency_code' => 'PKR', |
| 359 |
'currency_symbol' => 'Rs', |
| 360 |
'currency_prefix' => false |
| 361 |
), |
| 362 |
'PL' => array( |
| 363 |
'name' => 'Poland', |
| 364 |
'currency_code' => 'PLN', |
| 365 |
'currency_symbol' => 'zł', |
| 366 |
'currency_prefix' => false |
| 367 |
), |
| 368 |
'PT' => array( |
| 369 |
'name' => 'Portugal', |
| 370 |
'currency_code' => 'EUR', |
| 371 |
'currency_symbol' => '€', |
| 372 |
'currency_prefix' => false |
| 373 |
), |
| 374 |
'QA' => array( |
| 375 |
'name' => 'Qatar', |
| 376 |
'currency_code' => 'QAR', |
| 377 |
'currency_symbol' => 'QR', |
| 378 |
'currency_prefix' => false |
| 379 |
), |
| 380 |
'RU' => array( |
| 381 |
'name' => 'Russia', |
| 382 |
'currency_code' => 'RUB', |
| 383 |
'currency_symbol' => '₽', |
| 384 |
'currency_prefix' => true |
| 385 |
), |
| 386 |
'VC' => array( |
| 387 |
'name' => 'Saint Vincent and the Grenadines', |
| 388 |
'currency_code' => 'XCD', |
| 389 |
'currency_symbol' => '$', |
| 390 |
'currency_prefix' => true |
| 391 |
), |
| 392 |
'SA' => array( |
| 393 |
'name' => 'Saudi Arabia', |
| 394 |
'currency_code' => 'SAR', |
| 395 |
'currency_symbol' => '﷼', |
| 396 |
'currency_prefix' => false |
| 397 |
), |
| 398 |
'SG' => array( |
| 399 |
'name' => 'Singapore', |
| 400 |
'currency_code' => 'SGD', |
| 401 |
'currency_symbol' => '$', |
| 402 |
'currency_prefix' => true |
| 403 |
), |
| 404 |
'ZA' => array( |
| 405 |
'name' => 'South Africa', |
| 406 |
'currency_code' => 'ZAR', |
| 407 |
'currency_symbol' => 'R', |
| 408 |
'currency_prefix' => true |
| 409 |
), |
| 410 |
'KR' => array( |
| 411 |
'name' => 'South Korea', |
| 412 |
'currency_code' => 'KRW', |
| 413 |
'currency_symbol' => '₩', |
| 414 |
'currency_prefix' => true |
| 415 |
), |
| 416 |
'ES' => array( |
| 417 |
'name' => 'Spain', |
| 418 |
'currency_code' => 'EUR', |
| 419 |
'currency_symbol' => '€', |
| 420 |
'currency_prefix' => false |
| 421 |
), |
| 422 |
'SE' => array( |
| 423 |
'name' => 'Sweden', |
| 424 |
'currency_code' => 'SEK', |
| 425 |
'currency_symbol' => 'kr', |
| 426 |
'currency_prefix' => false |
| 427 |
), |
| 428 |
'CH' => array( |
| 429 |
'name' => 'Switzerland', |
| 430 |
'currency_code' => 'CHF', |
| 431 |
'currency_symbol' => 'CHF', |
| 432 |
'currency_prefix' => true |
| 433 |
), |
| 434 |
'TH' => array( |
| 435 |
'name' => 'Thailand', |
| 436 |
'currency_code' => 'THB', |
| 437 |
'currency_symbol' => '฿', |
| 438 |
'currency_prefix' => true |
| 439 |
), |
| 440 |
'TR' => array( |
| 441 |
'name' => 'Turkey', |
| 442 |
'currency_code' => 'TRY', |
| 443 |
'currency_symbol' => '₺', |
| 444 |
'currency_prefix' => true |
| 445 |
), |
| 446 |
'AE' => array( |
| 447 |
'name' => 'United Arab Emirates', |
| 448 |
'currency_code' => 'AED', |
| 449 |
'currency_symbol' => 'د.إ', |
| 450 |
'currency_prefix' => false |
| 451 |
), |
| 452 |
'GB' => array( |
| 453 |
'name' => 'United Kingdom', |
| 454 |
'currency_code' => 'GBP', |
| 455 |
'currency_symbol' => '£', |
| 456 |
'currency_prefix' => true |
| 457 |
), |
| 458 |
'US' => array( |
| 459 |
'name' => 'United States', |
| 460 |
'currency_code' => 'USD', |
| 461 |
'currency_symbol' => '$', |
| 462 |
'currency_prefix' => true |
| 463 |
), |
| 464 |
'VN' => array( |
| 465 |
'name' => 'Vietnam', |
| 466 |
'currency_code' => 'VND', |
| 467 |
'currency_symbol' => '₫', |
| 468 |
'currency_prefix' => true |
| 469 |
), |
| 470 |
); |
| 471 |
|
| 472 |
return apply_filters( 'propertyhive_countries', $countries ); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Outputs the list of countries for use in dropdown boxes. |
| 477 |
* @param string $selected_country (default: '') |
| 478 |
* @param bool $escape (default: false) |
| 479 |
*/ |
| 480 |
public function country_dropdown_options( $selected_country = '', $escape = false ) { |
| 481 |
if ( $this->countries ) |
| 482 |
{ |
| 483 |
foreach ( $this->countries as $key => $value ) |
| 484 |
{ |
| 485 |
echo '<option'; |
| 486 |
if ( $selected_country == $key || ( $selected_country == '' && $key == 'GB' ) ) { |
| 487 |
echo ' selected="selected"'; |
| 488 |
} |
| 489 |
echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value['name'] ) : $value['name'] ) . '</option>'; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
public function convert_price_to_gbp( $price, $currency_code ) |
| 495 |
{ |
| 496 |
if ( trim($price) == '' ) |
| 497 |
{ |
| 498 |
return $price; |
| 499 |
} |
| 500 |
|
| 501 |
$exchange_rates = get_option( 'propertyhive_currency_exchange_rates', array() ); |
| 502 |
|
| 503 |
if ( isset($exchange_rates[$currency_code]) ) |
| 504 |
{ |
| 505 |
$price = $price / $exchange_rates[$currency_code]; |
| 506 |
} |
| 507 |
|
| 508 |
return $price; |
| 509 |
} |
| 510 |
|
| 511 |
public function update_property_price_actual( $postID ) |
| 512 |
{ |
| 513 |
$countries = $this->countries; |
| 514 |
|
| 515 |
$department = get_post_meta( $postID, '_department', true ); |
| 516 |
if ( ph_get_custom_department_based_on( $department ) !== false ) |
| 517 |
{ |
| 518 |
$department = ph_get_custom_department_based_on( $department ); |
| 519 |
} |
| 520 |
|
| 521 |
$country = get_post_meta( $postID, '_address_country', true ); |
| 522 |
if ( $country == '' ) |
| 523 |
{ |
| 524 |
$country = get_option( 'propertyhive_default_country', 'GB' ); |
| 525 |
} |
| 526 |
|
| 527 |
if (isset($countries[$country])) |
| 528 |
{ |
| 529 |
if ( $department == 'residential-sales' ) |
| 530 |
{ |
| 531 |
$currency = get_post_meta( $postID, '_currency', true ); |
| 532 |
if ( $currency == '' ) |
| 533 |
{ |
| 534 |
$currency = $this->get_country($country); |
| 535 |
$currency = $currency['currency_code']; |
| 536 |
} |
| 537 |
|
| 538 |
$price = get_post_meta( $postID, '_price', true ); |
| 539 |
|
| 540 |
$converted_price = $this->convert_price_to_gbp( $price, $currency ); |
| 541 |
|
| 542 |
update_post_meta( $postID, '_price_actual', $converted_price ); |
| 543 |
} |
| 544 |
elseif ( $department == 'residential-lettings' ) |
| 545 |
{ |
| 546 |
$currency = get_post_meta( $postID, '_currency', true ); |
| 547 |
if ( $currency == '' ) |
| 548 |
{ |
| 549 |
$currency = $this->get_country($country); |
| 550 |
$currency = $currency['currency_code']; |
| 551 |
} |
| 552 |
|
| 553 |
$rent = get_post_meta( $postID, '_rent', true ); |
| 554 |
|
| 555 |
$converted_price = 0; |
| 556 |
if ( !empty($rent) && is_numeric($rent) ) |
| 557 |
{ |
| 558 |
$price = $rent; // Stored in pcm |
| 559 |
$rent_frequency = get_post_meta( $postID, '_rent_frequency', true ); |
| 560 |
switch ($rent_frequency) |
| 561 |
{ |
| 562 |
case "pd": { $price = ($rent * 365) / 12; break; } |
| 563 |
case "pppw": |
| 564 |
{ |
| 565 |
$bedrooms = get_post_meta( $postID, '_bedrooms', true ); |
| 566 |
if ( ( $bedrooms !== FALSE && $bedrooms != 0 && $bedrooms != '' ) && apply_filters( 'propertyhive_pppw_to_consider_bedrooms', true ) == true ) |
| 567 |
{ |
| 568 |
$price = (($rent * 52) / 12) * $bedrooms; |
| 569 |
} |
| 570 |
else |
| 571 |
{ |
| 572 |
$price = ($rent * 52) / 12; |
| 573 |
} |
| 574 |
break; |
| 575 |
} |
| 576 |
case "pw": { $price = ($rent * 52) / 12; break; } |
| 577 |
case "pcm": { $price = $rent; break; } |
| 578 |
case "pq": { $price = ($rent * 4) / 12; break; } |
| 579 |
case "pa": { $price = ($rent / 12); break; } |
| 580 |
} |
| 581 |
|
| 582 |
$converted_price = $this->convert_price_to_gbp( $price, $currency ); |
| 583 |
} |
| 584 |
|
| 585 |
update_post_meta( $postID, '_price_actual', $converted_price ); |
| 586 |
} |
| 587 |
if ( $department == 'commercial' ) |
| 588 |
{ |
| 589 |
if ( get_post_meta( $postID, '_for_sale', true ) == 'yes' ) |
| 590 |
{ |
| 591 |
$currency = get_post_meta( $postID, '_commercial_price_currency', true ); |
| 592 |
if ( $currency == '' ) |
| 593 |
{ |
| 594 |
$currency = $this->get_country($country); |
| 595 |
$currency = $currency['currency_code']; |
| 596 |
} |
| 597 |
|
| 598 |
$price = get_post_meta( $postID, '_price_from', true ); |
| 599 |
if ( $price == '' ) |
| 600 |
{ |
| 601 |
$price = get_post_meta( $postID, '_price_to', true ); |
| 602 |
} |
| 603 |
|
| 604 |
$converted_price = $this->convert_price_to_gbp( $price, $currency ); |
| 605 |
|
| 606 |
update_post_meta( $postID, '_price_from_actual', $converted_price ); |
| 607 |
|
| 608 |
$price = get_post_meta( $postID, '_price_to', true ); |
| 609 |
if ( $price == '' ) |
| 610 |
{ |
| 611 |
$price = get_post_meta( $postID, '_price_from', true ); |
| 612 |
} |
| 613 |
|
| 614 |
$converted_price = $this->convert_price_to_gbp( $price, $currency ); |
| 615 |
|
| 616 |
update_post_meta( $postID, '_price_to_actual', $converted_price ); |
| 617 |
} |
| 618 |
if ( get_post_meta( $postID, '_to_rent', true ) == 'yes' ) |
| 619 |
{ |
| 620 |
$currency = get_post_meta( $postID, '_commercial_rent_currency', true ); |
| 621 |
if ( $currency == '' ) |
| 622 |
{ |
| 623 |
$currency = $this->get_country($country); |
| 624 |
$currency = $currency['currency_code']; |
| 625 |
} |
| 626 |
|
| 627 |
$rent_units = get_post_meta( $postID, '_rent_units', true ); |
| 628 |
|
| 629 |
$price = get_post_meta( $postID, '_rent_from', true ); |
| 630 |
if ( $price == '' ) |
| 631 |
{ |
| 632 |
$price = get_post_meta( $postID, '_rent_to', true ); |
| 633 |
} |
| 634 |
if ( is_numeric($price) ) |
| 635 |
{ |
| 636 |
switch ($rent_units) |
| 637 |
{ |
| 638 |
case "pd": { $price = ($price * 365) / 12; break; } |
| 639 |
case "pw": { $price = ($price * 52) / 12; break; } |
| 640 |
case "pcm": { $price = $price; break; } |
| 641 |
case "pq": { $price = ($price * 4) / 12; break; } |
| 642 |
case "pa": { $price = ($price / 12); break; } |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
$converted_price = $this->convert_price_to_gbp( $price, $currency ); |
| 647 |
|
| 648 |
update_post_meta( $postID, '_rent_from_actual', $converted_price ); |
| 649 |
|
| 650 |
if ( get_post_meta( $postID, '_for_sale', true ) != 'yes' ) |
| 651 |
{ |
| 652 |
update_post_meta( $postID, '_price_from_actual', $converted_price ); |
| 653 |
} |
| 654 |
|
| 655 |
$price = get_post_meta( $postID, '_rent_to', true ); |
| 656 |
if ( $price == '' ) |
| 657 |
{ |
| 658 |
$price = get_post_meta( $postID, '_rent_from', true ); |
| 659 |
} |
| 660 |
if ( is_numeric($price) ) |
| 661 |
{ |
| 662 |
switch ($rent_units) |
| 663 |
{ |
| 664 |
case "pd": { $price = ($price * 365) / 12; break; } |
| 665 |
case "pw": { $price = ($price * 52) / 12; break; } |
| 666 |
case "pcm": { $price = $price; break; } |
| 667 |
case "pq": { $price = ($price * 4) / 12; break; } |
| 668 |
case "pa": { $price = ($price / 12); break; } |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
$converted_price = $this->convert_price_to_gbp( $price, $currency ); |
| 673 |
|
| 674 |
update_post_meta( $postID, '_rent_to_actual', $converted_price ); |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
do_action('propertyhive_property_price_actual_updated', $postID); |
| 680 |
} |
| 681 |
|
| 682 |
public function ph_update_currency_exchange_rates() |
| 683 |
{ |
| 684 |
global $wpdb; |
| 685 |
|
| 686 |
if ( $this->countries ) |
| 687 |
{ |
| 688 |
$countries = $this->countries; |
| 689 |
|
| 690 |
// Filter 'propertyhive_new_currency_exchange_rates' allows someone to use their own currency API |
| 691 |
// Return should be in format: |
| 692 |
// array( |
| 693 |
// 'EUR' => x, |
| 694 |
// 'USD' => x, |
| 695 |
// ... etc |
| 696 |
// ) |
| 697 |
$exchange_rates = apply_filters( 'propertyhive_new_currency_exchange_rates', array() ); |
| 698 |
$previous_exchange_rates = get_option( 'propertyhive_currency_exchange_rates' ); |
| 699 |
|
| 700 |
if ( empty($exchange_rates) ) |
| 701 |
{ |
| 702 |
// Get all currency exchange rates from GBP |
| 703 |
// We're using the API from https://github.com/fawazahmed0/exchange-api |
| 704 |
$url = 'https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/gbp.json'; |
| 705 |
$response = wp_remote_get( $url ); |
| 706 |
|
| 707 |
if ( is_array( $response ) ) |
| 708 |
{ |
| 709 |
$body = wp_remote_retrieve_body( $response ); |
| 710 |
$json = json_decode($body, true); |
| 711 |
|
| 712 |
// If response is valid JSON and contains the core gbp key |
| 713 |
if ( $json !== null && isset( $json['gbp'] ) ) |
| 714 |
{ |
| 715 |
$exchange_rates_array = $json['gbp']; |
| 716 |
|
| 717 |
foreach ( $countries as $country ) |
| 718 |
{ |
| 719 |
$currency_code = $country['currency_code']; |
| 720 |
|
| 721 |
// If we haven't already got this currency and it's not GBP |
| 722 |
if (!isset($exchange_rates[$currency_code]) && $currency_code != 'GBP') |
| 723 |
{ |
| 724 |
// If this currency is in the list we received from the API |
| 725 |
if ( isset( $exchange_rates_array[strtolower( $currency_code )] ) ) |
| 726 |
{ |
| 727 |
$exchange_rates[$currency_code] = (string)$exchange_rates_array[strtolower( $currency_code )]; |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
// Only update the settings if the API call was successful and we got exchange rates, or if there were none set previously |
| 736 |
if ( !empty( $exchange_rates ) || empty( $previous_exchange_rates ) ) |
| 737 |
{ |
| 738 |
$exchange_rates['GBP'] = "1.0000"; |
| 739 |
update_option( 'propertyhive_currency_exchange_rates', $exchange_rates ); |
| 740 |
update_option( 'propertyhive_currency_exchange_rates_updated', date("Y-m-d") ); |
| 741 |
} |
| 742 |
|
| 743 |
do_action('propertyhive_exchange_rates_updated', $exchange_rates); |
| 744 |
|
| 745 |
// Loop through all on market properties and update _price_actual meta value to be price in GBP |
| 746 |
$args = array( |
| 747 |
'post_type' => 'property', |
| 748 |
'fields' => 'ids', |
| 749 |
'post_status' => 'publish', |
| 750 |
'meta_query' => array( |
| 751 |
array( |
| 752 |
'key' => '_on_market', |
| 753 |
'value' => 'yes', |
| 754 |
), |
| 755 |
array( |
| 756 |
'key' => '_address_country', |
| 757 |
'value' => 'GB', |
| 758 |
'compare' => '!=', |
| 759 |
) |
| 760 |
), |
| 761 |
'nopaging' => true, |
| 762 |
'orderby' => 'rand', // order by rand incase there are lots of properties and it times out, at least they should eventually all get processed |
| 763 |
); |
| 764 |
$property_query = new WP_Query($args); |
| 765 |
|
| 766 |
if ($property_query->have_posts()) |
| 767 |
{ |
| 768 |
while ($property_query->have_posts()) |
| 769 |
{ |
| 770 |
$property_query->the_post(); |
| 771 |
|
| 772 |
$this->update_property_price_actual( get_the_ID() ); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
wp_reset_postdata(); |
| 777 |
|
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
} |
| 782 |
|