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/admin/option-panel.php
<?php
/*
Admin Panel Option
*/

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

// Start Class
if (!class_exists('adnk_importer_Options')) {

    class adnk_importer_Options
    {

        /**
         * Start things up
         *
         * @since 1.0.0
         */
        public function __construct()
        {

            // We only need to register the admin panel on the back-end
            if (is_admin()) {
                add_action('admin_menu', array('adnk_importer_Options', 'add_admin_menu'));
                add_action('admin_init', array('adnk_importer_Options', 'register_settings'));
            }

        }

        /**
         * Returns all adnk_importer options
         *
         * @since 1.0.0
         */
        public static function get_adnk_importer_options()
        {
            return get_option('adnk_importer_options');
        }

        /**
         * Returns single adnk_importer option
         *
         * @since 1.0.0
         */
        public static function get_adnk_importer_option($id)
        {
            $options = self::get_adnk_importer_options();
            if (isset($options[$id])) {
                return $options[$id];
            }
        }

        /**
         * Add sub menu page
         *
         * @since 1.0.0
         */
        public static function add_admin_menu()
        {
            add_menu_page(
                esc_html__('AdnKronos', 'adnk_importer'),
                esc_html__('AdnKronos', 'adnk_importer'),
                'manage_options',
                'adnk-plugin-settings',
                array('adnk_importer_Options', 'create_admin_page')
            );

            // Sottomenu per i log
            add_menu_page(
                'Log Viewer', // Titolo pagina
                'Log Viewer', // Testo menu
                'manage_options', // Permessi
                'adnk-importer-logs', // Slug menu
                array('adnk_importer_Options', 'adnk_importer_logs_page') // Funzione callback
            );
        }

        /**
         * Register a setting and its sanitization callback.
         *
         * We are only registering 1 setting so we can store all options in a single option as
         * an array. You could, however, register a new setting for each option
         *
         * @since 1.0.0
         */
        public static function register_settings()
        {
            register_setting('adnk_importer_options', 'adnk_importer_options', array('adnk_importer_Options', 'sanitize'));
        }

        /**
         * Sanitization callback
         *
         * @since 1.0.0
         */
        public static function sanitize($options)
        {
            //Get the active tab from the $_GET param
            $default_tab = null;
            $tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : $default_tab;

            // If we have options lets sanitize them
            if ($options) {
                if ($tab === null) {
                    // Checkbox consent_send_statistical_data
                    if (!empty($options['consent_send_statistical_data'])) {
                        $options['consent_send_statistical_data'] = 'on';
                    } else {
                        unset($options['consent_send_statistical_data']); // Remove from options if not checked
                    }
                } elseif ($tab === 'settings') {
                    // Select selected_post_owner
                    if (!empty($options['selected_post_owner'])) {
                        $options['selected_post_owner'] = sanitize_text_field($options['selected_post_owner']);
                    }

                    // frenquenza import
                    if (!empty($options['selected_freq_import'])) {
                        $options['selected_freq_import'] = sanitize_text_field($options['selected_freq_import']);
                    }

                    // Process all feeds - this is the corrected section
                    $feeds = [
                        'ultimora', 'primapagina', 'newsregionali', 'salute', 'lavoro', 'sostenibilita', 'toscana',
                        'comunicati', 'motori', 'fintech', 'facilitalia', 'finanza', 'tecnologia', 'wine', 'sport',
                        'fisco', 'demografica',
                    ];

                    foreach ($feeds as $feed) {
                        // Checkbox feed
                        if (!empty($options['url_' . $feed . '_feed'])) {
                            $options['url_' . $feed . '_feed'] = 'on';
                        } else {
                            unset($options['url_' . $feed . '_feed']); // Remove from options if not checked
                        }

                        // Feed category
                        if (!empty($options['selected_' . $feed . '_feed_category'])) {
                            $options['selected_' . $feed . '_feed_category'] = sanitize_text_field($options['selected_' . $feed . '_feed_category']);
                        }

                        // Feed post status
                        if (!empty($options['selected_' . $feed . '_post_status'])) {
                            $options['selected_' . $feed . '_post_status'] = sanitize_text_field($options['selected_' . $feed . '_post_status']);
                        }

                        // Feed post owner
                        if (!empty($options['selected_' . $feed . '_post_owner'])) {
                            $options['selected_' . $feed . '_post_owner'] = sanitize_text_field($options['selected_' . $feed . '_post_owner']);
                        }

                        // Feed username - NEW: Properly save username
                        if (isset($options['url_' . $feed . '_feed_username'])) {
                            $options['url_' . $feed . '_feed_username'] = sanitize_text_field($options['url_' . $feed . '_feed_username']);
                        }

                        // Feed password - NEW: Properly save password
                        if (isset($options['url_' . $feed . '_feed_password'])) {
                            $options['url_' . $feed . '_feed_password'] = sanitize_text_field($options['url_' . $feed . '_feed_password']);
                        }
                    }

                    // Also handle video feed separately as it has a different structure
                    if (!empty($options['url_video_feed'])) {
                        $options['url_video_feed'] = 'on';
                    } else {
                        unset($options['url_video_feed']);
                    }

                    if (!empty($options['selected_video_feed_category'])) {
                        $options['selected_video_feed_category'] = sanitize_text_field($options['selected_video_feed_category']);
                    }

                    if (!empty($options['selected_video_post_status'])) {
                        $options['selected_video_post_status'] = sanitize_text_field($options['selected_video_post_status']);
                    }
                }
            }

            return $options;
        }

        /**
         * Settings page output
         *
         * @since 1.0.0
         */
        public static function create_admin_page()
        {
            // Verifica l'utente registrato
            $regist_user = adnk_verify_site();

            // Determina se i campi devono essere disabilitati
            $field_disabled = (get_adnk_settings_option('consent_send_statistical_data') == 'on' || $regist_user) ? '' : 'disabled';

            // Imposta cron event per importazione
            $selected_freq_import = get_adnk_importer_option('selected_freq_import');
            adnk_import_activation($selected_freq_import);

            // Ottiene le categorie per i menu a discesa
            $categories = get_categories([
                "hide_empty" => 0,
                "type" => "post",
                "orderby" => "name",
                "order" => "ASC"
            ]);

            // Ottiene gli utenti per i menu a discesa
            $users = get_users(['fields' => ['user_email', 'user_login']]);

            // URL del logo
            $logo_url = plugin_dir_url(__FILE__) . "img/logoadnkronos.jpg";
            $logo_sidebar = plugin_dir_url(__FILE__) . "img/logo-adnkronos.svg";

            // Array dei feed disponibili
            $feeds = [
                'ultimora' => ['label' => "Ultim'ora", 'disabled' => false],
                'primapagina' => ['label' => 'Prima Pagina', 'disabled' => $field_disabled],
                'newsregionali' => ['label' => 'News Regionali', 'disabled' => $field_disabled],
                'salute' => ['label' => 'Salute', 'disabled' => $field_disabled],
                'lavoro' => ['label' => 'Lavoro', 'disabled' => $field_disabled],
                'sostenibilita' => ['label' => 'Sostenibilita', 'disabled' => $field_disabled],
                'comunicati' => ['label' => 'Comunicati', 'disabled' => $field_disabled],
                'motori' => ['label' => 'Motori', 'disabled' => $field_disabled],
                'fintech' => ['label' => 'Fintech', 'disabled' => $field_disabled],
                'tecnologia' => ['label' => 'Tecnologia', 'disabled' => $field_disabled],
                'wine' => ['label' => 'Wine', 'disabled' => $field_disabled],
                'toscana' => ['label' => 'Regione Toscana', 'disabled' => $field_disabled],
                'sport' => ['label' => 'Sport', 'disabled' => false],
                'facilitalia' => ['label' => 'Facilitalia', 'disabled' => false],
                'finanza' => ['label' => 'Finanza', 'disabled' => false],
                'fisco' => ['label' => 'Fisco', 'disabled' => false],
                'demografica' => ['label' => 'Demografica', 'disabled' => false]
            ];

            // Frequenze di importazione
            $import_frequencies = [
                'halfhourly' => 'Una volta ogni 30 minuti',
                'hourly' => 'Una volta ogni ora',
                'twohourly' => 'Una volta ogni due ore',
                'fourhourly' => 'Una volta ogni quattro ore',
                'sixhourly' => 'Una volta ogni sei ore',
                'twicedaily' => 'Ogni 12 ore',
                'daily' => 'Una volta al giorno'
            ];

            // Stati di pubblicazione
            $post_statuses = [
                'publish' => 'Pubblicato',
                'draft' => 'Bozza'
            ];
            ?>

            <div id="adk">
                <div class="container wrap">
                    <h1>
                        <img src="<?php echo esc_url($logo_url); ?>"><?php esc_html_e('AdnKronos Feed Importer Options', 'adnk_importer'); ?>
                    </h1>
                    <p></p>

                    <nav class="nav-tab-wrapper">
                        <a href="?page=adnk-plugin-settings"
                           class="nav-tab nav-tab-active"><?php esc_html_e('Impostazioni di Importazione', 'adnk_importer'); ?></a>
                        <a href="?page=adnk-plugin-account"
                           class="nav-tab"><?php esc_html_e('Abilitazione dominio', 'adnk_importer'); ?></a>
                        <a href="?page=adnk-plugin-log"
                           class="nav-tab"><?php esc_html_e('Log importazioni', 'adnk_importer'); ?></a>
                    </nav>

                    <div class="tab-content">
                        <form method="post" action="options.php" novalidate>
                            <div class="row">
                                <div class="col-9">
                                    <?php settings_fields('adnk_importer_options'); ?>

                                    <!-- Sezione Proprietari -->
                                    <div class="card card-static mt-4">
                                        <div class="card-header">
                                            <strong><?php esc_html_e('Utente di destinazione di articoli e foto', 'adnk_importer'); ?></strong>
                                        </div>
                                        <div class="card-body">
                                            <p>Selezionare l'utente che risulterà proprietario degli articoli dopo
                                                l'importazione.</p>
                                            <?php
                                            $selected_post_owner = self::get_adnk_importer_option('selected_post_owner');
                                            ?>
                                            <select required id="selected_post_owner"
                                                    name="adnk_importer_options[selected_post_owner]">
                                                <option value=""><?php esc_html_e('Seleziona proprietario del post', 'adnk_importer') ?></option>
                                                <?php foreach ($users as $user): ?>
                                                    <option value="<?php echo esc_attr($user->user_email); ?>" <?php selected($user->user_email, $selected_post_owner); ?>>
                                                        <?php echo esc_html($user->user_login) . ' - ' . esc_html($user->user_email); ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        <div class="card-body">
                                            <p>
                                                Selezionare l'utente che risulterà proprietario delle immagini dopo
                                                l'importazione.<br/>
                                                Può essere diverso dal proprietario del post.
                                            </p>
                                            <p>
                                                <?php $check_no_image = self::get_adnk_importer_option('check_no_image_import'); ?>
                                                <input type="checkbox"
                                                       name="adnk_importer_options[check_no_image_import]" <?php checked($check_no_image, 'on'); ?>>
                                                <?php esc_html_e('Non importare le immagini', 'adnk_importer'); ?>
                                            </p>
                                            <?php
                                            $selected_image_owner = self::get_adnk_importer_option('selected_image_owner');
                                            ?>
                                            <select required id="selected_image_owner"
                                                    name="adnk_importer_options[selected_image_owner]">
                                                <option value=""><?php esc_html_e('Seleziona proprietario delle immagini', 'adnk_importer') ?></option>
                                                <?php foreach ($users as $user): ?>
                                                    <option value="<?php echo esc_attr($user->user_email); ?>" <?php selected($user->user_email, $selected_image_owner); ?>>
                                                        <?php echo esc_html($user->user_login) . ' - ' . esc_html($user->user_email); ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>

                                    <!-- Sezione Intervallo Import -->
                                    <div class="card card-static mt-4">
                                        <div class="card-header">
                                            <strong><?php esc_html_e('Intervallo di Import articoli', 'adnk_importer'); ?></strong>
                                        </div>
                                        <div class="card-body">
                                            <p>
                                                Selezionare l'intervallo temporale di importazione automatica degli
                                                articoli.
                                                E' possibile avviare una importazione immediata con il pulsante "Importa
                                                Ora".
                                            </p>
                                            <div class="row">
                                                <div class="col-9">
                                                    <?php $current_freq = self::get_adnk_importer_option('selected_freq_import'); ?>
                                                    <select id="selected_freq_import"
                                                            name="adnk_importer_options[selected_freq_import]">
                                                        <?php foreach ($import_frequencies as $value => $label): ?>
                                                            <option value="<?php echo esc_attr($value); ?>" <?php selected($value, $current_freq); ?>>
                                                                <?php esc_html_e($label, 'adnk_importer') ?>
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                    <span class="message"></span>
                                                </div>
                                                <div class="col-3">
                                                    <a href="<?php echo esc_url(site_url()) ?>/wp-admin/admin.php?action=adnk_import_now"
                                                       style="margin-bottom: 5px;" class="button button-primary">Importa
                                                        Ora</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-3">
                                    <div class="card card-static border-primary mt-4">
                                        <div class="card-body">
                                            <p class="card-text">
                                                <img width="200px" src="<?php echo esc_url($logo_sidebar); ?>">
                                                <br/><br/>
                                                <b>ROMA</b> Piazza Mastai n.9 - 00153<br/>
                                                T: +39 06 5807666 <br/> F: +39 06 5807815<br/>
                                                <br/>
                                                <b>MILANO</b> Via Manin, 37 - 20121<br/>
                                                T: +39 02 763661
                                            </p>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <!-- Sezione Feed Disponibili -->
                                    <div class="card card-static">
                                        <div class="card-header">
                                            Feed disponibili
                                        </div>
                                        <div class="card-body">
                                            <p>
                                                E' possibile per ogni feed rilasciato da AdnKronos selezionare la
                                                categoria di
                                                destinazione ed il relativo stato di pubblicazione.<br/>
                                                Per abilitare l'accesso a tutti i feed è necessario registrare il tuo
                                                dominio.
                                            </p>
                                            <table class="form-table wpex-custom-admin-login-table">
                                                <tr valign="top">
                                                    <td>
                                                        <strong><?php esc_html_e('Feed da importare', 'adnk_importer'); ?></strong>
                                                    </td>
                                                    <td>
                                                        <strong><?php esc_html_e('Categoria di destinazione', 'adnk_importer'); ?></strong>
                                                    </td>
                                                    <td>
                                                        <strong><?php esc_html_e('Stato di destinazione', 'adnk_importer'); ?></strong>
                                                    </td>
                                                    <td><strong><?php esc_html_e('Autore', 'adnk_importer'); ?></strong>
                                                    </td>
                                                </tr>

                                                <?php
                                                // Genera le righe per tutti i feed standard
                                                foreach ($feeds as $feed_key => $feed_info):
                                                    $feed_enabled = self::get_adnk_importer_option("url_{$feed_key}_feed");
                                                    $feed_category = self::get_adnk_importer_option("selected_{$feed_key}_feed_category");
                                                    $feed_status = self::get_adnk_importer_option("selected_{$feed_key}_post_status");
                                                    $feed_owner = self::get_adnk_importer_option("selected_{$feed_key}_post_owner");
                                                    $feed_username = self::get_adnk_importer_option("url_{$feed_key}_feed_username");
                                                    $feed_password = self::get_adnk_importer_option("url_{$feed_key}_feed_password");
                                                    $is_disabled = $feed_info['disabled'];
                                                    ?>
                                                    <tr valign="top">
                                                        <td nowrap>
                                                            <input <?php echo $is_disabled; ?> type="checkbox"
                                                                                               name="adnk_importer_options[url_<?php echo $feed_key; ?>_feed]"
                                                                <?php checked($feed_enabled, 'on'); ?>>
                                                            <?php esc_html_e($feed_info['label'], 'adnk_importer'); ?>
                                                        </td>
                                                        <td>
                                                            <select required <?php echo $is_disabled; ?>
                                                                    id="selected_<?php echo $feed_key; ?>_feed_category"
                                                                    name="adnk_importer_options[selected_<?php echo $feed_key; ?>_feed_category]">
                                                                <option value=""><?php esc_html_e('---', 'adnk_importer') ?></option>
                                                                <?php foreach ($categories as $category): ?>
                                                                    <option value="<?php echo esc_attr($category->term_id); ?>"
                                                                        <?php selected($category->term_id, $feed_category); ?>>
                                                                        <?php echo esc_html($category->name); ?>
                                                                    </option>
                                                                <?php endforeach; ?>
                                                            </select><br>
                                                            <?php esc_html_e('username', 'adnk_importer'); ?>
                                                            <input type="text"
                                                                   name="adnk_importer_options[url_<?php echo $feed_key; ?>_feed_username]"
                                                                   value="<?php echo esc_attr($feed_username); ?>"
                                                                   placeholder="pluginadnk">
                                                        </td>
                                                        <td>
                                                            <select <?php echo $is_disabled; ?>
                                                                    id="selected_<?php echo $feed_key; ?>_post_status"
                                                                    name="adnk_importer_options[selected_<?php echo $feed_key; ?>_post_status]">
                                                                <?php foreach ($post_statuses as $value => $label): ?>
                                                                    <option value="<?php echo esc_attr($value); ?>"
                                                                        <?php selected($value, $feed_status); ?>>
                                                                        <?php esc_html_e($label, 'adnk_importer') ?>
                                                                    </option>
                                                                <?php endforeach; ?>
                                                            </select><br>
                                                            <?php esc_html_e('password', 'adnk_importer'); ?>
                                                            <input type="text"
                                                                   name="adnk_importer_options[url_<?php echo $feed_key; ?>_feed_password]"
                                                                   value="<?php echo esc_attr($feed_password); ?>"
                                                                   placeholder="pl8g1n45tg">
                                                        </td>
                                                        <td>
                                                            <select required
                                                                    id="selected_<?php echo $feed_key; ?>_post_owner"
                                                                    name="adnk_importer_options[selected_<?php echo $feed_key; ?>_post_owner]">
                                                                <option value=""><?php esc_html_e('Seleziona proprietario del post', 'adnk_importer') ?></option>
                                                                <?php foreach ($users as $user): ?>
                                                                    <option value="<?php echo esc_attr($user->user_email); ?>" <?php selected($user->user_email, $feed_owner); ?>>
                                                                        <?php echo esc_html($user->user_login) . ' - ' . esc_html($user->user_email); ?>
                                                                    </option>
                                                                <?php endforeach; ?>
                                                            </select>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>

                                                <!-- Sezione Video -->
                                                <tr valign="top" class="td-video">
                                                    <td colspan="4">
                                                        <div class="adnk-video-container">
                                                            <div class="adnk-video-disclaimer">
                                                                I video vengono scaricati in locale sul tuo sito. Questo
                                                                potrebbe portare ad un rapido consumo delle risorse
                                                                (spazio e banda) del tuo servizio web.<br/><br/>
                                                                I video vengono inclusi nel contenuto dell'articolo
                                                                tramite shortcode [video] come da standard WordPress.
                                                                Per poter essere visualizzati E' NECESSARIO che il sito
                                                                (tema o altri
                                                                plugin) siano in grado di elaborare correttamente lo
                                                                shortcode.
                                                            </div>
                                                            <div class="adnk-video-form">
                                                                <div>
                                                                    <?php $video_enabled = self::get_adnk_importer_option('url_video_feed'); ?>
                                                                    <input <?php echo $field_disabled; ?>
                                                                            type="checkbox"
                                                                            name="adnk_importer_options[url_video_feed]"
                                                                        <?php checked($video_enabled, 'on'); ?>>
                                                                    <?php esc_html_e('Video', 'adnk_importer'); ?>
                                                                </div>
                                                                <div>
                                                                    <?php $video_category = self::get_adnk_importer_option('selected_video_feed_category'); ?>
                                                                    <select required <?php echo $field_disabled; ?>
                                                                            id="selected_video_feed_category"
                                                                            name="adnk_importer_options[selected_video_feed_category]">
                                                                        <option value=""><?php esc_html_e('---', 'adnk_importer') ?></option>
                                                                        <?php foreach ($categories as $category): ?>
                                                                            <option value="<?php echo esc_attr($category->term_id); ?>"
                                                                                <?php selected($category->term_id, $video_category); ?>>
                                                                                <?php echo esc_html($category->name); ?>
                                                                            </option>
                                                                        <?php endforeach; ?>
                                                                    </select>
                                                                </div>
                                                                <div>
                                                                    <?php $video_status = self::get_adnk_importer_option('selected_video_post_status'); ?>
                                                                    <select <?php echo $field_disabled; ?>
                                                                            id="selected_video_post_status"
                                                                            name="adnk_importer_options[selected_video_post_status]">
                                                                        <?php foreach ($post_statuses as $value => $label): ?>
                                                                            <option value="<?php echo esc_attr($value); ?>"
                                                                                <?php selected($value, $video_status); ?>>
                                                                                <?php esc_html_e($label, 'adnk_importer') ?>
                                                                            </option>
                                                                        <?php endforeach; ?>
                                                                    </select>
                                                                </div>
                                                                <div>
                                                                    <?php $video_username = self::get_adnk_importer_option('url_video_feed_username'); ?>
                                                                    <input type="text"
                                                                           name="adnk_importer_options[url_video_feed_username]"
                                                                           value="<?php echo esc_attr($video_username); ?>"
                                                                           placeholder="pluginadnk">
                                                                </div>
                                                                <div>
                                                                    <?php $video_password = self::get_adnk_importer_option('url_video_feed_password'); ?>
                                                                    <input type="text"
                                                                           name="adnk_importer_options[url_video_feed_password]"
                                                                           value="<?php echo esc_attr($video_password); ?>"
                                                                           placeholder="pl8g1n45tg">
                                                                </div>
                                                                <div style="display: block;">
                                                                    <?php $video_owner = self::get_adnk_importer_option('selected_video_post_owner'); ?>
                                                                    <select required
                                                                            id="selected_<?php echo $feed_key; ?>_post_owner"
                                                                            name="adnk_importer_options[selected_video_post_owner]">
                                                                        <option value=""><?php esc_html_e('Seleziona proprietario del post', 'adnk_importer') ?></option>
                                                                        <?php foreach ($users as $user): ?>
                                                                            <option value="<?php echo esc_attr($user->user_email); ?>" <?php selected($user->user_email, $video_owner); ?>>
                                                                                <?php echo esc_html($user->user_login) . ' - ' . esc_html($user->user_email); ?>
                                                                            </option>
                                                                        <?php endforeach; ?>
                                                                    </select>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                    <?php submit_button(); ?>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        <?php }

    }
}
new adnk_importer_Options();

