PluginProbe
Property Hive / 1.4.53
Property Hive v1.4.53
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 1.4.53, at includes/class-ph-countries.php

546 lines 14.6 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 * 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 }
27
28 /**
29 * Auto-load in-accessible properties on demand.
30 * @param mixed $key
31 * @return mixed
32 */
33 public function __get( $key ) {
34 if ( 'countries' == $key ) {
35 return $this->get_countries();
36 }
37 }
38
39 public function ph_check_currency_change()
40 {
41 if ( is_post_type_archive('property') && isset($_GET['currency']) )
42 {
43 if ( $_GET['currency'] == '' )
44 {
45 // Set to blank to reset back to properties entered currency
46 unset( $_COOKIE['propertyhive_currency'] );
47 setcookie( 'propertyhive_currency', '', time() - ( 15 * 60 ) );
48 return true;
49 }
50
51 // TO DO: Make sure currency passed in is in list of countries they operate in
52 // so we can get the exchange rate
53
54 $currency = $this->get_currency( sanitize_text_field($_GET['currency']) );
55 if ( $currency === FALSE )
56 {
57 $default_country = get_option( 'propertyhive_default_country', 'GB' );
58 $default_country = $this->get_country( $default_country );
59
60 $currency = $this->get_currency( (isset($default_country['currency_code'])) ? $default_country['currency_code'] : 'GBP' );
61 }
62
63 $currency['exchange_rate'] = 1;
64 $exchange_rates = get_option( 'propertyhive_currency_exchange_rates', array() );
65 if ( isset($exchange_rates[$_GET['currency']]) )
66 {
67 $currency['exchange_rate'] = $exchange_rates[sanitize_text_field($_GET['currency'])];
68 }
69
70 ph_setcookie( 'propertyhive_currency', htmlentities(serialize($currency)), time() + (30 * DAY_IN_SECONDS), is_ssl() );
71 }
72 }
73
74
75
76 public function get_country( $country_code ) {
77
78 $countries = $this->get_countries();
79
80 if ( isset($countries[$country_code]) )
81 {
82 return $countries[$country_code];
83 }
84
85 return false;
86 }
87
88 public function get_currency( $currency_code ) {
89
90 $countries = $this->get_countries();
91
92 foreach ( $countries as $country )
93 {
94 if ( $country['currency_code'] == $currency_code )
95 {
96 return array(
97 'currency_symbol' => $country['currency_symbol'],
98 'currency_prefix' => $country['currency_prefix']
99 );
100 }
101 }
102
103 return false;
104 }
105
106 /**
107 * Get all countries.
108 * @return array
109 */
110 private function get_countries() {
111 $countries = array(
112 'AU' => array(
113 'name' => 'Australia',
114 'currency_code' => 'AUD',
115 'currency_symbol' => '$',
116 'currency_prefix' => true
117 ),
118 'AT' => array(
119 'name' => 'Austria',
120 'currency_code' => 'EUR',
121 'currency_symbol' => '&euro;',
122 'currency_prefix' => true
123 ),
124 'BE' => array(
125 'name' => 'Belgium',
126 'currency_code' => 'EUR',
127 'currency_symbol' => '&euro;',
128 'currency_prefix' => true
129 ),
130 'BG' => array(
131 'name' => 'Bulgaria',
132 'currency_code' => 'BGN',
133 'currency_symbol' => 'лв',
134 'currency_prefix' => true
135 ),
136 'CA' => array(
137 'name' => 'Canada',
138 'currency_code' => 'CAD',
139 'currency_symbol' => '$',
140 'currency_prefix' => true
141 ),
142 'HR' => array(
143 'name' => 'Croatia',
144 'currency_code' => 'HRK',
145 'currency_symbol' => 'kn',
146 'currency_prefix' => false
147 ),
148 'CY' => array(
149 'name' => 'Cyprus',
150 'currency_code' => 'EUR',
151 'currency_symbol' => '&euro;',
152 'currency_prefix' => true
153 ),
154 'CZ' => array(
155 'name' => 'Czech Republic',
156 'currency_code' => 'CZK',
157 'currency_symbol' => '',
158 'currency_prefix' => false
159 ),
160 'DK' => array(
161 'name' => 'Denmark',
162 'currency_code' => 'DKK',
163 'currency_symbol' => 'kr',
164 'currency_prefix' => false
165 ),
166 'FI' => array(
167 'name' => 'Finland',
168 'currency_code' => 'EUR',
169 'currency_symbol' => '&euro;',
170 'currency_prefix' => true
171 ),
172 'FR' => array(
173 'name' => 'France',
174 'currency_code' => 'EUR',
175 'currency_symbol' => '&euro;',
176 'currency_prefix' => true
177 ),
178 'DE' => array(
179 'name' => 'Germany',
180 'currency_code' => 'EUR',
181 'currency_symbol' => '&euro;',
182 'currency_prefix' => true
183 ),
184 'IE' => array(
185 'name' => 'Ireland',
186 'currency_code' => 'EUR',
187 'currency_symbol' => '&euro;',
188 'currency_prefix' => true
189 ),
190 'IT' => array(
191 'name' => 'Italy',
192 'currency_code' => 'EUR',
193 'currency_symbol' => '&euro;',
194 'currency_prefix' => true
195 ),
196 'JP' => array(
197 'name' => 'Japan',
198 'currency_code' => 'JPY',
199 'currency_symbol' => '&yen;',
200 'currency_prefix' => true
201 ),
202 'MT' => array(
203 'name' => 'Malta',
204 'currency_code' => 'EUR',
205 'currency_symbol' => '&euro;',
206 'currency_prefix' => true
207 ),
208 'MU' => array(
209 'name' => 'Mauritius',
210 'currency_code' => 'MUR',
211 'currency_symbol' => 'Rs',
212 'currency_prefix' => false
213 ),
214 'NO' => array(
215 'name' => 'Norway',
216 'currency_code' => 'NOK',
217 'currency_symbol' => 'kr',
218 'currency_prefix' => false
219 ),
220 'PT' => array(
221 'name' => 'Portugal',
222 'currency_code' => 'EUR',
223 'currency_symbol' => '&euro;',
224 'currency_prefix' => true
225 ),
226 'RU' => array(
227 'name' => 'Russia',
228 'currency_code' => 'RUB',
229 'currency_symbol' => '',
230 'currency_prefix' => true
231 ),
232 'ZA' => array(
233 'name' => 'South Africa',
234 'currency_code' => 'ZAR',
235 'currency_symbol' => 'R',
236 'currency_prefix' => true
237 ),
238 'ES' => array(
239 'name' => 'Spain',
240 'currency_code' => 'EUR',
241 'currency_symbol' => '&euro;',
242 'currency_prefix' => true
243 ),
244 'SE' => array(
245 'name' => 'Sweden',
246 'currency_code' => 'SEK',
247 'currency_symbol' => 'kr',
248 'currency_prefix' => false
249 ),
250 'AE' => array(
251 'name' => 'United Arab Emirates',
252 'currency_code' => 'AED',
253 'currency_symbol' => '‎د.إ',
254 'currency_prefix' => false
255 ),
256 'GB' => array(
257 'name' => 'United Kingdom',
258 'currency_code' => 'GBP',
259 'currency_symbol' => '&pound;',
260 'currency_prefix' => true
261 ),
262 'US' => array(
263 'name' => 'United States',
264 'currency_code' => 'USD',
265 'currency_symbol' => '$',
266 'currency_prefix' => true
267 ),
268 );
269
270 return apply_filters( 'propertyhive_countries', $countries );
271 }
272
273 /**
274 * Outputs the list of countries for use in dropdown boxes.
275 * @param string $selected_country (default: '')
276 * @param bool $escape (default: false)
277 */
278 public function country_dropdown_options( $selected_country = '', $escape = false ) {
279 if ( $this->countries )
280 {
281 foreach ( $this->countries as $key => $value )
282 {
283 echo '<option';
284 if ( $selected_country == $key || ( $selected_country == '' && $key == 'GB' ) ) {
285 echo ' selected="selected"';
286 }
287 echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value['name'] ) : $value['name'] ) . '</option>';
288 }
289 }
290 }
291
292 public function convert_price_to_gbp( $price, $currency_code )
293 {
294 if ( trim($price) == '' )
295 {
296 return $price;
297 }
298
299 $exchange_rates = get_option( 'propertyhive_currency_exchange_rates', array() );
300
301 if ( isset($exchange_rates[$currency_code]) )
302 {
303 $price = $price / $exchange_rates[$currency_code];
304 }
305
306 return $price;
307 }
308
309 public function update_property_price_actual( $postID )
310 {
311 $countries = $this->countries;
312
313 $department = get_post_meta( $postID, '_department', true );
314 $country = get_post_meta( $postID, '_address_country', true );
315
316 if (isset($countries[$country]))
317 {
318 if ( $department == 'residential-sales' )
319 {
320 $currency = get_post_meta( $postID, '_currency', true );
321 if ( $country == '' )
322 {
323 $country = get_option( 'propertyhive_default_country', 'GB' );
324 }
325 if ( $currency == '' )
326 {
327 $currency = $this->get_country($country);
328 $currency = $currency['currency_code'];
329 }
330
331 $price = get_post_meta( $postID, '_price', true );
332
333 $converted_price = $this->convert_price_to_gbp( $price, $currency );
334
335 update_post_meta( $postID, '_price_actual', $converted_price );
336 }
337 elseif ( $department == 'residential-lettings' )
338 {
339 $currency = get_post_meta( $postID, '_currency', true );
340 if ( $country == '' )
341 {
342 $country = get_option( 'propertyhive_default_country', 'GB' );
343 }
344 if ( $currency == '' )
345 {
346 $currency = $this->get_country($country);
347 $currency = $currency['currency_code'];
348 }
349
350 $rent = get_post_meta( $postID, '_rent', true );
351 $rent_frequency = get_post_meta( $postID, '_rent_frequency', true );
352
353 $price = $rent; // Stored in pcm
354 switch ($rent_frequency)
355 {
356 case "pppw":
357 {
358 $bedrooms = get_post_meta( $postID, '_bedrooms', true );
359 if ( ( $bedrooms !== FALSE && $bedrooms != 0 && $bedrooms != '' ) && apply_filters( 'propertyhive_pppw_to_consider_bedrooms', true ) == true )
360 {
361 $price = (($rent * 52) / 12) * $bedrooms;
362 }
363 else
364 {
365 $price = ($rent * 52) / 12;
366 }
367 break;
368 }
369 case "pw": { $price = ($rent * 52) / 12; break; }
370 case "pcm": { $price = $rent; break; }
371 case "pq": { $price = ($rent * 4) / 12; break; }
372 case "pa": { $price = ($rent / 12); break; }
373 }
374
375 $converted_price = $this->convert_price_to_gbp( $price, $currency );
376
377 update_post_meta( $postID, '_price_actual', $converted_price );
378 }
379 if ( $department == 'commercial' )
380 {
381 if ( get_post_meta( $postID, '_for_sale', true ) == 'yes' )
382 {
383 $currency = get_post_meta( $postID, '_commercial_price_currency', true );
384 if ( $country == '' )
385 {
386 $country = get_option( 'propertyhive_default_country', 'GB' );
387 }
388 if ( $currency == '' )
389 {
390 $currency = $this->get_country($country);
391 $currency = $currency['currency_code'];
392 }
393
394 $price = get_post_meta( $postID, '_price_from', true );
395
396 $converted_price = $this->convert_price_to_gbp( $price, $currency );
397
398 update_post_meta( $postID, '_price_from_actual', $converted_price );
399
400 $price = get_post_meta( $postID, '_price_to', true );
401
402 $converted_price = $this->convert_price_to_gbp( $price, $currency );
403
404 update_post_meta( $postID, '_price_to_actual', $converted_price );
405 }
406 if ( get_post_meta( $postID, '_to_rent', true ) == 'yes' )
407 {
408 $currency = get_post_meta( $postID, '_commercial_rent_currency', true );
409 if ( $country == '' )
410 {
411 $country = get_option( 'propertyhive_default_country', 'GB' );
412 }
413 if ( $currency == '' )
414 {
415 $currency = $this->get_country($country);
416 $currency = $currency['currency_code'];
417 }
418
419 $rent_units = get_post_meta( $postID, '_rent_units', true );
420
421 $price = get_post_meta( $postID, '_rent_from', true );
422 switch ($rent_units)
423 {
424 case "pw": { $price = ($price * 52) / 12; break; }
425 case "pcm": { $price = $price; break; }
426 case "pq": { $price = ($price * 4) / 52; break; }
427 case "pa": { $price = ($price / 52); break; }
428 }
429
430 $converted_price = $this->convert_price_to_gbp( $price, $currency );
431
432 update_post_meta( $postID, '_rent_from_actual', $converted_price );
433
434 if ( get_post_meta( $postID, '_for_sale', true ) != 'yes' )
435 {
436 update_post_meta( $postID, '_price_from_actual', $converted_price );
437 }
438
439 $price = get_post_meta( $postID, '_rent_to', true );
440 switch ($rent_units)
441 {
442 case "pw": { $price = ($price * 52) / 12; break; }
443 case "pcm": { $price = $price; break; }
444 case "pq": { $price = ($price * 4) / 52; break; }
445 case "pa": { $price = ($price / 52); break; }
446 }
447
448 $converted_price = $this->convert_price_to_gbp( $price, $currency );
449
450 update_post_meta( $postID, '_rent_to_actual', $converted_price );
451 }
452 }
453 }
454 }
455
456 public function ph_update_currency_exchange_rates()
457 {
458 global $wpdb;
459
460 if ( $this->countries )
461 {
462 $countries = $this->countries;
463
464 $exchange_rates = array();
465 $previous_exchange_rates = get_option( 'propertyhive_currency_exchange_rates' );
466
467 $default_country = get_option( 'propertyhive_default_country', 'GB' );
468 $selected_countries = get_option( 'propertyhive_countries', array( $default_country ) );
469
470 foreach ( $countries as $key => $value )
471 {
472 if (!isset($exchange_rates[$value['currency_code']]) && in_array($key, $selected_countries) && $value['currency_code'] != 'GBP')
473 {
474 // we haven't got this exchange rate
475 $from = 'GBP';
476 $to = $value['currency_code'];
477
478 $exchangeRate = '';
479
480 $url = 'https://finance.google.com/finance/converter?a=1&from=' . $from . '&to=' . $to;
481
482 $response = wp_remote_get( $url );
483
484 if ( is_array( $response ) )
485 {
486 $body = wp_remote_retrieve_body( $response );
487
488 preg_match("/<span class=bld>(.*)<\/span>/", $body, $converted);
489
490 if ( isset($converted[1]) && $converted[1] != '' )
491 {
492 $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
493
494 if ( $converted != '' )
495 {
496 $exchangeRate = $converted;
497 $exchange_rates[$to] = $exchangeRate;
498 }
499 }
500 }
501 else
502 {
503
504 }
505
506 if ( $exchangeRate == '' && isset($previous_exchange_rates[$to]) )
507 {
508 // if for some reason we get here and don't have an exchange rate
509 $exchange_rates[$to] = $previous_exchange_rates[$to];
510 }
511 }
512 }
513 $exchange_rates['GBP'] = "1.0000";
514 update_option( 'propertyhive_currency_exchange_rates', $exchange_rates );
515 update_option( 'propertyhive_currency_exchange_rates_updated', date("Y-m-d") );
516
517 // Loop through all on market properties and update _actual_price meta value to be price in GBP
518 $args = array(
519 'post_type' => 'property',
520 'post_status' => 'publish',
521 'meta_query' => array(
522 array(
523 'key' => '_on_market',
524 'value' => 'yes',
525 )
526 ),
527 'nopaging' => true,
528 );
529 $property_query = new WP_Query($args);
530
531 if ($property_query->have_posts())
532 {
533 while ($property_query->have_posts())
534 {
535 $property_query->the_post();
536
537 $this->update_property_price_actual( get_the_ID() );
538 }
539 }
540
541 wp_reset_postdata();
542
543 }
544 }
545
546 }