['name' => 'Nigerian Naira', 'symbol' => '₦', 'flag' => '🇳🇬'], 'USD' => ['name' => 'US Dollar', 'symbol' => '$', 'flag' => '🇺🇸'], 'GBP' => ['name' => 'British Pound', 'symbol' => '£', 'flag' => '🇬🇧'], 'EUR' => ['name' => 'Euro', 'symbol' => '€', 'flag' => '🇪🇺'], 'CAD' => ['name' => 'Canadian Dollar', 'symbol' => 'C$', 'flag' => '🇨🇦'], 'GHS' => ['name' => 'Ghanaian Cedi', 'symbol' => 'GH₵', 'flag' => '🇬🇭'], 'ZAR' => ['name' => 'South African Rand', 'symbol' => 'R', 'flag' => '🇿🇦'], 'KES' => ['name' => 'Kenyan Shilling', 'symbol' => 'KSh', 'flag' => '🇰🇪'], 'JPY' => ['name' => 'Japanese Yen', 'symbol' => '¥', 'flag' => '🇯🇵'], 'CNY' => ['name' => 'Chinese Yuan', 'symbol' => '¥', 'flag' => '🇨🇳'], 'AED' => ['name' => 'UAE Dirham', 'symbol' => 'د.إ', 'flag' => '🇦🇪'], 'INR' => ['name' => 'Indian Rupee', 'symbol' => '₹', 'flag' => '🇮🇳'], 'SAR' => ['name' => 'Saudi Riyal', 'symbol' => '﷼', 'flag' => '🇸🇦'], 'CHF' => ['name' => 'Swiss Franc', 'symbol' => 'Fr', 'flag' => '🇨🇭'], 'AUD' => ['name' => 'Australian Dollar', 'symbol' => 'A$', 'flag' => '🇦🇺'], ]; public static function init(): void { if (!Settings::get('currency_ticker_enabled', 1)) { return; } add_action('rankwell_after_breadcrumbs', [__CLASS__, 'render_ticker']); add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_assets']); } public static function enqueue_assets(): void { wp_enqueue_style( 'rw-currency-ticker', RANKWELL_NEWS_URI . '/assets/css/components/_currency-ticker.css', [], RANKWELL_NEWS_VERSION ); } private static function get_rates(): array { $cache_key = 'rankwell_currency_rates'; $rates = get_transient($cache_key); if (false !== $rates) { return $rates; } $base = Settings::get('currency_base', 'NGN'); $targets = Settings::get('currency_targets', 'USD,GBP,EUR'); $targets = str_replace(' ', '', strtoupper($targets)); $url = add_query_arg([ 'base' => $base, 'quotes' => $targets, ], self::API_URL . '/rates'); $response = wp_remote_get($url, ['timeout' => 10]); if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { return []; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body) || !is_array($body)) { return []; } // v2 returns array of {date, base, quote, rate} objects $rates = []; $date = ''; foreach ($body as $item) { if (empty($item['quote']) || !isset($item['rate'])) { continue; } // Convert: if 1 NGN = 0.00073 USD, show how many NGN per 1 USD $rates[$item['quote']] = (float) $item['rate']; if (empty($date) && !empty($item['date'])) { $date = $item['date']; } } if (empty($rates)) { return []; } $data = [ 'base' => $base, 'date' => $date, 'rates' => $rates, ]; set_transient($cache_key, $data, self::CACHE_DURATION); return $data; } public static function render_ticker(): void { $data = self::get_rates(); if (empty($data['rates'])) { return; } $base = $data['base']; $rates = $data['rates']; ?>
$rate): ?> $rate): ?>
$data) { $options[$code] = sprintf('%s %s (%s)', $data['flag'], $data['name'], $code); } return $options; } } ['name' => 'Nigerian Naira', 'symbol' => '₦', 'flag' => '🇳🇬'], 'USD' => ['name' => 'US Dollar', 'symbol' => '$', 'flag' => '🇺🇸'], 'GBP' => ['name' => 'British Pound', 'symbol' => '£', 'flag' => '🇬🇧'], 'EUR' => ['name' => 'Euro', 'symbol' => '€', 'flag' => '🇪🇺'], 'CAD' => ['name' => 'Canadian Dollar', 'symbol' => 'C$', 'flag' => '🇨🇦'], 'GHS' => ['name' => 'Ghanaian Cedi', 'symbol' => 'GH₵', 'flag' => '🇬🇭'], 'ZAR' => ['name' => 'South African Rand', 'symbol' => 'R', 'flag' => '🇿🇦'], 'KES' => ['name' => 'Kenyan Shilling', 'symbol' => 'KSh', 'flag' => '🇰🇪'], 'JPY' => ['name' => 'Japanese Yen', 'symbol' => '¥', 'flag' => '🇯🇵'], 'CNY' => ['name' => 'Chinese Yuan', 'symbol' => '¥', 'flag' => '🇨🇳'], 'AED' => ['name' => 'UAE Dirham', 'symbol' => 'د.إ', 'flag' => '🇦🇪'], 'INR' => ['name' => 'Indian Rupee', 'symbol' => '₹', 'flag' => '🇮🇳'], 'SAR' => ['name' => 'Saudi Riyal', 'symbol' => '﷼', 'flag' => '🇸🇦'], 'CHF' => ['name' => 'Swiss Franc', 'symbol' => 'Fr', 'flag' => '🇨🇭'], 'AUD' => ['name' => 'Australian Dollar', 'symbol' => 'A$', 'flag' => '🇦🇺'], ]; public static function init(): void { if (!Settings::get('currency_ticker_enabled', 1)) { return; } add_action('rankwell_after_breadcrumbs', [__CLASS__, 'render_ticker']); add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_assets']); } public static function enqueue_assets(): void { wp_enqueue_style( 'rw-currency-ticker', RANKWELL_NEWS_URI . '/assets/css/components/_currency-ticker.css', [], RANKWELL_NEWS_VERSION ); } private static function get_rates(): array { $cache_key = 'rankwell_currency_rates'; $rates = get_transient($cache_key); if (false !== $rates) { return $rates; } $base = Settings::get('currency_base', 'NGN'); $targets = Settings::get('currency_targets', 'USD,GBP,EUR'); $targets = str_replace(' ', '', strtoupper($targets)); $url = add_query_arg([ 'base' => $base, 'quotes' => $targets, ], self::API_URL . '/rates'); $response = wp_remote_get($url, ['timeout' => 10]); if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { return []; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body) || !is_array($body)) { return []; } // v2 returns array of {date, base, quote, rate} objects $rates = []; $date = ''; foreach ($body as $item) { if (empty($item['quote']) || !isset($item['rate'])) { continue; } // Convert: if 1 NGN = 0.00073 USD, show how many NGN per 1 USD $rates[$item['quote']] = (float) $item['rate']; if (empty($date) && !empty($item['date'])) { $date = $item['date']; } } if (empty($rates)) { return []; } $data = [ 'base' => $base, 'date' => $date, 'rates' => $rates, ]; set_transient($cache_key, $data, self::CACHE_DURATION); return $data; } public static function render_ticker(): void { $data = self::get_rates(); if (empty($data['rates'])) { return; } $base = $data['base']; $rates = $data['rates']; ?>
$rate): ?> $rate): ?>
$data) { $options[$code] = sprintf('%s %s (%s)', $data['flag'], $data['name'], $code); } return $options; } }