HEX
Server: Apache/2.4.65 (Debian)
System: Linux 88f31f35b0b8 6.1.0-38-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.147-1 (2025-08-02) x86_64
User: www-data (33)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/adnkronos-feed-importer/adnk-importer.php
<?php
/**
 * Plugin Name: AdnKronos Feed Importer
 * Plugin URI: https://adnkronos.com
 * Description: Import news from Feed AdnKronos
 * Version: 1.3.1
 * Author: Oimmei-AdnKronos
 * Author URI: https://adnkronos.com
 **/


if (!defined('ABSPATH')) exit; // Exit if accessed directly

// includo file di utitlity
include_once('inc/utility.php');
include_once('admin/option-panel.php');


function adnk_import_feed()
{
    /* get option from option page */
    $log_file = dirname(__FILE__) . '/logs/' . date('Ymd') . '_import.log';
    $orario = (new DateTime())->format('Y-m-d H:i:s');
    file_put_contents($log_file, $orario . " Inizio importazione\n", FILE_APPEND);
    sleep(random_int(0, 9));

    // Verifica se c'è un'importazione in corso
    $transient = get_transient('adnk_import_running');
    if (!(false === $transient)) {
        file_put_contents($log_file, $orario . " Importazione non lanciata perchè entro i 180s dalla precedente\n", FILE_APPEND);
        $url = wp_get_raw_referer();
        wp_redirect($url);
        return;
    } else {
        set_transient('adnk_import_running', 'running', 180);
        file_put_contents($log_file, $orario . " Avvio effettivo\n", FILE_APPEND);
    }

    $post_ids_array = [];

    // Opzioni generali
    $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');

    // Definizione dei feed disponibili
    $feeds = [
        'primapagina' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/PrimaPagina.xml?username=[username]&password=[password]',
        ],
        'newsregionali' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Regioni.xml?username=[username]&password=[password]',
        ],
        'salute' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Salute.xml?username=[username]&password=[password]',
        ],
        'lavoro' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Labitalia.xml?username=[username]&password=[password]',
        ],
        'sostenibilita' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Sostenibilita.xml?username=[username]&password=[password]',
        ],
        'toscana' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/RegToscana.xml?username=[username]&password=[password]',
        ],
        'comunicati' => [
            'url' => 'http://www.adnkronos.com/NewsFeed/Immediapress.xml?username=[username]&password=[password]',
        ],
        'motori' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Motori.xml?username=[username]&password=[password]',
        ],
        'sport' => [
            'url' => 'http://www.adnkronos.com/NewsFeed/Sport.xml?username=[username]&password=[password]',
        ],
        'fintech' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Fintech.xml?username=[username]&password=[password]',
        ],
        'tecnologia' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Tech&Games.xml?username=[username]&password=[password]',
        ],
        'wine' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Wine.xml?username=[username]&password=[password]',
        ],
        'facilitalia' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Facilitalia.xml?username=[username]&password=[password]',
        ],
        'finanza' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Finanza.xml?username=[username]&password=[password]',
        ],
        'fisco' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Fisco.xml?username=[username]&password=[password]',
        ],
        'demografica' => [
            'url' => 'https://demografica.adnkronos.com/feed/',
        ],
        'ultimora' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/Ultimora.xml?username=[username]&password=[password]',
        ],
        'video' => [
            'url' => 'https://www.adnkronos.com/NewsFeed/VideoVi.xml?username=[username]&password=[password]',
        ],
    ];

    $feed_url_array = [];
    $active_feeds = "";

    // Carica dinamicamente i feed attivi e le loro configurazioni
    foreach ($feeds as $feed_name => $feed_config) {
        // Verifica se il feed è attivo
        $feed_checked = get_adnk_importer_option('url_' . $feed_name . '_feed');

        if ($feed_checked == 'on') {
            $active_feeds .= " " . ucfirst($feed_name) . ",";

            // Aggiunge il feed all'array dei feed attivi
            $feed_url_array[$feed_name] = [
                'url' => $feed_config['url'],
                'status' => esc_html(get_adnk_importer_option('selected_' . $feed_name . '_post_status')),
                'owner' => esc_html(get_adnk_importer_option('selected_' . $feed_name . '_post_owner')),
                'category' => esc_html(get_adnk_importer_option('selected_' . $feed_name . '_feed_category')),
                'username' => esc_html(get_adnk_importer_option('url_' . $feed_name . '_feed_username')),
                'password' => esc_html(get_adnk_importer_option('url_' . $feed_name . '_feed_password'))
            ];
        }
    }

    // Registra nel log i feed attivi
    file_put_contents($log_file, $active_feeds . $orario . "\n", FILE_APPEND);

    // Processa ciascun feed attivo
    foreach ($feed_url_array as $key => $feed_argument) {

        $feed_url = str_replace([
            '[username]',
            '[password]',
        ], [
            $feed_argument['username'] ?: 'pluginadnk',
            $feed_argument['password'] ?: 'pl8g1n45tg',
        ], $feed_argument['url']);

        file_put_contents($log_file, $orario . "\n\n\nApro feed: " . $feed_url . "\n\tCategory:" . $feed_argument['category'] . "\n", FILE_APPEND);

        $post_feed_result_array = adnki_xml2array(adnki_feed2xml($feed_url));

        // Verifica che i dati del feed siano validi prima di procedere
        if (!isset($post_feed_result_array['channel']['item'])) {
            file_put_contents($log_file, $orario . " Errore: feed non valido o vuoto\n", FILE_APPEND);
            continue;
        }

        // Processa ogni articolo del feed
        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,
                $selected_image_owner_email
            );

            if (!is_null($new_post_id)) {
                $post_ids_array[] = $new_post_id;
            }
        }
    }

    // Rilascia il transient per permettere future importazioni
    delete_transient('adnk_import_running');
    file_put_contents($log_file, $orario . " Importazione completata\n", 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'] = [
        'interval' => 1800,
        'display' => __('halfhourly')
    ];

    $schedules['twohourly'] = [
        'interval' => 7200,
        'display' => __('twohourly')
    ];

    $schedules['fourhourly'] = [
        'interval' => 14400,
        'display' => __('fourhourly')
    ];

    $schedules['sixhourly'] = [
        'interval' => 21600,
        'display' => __('sixhourly')
    ];

    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 = date("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');