Guest User

Untitled

a guest
Jun 18th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.05 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class td_exchange {
  5.  
  6. private static $caching_time = 10800; // 3 hours
  7.  
  8. /**
  9. * Used by all the shortcodes + widget to render the exchange. The top bar has a separate function bellow
  10. * @param $atts
  11. * @return string
  12. */
  13. static function render_generic($atts) {
  14.  
  15. if (empty($atts['exchange_key'])) {
  16. return td_util::get_block_error('Exchange widget', "<strong>Api key</strong> is empty. Configure this block/widget and enter an api key :)");
  17. }
  18.  
  19. // prepare the data and do an api call
  20. $exchange_data = array (
  21. 'api_base' => '',
  22. 'api_rates' => array(),
  23. 'api_key' => $atts['exchange_key']
  24. );
  25.  
  26. $exchange_data_status = self::get_exchange_data((array)$atts, $exchange_data);
  27.  
  28. // check if we have an error and return that
  29. if ($exchange_data_status != 'api_fail_cache' and $exchange_data_status != 'api' and $exchange_data_status != 'cache') {
  30. return $exchange_data_status;
  31. }
  32.  
  33. // render the HTML
  34. $buffy = '<!-- td exchange source: ' . $exchange_data_status . ' -->';
  35.  
  36. // renders the block template
  37. $buffy .= self::render_block_template((array)$atts, $exchange_data);
  38.  
  39.  
  40. return $buffy;
  41. }
  42.  
  43. /**
  44. * @param $atts
  45. * @param $exchange_data
  46. * @return string - block render
  47. */
  48. private static function render_block_template($atts, $exchange_data) {
  49.  
  50. // stop render when no data is received
  51. if (empty($exchange_data['api_rates'])){
  52. $return_currency = 'eur';
  53. if (!empty($atts['e_base_currency'])) {
  54. $return_currency = $atts['e_base_currency'];
  55. }
  56. return td_util::get_block_error('Exchange widget', '<strong>Render failed - no data is received:</strong> ' . $return_currency);
  57. }
  58.  
  59. ob_start();
  60.  
  61. $td_currencies = array(
  62. 'eur' => 'Euro Member Countries',
  63. 'aud' => 'Australian Dollar',
  64. 'bgn' => 'Bulgarian Lev',
  65. 'brl' => 'Brazilian Real',
  66. 'cad' => 'Canadian Dollar',
  67. 'chf' => 'Swiss Franc',
  68. 'cny' => 'Chinese Yuan Renminbi',
  69. 'czk' => 'Czech Republic Koruna',
  70. 'dkk' => 'Danish Krone',
  71. 'gbp' => 'British Pound',
  72. 'hkd' => 'Hong Kong Dollar',
  73. 'hrk' => 'Croatian Kuna',
  74. 'huf' => 'Hungarian Forint',
  75. 'idr' => 'Indonesian Rupiah',
  76. 'ils' => 'Israeli Shekel',
  77. 'inr' => 'Indian Rupee',
  78. 'jpy' => 'Japanese Yen',
  79. 'krw' => 'Korean (South) Won',
  80. 'mxn' => 'Mexican Peso',
  81. 'myr' => 'Malaysian Ringgit',
  82. 'nok' => 'Norwegian Krone',
  83. 'nzd' => 'New Zealand Dollar',
  84. 'php' => 'Philippine Peso',
  85. 'pln' => 'Polish Zloty',
  86. 'ron' => 'Romanian (New) Leu',
  87. 'rub' => 'Russian Ruble',
  88. 'sek' => 'Swedish Krona',
  89. 'sgd' => 'Singapore Dollar',
  90. 'thb' => 'Thai Baht',
  91. 'try' => 'Turkish Lira',
  92. 'usd' => 'United States Dollar',
  93. 'zar' => 'South African Rand'
  94. );
  95.  
  96. if (empty($atts['e_base_currency'])) {
  97. // default base currency is EUR
  98. $atts['e_base_currency'] = 'eur';
  99. } else {
  100. // we have custom base currency - add EUR to the rates list
  101. $exchange_data['api_rates']['EUR'] = 1;
  102. // custom base currency code and rate
  103. $base_currency_code = strtoupper($atts['e_base_currency']);
  104. $base_currency_rate = $exchange_data['api_rates'][$base_currency_code];
  105. // recalculate all rates
  106. $exchange_data['api_rates'] = self::td_calculate_new_rates($base_currency_code, $base_currency_rate, $exchange_data['api_rates']);
  107. }
  108.  
  109. // set base currency title - ex. EUR - Euro Member Countries
  110. $base_currency_title = strtoupper($atts['e_base_currency']) . ' - ' . __td($td_currencies[$atts['e_base_currency']], TD_THEME_NAME);
  111.  
  112. // check if we have custom rates
  113. if (!empty($atts['e_custom_rates'])){
  114. // retrieve custom rates codes
  115. $e_custom_rates = explode(',',strtoupper($atts['e_custom_rates']));
  116. // store custom selected rates
  117. $custom_rates_output = array();
  118.  
  119. foreach ($e_custom_rates as $e_custom_rate) {
  120. // remove whitespace for each custom rate code
  121. $e_custom_rate = trim($e_custom_rate);
  122. // check if the custom exists in the api rates array
  123. if (isset($exchange_data['api_rates'][$e_custom_rate])){
  124. $custom_rates_output[$e_custom_rate] = $exchange_data['api_rates'][$e_custom_rate];
  125. }
  126. }
  127. // replace default rates with custom rates
  128. if (!empty($custom_rates_output)) {
  129. $exchange_data['api_rates'] = $custom_rates_output;
  130. }
  131. }
  132.  
  133. // default decimals is 4
  134. $e_rate_decimals = 4;
  135. // get rate decimals
  136. if (!empty($atts['e_rate_decimals'])) {
  137. $e_rate_decimals = $atts['e_rate_decimals'];
  138. }
  139.  
  140. ?>
  141.  
  142. <div class="td-exchange-header">
  143. <div class="td-exchange-base"><span class="td-flags td-flag-header td-flag-<?php echo $atts['e_base_currency'] ?>"></span><span class="td-base-title"><?php echo $base_currency_title ?></span></div>
  144. </div>
  145.  
  146.  
  147. <div class="td-exchange-rates">
  148. <?php
  149.  
  150. //print_r($exchange_data['api_rates']);
  151. foreach ($exchange_data['api_rates'] as $rate_code => $rate_value) {
  152. // use lowercase on classes
  153. $rate_code_class = strtolower($rate_code);
  154.  
  155. // if we get more currencies via the api than we have in the theme, ignore them
  156. if (!isset($td_currencies[$rate_code_class])) {
  157. continue;
  158. }
  159.  
  160. // change rate into currency - ex. 1usd = 1.3210eur
  161. $rate_value = 1 / $rate_value;
  162. // round the rate value using decimals set on block settings
  163. $rate_value = round($rate_value, $e_rate_decimals);
  164. // flag title
  165. $rate_title = 'title="' . __td($td_currencies[$rate_code_class], TD_THEME_NAME) . '"';
  166. ?>
  167. <div class="td-rate">
  168. <span <?php echo $rate_title ?> class="td-flags td-flags-all td-flag-<?php echo $rate_code_class ?>"></span>
  169. <div class="td-rate-currency td-rate-<?php echo $rate_code_class ?>"><?php echo $rate_code . '</div><div class="td-exchange-value">' . number_format_i18n($rate_value, $e_rate_decimals)?></div>
  170. </div>
  171. <?php
  172. }
  173. ?>
  174. </div>
  175. <div class="clearfix"></div>
  176.  
  177. <?php
  178. return ob_get_clean();
  179. }
  180.  
  181.  
  182. /**
  183. * @param $base_currency_code (string) - ex. AUD or USD
  184. * @param $base_currency_rate (integer)
  185. * @param $default_api_rates (array)
  186. * @return $new_rate (array) - new rate based on the base rate
  187. */
  188. private static function td_calculate_new_rates($base_currency_code, $base_currency_rate, $default_api_rates) {
  189. //return buffer
  190. $new_rates = array();
  191. foreach ($default_api_rates as $rate_code => $rate_value) {
  192. // remove the custom selected base rate from the the list
  193. if ($base_currency_code != $rate_code) {
  194. $new_rates[$rate_code] = $rate_value / $base_currency_rate;
  195. }
  196. }
  197. return $new_rates;
  198. }
  199.  
  200.  
  201. /**
  202. * @param $atts
  203. * @param $exchange_data - the precomputed exchange data
  204. * @return bool|string
  205. * - bool:true - we have the $exchange_data (from cache or from a real request)
  206. * - string - error message
  207. */
  208. private static function get_exchange_data($atts, &$exchange_data) {
  209.  
  210. $cache_key = 'td_exchange_eur';
  211. if (td_remote_cache::is_expired(__CLASS__, $cache_key) === true) {
  212. // cache is expired - do a request
  213. $fixed_api_data = self::fixer_get_data($atts, $exchange_data);
  214. // check the api call response
  215. if ($fixed_api_data !== true) {
  216. // we have an error in the api
  217. $exchange_data = td_remote_cache::get(__CLASS__, $cache_key);
  218. if ($exchange_data === false) { // miss and io error... shit / die
  219. return td_util::get_block_error('Exchange widget', '<strong>Exchange API error</strong>: ' . $fixed_api_data);
  220. }
  221.  
  222. td_remote_cache::extend(__CLASS__, $cache_key, self::$caching_time);
  223. return 'api_fail_cache';
  224. }
  225.  
  226. td_remote_cache::set(__CLASS__, $cache_key, $exchange_data, self::$caching_time); //we have a reply and we set it
  227. return 'api';
  228.  
  229. } else {
  230. // cache is valid
  231. $exchange_data = td_remote_cache::get(__CLASS__, $cache_key);
  232. return 'cache';
  233. }
  234. }
  235.  
  236.  
  237. /**
  238. * adds to the &$exchange_data the information from FIXER
  239. * @param $atts - the shortcode atts
  240. * @param $exchange_data - BYREF exchange data - this function will add to it
  241. *
  242. * @return bool|string
  243. * - true: if everything is ok
  244. * - string: the error message, if there was an error
  245. */
  246. private static function fixer_get_data($atts, &$exchange_data){
  247.  
  248. // default base currency is eur and it returns all rates
  249. $api_url = 'http://data.fixer.io/api/latest?access_key=' . $exchange_data['api_key'];
  250. $json_api_response = td_remote_http::get_page($api_url, __CLASS__);
  251.  
  252. // check for a response
  253. if ($json_api_response === false) {
  254. td_log::log(__FILE__, __FUNCTION__, 'Api call failed', $api_url);
  255. return 'Api call failed';
  256. }
  257.  
  258. // try to decode the json
  259. $api_response = @json_decode($json_api_response, true);
  260. if ($api_response === null and json_last_error() !== JSON_ERROR_NONE) {
  261. td_log::log(__FILE__, __FUNCTION__, 'Error decoding the json', $api_response);
  262. return 'Error decoding the json';
  263. }
  264.  
  265. // error
  266. if (isset($api_response['error'])) {
  267. td_log::log(__FILE__, __FUNCTION__, 'Error response from server, the key may be invalid', $api_response);
  268. return 'Error response from server, the key may be invalid';
  269. }
  270.  
  271. // expected data is missing
  272. if (!isset($api_response['base']) || !isset($api_response['rates'])) {
  273. td_log::log(__FILE__, __FUNCTION__, 'Expected data is missing', $api_response);
  274. return 'Expected data is missing';
  275. }
  276.  
  277. // current base currency
  278. $exchange_data['api_base'] = $api_response['base'];
  279. $exchange_data['api_rates'] = $api_response['rates'];
  280.  
  281. return true;
  282. }
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment