Ładowanie...

krupapiotr.pl

Technologia, na której można polegać

wymagania

/** * ========================================================================== * KRUPAS CORE V56.2.7 - THE WEATHER CACHE & API SYNC ENGINE (CORE 2) * ========================================================================== * OPIS: Autonomiczny silnik pobierania danych z OpenWeatherMap API dla Nasutowa. * Zasilający bezpośrednio CORE 1 poprzez bezpieczne zarządzanie pamięcią podręczną * (Transients). Zapobiega timeoutom i nie wymaga zmian w kodzie CORE 1. * CACHING: Wydłużony do 1 godziny (3600s), chroniąc przed blokadami API. * BEZPIECZEŃSTWO: Brak literalnych sekwencji nowej linii (\n) w kodzie. * ========================================================================== */ if (!defined('ABSPATH')) exit; // Automatyczne zerowanie starego, wadliwego cache przy wdrożeniu nowego kodu if (!get_option('krupa_weather_v56_initialized')) { delete_transient('krupa_current_weather_data'); delete_transient('krupa_weather_transient_data'); update_option('krupa_weather_v56_initialized', time()); } if (!function_exists('krupa_weather_api_sync_engine')) { function krupa_weather_api_sync_engine() { // Parametry identyczne z konfiguracją Twojego CORE 1 $api_key = '3fce60aeedeb0edf258f9d208efa7f29'; $lat = '51.3556'; $lon = '22.5694'; // Opcjonalny wymuszony reset przez dopisanie ?refresh_weather=1 w URL if (isset($_GET['refresh_weather']) && current_user_can('manage_options')) { delete_transient('krupa_current_weather_data'); delete_transient('krupa_weather_transient_data'); } // 1. Synchronizacja bieżącej pogody (Current Weather) $cc = get_transient('krupa_current_weather_data'); if (false === $cc || !is_array($cc) || empty($cc['weather'])) { $url_current = "https://api.openweathermap.org/data/2.5/weather?lat={$lat}&lon={$lon}&appid={$api_key}&units=metric&lang=pl"; $res_current = wp_remote_get($url_current, ['timeout' => 10]); if (!is_wp_error($res_current)) { $body = wp_remote_retrieve_body($res_current); $data = json_decode($body, true); if (is_array($data) && isset($data['weather'])) { // Zapisujemy czyste dane na 1 godzinę (3600 sekund) set_transient('krupa_current_weather_data', $data, 3600); } } } // 2. Synchronizacja prognozy (Forecast - w tym pogoda na jutro) $fj = get_transient('krupa_weather_transient_data'); if (false === $fj || !is_array($fj) || empty($fj['list'])) { $url_forecast = "https://api.openweathermap.org/data/2.5/forecast?lat={$lat}&lon={$lon}&appid={$api_key}&units=metric&lang=pl"; $res_forecast = wp_remote_get($url_forecast, ['timeout' => 10]); if (!is_wp_error($res_forecast)) { $body = wp_remote_retrieve_body($res_forecast); $data = json_decode($body, true); if (is_array($data) && isset($data['list'])) { // Zapisujemy czyste dane na 1 godzinę (3600 sekund) set_transient('krupa_weather_transient_data', $data, 3600); } } } } } // Podpięcie silnika bardzo wcześnie pod proces inicjalizacji WordPressa (przed AJAX) add_action('init', 'krupa_weather_api_sync_engine', 5);