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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / multi-currency / exchange-rate-services / Service.php
woocommerce-multilingual / classes / multi-currency / exchange-rate-services Last commit date
ApiLayerService.php 4 weeks ago CurrencyLayer.php 4 weeks ago ExchangeRatesApi.php 4 weeks ago Fixerio.php 4 weeks ago OpenExchangeRates.php 4 weeks ago Service.php 4 weeks ago
Service.php
148 lines
1 <?php
2
3 namespace WCML\MultiCurrency\ExchangeRateServices;
4
5 use WPML\FP\Obj;
6
7 abstract class Service {
8
9 private $settings;
10
11 abstract public function getId();
12
13 abstract public function getName();
14
15 abstract public function getUrl();
16
17 abstract public function getApiUrl();
18
19 abstract public function isKeyRequired();
20
21 public function resetConnectionCache() {
22
23 }
24
25 public function getRates( $from, $tos ) {
26 $this->clearLastError();
27
28 $response = $this->makeRequest( $from, $tos );
29
30 if ( is_wp_error( $response ) ) {
31 $http_error = implode( "\n", $response->get_error_messages() );
32 $this->saveLastError( $http_error );
33 throw new \Exception( $http_error );
34 }
35
36 $data = json_decode( $response['body'] );
37
38 if ( $this->isInvalidResponse( $data ) ) {
39 $error = self::get_formatted_error( $data );
40 $this->saveLastError( $error );
41 throw new \Exception( $error );
42 }
43
44 return $this->extractRates( $data, $from, $tos );
45 }
46
47 protected function makeRequest( $from, $tos ) {
48 if ( $this->isKeyRequired() ) {
49 $url = sprintf( $this->getApiUrl(), $this->getApiKey(), $from, implode( ',', $tos ) );
50 } else {
51 $url = sprintf( $this->getApiUrl(), $from, implode( ',', $tos ) );
52 }
53
54 return wp_safe_remote_get( $url, [ 'headers' => $this->getRequestHeaders() ] );
55 }
56
57 protected function getRequestHeaders() {
58 return [];
59 }
60
61 protected function isInvalidResponse( $decodedData ) {
62 return empty( $decodedData->rates );
63 }
64
65 protected function extractRates( $validData, $from, $tos ) {
66 $rates = [];
67
68 foreach ( $validData->rates as $to => $rate ) {
69 $rates[ $to ] = round( $rate, \WCML_Exchange_Rates::DIGITS_AFTER_DECIMAL_POINT );
70 }
71
72 return $rates;
73 }
74
75 public static function get_formatted_error( $response ) {
76 $getFromPath = function( $path ) use ( $response ) {
77 try {
78 $value = Obj::path( $path, $response );
79 return is_string( $value ) || is_int( $value ) ? $value : null;
80 } catch ( \Exception $e ) {
81 return null;
82 }
83 };
84
85 $formattedError = wpml_collect( [
86 'error' => $getFromPath( [ 'error' ] ),
87 'error_code' => $getFromPath( [ 'error', 'code' ] ),
88 'error_type' => $getFromPath( [ 'error', 'type' ] ),
89 'error_info' => $getFromPath( [ 'error', 'info' ] ),
90 'error_message' => $getFromPath( [ 'error', 'message' ] ),
91 'message' => $getFromPath( [ 'message' ] ),
92 'description' => $getFromPath( [ 'description' ] ),
93 ] )->filter()
94 ->map( function( $value, $key ) {
95 return "$key: $value";
96 } )
97 ->implode( ' - ' );
98
99 return $formattedError
100 ? strip_tags( $formattedError )
101 : esc_html__( 'Cannot get exchange rates. Connection failed.', 'woocommerce-multilingual' );
102 }
103
104 public function getSettings() {
105 if ( null === $this->settings ) {
106 $this->settings = get_option( 'wcml_exchange_rate_service_' . $this->getId(), [] );
107 }
108
109 return $this->settings;
110 }
111
112 private function saveSettings() {
113 update_option( 'wcml_exchange_rate_service_' . $this->getId(), $this->getSettings() );
114 }
115
116 public function getSetting( $key ) {
117 return Obj::prop( $key, $this->getSettings() );
118 }
119
120 public function saveSetting( $key, $value ) {
121 $this->getSettings();
122 $this->settings[ $key ] = $value;
123 $this->saveSettings();
124 }
125
126 public function saveLastError( $error_message ) {
127 $this->saveSetting(
128 'last_error',
129 [
130 'text' => $error_message,
131 'time' => date_i18n( 'F j, Y g:i a', false, true ),
132 ]
133 );
134 }
135
136 public function clearLastError() {
137 $this->saveSetting( 'last_error', false );
138 }
139
140 public function getLastError() {
141 return $this->getSetting( 'last_error' );
142 }
143
144 protected function getApiKey() {
145 return $this->getSetting( 'api-key' );
146 }
147 }
148