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 '';
 }
Example #2
0
    public function widget($args, $instance)
    {
        $instance = $this->_merge_instance_with_default_instance($instance);
        echo $args['before_widget'];
        /**
         * Title
         */
        $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
        if ($title) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        /**
         * The list of currencies and base currency are required.
         */
        if (!empty($instance['currency_list']) && !empty($instance['base_currency'])) {
            $currency_pair = new Currency($instance['base_currency'], $instance['base_currency']);
            // Base currency available or not?
            if ($currency_pair->is_available()) {
                // Prepare currency list (string with commas comes to an array with tickers)
                $instance['currency_list'] = str_replace(' ', '', $instance['currency_list']);
                $instance['currency_list'] = explode(',', $instance['currency_list']);
                // Get base currency name and their country name
                $base_currency_name = (string) $currency_pair->get_base_currency_name();
                $base_currency_country_name = (string) $currency_pair->get_base_currency_country_name();
                // We need currencies and base currencies credits
                if (!empty($instance['currency_list']) && !empty($base_currency_name) && !empty($base_currency_country_name)) {
                    ?>
					<div class="currencyconverter-minimalistic-ver2-container">

						<div class="currencyconverter-minimalistic-ver2-header">
							<div class="currencyconverter-minimalistic-ver2-header-base-currency"><?php 
                    // Base currency count (for example 1 USD or 1 EUR).
                    echo Text::add_right_or_left('1', $instance['base_currency'], $instance['currency_position']);
                    ?>
</div>
							<div class="currencyconverter-minimalistic-ver2-header-currency-caption">
								<span class="currencyconverter-minimalistic-ver2-header-currency-caption-currency-name"><?php 
                    echo $base_currency_name;
                    ?>
.</span> <span class="currencyconverter-minimalistic-ver2-header-currency-caption-country-name"><?php 
                    echo $base_currency_country_name;
                    ?>
</span>
							</div>
						</div>

						<div class="currencyconverter-minimalistic-ver2-header-equal currencyconverter-f-row currencyconverter-f-row-hor-justify currencyconverter-f-row-ver-center">
							<span class="currencyconverter-f-col currencyconverter-f-col-dash-1">
								<span class="currencyconverter-minimalistic-ver2-separator-dash"></span>
							</span>
							<span class="currencyconverter-f-col currencyconverter-f-col-equal">=</span>
							<span class="currencyconverter-f-col currencyconverter-f-col-dash-2">
								<span class="currencyconverter-minimalistic-ver2-separator-dash"></span>
							</span>
						</div>

						<?php 
                    // Delete base currency meta information (name and country name). We output it above.
                    unset($base_currency_name);
                    unset($base_currency_country_name);
                    $first_currency = true;
                    // Foreach currency in the list
                    foreach ($instance['currency_list'] as $currency_ticker) {
                        // Setup our base currency and currency
                        $currency_pair->set_currencies($currency_ticker, $instance['base_currency']);
                        // This pair available?
                        if ($currency_pair->is_available()) {
                            // Prepare extra small numbers like 0.000001 $
                            $currency_data_filtered = Text::currency_info_for_round($currency_pair, 2);
                            ?>
										<div class="currencyconverter-minimalistic-ver2-single-currency <?php 
                            echo $first_currency == true ? '' : 'currencyconverter-minimalistic-ver2-separator-dash';
                            ?>
">
											<div class="currencyconverter-minimalistic-ver2-row">
												<span class="currencyconverter-minimalistic-ver2-currency-price"><?php 
                            echo number_format_i18n($currency_data_filtered['rate'], 2);
                            ?>
</span>
											</div>
											<div class="currencyconverter-minimalistic-ver2-row currencyconverter-minimalistic-ver2-row-captions">
												<span class="currencyconverter-minimalistic-ver2-inline-list">
													<span class="currencyconverter-minimalistic-ver2-inline-list-item currencyconverter-minimalistic-ver2-ticker">
														<?php 
                            echo $currency_ticker;
                            ?>
													</span><?php 
                            // Try to get country flag
                            $country_obj = new Country();
                            $country_obj->set_country_by_currency($currency_ticker);
                            $flag = $country_obj->get_flag_url($instance['flag_size']);
                            if ($flag) {
                                echo '<span class="currencyconverter-minimalistic-ver2-inline-list-item currencyconverter-minimalistic-ver2-inline-list-item-flag">';
                                printf('<img src="%1$s" class="currencyconverter-flag-icon currencyconverter-flag-icon-%2$s">', esc_url($flag), esc_attr($instance['flag_size']));
                                echo '</span>';
                            }
                            ?>
<span class="currencyconverter-minimalistic-ver2-inline-list-item currencyconverter-minimalistic-ver2-change-percentage"><?php 
                            printf(__('%s<span class="currencyconverter-percentage-symbol">%%</span>', Plugin::NAME), Text::number_format_i18n_plus_minus($currency_data_filtered['change_percentage'], 2));
                            ?>
</span><?php 
                            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). */
                                echo '<span class="currencyconverter-minimalistic-ver2-inline-list-item currencyconverter-minimalistic-ver2-per">' . esc_html(sprintf(__('Per %s', Plugin::NAME), number_format_i18n($currency_data_filtered['per']))) . '</span>';
                            }
                            ?>
												</span>
											</div>
										</div>
									<?php 
                            $first_currency = false;
                        }
                    }
                    ?>
					</div>
				<?php 
                }
            }
        }
        // Caption with update rates date
        if ($instance['caption_status']) {
            $plugin_developer = new PluginDeveloper();
            $plugin_developer->set_base_currency($instance['base_currency']);
            if ($plugin_developer->is_valid()) {
                echo '<p class="currencyconverter-support-info-container">' . $plugin_developer->get_caption_with_base_currency_link() . '</p>';
            }
        }
        // Styles for this widget
        $this->print_gradiented_styles('#' . $args['widget_id'], $instance);
        echo $args['after_widget'];
    }
