$bing_api_key ); $response = wp_remote_get($url, array('headers' => $headers)); if (is_wp_error($response)) { return []; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); return $data['value'] ?? []; // Возвращает массив статей } // Функция для получения новостей через Google Custom Search JSON API function ai_content_wizard_fetch_news_from_google($query, $google_api_key) { $cx = ai_content_wizard_get_setting('ai_content_wizard_google_cse_id', ''); // Получение CSE ID из настроек if (empty($cx)) { return []; // Если CSE ID не настроен, возвращаем пустой массив } $url = "https://www.googleapis.com/customsearch/v1?q=" . urlencode($query) . "&cx={$cx}&key={$google_api_key}"; $response = wp_remote_get($url); if (is_wp_error($response)) { return []; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); return $data['items'] ?? []; // Возвращает массив результатов поиска } // Функция для получения новостей через RSS сервис function ai_content_wizard_fetch_news_from_rss($rss_feed_url) { $response = wp_remote_get($rss_feed_url); if (is_wp_error($response)) { error_log('Ошибка при получении RSS ленты: ' . $response->get_error_message()); return []; } $body = wp_remote_retrieve_body($response); $rss_items = simplexml_load_string($body); $news = []; if(!$rss_items) { error_log('Ошибка при обработке RSS ленты: не удалось преобразовать XML'); return []; } foreach ($rss_items->channel->item as $item) { if(count($news) >= 50) break; // Ограничиваем количество новостей до 10 $news[] = [ 'title' => (string) $item->title, 'link' => (string) $item->link, 'description' => (string) $item->description, 'pubDate' => (string) $item->pubDate ]; } return $news; } // Общая функция для выбора метода получения новостей function ai_content_wizard_fetch_latest_news($query) { $bing_api_key = ai_content_wizard_get_setting('ai_content_wizard_bing_api_key'); $google_api_key = ai_content_wizard_get_setting('ai_content_wizard_google_api_key'); // $free_service_key = ai_content_wizard_get_setting('ai_content_wizard_free_service_key'); // Если у вас есть ключ для бесплатного сервиса if (!empty($bing_api_key)) { return ai_content_wizard_fetch_news_from_bing($query, $bing_api_key); } elseif (!empty($google_api_key)) { return ai_content_wizard_fetch_news_from_google($query, $google_api_key); } else { return ai_content_wizard_fetch_news_from_rss(); // Использование RSS-ленты } }