public function get_table() { if ($this->is_valid()) { $this->table = new Table(); $have_data = false; // Header if (!empty($this->parameters['table_headers_currencies']) || !empty($this->parameters['table_headers_price']) || !empty($this->parameters['table_headers_change'])) { $this->table->set_heading(array($this->parameters['table_headers_currencies'], $this->parameters['table_headers_price'], $this->parameters['table_headers_change'])); $have_data = true; } foreach ($this->parameters['currency_list'] as $currency) { $currency_obj = new Currency($this->parameters['base_currency'], $currency); // Проверяем доступность валюты if ($currency_obj->is_available()) { $currency_data_filtered = Text::currency_info_for_round($currency_obj, 2); $currency_data_filtered['trend'] = esc_attr($currency_data_filtered['trend']); $have_data = true; // Страна $country_obj = new Country(); $country_obj->set_country_by_currency($currency); // Получаем флаг, цену и изменение. Форматируем числа. $output_data = array(); $flag = $country_obj->get_flag_url($this->parameters['flag_size']); if ($flag) { $flag = sprintf('<img src="%1$s" class="currencyconverter-flag-icon currencyconverter-flag-icon-%2$s">', esc_url($flag), esc_attr($this->parameters['flag_size'])); } else { $flag = ''; } // Flag $output_data[0] = $flag . ' ' . $currency; // Rate (price) $output_data[1] = number_format_i18n($currency_data_filtered['rate'], 2); // Change % $output_data[2] = sprintf(__('%s<span class="currencyconverter-percentage-symbol">%%</span>', Plugin::NAME), Text::number_format_i18n_plus_minus($currency_data_filtered['change_percentage'], 2)); // Wrap into the colored spans. $content_wrapper_template = '<span class="currencyconverter-color-%1$s">%2$s</span>'; $output_data[1] = sprintf($content_wrapper_template, $currency_data_filtered['trend'], $output_data[1]); if ($currency_data_filtered['per'] > 1) { /* translators: Some of currencies (units) are very small. For example 1 US dollar (USD) = 0.0026528435830000001 bitcoins (BTC). Sometimes we round this to 0.00 by round() func. To avoid this small currencies (units) recalculated by multiplying "small" number by 1000 or 1000000. And after this: 1000 USD = 0.26 BTC (0.26 BTC per 1000 USD). */ $output_data[1] .= ' <span class="currencyconverter-per">' . esc_html(sprintf(__('per %s', Plugin::NAME), number_format_i18n($currency_data_filtered['per']))) . '</span>'; } // Arrow up-bottom $trend = sprintf('<span class="currencyconverter-trend currencyconverter-trend-%1$s"></span>', $currency_data_filtered['trend']); $output_data[2] = $trend . sprintf($content_wrapper_template, $currency_data_filtered['trend'], $output_data[2]); // Добавляем ряд (строчку) в таблицу $this->table->add_row($output_data); } } if ($have_data) { return $this->table->generate(); } } return ''; }
/** * Some of currencies (units) are very small. For example 1 US dollar (USD) = 0.0026528435830000001 bitcoins (BTC). * Sometimes we round this to 0.00. To avoid this small currencies (units) recalculated by multiplying "small" * number by 1000 or 1000000. And after this: 1000 USD = 0.26528435830000001 BTC * * @param Currency $currency_obj * * @return array Filtered values */ public static function currency_info_for_round(Currency $currency_obj, $preciese = 2) { $out = array('rate' => $currency_obj->get_rate(), 'per' => 1, 'change_percentage' => $currency_obj->get_change_percentage(), 'change' => $currency_obj->get_change(), 'trend' => $currency_obj->get_trend()); $out['change_percentage'] = (double) round($out['change_percentage'], $preciese); if ($out['rate'] >= 0.01) { } elseif ($out['rate'] >= 0.001) { $out['rate'] = $out['rate'] * 1000; $out['per'] = 1000; $out['change'] = $out['change'] * 1000; } elseif ($out['rate'] >= 1.0E-6) { $out['rate'] = $out['rate'] * 1000000; $out['per'] = 1000000; $out['change'] = $out['change'] * 1000000; } elseif ($out['rate'] >= 1.0E-8) { $out['rate'] = $out['rate'] * 100000000; $out['per'] = 100000000; $out['change'] = $out['change'] * 100000000; } if ($out['change_percentage'] === 0.0) { $out['trend'] = 'flat'; $out['change'] = 0.0; } return $out; }
public function form($instance) { static $first = true; $currency = new Currency('USD', 'USD'); if (!$currency->is_available()) { ?> <p><?php printf(__('Select data provider in <a href="%1$s">plugin settings</a>.', Plugin::NAME), esc_url(\Korobochkin\CurrencyConverter\Admin\Settings\General\Pages\General\Page::get_url())); ?> </p><?php return; } unset($currency); $instance = $this->_merge_instance_with_default_instance($instance); $rates = get_option(\Korobochkin\CurrencyConverter\Plugin::NAME . '_rates'); ?> <p><label for="<?php echo $this->get_field_id('title'); ?> "><?php _e('Title:', Plugin::NAME); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?> " name="<?php echo $this->get_field_name('title'); ?> " type="text" value="<?php echo esc_attr($instance['title']); ?> "></p> <p><label for="<?php echo $this->get_field_id('base_currency'); ?> "><?php _e('Base currency:', Plugin::NAME); ?> </label> <select class="widefat plugin__currency__select-autocomplete" id="<?php echo $this->get_field_id('base_currency'); ?> " name="<?php echo $this->get_field_name('base_currency'); ?> "> <?php foreach ($rates[0]['rates'] as $key => $value) { printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($key, $instance['base_currency'], false), esc_html($key)); } ?> </select> </p> <p class="description"><?php _e('The currency in which will be settled other currencies.', Plugin::NAME); ?> </p> <p><label for="<?php echo $this->get_field_id('currency_position'); ?> "><?php _e('Currency position:', Plugin::NAME); ?> </label> <select class="widefat" id="<?php echo $this->get_field_id('currency_position'); ?> " name="<?php echo $this->get_field_name('currency_position'); ?> "> <?php printf('<option value="%s"%s>%s</option>', esc_attr('left'), selected('left', $instance['currency_position'], false), esc_html(__('Left — $1.99', Plugin::NAME))); printf('<option value="%s"%s>%s</option>', esc_attr('right'), selected('right', $instance['currency_position'], false), esc_html(__('Right — 1.99$', Plugin::NAME))); ?> </select> </p> <p><label for="<?php echo $this->get_field_id('currency_list'); ?> "><?php _e('Currencies list:', Plugin::NAME); ?> </label> <input class="widefat plugin__currency__autocomplete" id="<?php echo $this->get_field_id('currency_list'); ?> " name="<?php echo $this->get_field_name('currency_list'); ?> " type="text" value="<?php echo esc_attr($instance['currency_list']); ?> "></p> <p class="description"><?php _e('The currencies which will be displayed in table. Separate by commas.', Plugin::NAME); ?> </p> <script type="text/javascript"> <?php if (!$first) { $tickers = array(); foreach ($rates[0]['rates'] as $key => $value) { $tickers[] = $key; } /** * TODO: Использовать wp_json_encode(), если решим поддерживать совместимость с версии больше 4.1.0 * Делать экранизацию не нужно, потому что см. https://codex.wordpress.org/Function_Reference/esc_js */ echo 'var currenciesTickersList =' . json_encode($tickers) . ';'; } ?> </script> <p><label for="<?php echo $this->get_field_id('flag_icons'); ?> "><?php _e('Flag icons:', Plugin::NAME); ?> </label> <select class="widefat" id="<?php echo $this->get_field_id('flag_size'); ?> " name="<?php echo $this->get_field_name('flag_size'); ?> "> <option value="0" <?php selected(0, $instance['flag_size'], false); ?> ><?php _e('No flag icon', Plugin::NAME); ?> </option> <?php $flags_sizes = new \Korobochkin\CurrencyConverter\Models\Flags(); foreach ($flags_sizes->sizes as $size) { printf('<option value="%s"%s>%s</option>', esc_attr($size), selected($size, $instance['flag_size'], false), esc_html($size . ' px')); } ?> </select> </p> <h3><?php _e('Background color', Plugin::NAME); ?> </h3> <p><?php _e('Predefined color schemes:', Plugin::NAME); ?> </p> <?php $default_presets = Defaults::get_default_color_schemes(); echo '<ul class="currencyconverter-minimalistic-ver2-widget-settings-palettes">'; foreach ($default_presets as $default_key => $default_preset) { $id = $this->get_field_id('palettes-' . $default_key); ?> <li id="<?php echo esc_attr($id); ?> " class="color-grid color-grid-gradient currencyconverter-color-grid-<?php echo esc_attr($default_key); ?> " data-currency-converter-palettes-switcher="true"> <span class="currencyconverter-minimalistic-ver2-container" <?php echo $this->_generate_html_attrs($default_preset, $id); ?> >Abc</span> </li><?php $this->_generate_switch_color_scheme_scripts($id); } echo '</ul>'; ?> <p> <label for="<?php echo $this->get_field_id('bg_color_1'); ?> "><?php _e('First background color:', Plugin::NAME); ?> </label> <div> <input class="widefat" id="<?php echo $this->get_field_id('bg_color_1'); ?> " name="<?php echo $this->get_field_name('bg_color_1'); ?> " type="text" value="<?php echo esc_attr($instance['bg_color_1']); ?> " size="6" data-currency-converter-minimalistic-palette-color="true"> </div> </p> <p> <label for="<?php echo $this->get_field_id('bg_color_2'); ?> "><?php _e('Second background color:', Plugin::NAME); ?> </label> <div> <input class="widefat" id="<?php echo $this->get_field_id('bg_color_2'); ?> " name="<?php echo $this->get_field_name('bg_color_2'); ?> " type="text" value="<?php echo esc_attr($instance['bg_color_2']); ?> " size="6" data-currency-converter-minimalistic-palette-color="true"> </div> </p> <p> <label for="<?php echo $this->get_field_id('color'); ?> "><?php _e('Font color:', Plugin::NAME); ?> </label> <div> <input class="widefat" id="<?php echo $this->get_field_id('color'); ?> " name="<?php echo $this->get_field_name('color'); ?> " type="text" value="<?php echo esc_attr($instance['color']); ?> " size="6" data-currency-converter-minimalistic-palette-color="true"> </div> </p> <p> <label for="<?php echo $this->get_field_id('separator_color'); ?> "><?php _e('Separator line color:', Plugin::NAME); ?> </label> <div> <input class="widefat" id="<?php echo $this->get_field_id('separator_color'); ?> " name="<?php echo $this->get_field_name('separator_color'); ?> " type="text" value="<?php echo esc_attr($instance['separator_color']); ?> " size="6" data-currency-converter-minimalistic-palette-color="true"> </div> </p> <p> <label for="<?php echo $this->get_field_id('separator_opacity'); ?> "><?php _e('Separator line opacity:', Plugin::NAME); ?> </label> <input class="" id="<?php echo $this->get_field_id('separator_opacity'); ?> " name="<?php echo $this->get_field_name('separator_opacity'); ?> " type="text" value="<?php echo esc_attr($instance['separator_opacity']); ?> " size="6"> % </p> <p><input id="<?php echo $this->get_field_id('caption_status'); ?> " name="<?php echo $this->get_field_name('caption_status'); ?> " type="checkbox" <?php checked($instance['caption_status']); ?> > <label for="<?php echo $this->get_field_id('caption_status'); ?> "><?php _e('Show last update date of currency exchange rate.', Plugin::NAME); ?> </label></p> <?php if ($first) { /** * Print color palettes styles only one time (they similar for all widgets). */ foreach ($default_presets as $default_key => $default_preset) { $this->print_gradiented_styles('.currencyconverter-color-grid-' . $default_key, $default_preset); } // TODO: Добавить динамическое изменение ширины для Iris /** * JS Init see http://wordpress.stackexchange.com/a/212676/46077 */ ?> <script type="text/javascript"> (function($){ function init(widget) { widget.find('*[data-currency-converter-minimalistic-ver2-palette-color="true"]').wpColorPicker( { change: (typeof _ !== 'undefined') ? _.throttle(function() { $(this).trigger('change'); }, 1000 ) : function(){}, width: 246 }); } $(document).on('widget-added widget-updated', function(event, widget) { init(widget); }); $(document).ready(function() { $('#widgets-right .widget').each(function() { init($(this)); }); }); }(jQuery)); </script> <?php $first = false; } }
public function form($instance) { static $first = true; $currency = new Currency('USD', 'USD'); if (!$currency->is_available()) { ?> <p><?php printf(__('Select data provider in <a href="%1$s">plugin settings</a>.', Plugin::NAME), esc_url(\Korobochkin\CurrencyConverter\Admin\Settings\General\Pages\General\Page::get_url())); ?> </p><?php return; } unset($currency); $instance = $this->_merge_instance_with_default_instance($instance); $rates = get_option(\Korobochkin\CurrencyConverter\Plugin::NAME . '_rates'); ?> <p><label for="<?php echo $this->get_field_id('title'); ?> "><?php _e('Title:', Plugin::NAME); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?> " name="<?php echo $this->get_field_name('title'); ?> " type="text" value="<?php echo esc_attr($instance['title']); ?> "></p> <p><label for="<?php echo $this->get_field_id('base_currency'); ?> "><?php _e('Base currency:', Plugin::NAME); ?> </label> <select class="widefat plugin__currency__select-autocomplete" id="<?php echo $this->get_field_id('base_currency'); ?> " name="<?php echo $this->get_field_name('base_currency'); ?> "> <?php foreach ($rates[0]['rates'] as $key => $value) { printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($key, $instance['base_currency'], false), esc_html($key)); } ?> </select> </p> <p class="description"><?php _e('The currency in which will be settled other currencies.', Plugin::NAME); ?> </p> <p><label for="<?php echo $this->get_field_id('currency_list'); ?> "><?php _e('Currencies list:', Plugin::NAME); ?> </label> <input class="widefat plugin__currency__autocomplete" id="<?php echo $this->get_field_id('currency_list'); ?> " name="<?php echo $this->get_field_name('currency_list'); ?> " type="text" value="<?php echo esc_attr($instance['currency_list']); ?> "></p> <p class="description"><?php _e('The currencies which will be displayed in table. Separate by commas.', Plugin::NAME); ?> </p> <script type="text/javascript"> <?php if (!$first) { $tickers = array(); foreach ($rates[0]['rates'] as $key => $value) { $tickers[] = $key; } /** * TODO: Использовать wp_json_encode(), если решим поддерживать совместимость с версии больше 4.1.0 * Делать экранизацию не нужно, потому что см. https://codex.wordpress.org/Function_Reference/esc_js */ echo 'var currenciesTickersList =' . json_encode($tickers) . ';'; } ?> </script> <p><label for="<?php echo $this->get_field_id('flag_icons'); ?> "><?php _e('Flag icons:', Plugin::NAME); ?> </label> <select class="widefat" id="<?php echo $this->get_field_id('flag_size'); ?> " name="<?php echo $this->get_field_name('flag_size'); ?> "> <option value="0"><?php _e('No flag icon', Plugin::NAME); ?> </option> <?php $flags_sizes = new \Korobochkin\CurrencyConverter\Models\Flags(); foreach ($flags_sizes->sizes as $size) { printf('<option value="%s"%s>%s</option>', esc_attr($size), selected($size, $instance['flag_size'], false), esc_html($size . ' px')); } ?> </select> </p> <h3><?php _e('Table headers', Plugin::NAME); ?> </h3> <p> <label for="<?php echo $this->get_field_id('table_headers_currencies'); ?> "><?php _e('Currencies names col:', Plugin::NAME); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id('table_headers_currencies'); ?> " name="<?php echo $this->get_field_name('table_headers_currencies'); ?> " type="text" value="<?php echo esc_attr($instance['table_headers_currencies']); ?> "> </p> <p> <label for="<?php echo $this->get_field_id('table_headers_price'); ?> "><?php _e('Price col:', Plugin::NAME); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id('table_headers_price'); ?> " name="<?php echo $this->get_field_name('table_headers_price'); ?> " type="text" value="<?php echo esc_attr($instance['table_headers_price']); ?> "> </p> <p> <label for="<?php echo $this->get_field_id('table_headers_change'); ?> "><?php _e('Change col:', Plugin::NAME); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id('table_headers_change'); ?> " name="<?php echo $this->get_field_name('table_headers_change'); ?> " type="text" value="<?php echo esc_attr($instance['table_headers_change']); ?> "> </p> <p><input id="<?php echo $this->get_field_id('caption_status'); ?> " name="<?php echo $this->get_field_name('caption_status'); ?> " type="checkbox" <?php checked($instance['caption_status']); ?> > <label for="<?php echo $this->get_field_id('caption_status'); ?> "><?php _e('Show last update date of currency exchange rate.', Plugin::NAME); ?> </label></p> <?php $first = false; }