PluginProbe
Property Hive / 1.4.59
Property Hive v1.4.59
2.3.1 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 All 261 releases
propertyhive / includes / class-ph-countries.php

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

553 lines 14.7 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 'TR' => array(
251 'name' => 'Turkey',
252 'currency_code' => 'TRY',
253 'currency_symbol' => '‎₺',
254 'currency_prefix' => true
255 ),
256 'AE' => array(
257 'name' => 'United Arab Emirates',
258 'currency_code' => 'AED',
259 'currency_symbol' => '‎د.إ',
260 'currency_prefix' => false
261 ),
262 'GB' => array(
263 'name' => 'United Kingdom',
264 'currency_code' => 'GBP',
265 'currency_symbol' => '&pound;',
266 'currency_prefix' => true
267 ),
268 'US' => array(
269 'name' => 'United States',
270 'currency_code' => 'USD',
271 'currency_symbol' => '$',
272 'currency_prefix' => true
273 ),
274 );
275
276 return apply_filters( 'propertyhive_countries', $countries );
277 }
278
279 /**
280 * Outputs the list of countries for use in dropdown boxes.
281 * @param string $selected_country (default: '')
282 * @param bool $escape (default: false)
283 */
284 public function country_dropdown_options( $selected_country = '', $escape = false ) {
285 if ( $this->countries )
286 {
287 foreach ( $this->countries as $key => $value )
288 {
289 echo '<option';
290 if ( $selected_country == $key || ( $selected_country == '' && $key == 'GB' ) ) {
291 echo ' selected="selected"';
292 }
293 echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value['name'] ) : $value['name'] ) . '</option>';
294 }
295 }
296 }
297
298 public function convert_price_to_gbp( $price, $currency_code )
299 {
300 if ( trim($price) == '' )
301 {
302 return $price;
303 }
304
305 $exchange_rates = get_option( 'propertyhive_currency_exchange_rates', array() );
306
307 if ( isset($exchange_rates[$currency_code]) )
308 {
309 $price = $price / $exchange_rates[$currency_code];
310 }
311
312 return $price;
313 }
314
315 public function update_property_price_actual( $postID )
316 {
317 $countries = $this->countries;
318
319 $department = get_post_meta( $postID, '_department', true );
320 $country = get_post_meta( $postID, '_address_country', true );
321
322 if (isset($countries[$country]))
323 {
324 if ( $department == 'residential-sales' )
325 {
326 $currency = get_post_meta( $postID, '_currency', true );
327 if ( $country == '' )
328 {
329 $country = get_option( 'propertyhive_default_country', 'GB' );
330 }
331 if ( $currency == '' )
332 {
333 $currency = $this->get_country($country);
334 $currency = $currency['currency_code'];
335 }
336
337 $price = get_post_meta( $postID, '_price', true );
338
339 $converted_price = $this->convert_price_to_gbp( $price, $currency );
340
341 update_post_meta( $postID, '_price_actual', $converted_price );
342 }
343 elseif ( $department == 'residential-lettings' )
344 {
345 $currency = get_post_meta( $postID, '_currency', true );
346 if ( $country == '' )
347 {
348 $country = get_option( 'propertyhive_default_country', 'GB' );
349 }
350 if ( $currency == '' )
351 {
352 $currency = $this->get_country($country);
353 $currency = $currency['currency_code'];
354 }
355
356 $rent = get_post_meta( $postID, '_rent', true );
357 $rent_frequency = get_post_meta( $postID, '_rent_frequency', true );
358
359 $price = $rent; // Stored in pcm
360 switch ($rent_frequency)
361 {
362 case "pppw":
363 {
364 $bedrooms = get_post_meta( $postID, '_bedrooms', true );
365 if ( ( $bedrooms !== FALSE && $bedrooms != 0 && $bedrooms != '' ) && apply_filters( 'propertyhive_pppw_to_consider_bedrooms', true ) == true )
366 {
367 $price = (($rent * 52) / 12) * $bedrooms;
368 }
369 else
370 {
371 $price = ($rent * 52) / 12;
372 }
373 break;
374 }
375 case "pw": { $price = ($rent * 52) / 12; break; }
376 case "pcm": { $price = $rent; break; }
377 case "pq": { $price = ($rent * 4) / 12; break; }
378 case "pa": { $price = ($rent / 12); break; }
379 }
380
381 $converted_price = $this->convert_price_to_gbp( $price, $currency );
382
383 update_post_meta( $postID, '_price_actual', $converted_price );
384 }
385 if ( $department == 'commercial' )
386 {
387 if ( get_post_meta( $postID, '_for_sale', true ) == 'yes' )
388 {
389 $currency = get_post_meta( $postID, '_commercial_price_currency', true );
390 if ( $country == '' )
391 {
392 $country = get_option( 'propertyhive_default_country', 'GB' );
393 }
394 if ( $currency == '' )
395 {
396 $currency = $this->get_country($country);
397 $currency = $currency['currency_code'];
398 }
399
400 $price = get_post_meta( $postID, '_price_from', true );
401
402 $converted_price = $this->convert_price_to_gbp( $price, $currency );
403
404 update_post_meta( $postID, '_price_from_actual', $converted_price );
405
406 $price = get_post_meta( $postID, '_price_to', true );
407
408 $converted_price = $this->convert_price_to_gbp( $price, $currency );
409
410 update_post_meta( $postID, '_price_to_actual', $converted_price );
411 }
412 if ( get_post_meta( $postID, '_to_rent', true ) == 'yes' )
413 {
414 $currency = get_post_meta( $postID, '_commercial_rent_currency', true );
415 if ( $country == '' )
416 {
417 $country = get_option( 'propertyhive_default_country', 'GB' );
418 }
419 if ( $currency == '' )
420 {
421 $currency = $this->get_country($country);
422 $currency = $currency['currency_code'];
423 }
424
425 $rent_units = get_post_meta( $postID, '_rent_units', true );
426
427 $price = get_post_meta( $postID, '_rent_from', true );
428 switch ($rent_units)
429 {
430 case "pw": { $price = ($price * 52) / 12; break; }
431 case "pcm": { $price = $price; break; }
432 case "pq": { $price = ($price * 4) / 52; break; }
433 case "pa": { $price = ($price / 52); break; }
434 }
435
436 $converted_price = $this->convert_price_to_gbp( $price, $currency );
437
438 update_post_meta( $postID, '_rent_from_actual', $converted_price );
439
440 if ( get_post_meta( $postID, '_for_sale', true ) != 'yes' )
441 {
442 update_post_meta( $postID, '_price_from_actual', $converted_price );
443 }
444
445 $price = get_post_meta( $postID, '_rent_to', true );
446 switch ($rent_units)
447 {
448 case "pw": { $price = ($price * 52) / 12; break; }
449 case "pcm": { $price = $price; break; }
450 case "pq": { $price = ($price * 4) / 52; break; }
451 case "pa": { $price = ($price / 52); break; }
452 }
453
454 $converted_price = $this->convert_price_to_gbp( $price, $currency );
455
456 update_post_meta( $postID, '_rent_to_actual', $converted_price );
457 }
458 }
459 }
460 }
461
462 public function ph_update_currency_exchange_rates()
463 {
464 global $wpdb;
465
466 if ( $this->countries )
467 {
468 $countries = $this->countries;
469
470 $exchange_rates = array();
471 $previous_exchange_rates = get_option( 'propertyhive_currency_exchange_rates' );
472
473 $default_country = get_option( 'propertyhive_default_country', 'GB' );
474 $selected_countries = get_option( 'propertyhive_countries', array( $default_country ) );
475
476 foreach ( $countries as $key => $value )
477 {
478 if (!isset($exchange_rates[$value['currency_code']]) && in_array($key, $selected_countries) && $value['currency_code'] != 'GBP')
479 {
480 // we haven't got this exchange rate
481 $from = 'GBP';
482 $to = $value['currency_code'];
483
484 $exchangeRate = '';
485
486 $url = 'https://finance.google.com/finance/converter?a=1&from=' . $from . '&to=' . $to;
487
488 $response = wp_remote_get( $url );
489
490 if ( is_array( $response ) )
491 {
492 $body = wp_remote_retrieve_body( $response );
493
494 preg_match("/<span class=bld>(.*)<\/span>/", $body, $converted);
495
496 if ( isset($converted[1]) && $converted[1] != '' )
497 {
498 $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
499
500 if ( $converted != '' )
501 {
502 $exchangeRate = $converted;
503 $exchange_rates[$to] = $exchangeRate;
504 }
505 }
506 }
507 else
508 {
509
510 }
511
512 if ( $exchangeRate == '' && isset($previous_exchange_rates[$to]) )
513 {
514 // if for some reason we get here and don't have an exchange rate
515 $exchange_rates[$to] = $previous_exchange_rates[$to];
516 }
517 }
518 }
519 $exchange_rates['GBP'] = "1.0000";
520 update_option( 'propertyhive_currency_exchange_rates', $exchange_rates );
521 update_option( 'propertyhive_currency_exchange_rates_updated', date("Y-m-d") );
522
523 // Loop through all on market properties and update _actual_price meta value to be price in GBP
524 $args = array(
525 'post_type' => 'property',
526 'fields' => 'ids',
527 'post_status' => 'publish',
528 'meta_query' => array(
529 array(
530 'key' => '_on_market',
531 'value' => 'yes',
532 )
533 ),
534 'nopaging' => true,
535 );
536 $property_query = new WP_Query($args);
537
538 if ($property_query->have_posts())
539 {
540 while ($property_query->have_posts())
541 {
542 $property_query->the_post();
543
544 $this->update_property_price_actual( get_the_ID() );
545 }
546 }
547
548 wp_reset_postdata();
549
550 }
551 }
552
553 }