WCML_Exchange_Rates_Fixierio

The WooCommerce Multilingual WCML Exchange Rates Fixierio class.

Defined (1)

The class is defined in the following location(s).

/inc/currencies/exchange-rate-services/class-wcml-exchange-rates-fixierio.php  
  1. class WCML_Exchange_Rates_Fixierio extends WCML_Exchange_Rate_Service{ 
  2.  
  3. private $id = 'fixierio'; 
  4. private $name = 'Fixer.io'; 
  5. private $url = 'http://fixer.io/'; 
  6. private $api_url = 'http://api.fixer.io/latest?base=%s&symbols=%s'; 
  7.  
  8. protected $api_key = ''; 
  9. const REQUIRES_KEY = false; 
  10.  
  11. function __construct() { 
  12. parent::__construct( $this->id, $this->name, $this->api_url, $this->url ); 
  13.  
  14. /** 
  15. * @param $from string 
  16. * @param $to array 
  17. * @return array 
  18. * @throws Exception 
  19. */ 
  20. public function get_rates( $from, $tos ) { 
  21.  
  22. parent::clear_last_error( ); 
  23. $rates = array(); 
  24.  
  25. $url = sprintf( $this->api_url, $from, join(', ', $tos) ); 
  26.  
  27. $http = new WP_Http(); 
  28. $data = $http->request( $url ); 
  29.  
  30. if( is_wp_error( $data ) ) { 
  31.  
  32. $http_error = join("\n", $data->get_error_messages() ); 
  33. parent::save_last_error( $http_error ); 
  34. throw new Exception( $http_error ); 
  35.  
  36. } else { 
  37.  
  38. $json = json_decode( $data['body'] ); 
  39.  
  40. if( isset( $json->base ) && isset( $json->rates ) ) { 
  41. foreach( $json->rates as $to => $rate ) { 
  42. $rates[$to] = round( $rate, 4 ); 
  43. } else{ 
  44. $error = isset( $json->error ) ? $json->error : 
  45. __( 'Cannot get exchange rates. Connection failed.', 'woocommerce-multilingual' ); 
  46. parent::save_last_error( $error ); 
  47. throw new Exception( $error ); 
  48.  
  49.  
  50. return $rates; 
  51.  
  52.