File: /var/www/html/wp-content/plugins/adnkronos/adnk-importer.php
<?php
/**
* Plugin Name: AdnKronos Feed Importer 2
* Plugin URI: https://adnkronos.com
* Description: Import news from Feed AdnKronos
* Version: 2.1.1
* Author: Oimmei AdnKronos
* Author URI: https://adnkronos.com
* License: GPLv2 or later
**/
if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly
// includo file di utitlity
include_once('inc/utility.php');
include_once('admin/option-panel.php');
include_once('admin/log-panel.php');
if (!defined('ADNK_API_BASE_URL')) {
define('ADNK_API_BASE_URL', 'https://pluginv2.adnkronos.com');
}
function adnk_import_feed_by_button()
{
adnk_import_feed();
wp_redirect(site_url() . '/wp-admin/edit.php');
}
function adnk_import_feed()
{
$post_ids_array = [];
try {
$inizio = new DateTime();
/* get option from option page */
$log_file = dirname(__FILE__) . '/logs/' . gmdate('Ymd') . '_import.log';
$orario = (new DateTime())->format('Y-m-d H:i:s');
file_put_contents($log_file, $orario . " Inizio importazione\n", FILE_APPEND);
$transient = get_transient('adnk_import_running');
if (!(false === $transient)) {
file_put_contents($log_file,
$orario . " Importazione non lanciata perchè entro i 90s dalla precedente\n",
FILE_APPEND);
$url = wp_get_raw_referer();
wp_redirect($url);
return;
} else {
set_transient('adnk_import_running', 'running', 90);
file_put_contents($log_file, $orario . " Avvio effettivo\n", FILE_APPEND);
}
$selected_post_owner_email = get_adnk_importer_option('selected_post_owner');
$selected_image_owner_email = get_adnk_importer_option('selected_image_owner');
$check_no_image_import = get_adnk_importer_option('check_no_image_import');
$consent_send_statistical_data = get_adnk_settings_option('consent_send_statistical_data');
$feeds = adnk_feed_by_site();
if ($feeds === false) {
file_put_contents($log_file,
$orario . " Importazione non possibile per problema chiamata elenco feed (codice 63)\n",
FILE_APPEND);
$url = wp_get_raw_referer();
wp_redirect($url);
return;
}
$feed_url_array = array();
foreach ($feeds as $feed) {
$checkOptionCode = 'url_' . $feed['code'] . '_feed';
$checkOptionValue = get_adnk_importer_option($checkOptionCode);
$categoryOptionCode = 'selected_' . $feed['code'] . '_feed_category';
$categoryOptionValue = get_adnk_importer_option($categoryOptionCode);
$statusOptionCode = 'selected_' . $feed['code'] . '_post_status';
$statusOptionValue = get_adnk_importer_option($statusOptionCode);
$ownerOptionCode = 'selected_' . $feed['code'] . '_post_owner';
$ownerOptionValue = get_adnk_importer_option($ownerOptionCode);
if ($checkOptionValue == 'on') {
$feed_url_array[$feed['code']]['name'] = "{$feed['name']} - {$feed['link']}";
$feed_url_array[$feed['code']]['url'] = $feed['link'];
$feed_url_array[$feed['code']]['status'] = $statusOptionValue;
$feed_url_array[$feed['code']]['category'] = $categoryOptionValue;
$feed_url_array[$feed['code']]['owner'] = $ownerOptionValue;
$feed_url_array[$feed['code']]['youtube'] = $feed['youtube'];
}
}
$feedJson = json_encode($feed_url_array);
foreach ($feed_url_array as $key => $feed_argument) {
$orario = (new DateTime())->format('Y-m-d H:i:s');
file_put_contents($log_file, "\n" . $orario . " Letture {$feed_argument['name']}\n", FILE_APPEND);
$xml_string = adnki_feed2string($feed_argument['url']);
/* creare i post della categoria giusta e nello stato selezionato */
//file_put_contents($log_file, $orario." Ricevuto feed:\n ".json_encode($post_feed_result_array)."\n", FILE_APPEND);
if ($feed_argument['youtube']) {
$post_feed_result_array = youtube_xml_to_array($xml_string);
//video
foreach ($post_feed_result_array['entries'] as $videopost) {
// guid creare custom field e controllare se già esiste prima di importarlo
$new_post_id = adnk_create_video_post_from_result(
$videopost,
$feed_argument['category'],
$feed_argument['status'],
$feed_argument['owner'] ?: $selected_post_owner_email,
$key,
$check_no_image_import,
$feed_argument['owner'] ?: $selected_image_owner_email
);
if (!is_null($new_post_id)) {
$post_ids_array[] = $new_post_id;
}
}
} else {
$post_feed_result_array = adnki_feed2xml($feed_argument['url']);
//non video
foreach ($post_feed_result_array->channel->item as $post) {
// guid creare custom field e controllare se già esiste prima di importarlo
$new_post_id = adnk_create_post_from_result($post,
$feed_argument['category'],
$feed_argument['status'],
$feed_argument['owner'] ?: $selected_post_owner_email,
$key,
$check_no_image_import,
$feed_argument['owner'] ?: $selected_image_owner_email
);
if (!is_null($new_post_id)) {
$post_ids_array[] = $new_post_id;
}
//error_log($new_post_id);
}
}
}
$fine = new DateTime();
$orario = $fine->format('Y-m-d H:i:s');
$secondi = $fine->getTimestamp() - $inizio->getTimestamp();
file_put_contents($log_file, $orario . " Fine importazione ({$secondi} secondi)\n", FILE_APPEND);
//adnk_eventual_consistency($post_ids_array);
//wp_redirect( site_url() . '/wp-admin/edit.php' );
} catch (Exception $e) {
file_put_contents($log_file, $orario . " Eccezione: \n" . $e->getMessage(), FILE_APPEND);
}
return $post_ids_array;
}
/* set cron job */
add_filter('cron_schedules', 'adnk_import_cron_schedule');
function adnk_import_cron_schedule($schedules)
{
$schedules['halfhourly'] = array(
'interval' => 1800,
'display' => __('halfhourly', 'adnkronos'),
);
$schedules['twohourly'] = array(
'interval' => 7200,
'display' => __('twohourly', 'adnkronos'),
);
$schedules['fourhourly'] = array(
'interval' => 14400,
'display' => __('fourhourly', 'adnkronos'),
);
$schedules['sixhourly'] = array(
'interval' => 21600,
'display' => __('sixhourly', 'adnkronos'),
);
return $schedules;
}
register_activation_hook(__FILE__, 'adnk_import_activation');
function adnk_import_activation($selected_freq_import = 'daily')
{
wp_clear_scheduled_hook('adnk_import_event');
if (!wp_next_scheduled('adnk_import_event')) {
$date = gmdate("h:i:sa", strtotime('+5 minutes'));
wp_schedule_event(strtotime($date), $selected_freq_import, 'adnk_import_event');
}
/* cron invio dati statistici */
// Schedule an action if it's not already scheduled
if (!wp_next_scheduled('adki_add_cron_onceaday')) {
wp_schedule_event(time(), 'daily', 'adki_add_cron_onceaday');
}
}
add_action('adnk_import_event', 'adnk_import_feed');