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

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