Example #3
0
    public function widget($args, $instance)
    {
        $instance = $this->_merge_instance_with_default_instance($instance);
        echo $args['before_widget'];
        /**
         * Title
         */
        $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
        if ($title) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        if (!empty($instance['currency_list']) && !empty($instance['base_currency'])) {
            $instance['currency_list'] = str_replace(' ', '', $instance['currency_list']);
            $instance['currency_list'] = explode(',', $instance['currency_list']);
            if (!empty($instance['currency_list'])) {
                echo '<div class="currencyconverter-minimalistic-container">';
                foreach ($instance['currency_list'] as $currency_ticker) {
                    $currency_obj = new Currency($instance['base_currency'], $currency_ticker);
                    if ($currency_obj->is_available()) {
                        $currency_data_filtered = Text::currency_info_for_round($currency_obj, 2);
                        ?>
						<div class="currencyconverter-minimalistic-single-currency">
							<div class="currencyconverter-minimalistic-row">
								<span class="currencyconverter-minimalistic-currency-price"><?php 
                        echo number_format_i18n($currency_data_filtered['rate'], 2);
                        ?>
</span>
							</div>
							<div class="currencyconverter-minimalistic-row">
								<span class="currencyconverter-minimalistic-inline-list">
									<span class="currencyconverter-minimalistic-inline-list-item currencyconverter-minimalistic-ticker">
										<?php 
                        echo $currency_ticker;
                        ?>
									</span><span class="currencyconverter-minimalistic-inline-list-item currencyconverter-minimalistic-change-percentage">
										<?php 
                        printf(__('%s<span class="currencyconverter-percentage-symbol">%%</span>', Plugin::NAME), Text::number_format_i18n_plus_minus($currency_data_filtered['change_percentage'], 2));
                        ?>
									</span><?php 
                        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). */
                            $per_value = '<span class="currencyconverter-minimalistic-inline-list-item currencyconverter-minimalistic-per">' . esc_html(sprintf(__('Per %s', Plugin::NAME), number_format_i18n($currency_data_filtered['per']))) . '</span>';
                            echo $per_value;
                        }
                        ?>
								</span>
							</div>
						</div>
						<?php 
                    }
                }
                echo '</div>';
            }
        }
        if ($instance['caption_status']) {
            $plugin_developer = new PluginDeveloper();
            $plugin_developer->set_base_currency($instance['base_currency']);
            if ($plugin_developer->is_valid()) {
                echo '<p class="currencyconverter-support-info-container">' . $plugin_developer->get_caption_with_base_currency_link() . '</p>';
            }
        }
        $this->print_gradiented_styles('#' . $args['widget_id'], $instance);
        echo $args['after_widget'];
    }