PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
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-countries.php

class-ph-countries.php in Property Hive 2.3.0, at includes/class-ph-countries.php

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