|
<?php |
|
class CurrencySwitcher { |
|
|
|
private $currencies = array ( |
|
'gbp' => array ( 'sign' => '£', 'position' => 'before', 'rate' => 1, 'name' => 'British Pound', 'short' => 'GBP' ), |
|
'eur' => array ( 'sign' => '€', 'position' => 'before', 'rate' => 1.142530706, 'name' => 'Euro', 'short' => 'EUR' ), |
|
'usd' => array ( 'sign' => '$', 'position' => 'before', 'rate' => 1.02771855, 'name' => 'US Dollar', 'short' => 'USD' ), |
|
'aud' => array ( 'sign' => '$', 'position' => 'before', 'rate' => 1.730482852, 'name' => 'Australian Dollar', 'short' => 'AUD' ) |
|
); |
|
|
|
private $currency = 'gbp'; |
|
|
|
public function __construct( $default ){ |
|
if( array_key_exists( $default, $this->currencies ) ){ |
|
$this->currency = $default; |
|
} |
|
add_action( 'init', array( $this, 'set_currency' ) ); |
|
add_action( 'gonzo_daily', array( $this, 'get_rates' ) ); |
|
$this->set_rates(); |
|
} |
|
|
|
public function get_rates(){ |
|
|
|
$ratesURL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'; |
|
|
|
$response = wp_remote_get( $ratesURL, array( 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0' ) ); |
|
|
|
$ratesXML = new SimpleXMLElement( $response['body'] ); |
|
|
|
$currencies = array_keys( $this->currencies ); |
|
// We put a default EUR rate that we will later reference to GBP |
|
$rates = array( 'eur' => 1 ); |
|
|
|
foreach( $ratesXML->Cube->Cube->Cube as $rate ){ |
|
$currency = strtolower( $rate['currency'] ); |
|
if( in_array( $currency, $currencies ) ) { |
|
$rates[$currency] = (float)$rate['rate']; |
|
} |
|
} |
|
|
|
if( $rates['gbp'] != 0 ) { |
|
|
|
$eur2gbp = 1 / $rates['gbp']; |
|
|
|
foreach( $currencies as $currency ){ |
|
$rates[$currency] = round( $eur2gbp * $rates[$currency], 4 ); |
|
} |
|
|
|
set_transient( 'gonzo_currency_rates', $rates, 48 * HOUR_IN_SECONDS ); |
|
|
|
return $rates; |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
private function set_rates(){ |
|
|
|
if ( false === ( $rates = get_transient( 'gonzo_currency_rates' ) ) ) { |
|
if ( true === ( $rates = @$this->get_rates() ) ) { |
|
set_transient( 'gonzo_currency_rates', $rates, 48 * HOUR_IN_SECONDS ); |
|
} |
|
} |
|
|
|
if( $rates ) { |
|
foreach ( $this->currencies as $key => $currency ) { |
|
if( $rates[$key] != 0 ) { |
|
$this->currencies[$key]['rate'] = $rates[$key]; |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
public function set_currency(){ |
|
if( isset( $_GET['currency'] ) ){ |
|
if( array_key_exists( $_GET['currency'], $this->currencies ) ){ |
|
$this->currency = $_GET['currency']; |
|
} |
|
$expiry = strtotime('+1 year'); |
|
setcookie( 'currency', $this->currency, $expiry, COOKIEPATH, COOKIE_DOMAIN ); |
|
} |
|
elseif ( isset( $_COOKIE['currency'] ) ) { |
|
if( array_key_exists( $_COOKIE['currency'], $this->currencies ) ){ |
|
$this->currency = $_COOKIE['currency']; |
|
} |
|
} |
|
} |
|
|
|
public function get_currency(){ |
|
return $this->currency; |
|
} |
|
|
|
public function get_currencies(){ |
|
return $this->currencies; |
|
} |
|
|
|
public function format( $price ) { |
|
$currency = $this->currencies[$this->currency]; |
|
if( $currency['position'] == 'before' ){ |
|
return $currency['sign'] . '' . number_format_i18n( $price ); |
|
} |
|
else { |
|
return number_format_i18n( $price ) . '' . $currency['sign']; |
|
} |
|
} |
|
|
|
public function convert( $price, $precision ){ |
|
return round( $price * $this->currencies[$this->currency]['rate'], $precision ); |
|
} |
|
|
|
} |
|
|
|
$gonzo_cw = new CurrencySwitcher( 'gbp' ); |
|
|
|
?> |