function fetch_feed_content($url) {
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return false;
}
return wp_remote_retrieve_body($response);
}
function ae_update_news_ajax() {
check_ajax_referer('ae_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_die('Unauthorized user');
}
$selected_feed = sanitize_text_field($_POST['feeds']);
if ($selected_feed == 'all') {
$feeds = get_option('ae_feeds', array());
$feed_urls = array_values(array_column($feeds, 'url'));
} else {
$feed_urls = array($selected_feed);
}
if (empty($feed_urls)) {
$response = array('error' => 'Nenhum feed selecionado.');
echo json_encode($response);
wp_die();
}
$feed_data = array();
$now = new DateTime();
foreach ($feed_urls as $feed_url) {
$feed_content = fetch_feed_content($feed_url);
if ($feed_content === false) {
$response = array('error' => 'Não foi possível acessar o feed: ' . esc_url($feed_url));
echo json_encode($response);
wp_die();
}
$feed_xml = simplexml_load_string($feed_content);
if ($feed_xml === false) {
$response = array('error' => 'Erro ao processar o XML do feed: ' . esc_url($feed_url));
echo json_encode($response);
wp_die();
}
foreach ($feed_xml->channel->item as $item) {
$pubDate = new DateTime((string) $item->pubDate);
$interval = $now->diff($pubDate);
if ($interval->days >= 1) {
continue;
}
$title = sanitize_text_field((string) $item->title);
$description = wp_kses_post((string) $item->description);
$source = sanitize_text_field((string) $feed_xml->channel->title);
$date = $pubDate->format('Y-m-d H:i:s');
$feed_data[] = array(
'title' => $title,
'description' => $description,
'source' => $source,
'date' => $date
);
}
}
$collected_posts = get_option('ae_collected_posts', array());
$titles = array_column($collected_posts, 'title');
foreach ($feed_data as $data) {
if (!in_array($data['title'], $titles)) {
$collected_posts[] = $data;
}
}
usort($collected_posts, function($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
});
update_option('ae_collected_posts', $collected_posts);
if (empty($feed_data)) {
$response = array(
'progress' => 100,
'message' => 'Nenhuma nova notícia encontrada.',
'new_results' => $collected_posts,
'finished' => true
);
} else {
$response = array(
'progress' => 100,
'message' => 'Atualização concluída!',
'new_results' => $collected_posts,
'finished' => true
);
}
echo json_encode($response);
wp_die();
}
add_action('wp_ajax_ae_update_news_ajax', 'ae_update_news_ajax');
function fetch_and_save_news_ajax() {
check_ajax_referer('ae_nonce', 'security');
if (!current_user_can('manage_options')) {
wp_send_json_error('Você não tem permissão para realizar esta ação.');
}
$feed_name = sanitize_text_field($_POST['feed_name']);
$feed_url = esc_url_raw($_POST['feed_url']);
$use_news_api = sanitize_text_field($_POST['use_news_api']);
if (empty($feed_name) || empty($feed_url)) {
wp_send_json_error('Nome do feed e URL são obrigatórios.');
}
$collected_posts = fetch_and_save_news($feed_name, $feed_url, $use_news_api);
if ($collected_posts) {
wp_send_json_success($collected_posts);
} else {
wp_send_json_error('Erro ao coletar notícias. Verifique a URL do feed ou a chave API.');
}
}
add_action('wp_ajax_fetch_and_save_news_ajax', 'fetch_and_save_news_ajax');
function fetch_and_save_news($feed_name, $feed_url, $use_news_api) {
$articles = array();
if ($use_news_api) {
$articles = fetch_news_from_api($feed_name);
} else {
$feed_content = fetch_feed_content($feed_url);
if ($feed_content) {
$feed_xml = simplexml_load_string($feed_content);
foreach ($feed_xml->channel->item as $item) {
$articles[] = array(
'title' => sanitize_text_field($item->title),
'description' => wp_kses_post($item->description),
'url' => esc_url($item->link),
'date' => date('Y-m-d H:i:s', strtotime($item->pubDate))
);
}
} else {
return false;
}
}
$collected_posts = get_option('ae_collected_posts', array());
$titles = array_column($collected_posts, 'title');
foreach ($articles as $article) {
if (!in_array($article['title'], $titles)) {
$collected_posts[] = $article;
}
}
usort($collected_posts, function($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
});
update_option('ae_collected_posts', $collected_posts);
return $collected_posts;
}
function fetch_news_from_api($feed_name) {
$api_key = get_option('news_api_key', '');
if (empty($api_key)) {
return false;
}
$url = 'https://newsapi.org/v2/everything?' . http_build_query(array(
'q' => $feed_name,
'apiKey' => $api_key
));
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (empty($data['articles'])) {
return array();
}
$articles = array();
foreach ($data['articles'] as $article) {
$articles[] = array(
'title' => sanitize_text_field($article['title']),
'description' => wp_kses_post($article['description']),
'url' => esc_url($article['url']),
'date' => date('Y-m-d H:i:s', strtotime($article['publishedAt']))
);
}
return $articles;
}
Em Piracicaba, Terminal Central de Integração (TCI) passara por modernização – PIRANOT
No Result
View All Result
O Prefeito Barjas Negri anunciou neste tarde de segunda-feira (18), a abertura de licitação para reforma no Terminal Central de Integração (TCI), no centro de Piracicaba (SP).
A reforma é para modernizar o terminal, visto que o local é onde se tem o maior volume de pessoas. A previsão é que as obras cheguem ao fim no começo de 2020.
Assista abaixo o vídeo: APP DO PIRANOT – Baixe nosso aplicativo de notícias e receba as informações mais importantes de Piracicaba na tela do seu celular. Clique aqui e saiba mais.
Obrigado por ler este post!
No Result
View All Result
© 2023/2024. Todos os direitos reservados para a Empresa Júnior Cardoso LTDA ME.