function wc_currency_get_prices() { $api_all = trim((string) get_option('wc_currency_api_all', '')); $symbols = explode(',', get_option('wc_currency_symbols', 'USD,AED')); $minutes = max(1, intval(get_option('wc_currency_cache_minutes', 30))); $cache_key = 'wc_currency_prices_cache'; $cached = get_transient($cache_key); if ($cached && is_array($cached)) { return $cached; } $prices = []; $resp = wp_remote_get($api_all, ['timeout' => 15]); if (is_wp_error($resp)) { error_log('API ERROR: ' . $resp->get_error_message()); return []; } $body = wp_remote_retrieve_body($resp); error_log('RAW BODY: ' . $body); $data = json_decode($body, true); error_log('DECODED DATA: ' . print_r($data, true)); // حالت آبجکت تکی if (isset($data['symbol'])) { $sym = strtoupper($data['symbol']); $prices[strtolower($sym)] = $data['price'] . ' ' . $data['unit']; } // حالت آرایه از چند آبجکت elseif (is_array($data) && isset($data[0])) { foreach ($symbols as $sym) { $sym = strtoupper(trim($sym)); $found = 'N/A'; foreach ($data as $item) { if (isset($item['symbol']) && strtoupper($item['symbol']) === $sym) { $found = $item['price'] . ' ' . $item['unit']; break; } } $prices[strtolower($sym)] = $found; } } error_log('FINAL PRICES: ' . print_r($prices, true)); set_transient($cache_key, $prices, $minutes * MINUTE_IN_SECONDS); return $prices; }