// Helper function to use in your theme to return a theme option value
function get_adnk_importer_option($id = '')
{
    return adnk_importer_Options::get_adnk_importer_option($id);
}

add_action('admin_enqueue_scripts', 'ecctdm_enqueue_color_picker');
function ecctdm_enqueue_color_picker($hook_suffix)
{
    // first check that $hook_suffix is appropriate for your admin page
    wp_enqueue_style('wp-color-picker');
    wp_enqueue_style('admin-style-adk', plugin_dir_url(__FILE__) . '/css/admin.css');

}

if (!class_exists('adnk_settings_Options')) {

    class adnk_settings_Options
    {

        /**
         * Start things up
         *
         * @since 1.0.0
         */
        public function __construct()
        {

            // We only need to register the admin panel on the back-end
            if (is_admin()) {
                add_action('admin_menu', array('adnk_settings_Options', 'add_settings_menu_page'));
                add_action('admin_init', array('adnk_settings_Options', 'register_settings'));
            }

        }

        /**
         * Returns all adnk_settings options
         *
         * @since 1.0.0
         */
        public static function get_adnk_settings_options()
        {
            return get_option('adnk_settings_options');
        }

        /**
         * Returns single adnk_settings option
         *
         * @since 1.0.0
         */
        public static function get_adnk_settings_option($id)
        {
            $options = self::get_adnk_settings_options();
            if (isset($options[$id])) {
                return $options[$id];
            }
        }

        /**
         * Add sub menu page
         *
         * @since 1.0.0
         */
        public static function add_settings_menu_page()
        {
            add_submenu_page('adnk-plugin-settings', 'Impostazioni Account', 'Impostazioni Account', 'manage_options', 'adnk-plugin-account', array('adnk_settings_Options', 'adnk_plugin_func_accsettings'));
            add_submenu_page('adnk-plugin-log', 'Log importazioni', 'Log importazioni', 'manage_options', 'adnk-plugin-log', array('adnk_settings_Options', 'adnk_importer_logs_page'));
        }

        /**
         * Register a setting and its sanitization callback.
         *
         * We are only registering 1 setting so we can store all options in a single option as
         * an array. You could, however, register a new setting for each option
         *
         * @since 1.0.0
         */
        public static function register_settings()
        {
            register_setting('adnk_settings_options', 'adnk_settings_options', array('adnk_settings_Options', 'sanitize_settings'));
            register_setting('adnk_oauth_delete', 'adnk_oauth_delete');
            register_setting('adnk_oauth', 'adnk_oauth');
        }

        public static function sanitize_settings($options)
        {

            // If we have options lets sanitize them
            if ($options) {

                // Checkbox consent_send_statistical_data
                if (!empty($options['consent_send_statistical_data'])) {
                    $options['consent_send_statistical_data'] = 'on';
                } else {
                    unset($options['consent_send_statistical_data']); // Remove from options if not checked
                }
            }

            // Return sanitized options
            return $options;
        }

        // Funzione helper per formattare le dimensioni dei file
        private static function adnk_format_file_size($bytes)
        {
            $units = ['B', 'KB', 'MB', 'GB', 'TB'];
            $bytes = max($bytes, 0);
            $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
            $pow = min($pow, count($units) - 1);
            $bytes /= (1 << (10 * $pow));
            return round($bytes, 2) . ' ' . $units[$pow];
        }

        // Funzione helper per l'attributo selected
        private static function adnk_selected($value1, $value2)
        {
            return "{$value1}" === "{$value2}" ? ' selected="selected"' : '';
        }

        private static function adnk_sanitize_text_field($text)
        {
            // Rimuove caratteri nulli
            $text = str_replace(chr(0), '', $text);

            // Rimuove spazi bianchi all'inizio e alla fine
            $text = trim($text);

            // Normalizza whitespace
            $text = preg_replace('/\s+/', ' ', $text);

            // Rimuove caratteri di controllo
            $text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $text);

            return $text;
        }

        public static function adnk_importer_logs_page()
        {
            ?>
            <div class="wrap">
                <h1>ADNK Importer - Log Viewer</h1>
                <p></p>

                <nav class="nav-tab-wrapper">
                    <a href="?page=adnk-plugin-settings"
                       class="nav-tab "><?php esc_html_e('Impostazioni di Importazione', 'adnk_importer'); ?></a>
                    <a href="?page=adnk-plugin-account"
                       class="nav-tab"><?php esc_html_e('Account Settings', 'adnk_importer'); ?></a>
                    <a href="?page=adnk-plugin-log"
                       class="nav-tab nav-tab-active">Log Importazioni</a>
                </nav>
                <?php
                // Ottieni la lista dei file di log disponibili
                $logs_dir = plugin_dir_path(dirname(__FILE__)) . 'logs/';
                $log_files = [];

                if (is_dir($logs_dir)) {
                    foreach (glob($logs_dir . "*_import.log") as $log_file) {
                        $filename = basename($log_file);
                        if (preg_match('/^(\d{8})_import\.log$/', $filename, $matches)) {
                            $date = $matches[1];
                            $formatted_date = date('d/m/Y', strtotime(substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2)));

                            // Cerca il file CSV corrispondente
                            $csv_pattern = $logs_dir . $date . '_*_import.csv';
                            $csv_files = glob($csv_pattern);
                            $csv_file = !empty($csv_files) ? $csv_files[0] : null;

                            $log_files[$date] = [
                                'date' => $formatted_date,
                                'log_file' => $log_file,
                                'log_size' => filesize($log_file),
                                'csv_file' => $csv_file,
                                'csv_size' => $csv_file ? filesize($csv_file) : 0,
                                'hostname' => $csv_file ? preg_replace('/^' . $date . '_(.+)_import\.csv$/', '$1', basename($csv_file)) : '',
                                'raw_date' => $date
                            ];
                        }
                    }

                    // Ordina per data decrescente
                    krsort($log_files);
                }

                if (empty($log_files)) {
                    echo '<p>Nessun file di log trovato.</p>';
                    return;
                }

                // Determina quale data visualizzare
                $selected_date = isset($_GET['date']) ? self::adnk_sanitize_text_field($_GET['date']) : key($log_files);
                $selected_log = isset($log_files[$selected_date]) ? $log_files[$selected_date] : null;

                if (!$selected_log) {
                    echo '<p>Log non disponibile.</p>';
                    return;
                }

                // Analizza i 7 CSV più recenti per il riepilogo delle categorie
                $category_summary = [];
                $recent_logs = array_slice($log_files, 0, 7, true);

                foreach ($recent_logs as $date_key => $log_info) {
                    if ($log_info['csv_file'] && file_exists($log_info['csv_file'])) {
                        $csv_handle = fopen($log_info['csv_file'], 'r');
                        $header = fgetcsv($csv_handle, 1000, ';');

                        // Trova gli indici delle colonne necessarie
                        $cat_index = array_search('CATEGORIA', $header);
                        $import_date_index = array_search('IMPORTATAZIONE', $header);

                        if ($cat_index !== false && $import_date_index !== false) {
                            while (($data = fgetcsv($csv_handle, 1000, ';')) !== false) {
                                if (isset($data[$cat_index]) && !empty($data[$cat_index])) {
                                    $category = $data[$cat_index];
                                    $import_date = isset($data[$import_date_index]) ? $data[$import_date_index] : '';

                                    if (!isset($category_summary[$category])) {
                                        $category_summary[$category] = [
                                            'count' => 0,
                                            'last_import' => '',
                                            'raw_date' => 0
                                        ];
                                    }

                                    $category_summary[$category]['count']++;

                                    // Aggiorna la data di ultima importazione se più recente
                                    if (!empty($import_date)) {
                                        $import_timestamp = strtotime($import_date);
                                        if ($import_timestamp &&
                                            ($category_summary[$category]['raw_date'] == 0 ||
                                                $import_timestamp > $category_summary[$category]['raw_date'])) {
                                            $category_summary[$category]['last_import'] = $import_date;
                                            $category_summary[$category]['raw_date'] = $import_timestamp;
                                        }
                                    }
                                }
                            }
                        }
                        fclose($csv_handle);
                    }
                }

                // Visualizza il riepilogo delle categorie
                if (!empty($category_summary)) {
                    ?>
                    <h2>Riepilogo Importazioni per Categoria (ultimi 7 giorni)</h2>
                    <div class="category-summary" style="margin-bottom: 30px;">
                        <table class="wp-list-table widefat fixed striped">
                            <thead>
                            <tr>
                                <th>Categoria</th>
                                <th>Numero Articoli</th>
                                <th>Ultima Importazione</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($category_summary as $category => $data): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($category); ?></td>
                                    <td><?php echo htmlspecialchars($data['count']); ?></td>
                                    <td><?php echo htmlspecialchars($data['last_import']); ?></td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php
                }

                // Dropdown per selezionare la data
                ?>
                <div class="log-selector">
                    <form method="get" action="">
                        <input type="hidden" name="page" value="adnk-plugin-log">
                        <select name="date" id="log-date-selector" onchange="this.form.submit()">
                            <?php foreach ($log_files as $date_key => $log_info): ?>
                                <option value="<?php echo htmlspecialchars($date_key, ENT_QUOTES, 'UTF-8'); ?>"<?php echo self::adnk_selected($date_key, $selected_date); ?>>
                                    <?php echo htmlspecialchars($log_info['date'], ENT_QUOTES, 'UTF-8'); ?> -
                                    CSV: <?php echo htmlspecialchars(self::adnk_format_file_size($log_info['csv_size']), ENT_QUOTES, 'UTF-8'); ?>
                                    ,
                                    LOG: <?php echo htmlspecialchars(self::adnk_format_file_size($log_info['log_size']), ENT_QUOTES, 'UTF-8'); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </form>
                </div>

                <?php
                // Visualizza il contenuto del file CSV in formato tabellare con intestazione fissa
                if ($selected_log['csv_file'] && file_exists($selected_log['csv_file'])) {
                    echo '<h2>CSV Import Data</h2>';
                    echo '<div class="csv-viewer">';
                    echo '<table class="wp-list-table widefat fixed striped csv-data-table">';

                    $csv_handle = fopen($selected_log['csv_file'], 'r');
                    $row_count = 0;

                    while (($data = fgetcsv($csv_handle, 1000, ';')) !== false) {
                        if ($row_count === 0) {
                            echo '<thead>';
                            echo '<tr>';
                            foreach ($data as $header) {
                                echo '<th>' . htmlspecialchars($header) . '</th>';
                            }
                            echo '</tr>';
                            echo '</thead>';
                            echo '<tbody>';
                        } else {
                            echo '<tr>';
                            foreach ($data as $cell) {
                                echo '<td>' . htmlspecialchars($cell) . '</td>';
                            }
                            echo '</tr>';
                        }
                        $row_count++;
                    }

                    fclose($csv_handle);
                    echo '</tbody>';
                    echo '</table>';
                    echo '</div>';
                } else {
                    echo '<p>File CSV non trovato.</p>';
                }

                // Visualizza il contenuto del file di log
                if ($selected_log['log_file'] && file_exists($selected_log['log_file'])) {
                    echo '<h2>Log File</h2>';
                    echo '<div class="log-viewer" style="max-height: 400px; overflow-y: auto; background: #f1f1f1; padding: 10px; font-family: monospace;">';

                    // Leggi e mostra il contenuto del file di log
                    $log_content = file_get_contents($selected_log['log_file']);
                    echo '<pre>' . htmlspecialchars($log_content) . '</pre>';

                    echo '</div>';
                } else {
                    echo '<p>File di log non trovato.</p>';
                }
                ?>
            </div>

            <style>
                .log-selector {
                    margin: 20px 0;
                }

                #log-date-selector {
                    min-width: 300px;
                }

                /* Stile per l'intestazione fissa della tabella CSV */
                .csv-viewer {
                    max-height: 400px;
                    overflow-y: auto;
                    margin-bottom: 20px;
                    position: relative;
                }

                .csv-data-table {
                    width: 100%;
                }

                .csv-data-table thead {
                    position: sticky;
                    top: 0;
                    z-index: 1;
                    background-color: #f1f1f1;
                }

                .csv-data-table th {
                    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
                }
            </style>
            <?php
        }

        public static function adnk_plugin_func_accsettings()
        {
            ?>
            <div id="adk">
                <div class="container wrap">

                    <?php $logo_tdm_url = plugin_dir_url(__FILE__) . "img/logoadnkronos.jpg" ?>
                    <h1>
                        <img src="<?php echo esc_url($logo_tdm_url) ?>"><?php esc_html_e('AdnKronos Feed Importer Options', 'adnk_importer'); ?>
                    </h1>
                    <p></p>

                    <nav class="nav-tab-wrapper">
                        <a href="?page=adnk-plugin-settings"
                           class="nav-tab "><?php esc_html_e('Impostazioni di Importazione', 'adnk_importer'); ?></a>
                        <a href="?page=adnk-plugin-account"
                           class="nav-tab nav-tab-active"><?php esc_html_e('Account Settings', 'adnk_importer'); ?></a>
                        <a href="?page=adnk-plugin-log"
                           class="nav-tab ">Log Importazioni</a>
                    </nav>

                    <div class="tab-content">
                        <div class="row">
                            <div class="col-9">
                                <form method="post" action="options.php">

                                    <?php settings_fields('adnk_settings_options'); ?>

                                    <div class="card card-static mt-4">
                                        <div class="card-header">
                                            <strong><?php esc_html_e('Consenso invio dati statistici', 'adnk_importer'); ?></strong>
                                        </div>
                                        <div class="card-body">
                                            <p>
                                                Il sistema non raccoglie dati di navigazione degli utenti, siano essi
                                                visitatori del sito o gestori.<br/>
                                                Le statistiche raccolte sono relative al numero di articoli importati e
                                                pubblicati per categoria, non rientrano pertanto
                                                nella categoria dei "dati sensibili" o "dati personali".<br/>
                                                Il consenso è necessario per poter abilitare tutti i feed.
                                            </p>
                                            <?php $value = self::get_adnk_settings_option('consent_send_statistical_data'); ?>
                                            <input type="checkbox"
                                                   name="adnk_settings_options[consent_send_statistical_data]" <?php checked($value, 'on'); ?>> <?php esc_html_e('Acconsento all\'invio dei dati', 'adnk_importer'); ?>
                                        </div>
                                    </div>
                                    <?php $regist_user = get_option('adn_site_active', 0);
                                    if (!$regist_user) {
                                        ?>
                                        <div class="card card-static mt-4">
                                            <div class="card-header">
                                                <strong><?php esc_html_e('Verifica il tuo dominio', 'adnk_importer'); ?></strong>
                                            </div>
                                            <div class="card-body">
                                                <p>
                                                    Per verificare il tuo dominio <a target="_blank"
                                                                                     href="https://plugin.adnkronos.com?domain=<?php echo urlencode(esc_url(get_site_url())) ?>">compila
                                                        la richiesta cliccando qui registrando il tuo
                                                        dominio <?php echo esc_url(get_site_url()); ?></a>!
                                                </p>
                                            </div>
                                        </div>
                                    <?php } ?>
                                    <?php submit_button(); ?>
                                </form>
                            </div>
                            <div class="col-3">
                                <div class="card card-static border-primary mt-4">
                                    <div class="card-body">
                                        <p class="card-text">
                                            <?php $logo_tdm_url = plugin_dir_url(__FILE__) . "img/logo-adnkronos.svg" ?>
                                            <img width="200px" src="<?php echo esc_url($logo_tdm_url) ?>">
                                            <br/><br/>
                                            <b>ROMA</b> Piazza Mastai n.9 - 00153<br/>
                                            T: +39 06 5807666 <br/> F: +39 06 5807815<br/>
                                            <br/>
                                            <b>MILANO</b> Via Manin, 37 - 20121<br/>
                                            T: +39 02 763661
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                        `
                    </div>
                </div>
            </div>
            <?php
        }


    }
}

new adnk_settings_Options();

// Helper function to use in your theme to return a theme option value
function get_adnk_settings_option($id = '')
{
    return adnk_settings_Options::get_adnk_settings_option($id);
}

/* admin notices */
function adnk_admin_notice()
{

    //$site_url = get_home_url();
    $regist_user = get_option('adn_site_active', 0);

    if ($regist_user != true) {
        $user = wp_get_current_user();
        if (in_array('administrator', (array)$user->roles)) {
            echo '<div class="notice notice-warning is-dismissible">
                  <p>Adnkronos importer non è registrato <a href="/wp-admin/admin.php?page=adnk-plugin-account">Registra il tuo dominio</a></p>
                 </div>';
        }
    }

}

add_action('admin_notices', 'adnk_admin_notice');