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

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