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

522 lines 14.0 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 'FR' => array(
161 'name' => 'France',
162 'currency_code' => 'EUR',
163 'currency_symbol' => '&euro;',
164 'currency_prefix' => true
165 ),
166 'DE' => array(
167 'name' => 'Germany',
168 'currency_code' => 'EUR',
169 'currency_symbol' => '&euro;',
170 'currency_prefix' => true
171 ),
172 'IE' => array(
173 'name' => 'Ireland',
174 'currency_code' => 'EUR',
175 'currency_symbol' => '&euro;',
176 'currency_prefix' => true
177 ),
178 'IT' => array(
179 'name' => 'Italy',
180 'currency_code' => 'EUR',
181 'currency_symbol' => '&euro;',
182 'currency_prefix' => true
183 ),
184 'JP' => array(
185 'name' => 'Japan',
186 'currency_code' => 'JPY',
187 'currency_symbol' => '&yen;',
188 'currency_prefix' => true
189 ),
190 'MU' => array(
191 'name' => 'Mauritius',
192 'currency_code' => 'MUR',
193 'currency_symbol' => 'Rs',
194 'currency_prefix' => false
195 ),
196 'NO' => array(
197 'name' => 'Norway',
198 'currency_code' => 'NOK',
199 'currency_symbol' => 'kr',
200 'currency_prefix' => false
201 ),
202 'PT' => array(
203 'name' => 'Portugal',
204 'currency_code' => 'EUR',
205 'currency_symbol' => '&euro;',
206 'currency_prefix' => true
207 ),
208 'RU' => array(
209 'name' => 'Russia',
210 'currency_code' => 'RUB',
211 'currency_symbol' => '',
212 'currency_prefix' => true
213 ),
214 'ZA' => array(
215 'name' => 'South Africa',
216 'currency_code' => 'ZAR',
217 'currency_symbol' => 'R',
218 'currency_prefix' => true
219 ),
220 'ES' => array(
221 'name' => 'Spain',
222 'currency_code' => 'EUR',
223 'currency_symbol' => '&euro;',
224 'currency_prefix' => true
225 ),
226 'AE' => array(
227 'name' => 'United Arab Emirates',
228 'currency_code' => 'AED',
229 'currency_symbol' => '‎د.إ',
230 'currency_prefix' => false
231 ),
232 'GB' => array(
233 'name' => 'United Kingdom',
234 'currency_code' => 'GBP',
235 'currency_symbol' => '&pound;',
236 'currency_prefix' => true
237 ),
238 'US' => array(
239 'name' => 'United States',
240 'currency_code' => 'USD',
241 'currency_symbol' => '$',
242 'currency_prefix' => true
243 ),
244 );
245
246 return apply_filters( 'propertyhive_countries', $countries );
247 }
248
249 /**
250 * Outputs the list of countries for use in dropdown boxes.
251 * @param string $selected_country (default: '')
252 * @param bool $escape (default: false)
253 */
254 public function country_dropdown_options( $selected_country = '', $escape = false ) {
255 if ( $this->countries )
256 {
257 foreach ( $this->countries as $key => $value )
258 {
259 echo '<option';
260 if ( $selected_country == $key || ( $selected_country == '' && $key == 'GB' ) ) {
261 echo ' selected="selected"';
262 }
263 echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value['name'] ) : $value['name'] ) . '</option>';
264 }
265 }
266 }
267
268 public function convert_price_to_gbp( $price, $currency_code )
269 {
270 if ( trim($price) == '' )
271 {
272 return $price;
273 }
274
275 $exchange_rates = get_option( 'propertyhive_currency_exchange_rates', array() );
276
277 if ( isset($exchange_rates[$currency_code]) )
278 {
279 $price = $price / $exchange_rates[$currency_code];
280 }
281
282 return $price;
283 }
284
285 public function update_property_price_actual( $postID )
286 {
287 $countries = $this->countries;
288
289 $department = get_post_meta( $postID, '_department', true );
290 $country = get_post_meta( $postID, '_address_country', true );
291
292 if (isset($countries[$country]))
293 {
294 if ( $department == 'residential-sales' )
295 {
296 $currency = get_post_meta( $postID, '_currency', true );
297 if ( $country == '' )
298 {
299 $country = get_option( 'propertyhive_default_country', 'GB' );
300 }
301 if ( $currency == '' )
302 {
303 $currency = $this->get_country($country);
304 $currency = $currency['currency_code'];
305 }
306
307 $price = get_post_meta( $postID, '_price', true );
308
309 $converted_price = $this->convert_price_to_gbp( $price, $currency );
310
311 update_post_meta( $postID, '_price_actual', $converted_price );
312 }
313 elseif ( $department == 'residential-lettings' )
314 {
315 $currency = get_post_meta( $postID, '_currency', true );
316 if ( $country == '' )
317 {
318 $country = get_option( 'propertyhive_default_country', 'GB' );
319 }
320 if ( $currency == '' )
321 {
322 $currency = $this->get_country($country);
323 $currency = $currency['currency_code'];
324 }
325
326 $rent = get_post_meta( $postID, '_rent', true );
327 $rent_frequency = get_post_meta( $postID, '_rent_frequency', true );
328
329 $price = $rent; // Stored in pcm
330 switch ($rent_frequency)
331 {
332 case "pppw":
333 {
334 $bedrooms = get_post_meta( $postID, '_bedrooms', true );
335 if ( ( $bedrooms !== FALSE && $bedrooms != 0 && $bedrooms != '' ) && apply_filters( 'propertyhive_pppw_to_consider_bedrooms', true ) == true )
336 {
337 $price = (($rent * 52) / 12) * $bedrooms;
338 }
339 else
340 {
341 $price = ($rent * 52) / 12;
342 }
343 break;
344 }
345 case "pw": { $price = ($rent * 52) / 12; break; }
346 case "pcm": { $price = $rent; break; }
347 case "pq": { $price = ($rent * 4) / 12; break; }
348 case "pa": { $price = ($rent / 12); break; }
349 }
350
351 $converted_price = $this->convert_price_to_gbp( $price, $currency );
352
353 update_post_meta( $postID, '_price_actual', $converted_price );
354 }
355 if ( $department == 'commercial' )
356 {
357 if ( get_post_meta( $postID, '_for_sale', true ) == 'yes' )
358 {
359 $currency = get_post_meta( $postID, '_commercial_price_currency', true );
360 if ( $country == '' )
361 {
362 $country = get_option( 'propertyhive_default_country', 'GB' );
363 }
364 if ( $currency == '' )
365 {
366 $currency = $this->get_country($country);
367 $currency = $currency['currency_code'];
368 }
369
370 $price = get_post_meta( $postID, '_price_from', true );
371
372 $converted_price = $this->convert_price_to_gbp( $price, $currency );
373
374 update_post_meta( $postID, '_price_from_actual', $converted_price );
375
376 $price = get_post_meta( $postID, '_price_to', true );
377
378 $converted_price = $this->convert_price_to_gbp( $price, $currency );
379
380 update_post_meta( $postID, '_price_to_actual', $converted_price );
381 }
382 if ( get_post_meta( $postID, '_to_rent', true ) == 'yes' )
383 {
384 $currency = get_post_meta( $postID, '_commercial_rent_currency', true );
385 if ( $country == '' )
386 {
387 $country = get_option( 'propertyhive_default_country', 'GB' );
388 }
389 if ( $currency == '' )
390 {
391 $currency = $this->get_country($country);
392 $currency = $currency['currency_code'];
393 }
394
395 $rent_units = get_post_meta( $postID, '_rent_units', true );
396
397 $price = get_post_meta( $postID, '_rent_from', true );
398 switch ($rent_units)
399 {
400 case "pw": { $price = ($price * 52) / 12; break; }
401 case "pcm": { $price = $price; break; }
402 case "pq": { $price = ($price * 4) / 52; break; }
403 case "pa": { $price = ($price / 52); break; }
404 }
405
406 $converted_price = $this->convert_price_to_gbp( $price, $currency );
407
408 update_post_meta( $postID, '_rent_from_actual', $converted_price );
409
410 if ( get_post_meta( $postID, '_for_sale', true ) != 'yes' )
411 {
412 update_post_meta( $postID, '_price_from_actual', $converted_price );
413 }
414
415 $price = get_post_meta( $postID, '_rent_to', true );
416 switch ($rent_units)
417 {
418 case "pw": { $price = ($price * 52) / 12; break; }
419 case "pcm": { $price = $price; break; }
420 case "pq": { $price = ($price * 4) / 52; break; }
421 case "pa": { $price = ($price / 52); break; }
422 }
423
424 $converted_price = $this->convert_price_to_gbp( $price, $currency );
425
426 update_post_meta( $postID, '_rent_to_actual', $converted_price );
427 }
428 }
429 }
430 }
431
432 public function ph_update_currency_exchange_rates()
433 {
434 global $wpdb;
435
436 if ( $this->countries )
437 {
438 $countries = $this->countries;
439
440 $exchange_rates = array();
441 $previous_exchange_rates = get_option( 'propertyhive_currency_exchange_rates' );
442
443 $default_country = get_option( 'propertyhive_default_country', 'GB' );
444 $selected_countries = get_option( 'propertyhive_countries', array( $default_country ) );
445
446 foreach ( $countries as $key => $value )
447 {
448 if (!isset($exchange_rates[$value['currency_code']]) && in_array($key, $selected_countries) && $value['currency_code'] != 'GBP')
449 {
450 // we haven't got this exchange rate
451 $from = 'GBP';
452 $to = $value['currency_code'];
453
454 $exchangeRate = '';
455
456 $url = 'https://finance.google.com/finance/converter?a=1&from=' . $from . '&to=' . $to;
457
458 $response = wp_remote_get( $url );
459
460 if ( is_array( $response ) )
461 {
462 $body = wp_remote_retrieve_body( $response );
463
464 preg_match("/<span class=bld>(.*)<\/span>/", $body, $converted);
465
466 if ( isset($converted[1]) && $converted[1] != '' )
467 {
468 $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
469
470 if ( $converted != '' )
471 {
472 $exchangeRate = $converted;
473 $exchange_rates[$to] = $exchangeRate;
474 }
475 }
476 }
477 else
478 {
479
480 }
481
482 if ( $exchangeRate == '' && isset($previous_exchange_rates[$to]) )
483 {
484 // if for some reason we get here and don't have an exchange rate
485 $exchange_rates[$to] = $previous_exchange_rates[$to];
486 }
487 }
488 }
489 $exchange_rates['GBP'] = "1.0000";
490 update_option( 'propertyhive_currency_exchange_rates', $exchange_rates );
491 update_option( 'propertyhive_currency_exchange_rates_updated', date("Y-m-d") );
492
493 // Loop through all on market properties and update _actual_price meta value to be price in GBP
494 $args = array(
495 'post_type' => 'property',
496 'post_status' => 'publish',
497 'meta_query' => array(
498 array(
499 'key' => '_on_market',
500 'value' => 'yes',
501 )
502 ),
503 'nopaging' => true,
504 );
505 $property_query = new WP_Query($args);
506
507 if ($property_query->have_posts())
508 {
509 while ($property_query->have_posts())
510 {
511 $property_query->the_post();
512
513 $this->update_property_price_actual( get_the_ID() );
514 }
515 }
516
517 wp_reset_postdata();
518
519 }
520 }
521
522 }