WCML_Exchange_Rates_Currencylayer

The WooCommerce Multilingual WCML Exchange Rates Currencylayer class.

Defined (1)

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

/inc/currencies/exchange-rate-services/class-wcml-exchange-rates-currencylayer.php  
  1. class WCML_Exchange_Rates_Currencylayer extends WCML_Exchange_Rate_Service{ 
  2.  
  3. private $id = 'currencylayer'; 
  4. private $name = 'currencylayer'; 
  5. private $url = 'https://currencylayer.com/'; 
  6. private $api_url = 'http://apilayer.net/api/live?access_key=%s&source=%s¤cies=%s&amount=1'; 
  7.  
  8. protected $api_key = ''; 
  9. protected $requires_key = true; 
  10.  
  11. const REQUIRES_KEY = true; 
  12.  
  13. function __construct() { 
  14. parent::__construct( $this->id, $this->name, $this->api_url, $this->url ); 
  15.  
  16. /** 
  17. * @param $from string 
  18. * @param $to array 
  19. * @return array 
  20. * @throws Exception 
  21. */ 
  22. public function get_rates( $from, $tos ) { 
  23.  
  24. parent::clear_last_error( ); 
  25. $rates = array(); 
  26.  
  27. $url = sprintf( $this->api_url, $this->api_key, $from, join(', ', $tos) ); 
  28.  
  29. $http = new WP_Http(); 
  30. $data = $http->request( $url ); 
  31.  
  32.  
  33. if( is_wp_error( $data ) ) { 
  34.  
  35. $http_error = join("\n", $data->get_error_messages() ); 
  36. parent::save_last_error( $http_error ); 
  37. throw new Exception( $http_error ); 
  38.  
  39. } else { 
  40.  
  41. $json = json_decode( $data['body'] ); 
  42.  
  43. if( empty( $json->success ) ) { 
  44. if( !empty( $json->error->info ) ) { 
  45. if( strpos( $json->error->info, 'You have not supplied an API Access Key' ) !== false ) { 
  46. $error = __('You have entered an incorrect API Access Key', 'woocommerce-multilingual'); 
  47. }else{ 
  48. $error = $json->error->info; 
  49. } else{ 
  50. $error = __( 'Cannot get exchange rates. Connection failed.', 'woocommerce-multilingual' ); 
  51. parent::save_last_error( $error ); 
  52. throw new Exception( $error ); 
  53. } else{ 
  54.  
  55. if( isset( $json->quotes ) ) { 
  56. foreach( $tos as $to ) { 
  57. if( isset( $json->quotes->{$from.$to} ) ) { 
  58. $rates[$to] = round( $json->quotes->{$from.$to}, 4 ); 
  59.  
  60.  
  61. return $rates; 
  62.  
  63.