<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://kerosin.digital/
 * @since      1.0.0
 *
 * @package    Rss_Chimp
 * @subpackage Rss_Chimp/admin
 */
/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Rss_Chimp
 * @subpackage Rss_Chimp/admin
 * @author     kerosin.digital <hello@kerosin.digital>
 */
define( 'RSS_CHIMP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
class Rss_Chimp_Admin {
    const CACHE_KEY = 'rss_chimp_feed_cache';

    const CACHE_EXPIRATION = 3600;

    // Cache expiration time in seconds (e.g., 1 hour)
    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_name    The ID of this plugin.
     */
    private $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $version    The current version of this plugin.
     */
    private $version;

    /**
     * The options name to be used in this plugin
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $option_name    Option name of this plugin
     */
    private $option_name = 'rss_chimp';

    /**
     * The screen hook suffix for the plugin admin page.
     *
     * @since    1.3.0
     * @access   private
     * @var      string    $plugin_screen_hook_suffix    The screen hook suffix for the plugin admin page.
     */
    private $plugin_screen_hook_suffix;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string    $plugin_name       The name of this plugin.
     * @param      string    $version    The version of this plugin.
     */
    public function __construct( $plugin_name, $version ) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
    }

    /**
     * Register the stylesheets for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_styles( $hook ) {
        if ( $hook != $this->plugin_screen_hook_suffix ) {
            return;
        }
        wp_enqueue_style(
            $this->plugin_name,
            RSS_CHIMP_PLUGIN_URL . 'css/rss-chimp-admin.css',
            [],
            $this->version,
            'all'
        );
    }

    /**
     * Register the JavaScript for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts( $hook ) {
        if ( $hook != $this->plugin_screen_hook_suffix ) {
            return;
        }
        wp_enqueue_script(
            $this->plugin_name,
            RSS_CHIMP_PLUGIN_URL . 'js/rss-chimp-admin.js',
            ['jquery'],
            $this->version,
            true
        );
        // Enable JS translations for rss-chimp-admin.js
        wp_set_script_translations( $this->plugin_name, 'rss-chimp', plugin_dir_path( __DIR__ ) . '/../languages' );
        wp_enqueue_media();
        // Enqueue the media uploader
    }

    /**
     * Create plugin pages.
     *
     * @since    1.0.0
     */
    public function rss_chimp_menu_pages() {
        if ( class_exists( 'Adminion' ) ) {
            $this->plugin_screen_hook_suffix = add_submenu_page(
                'adminion',
                __( 'RSS Chimp', 'rss-chimp' ),
                __( 'RSS Chimp', 'rss-chimp' ),
                'manage_options',
                $this->plugin_name,
                array($this, 'rss_chimp_admin_display')
            );
        } else {
            $this->plugin_screen_hook_suffix = add_menu_page(
                __( 'RSS Chimp', 'rss-chimp' ),
                __( 'RSS Chimp', 'rss-chimp' ),
                'manage_options',
                'rss-chimp',
                array($this, 'rss_chimp_admin_display'),
                'dashicons-rss'
            );
        }
    }

    /**
     * Render plugin pages.
     *
     * @since  1.0.0
     */
    public function rss_chimp_admin_display() {
        include_once 'partials/rss-chimp-admin-display.php';
    }

    /**
     * Register all related settings of this plugin
     *
     * @since  1.0.0
     */
    public function rss_chimp_settings_init() {
        $this->add_channel_settings();
        $this->add_performance_settings();
        $this->add_feed_delay_settings();
        $this->add_thumbnail_settings();
        $this->add_other_settings();
        $this->add_default_feed_settings();
        $this->add_custom_feed_settings();
    }

    /**
     * Add channel settings.
     *
     * @since  1.0.0
     */
    private function add_channel_settings() {
        add_settings_section(
            $this->option_name . '_channel_settings',
            __( 'Feed channel options', 'rss-chimp' ),
            array($this, $this->option_name . '_channel_settings_cb'),
            $this->plugin_name . '_channel_settings'
        );
        add_settings_field(
            $this->option_name . '_channel_choose_image',
            __( 'Preferred logo', 'rss-chimp' ),
            array($this, $this->option_name . '_channel_choose_image_cb'),
            $this->plugin_name . '_channel_settings',
            $this->option_name . '_channel_settings',
            array(
                'label_for' => $this->option_name . '_channel_choose_image',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_channel_choose_image', [$this, 'sanitize_channel_choose_image'] );
        add_settings_field(
            $this->option_name . '_channel_custom_image',
            __( 'Custom logo', 'rss-chimp' ),
            array($this, $this->option_name . '_channel_custom_image_cb'),
            $this->plugin_name . '_channel_settings',
            $this->option_name . '_channel_settings',
            array(
                'label_for' => $this->option_name . '_channel_custom_image',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_channel_custom_image', [$this, 'sanitize_url'] );
        add_settings_field(
            $this->option_name . '_channel_copyright',
            __( 'Copyright', 'rss-chimp' ),
            array($this, $this->option_name . '_channel_copyright_cb'),
            $this->plugin_name . '_channel_settings',
            $this->option_name . '_channel_settings',
            array(
                'label_for' => $this->option_name . '_channel_copyright',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_channel_copyright', [$this, 'sanitize_text_field'] );
        add_settings_field(
            $this->option_name . '_channel_editor',
            __( 'Editor', 'rss-chimp' ),
            array($this, $this->option_name . '_channel_editor_cb'),
            $this->plugin_name . '_channel_settings',
            $this->option_name . '_channel_settings',
            array(
                'label_for' => $this->option_name . '_channel_editor',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_channel_editor', [$this, 'sanitize_text_field'] );
        add_settings_field(
            $this->option_name . '_channel_webmaster',
            __( 'Webmaster', 'rss-chimp' ),
            array($this, $this->option_name . '_channel_webmaster_cb'),
            $this->plugin_name . '_channel_settings',
            $this->option_name . '_channel_settings',
            array(
                'label_for' => $this->option_name . '_channel_webmaster',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_channel_webmaster', [$this, 'sanitize_text_field'] );
    }

    /**
     * Add feed performance settings.
     *
     * @since  1.3.0
     */
    private function add_performance_settings() {
        add_settings_section(
            $this->option_name . '_performance_settings',
            __( 'Performance Settings', 'rss-chimp' ),
            array($this, $this->option_name . '_performance_settings_cb'),
            $this->plugin_name . '_performance_settings'
        );
        add_settings_field(
            $this->option_name . '_lazy_loading',
            __( 'Enable lazy loading images', 'rss-chimp' ),
            array($this, $this->option_name . '_lazy_loading_cb'),
            $this->plugin_name . '_performance_settings',
            $this->option_name . '_performance_settings',
            array(
                'label_for' => $this->option_name . '_lazy_loading',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_lazy_loading', array($this, 'sanitize_checkbox') );
    }

    /**
     * Add feed delay settings.
     *
     * @since  1.0.0
     */
    private function add_feed_delay_settings() {
        if ( rss_chimp_fs()->is__premium_only() && rss_chimp_fs()->is_plan( 'pro', true ) ) {
            add_settings_section(
                $this->option_name . '_feed_delay_settings',
                __( 'Feed delay', 'rss-chimp' ),
                array($this, $this->option_name . '_feed_delay_settings_cb'),
                $this->plugin_name . '_feed_delay_settings'
            );
            add_settings_field(
                $this->option_name . '_feed_delay_status',
                __( 'Status', 'rss-chimp' ),
                array($this, $this->option_name . '_feed_delay_status_cb'),
                $this->plugin_name . '_feed_delay_settings',
                $this->option_name . '_feed_delay_settings',
                array(
                    'label_for' => $this->option_name . '_feed_delay_status',
                )
            );
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_feed_delay_status', array($this, 'sanitize_on_off') );
            add_settings_field(
                $this->option_name . '_feed_delay_value',
                __( 'Delay', 'rss-chimp' ),
                array($this, $this->option_name . '_feed_delay_value_cb'),
                $this->plugin_name . '_feed_delay_settings',
                $this->option_name . '_feed_delay_settings',
                array(
                    'label_for' => $this->option_name . '_feed_delay_value',
                )
            );
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_feed_delay_value', array($this, 'sanitize_feed_delay_value') );
        }
    }

    /**
     * Add thumbnail settings.
     *
     * @since  1.0.0
     */
    private function add_thumbnail_settings() {
        add_settings_section(
            $this->option_name . '_thumbnail_settings',
            __( 'Thumbnail options', 'rss-chimp' ),
            array($this, $this->option_name . '_thumbnail_settings_cb'),
            $this->plugin_name . '_thumbnail_settings'
        );
        add_settings_field(
            $this->option_name . '_thumbnail_small',
            __( 'RSS Chimp Small', 'rss-chimp' ),
            array($this, $this->option_name . '_thumbnail_small_cb'),
            $this->plugin_name . '_thumbnail_settings',
            $this->option_name . '_thumbnail_settings',
            array(
                'label_for' => $this->option_name . '_thumbnail_small',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_thumbnail_small', [$this, 'sanitize_checkbox'] );
        if ( rss_chimp_fs()->is__premium_only() && rss_chimp_fs()->is_plan( 'pro', true ) ) {
            add_settings_field(
                $this->option_name . '_thumbnail_medium',
                __( 'RSS Chimp Medium', 'rss-chimp' ),
                array($this, $this->option_name . '_thumbnail_medium_cb'),
                $this->plugin_name . '_thumbnail_settings',
                $this->option_name . '_thumbnail_settings',
                array(
                    'label_for' => $this->option_name . '_thumbnail_medium',
                )
            );
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_thumbnail_medium', [$this, 'sanitize_checkbox'] );
        }
    }

    /**
     * Add other settings.
     *
     * @since  1.0.0
     */
    private function add_other_settings() {
        add_settings_section(
            $this->option_name . '_other_settings',
            __( 'Other options', 'rss-chimp' ),
            array($this, $this->option_name . '_other_settings_cb'),
            $this->plugin_name . '_other_settings'
        );
        add_settings_field(
            $this->option_name . '_disable_commentsfeed',
            __( 'Deactivate comments feed', 'rss-chimp' ),
            array($this, $this->option_name . '_disable_commentsfeed_cb'),
            $this->plugin_name . '_other_settings',
            $this->option_name . '_other_settings',
            array(
                'label_for' => $this->option_name . '_disable_commentsfeed',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_disable_commentsfeed', [$this, 'sanitize_checkbox'] );
        if ( rss_chimp_fs()->is__premium_only() && rss_chimp_fs()->is_plan( 'pro', true ) ) {
            add_settings_field(
                $this->option_name . '_generatortag',
                __( 'Hide WordPress version from all feeds', 'rss-chimp' ),
                array($this, $this->option_name . '_generatortag_cb__premium_only'),
                $this->plugin_name . '_other_settings',
                $this->option_name . '_other_settings',
                array(
                    'label_for' => $this->option_name . '_generatortag',
                )
            );
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_generatortag', [$this, 'sanitize_checkbox'] );
        }
        add_settings_field(
            $this->option_name . '_reset',
            __( 'Reset all options on deactivation', 'rss-chimp' ),
            array($this, $this->option_name . '_reset_cb'),
            $this->plugin_name . '_other_settings',
            $this->option_name . '_other_settings',
            array(
                'label_for' => $this->option_name . '_reset',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_reset', [$this, 'sanitize_checkbox'] );
    }

    /**
     * Add default feed settings.
     *
     * @since  1.0.0
     */
    private function add_default_feed_settings() {
        add_settings_section(
            $this->option_name . '_defaultfeed_settings',
            __( 'Default feed', 'rss-chimp' ),
            array($this, $this->option_name . '_defaultfeed_settings_cb'),
            $this->plugin_name . '_defaultfeed_settings'
        );
        add_settings_field(
            $this->option_name . '_defaultfeed_disable',
            __( 'Disable default feeds', 'rss-chimp' ),
            array($this, $this->option_name . '_defaultfeed_disable_cb'),
            $this->plugin_name . '_defaultfeed_settings',
            $this->option_name . '_defaultfeed_settings',
            array(
                'label_for' => $this->option_name . '_defaultfeed_disable',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_disable', [$this, 'sanitize_checkbox'] );
        add_settings_field(
            $this->option_name . '_defaultfeed_imagesize',
            __( 'Image size for the default WordPress feed', 'rss-chimp' ),
            array($this, $this->option_name . '_defaultfeed_imagesize_cb'),
            $this->plugin_name . '_defaultfeed_settings',
            $this->option_name . '_defaultfeed_settings',
            array(
                'label_for' => $this->option_name . '_defaultfeed_imagesize',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_imagesize', [$this, 'sanitize_text_field'] );
        add_settings_field(
            $this->option_name . '_defaultfeed_enclosure',
            __( 'Also add image with enclosure tag to the feed', 'rss-chimp' ),
            array($this, $this->option_name . '_defaultfeed_enclosure_cb'),
            $this->plugin_name . '_defaultfeed_settings',
            $this->option_name . '_defaultfeed_settings',
            array(
                'label_for' => $this->option_name . '_defaultfeed_enclosure',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_enclosure', [$this, 'sanitize_checkbox'] );
        add_settings_field(
            $this->option_name . '_defaultfeed_image_title',
            __( 'Include the title of the featured image', 'rss-chimp' ),
            array($this, $this->option_name . '_defaultfeed_image_title_cb'),
            $this->plugin_name . '_defaultfeed_settings',
            $this->option_name . '_defaultfeed_settings',
            array(
                'label_for' => $this->option_name . '_defaultfeed_image_title',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_image_title', [$this, 'sanitize_checkbox'] );
        add_settings_field(
            $this->option_name . '_defaultfeed_image_description',
            __( 'Include the description of the featured image', 'rss-chimp' ),
            array($this, $this->option_name . '_defaultfeed_image_description_cb'),
            $this->plugin_name . '_defaultfeed_settings',
            $this->option_name . '_defaultfeed_settings',
            array(
                'label_for' => $this->option_name . '_defaultfeed_image_description',
            )
        );
        register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_image_description', [$this, 'sanitize_checkbox'] );
        if ( rss_chimp_fs()->is__premium_only() && rss_chimp_fs()->is_plan( 'pro', true ) ) {
            add_settings_field(
                $this->option_name . '_defaultfeed_content_before',
                __( 'Custom HTML before each RSS item', 'rss-chimp' ),
                array($this, $this->option_name . '_defaultfeed_content_before_cb__premium_only'),
                $this->plugin_name . '_defaultfeed_settings',
                $this->option_name . '_defaultfeed_settings',
                array(
                    'label_for' => $this->option_name . '_defaultfeed_content_before',
                )
            );
            // Allow limited HTML so it can render in feeds safely
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_content_before', [$this, 'sanitize_html_field'] );
            add_settings_field(
                $this->option_name . '_defaultfeed_content_after',
                __( 'Custom HTML after each RSS item', 'rss-chimp' ),
                array($this, $this->option_name . '_defaultfeed_content_after_cb__premium_only'),
                $this->plugin_name . '_defaultfeed_settings',
                $this->option_name . '_defaultfeed_settings',
                array(
                    'label_for' => $this->option_name . '_defaultfeed_content_after',
                )
            );
            // Allow limited HTML so it can render in feeds safely
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_content_after', [$this, 'sanitize_html_field'] );
        }
        if ( rss_chimp_fs()->is__premium_only() && rss_chimp_fs()->is_plan( 'pro', true ) ) {
            add_settings_field(
                $this->option_name . '_defaultfeed_link_parameter',
                __( 'URL parameter', 'rss-chimp' ),
                array($this, $this->option_name . '_defaultfeed_link_parameter_cb'),
                $this->plugin_name . '_defaultfeed_settings',
                $this->option_name . '_defaultfeed_settings',
                array(
                    'label_for' => $this->option_name . '_defaultfeed_link_parameter',
                )
            );
            register_setting( $this->plugin_name . '_tab_general', $this->option_name . '_defaultfeed_link_parameter', [$this, 'sanitize_text_field'] );
        }
    }

    /**
     * Add custom feed settings.
     *
     * @since  1.0.0
     */
    private function add_custom_feed_settings() {
        if ( rss_chimp_fs()->is__premium_only() && rss_chimp_fs()->is_plan( 'pro', true ) ) {
            add_settings_section(
                $this->option_name . '_customfeed_settings',
                __( 'Custom feed', 'rss-chimp' ),
                array($this, $this->option_name . '_customfeed_settings_cb__premium_only'),
                $this->plugin_name . '_customfeed_settings'
            );
            add_settings_field(
                $this->option_name . '_customfeed',
                __( 'Custom feed', 'rss-chimp' ),
                array($this, $this->option_name . '_customfeed_cb'),
                $this->plugin_name . '_customfeed_settings',
                $this->option_name . '_customfeed_settings',
                array(
                    'label_for' => $this->option_name . '_customfeed',
                )
            );
            register_setting( $this->plugin_name . '_tab_feeds', $this->option_name . '_customfeed', [$this, 'sanitize_custom_feed'] );
        }
    }

    /**
     * Sanitize text field.
     *
     * @since  1.3.0
     */
    public function sanitize_text_field( $input ) {
        return sanitize_text_field( $input );
    }

    /**
     * Sanitize textarea field.
     *
     * @since  1.3.0
     */
    public function sanitize_textarea_field( $input ) {
        return sanitize_textarea_field( $input );
    }

    /**
     * Sanitize HTML field allowing safe tags for feed output.
     *
     * @since  1.3.0
     */
    public function sanitize_html_field( $input ) {
        // Allow common formatting tags; developers can extend via filter
        $allowed = apply_filters( 'rss_chimp_allowed_html', wp_kses_allowed_html( 'post' ) );
        return wp_kses( $input, $allowed );
    }

    /**
     * Sanitize feed delay value.
     *
     * @since  1.3.0
     */
    public function sanitize_feed_delay_value( $input ) {
        $sanitized_input = [];
        $sanitized_input['VALUE'] = ( isset( $input['VALUE'] ) ? max( 0, intval( $input['VALUE'] ) ) : 0 );
        $allowed_units = array(
            'SECOND',
            'MINUTE',
            'HOUR',
            'DAY',
            'WEEK',
            'MONTH',
            'YEAR'
        );
        $unit = ( isset( $input['UNIT'] ) ? sanitize_text_field( $input['UNIT'] ) : 'HOUR' );
        $sanitized_input['UNIT'] = ( in_array( $unit, $allowed_units, true ) ? $unit : 'HOUR' );
        return $sanitized_input;
    }

    /**
     * Sanitize checkbox values (1 or 0)
     */
    public function sanitize_checkbox( $input ) {
        return ( $input === '1' || $input === 1 ? '1' : '0' );
    }

    /**
     * Sanitize on/off selects
     */
    public function sanitize_on_off( $input ) {
        $val = ( $input === 'on' ? 'on' : 'off' );
        return $val;
    }

    /**
     * Sanitize URL fields
     */
    public function sanitize_url( $input ) {
        return esc_url_raw( $input );
    }

    /**
     * Sanitize channel image choice
     */
    public function sanitize_channel_choose_image( $input ) {
        $allowed = array(
            'off',
            'favicon',
            'sitelogo',
            'customimage'
        );
        $val = sanitize_text_field( $input );
        return ( in_array( $val, $allowed, true ) ? $val : 'off' );
    }

    /**
     * Sanitize custom feed.
     *
     * @since  1.3.0
     */
    public function sanitize_custom_feed( $input ) {
        $sanitized_input = [];
        if ( isset( $input['name'] ) && is_array( $input['name'] ) ) {
            $sanitized_input['name'] = array_map( 'sanitize_text_field', $input['name'] );
        } else {
            $sanitized_input['name'] = [];
        }
        if ( isset( $input['imagesize'] ) && is_array( $input['imagesize'] ) ) {
            $sanitized_input['imagesize'] = array_map( 'sanitize_text_field', $input['imagesize'] );
        } else {
            $sanitized_input['imagesize'] = [];
        }
        if ( isset( $input['image_title'] ) && is_array( $input['image_title'] ) ) {
            $sanitized_input['image_title'] = array_map( 'intval', $input['image_title'] );
        } else {
            $sanitized_input['image_title'] = [];
        }
        if ( isset( $input['image_description'] ) && is_array( $input['image_description'] ) ) {
            $sanitized_input['image_description'] = array_map( 'intval', $input['image_description'] );
        } else {
            $sanitized_input['image_description'] = [];
        }
        if ( isset( $input['enclosure'] ) && is_array( $input['enclosure'] ) ) {
            $sanitized_input['enclosure'] = array_map( 'intval', $input['enclosure'] );
        } else {
            $sanitized_input['enclosure'] = [];
        }
        if ( isset( $input['htmlbefore'] ) && is_array( $input['htmlbefore'] ) ) {
            $sanitized_input['htmlbefore'] = array_map( function ( $v ) {
                $allowed = apply_filters( 'rss_chimp_allowed_html', wp_kses_allowed_html( 'post' ) );
                return wp_kses( $v, $allowed );
            }, $input['htmlbefore'] );
        } else {
            $sanitized_input['htmlbefore'] = [];
        }
        if ( isset( $input['htmlafter'] ) && is_array( $input['htmlafter'] ) ) {
            $sanitized_input['htmlafter'] = array_map( function ( $v ) {
                $allowed = apply_filters( 'rss_chimp_allowed_html', wp_kses_allowed_html( 'post' ) );
                return wp_kses( $v, $allowed );
            }, $input['htmlafter'] );
        } else {
            $sanitized_input['htmlafter'] = [];
        }
        if ( isset( $input['parameter'] ) && is_array( $input['parameter'] ) ) {
            $sanitized_input['parameter'] = array_map( 'sanitize_text_field', $input['parameter'] );
        } else {
            $sanitized_input['parameter'] = [];
        }
        if ( isset( $input['length'] ) && is_array( $input['length'] ) ) {
            $sanitized_input['length'] = array_map( 'sanitize_text_field', $input['length'] );
        } else {
            $sanitized_input['length'] = [];
        }
        return $sanitized_input;
    }

    /**
     * Render the text for the channel settings
     *
     * @since  1.2.0
     */
    public function rss_chimp_channel_settings_cb() {
        //echo '<p>' . esc_html__('Adapt the feeds created by WordPress to your needs.', 'rss-chimp') . '</p>';
    }

    /**
     * Render the text for the performance settings
     *
     * @since  1.3.0
     */
    public function rss_chimp_performance_settings_cb() {
        echo '<p>' . __( 'Settings to improve the performance and loading time of your feeds.', 'rss-chimp' ) . '</p>';
    }

    /**
     * Render the text for the feed delay settings
     *
     * @since  1.2.0
     */
    public function rss_chimp_feed_delay_settings_cb() {
        //echo '<p>' . esc_html__('Adapt the feeds created by WordPress to your needs.', 'rss-chimp') . '</p>';
    }

    /**
     * Render the text for the thumbnail settings
     *
     * @since  1.2.3
     */
    public function rss_chimp_thumbnail_settings_cb() {
        //echo '<p>' . esc_html__('Adapt the feeds created by WordPress to your needs.', 'rss-chimp') . '</p>';
    }

    /**
     * Render the text for the other settings
     *
     * @since  1.0.0
     */
    public function rss_chimp_other_settings_cb() {
        //echo '<p>' . esc_html__('Adapt the feeds created by WordPress to your needs.', 'rss-chimp') . '</p>';
    }

    /**
     * Render the text for the default feed settings
     *
     * @since  1.0.0
     */
    public function rss_chimp_defaultfeed_settings_cb() {
        $upgradelink = rss_chimp_fs()->get_upgrade_url();
        echo '<p>';
        echo esc_html__( 'Here you can customize the default WordPress feed and set the size of the attached image. ', 'rss-chimp' );
        echo '</p>';
        if ( rss_chimp_fs()->is_not_paying() ) {
            echo '<p>';
            printf( esc_html__( '%1$s for all image sizes and many more features.', 'rss-chimp' ), '<a href="' . esc_url( $upgradelink ) . '">' . esc_html__( 'Upgrade to pro', 'rss-chimp' ) . '</a>' );
            echo '</p>';
        }
    }

    /**
     * Render the select for chosing the channel image
     *
     * @since    1.2.0
     */
    public function rss_chimp_channel_choose_image_cb() {
        echo '<select name="' . $this->option_name . '_channel_choose_image' . '">';
        echo '<option value="off" ' . selected( 'off', get_option( 'rss_chimp_channel_choose_image' ), false ) . '>' . __( 'Disabled', 'rss-chimp' ) . '</option>';
        echo '<option value="favicon" ' . selected( 'favicon', get_option( 'rss_chimp_channel_choose_image' ), false ) . '>' . __( 'Favicon', 'rss-chimp' ) . '</option>';
        echo '<option value="sitelogo" ' . selected( 'sitelogo', get_option( 'rss_chimp_channel_choose_image' ), false ) . '>' . __( 'Site logo', 'rss-chimp' ) . '</option>';
        echo '<option value="customimage" ' . selected( 'customimage', get_option( 'rss_chimp_channel_choose_image' ), false ) . '>' . __( 'Custom image', 'rss-chimp' ) . '</option>';
        echo '</select>';
    }

    /**
     * Render the media select for the channel image
     *
     * @since  1.2.0
     */
    public function rss_chimp_channel_custom_image_cb() {
        echo '<input id="background_image" type="hidden" name="' . $this->option_name . '_channel_custom_image' . '" value="' . esc_attr( get_option( 'rss_chimp_channel_custom_image' ) ) . '" />';
        echo '<input id="upload_image_button" type="button" class="button button-primary kd-media-select-button" value="' . esc_js( __( 'Select logo', 'rss-chimp' ) ) . '" />';
        echo '<input id="reset_image_button" type="button" onclick="ClearFields();" class="button remove-button kd-media-reset-button" value="' . __( 'Remove logo', 'rss-chimp' ) . '" />';
        echo '<p><small>' . __( 'Some services can handle svg images, but if you select an svg image the W3C Feed Validation Service will no longer validate your feed.', 'rss-chimp' ) . '</small></p>';
        $src = esc_url( get_option( 'rss_chimp_channel_custom_image' ) );
        echo '<img id="background_image_thumb" class="kd-media-select-img" src="' . $src . '" alt="" />';
    }

    /**
     * Render the textfield for the channel copyright
     *
     * @since  1.2.0
     */
    public function rss_chimp_channel_copyright_cb() {
        echo '<input type="text" name="' . $this->option_name . '_channel_copyright' . '" value="' . esc_attr( get_option( 'rss_chimp_channel_copyright' ) ) . '" placeholder="' . esc_attr__( '&copy 2022, Company name', 'rss-chimp' ) . '" />';
    }

    /**
     * Render the textfield for the channel editor
     *
     * @since  1.2.0
     */
    public function rss_chimp_channel_editor_cb() {
        echo '<input type="text" name="' . $this->option_name . '_channel_editor' . '" value="' . esc_attr( get_option( 'rss_chimp_channel_editor' ) ) . '" placeholder="' . esc_attr__( 'johnny@example.com (Johnny Controletti)', 'rss-chimp' ) . '" />';
    }

    /**
     * Render the textfield for the channel webmaster
     *
     * @since  1.2.0
     */
    public function rss_chimp_channel_webmaster_cb() {
        echo '<input type="text" name="' . $this->option_name . '_channel_webmaster' . '" value="' . esc_attr( get_option( 'rss_chimp_channel_webmaster' ) ) . '" placeholder="' . esc_attr__( 'jane@example.com (Jane Controletti)', 'rss-chimp' ) . '" />';
    }

    /**
     * Render the option for lazy loading the images in the feed
     *
     * @since  1.3.0
     */
    public function rss_chimp_lazy_loading_cb() {
        $option = get_option( $this->option_name . '_lazy_loading' );
        echo '<input type="checkbox" name="' . $this->option_name . '_lazy_loading' . '" value="1"' . checked( 1, $option, false ) . '/>';
    }

    /**
     * Render the select for enabling/disabling the feed delay
     *
     * @since    1.2.0
     */
    public function rss_chimp_feed_delay_status_cb() {
        echo '<select name="' . $this->option_name . '_feed_delay_status' . '">';
        echo '<option value="off" ' . selected( 'off', get_option( 'rss_chimp_feed_delay_status' ), false ) . '>' . __( 'Disabled', 'rss-chimp' ) . '</option>';
        echo '<option value="on" ' . selected( 'on', get_option( 'rss_chimp_feed_delay_status' ), false ) . '>' . __( 'Enabled', 'rss-chimp' ) . '</option>';
        echo '</select>';
    }

    /**
     * Render the fields for the feed delay
     *
     * @since  1.2.0
     */
    public function rss_chimp_feed_delay_value_cb() {
        $options = get_option( 'rss_chimp_feed_delay_value', [] );
        $args = ( isset( $options ) ? (array) $options : [] );
        ?>
        <input type="number" name="rss_chimp_feed_delay_value[VALUE]" value="<?php 
        echo ( isset( $options['VALUE'] ) ? $options['VALUE'] : '' );
        ?>" />
        <select id="rss_chimp_admin_widget" name="rss_chimp_feed_delay_value[UNIT]">
            <option <?php 
        selected( in_array( 'SECOND', $args ), 1 );
        ?> value="SECOND"><?php 
        _e( 'Seconds', 'rss-chimp' );
        ?></option>
            <option <?php 
        selected( in_array( 'MINUTE', $args ), 1 );
        ?> value="MINUTE"><?php 
        _e( 'Minutes', 'rss-chimp' );
        ?></option>
            <option <?php 
        selected( in_array( 'HOUR', $args ), 1 );
        ?> value="HOUR"><?php 
        _e( 'Hours', 'rss-chimp' );
        ?></option>
            <option <?php 
        selected( in_array( 'DAY', $args ), 1 );
        ?> value="DAY"><?php 
        _e( 'Days', 'rss-chimp' );
        ?></option>
            <option <?php 
        selected( in_array( 'WEEK', $args ), 1 );
        ?> value="WEEK"><?php 
        _e( 'Weeks', 'rss-chimp' );
        ?></option>
            <option <?php 
        selected( in_array( 'MONTH', $args ), 1 );
        ?> value="MONTH"><?php 
        _e( 'Months', 'rss-chimp' );
        ?></option>
            <option <?php 
        selected( in_array( 'YEAR', $args ), 1 );
        ?> value="YEAR"><?php 
        _e( 'Years', 'rss-chimp' );
        ?></option>
        </select>
        <?php 
    }

    /**
     * Render the checkbox for removing version number from feeds
     *
     * @since  1.2.6
     */
    public function rss_chimp_disable_commentsfeed_cb() {
        echo '<input type="checkbox" name="' . $this->option_name . '_disable_commentsfeed' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_disable_commentsfeed' ), false ) . ' />';
    }

    /**
     * Render the options for thumbnails
     *
     * @since  1.2.3
     */
    public function rss_chimp_thumbnail_small_cb() {
        echo '<label for="rss_chimp_thumbnail_small">';
        echo '<input type="checkbox" name="' . $this->option_name . '_thumbnail_small' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_thumbnail_small' ), false ) . ' />';
        echo '(150x84 Pixel, cropped)';
        echo '</label">';
    }

    public function rss_chimp_thumbnail_medium_cb() {
        echo '<label for="rss_chimp_thumbnail_medium">';
        echo '<input type="checkbox" name="' . $this->option_name . '_thumbnail_medium' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_thumbnail_medium' ), false ) . ' />';
        echo '(640x360 Pixel, cropped)';
        echo '</label">';
    }

    /**
     * Render the checkbox for reseting the plugin upon activation
     *
     * @since  1.1.0
     */
    public function rss_chimp_reset_cb() {
        echo '<input type="checkbox" name="' . $this->option_name . '_reset' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_reset' ), false ) . ' />';
    }

    /**
     * Render the checkbox for disabeling the default feeds
     *
     * @since  1.0.0
     */
    public function rss_chimp_defaultfeed_disable_cb() {
        echo '<input type="checkbox" name="' . $this->option_name . '_defaultfeed_disable' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_defaultfeed_disable' ), false ) . ' />';
    }

    /**
     * Render the select for choosing the default feed image size
     *
     * @since  1.0.0
     */
    public function rss_chimp_defaultfeed_imagesize_cb() {
        if ( rss_chimp_fs()->is_plan( 'pro', true ) ) {
            $position = get_option( $this->option_name . '_defaultfeed_imagesize' );
            $image_sizes = get_intermediate_image_sizes();
            echo '<select name="' . $this->option_name . '_defaultfeed_imagesize' . '" class="input">';
            foreach ( $image_sizes as $size ) {
                echo '<option value="' . $size . '"' . selected( get_option( 'rss_chimp_defaultfeed_imagesize' ), $size ) . '>' . $size . '</option>';
            }
            echo '</select>';
        } else {
            $position = get_option( $this->option_name . '_defaultfeed_imagesize' );
            if ( get_option( 'rss_chimp_thumbnail_small' ) == '1' ) {
                $image_sizes = array(
                    "thumbnail"       => "thumbnail",
                    "medium"          => "medium",
                    "rss-chimp-small" => "rss-chimp-small",
                );
            } else {
                $image_sizes = array(
                    "thumbnail" => "thumbnail",
                    "medium"    => "medium",
                );
            }
            echo '<select name="' . $this->option_name . '_defaultfeed_imagesize' . '" class="input">';
            foreach ( $image_sizes as $size ) {
                echo '<option value="' . $size . '"' . selected( get_option( 'rss_chimp_defaultfeed_imagesize' ), $size ) . '>' . $size . '</option>';
            }
            echo '</select>';
        }
    }

    /**
     * Render the checkbox for the image enclosure tag
     *
     * @since  1.2.4
     */
    public function rss_chimp_defaultfeed_enclosure_cb() {
        echo '<input type="checkbox" name="' . $this->option_name . '_defaultfeed_enclosure' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_defaultfeed_enclosure' ), false ) . ' />';
    }

    /**
     * Render the checkbox for the image title in the default feed
     *
     * @since  1.2.1
     */
    public function rss_chimp_defaultfeed_image_title_cb() {
        echo '<input type="checkbox" name="' . $this->option_name . '_defaultfeed_image_title' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_defaultfeed_image_title' ), false ) . ' />';
    }

    /**
     * Render the checkbox for the image description in the default feed
     *
     * @since  1.2.1
     */
    public function rss_chimp_defaultfeed_image_description_cb() {
        echo '<input type="checkbox" name="' . $this->option_name . '_defaultfeed_image_description' . '" value="1" ' . checked( 1, get_option( 'rss_chimp_defaultfeed_image_description' ), false ) . ' />';
    }

    /**
     * Render the input for the default feed URL parameter
     *
     * @since  1.2.2
     */
    public function rss_chimp_defaultfeed_link_parameter_cb() {
        echo '<input type="text" name="' . $this->option_name . '_defaultfeed_link_parameter' . '" value="' . get_option( 'rss_chimp_defaultfeed_link_parameter' ) . '" />';
    }

    /**
     * Render the fields for the custom feed
     *
     * @since  1.0.0
     */
    public function rss_chimp_customfeed_cb() {
        $options = get_option( 'rss_chimp_customfeed', [] );
        if ( !$options || empty( $options['name'] ) ) {
            $options = [
                'name'              => [""],
                'imagesize'         => [""],
                'image_title'       => [0],
                'image_description' => [0],
                'enclosure'         => [0],
                'htmlbefore'        => [""],
                'htmlafter'         => [""],
                'parameter'         => [""],
                'length'            => ["excerpt"],
            ];
        }
        echo '<div class="form-main">';
        for ($i = 0; $i < count( $options['imagesize'] ); $i++) {
            echo '<div class="form-block" id="rccf' . $i . '">';
            echo '<div class="kd-grid-inline">';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Slug', 'rss-chimp' ) . '</label>';
            echo '<input type="text" name="rss_chimp_customfeed[name][]" value="' . esc_attr( $options['name'][$i] ) . '">';
            echo '</div>';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'URL parameter', 'rss-chimp' ) . '</label>';
            echo '<input type="text" name="rss_chimp_customfeed[parameter][]" value="' . esc_attr( $options['parameter'][$i] ) . '">';
            echo '</div>';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Length', 'rss-chimp' ) . '</label>';
            echo '<select name="rss_chimp_customfeed[length][]" class="input">';
            echo '<option value="excerpt"' . selected( $options['length'][$i], 'excerpt', false ) . '>Excerpt</option>';
            echo '<option value="full"' . selected( $options['length'][$i], 'full', false ) . '>Full</option>';
            echo '</select>';
            echo '</div>';
            echo '</div>';
            echo '<div class="kd-grid-inline">';
            $image_sizes = get_intermediate_image_sizes();
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Image size', 'rss-chimp' ) . '</label>';
            echo '<select name="rss_chimp_customfeed[imagesize][]" class="input">';
            foreach ( $image_sizes as $size ) {
                echo '<option value="' . esc_attr( $size ) . '"' . selected( $options['imagesize'][$i], $size, false ) . '>' . esc_html( $size ) . '</option>';
            }
            echo '</select>';
            echo '</div>';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Include image title', 'rss-chimp' ) . '</label>';
            echo '<select name="rss_chimp_customfeed[image_title][]" class="input">';
            echo '<option value="0"' . selected( $options['image_title'][$i], 0, false ) . '>Disabled</option>';
            echo '<option value="1"' . selected( $options['image_title'][$i], 1, false ) . '>Enabled</option>';
            echo '</select>';
            echo '</div>';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Include image description', 'rss-chimp' ) . '</label>';
            echo '<select name="rss_chimp_customfeed[image_description][]" class="input">';
            echo '<option value="0"' . selected( $options['image_description'][$i], 0, false ) . '>Disabled</option>';
            echo '<option value="1"' . selected( $options['image_description'][$i], 1, false ) . '>Enabled</option>';
            echo '</select>';
            echo '</div>';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Add image with enclosure tag', 'rss-chimp' ) . '</label>';
            echo '<select name="rss_chimp_customfeed[enclosure][]" class="input">';
            echo '<option value="0"' . selected( $options['enclosure'][$i], 0, false ) . '>Disabled</option>';
            echo '<option value="1"' . selected( $options['enclosure'][$i], 1, false ) . '>Enabled</option>';
            echo '</select>';
            echo '</div>';
            echo '</div>';
            echo '<div class="kd-grid-inline">';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Custom HTML before each RSS item', 'rss-chimp' ) . '</label>';
            echo '<textarea type="textarea" name="rss_chimp_customfeed[htmlbefore][]" rows="4" cols="50">' . esc_textarea( $options['htmlbefore'][$i] ) . '</textarea>';
            echo '</div>';
            echo '<div class="kd-grid-group">';
            echo '<label for="email">' . __( 'Custom HTML after each RSS item', 'rss-chimp' ) . '</label>';
            echo '<textarea type="textarea" name="rss_chimp_customfeed[htmlafter][]" rows="4" cols="50">' . esc_textarea( $options['htmlafter'][$i] ) . '</textarea>';
            echo '</div>';
            echo '</div>';
            echo '<div class="kd-grid-inline">';
            echo '<div class="kd-grid-group">';
            echo '<button type="button" class="but_delete"><span class="dashicons dashicons-trash"></span> ' . __( 'Delete', 'rss-chimp' ) . '</button>';
            echo '<button type="button" class="reset-button button" style="display:none;"><span class="dashicons dashicons-trash"></span> ' . __( 'Reset', 'rss-chimp' ) . '</button>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
        echo '</div>';
        echo '<button type="button" id="but_add"><span class="dashicons dashicons-plus"></span> ' . __( 'Add', 'rss-chimp' ) . '</button>';
    }

    /**
     * Add additional image sizes
     *
     * @since    1.0.0
     */
    function rss_chimp_imagesizes() {
        if ( get_option( 'rss_chimp_thumbnail_small' ) == '1' ) {
            add_image_size(
                'rss-chimp-small',
                150,
                84,
                TRUE
            );
        }
    }

    /**
     * Extend the RSS namespace with the media specification
     *
     * @since    1.0.0
     */
    function rss_chimp_add_namespace() {
        echo 'xmlns:media="http://search.yahoo.com/mrss/"' . "\n";
    }

    /**
     * Add the featured image
     *
     * @since    1.0.0
     */
    function rss_chimp_add_featuredimage() {
        global $post;
        $output = '';
        if ( get_post_type() !== 'post' ) {
            return;
        }
        if ( has_post_thumbnail( $post->ID ) ) {
            // Get image size
            $imagesize = get_option( $this->option_name . '_defaultfeed_imagesize' );
            // Get thumbnail url of featured image
            $thumbnail_url = get_the_post_thumbnail_url( $post->ID, $imagesize );
            // Get the file size of the featured image
            $thumbnail_size = 0;
            if ( $thumbnail_url ) {
                $attachment_id = get_post_thumbnail_id( $post->ID );
                $thumbnail_path = get_attached_file( $attachment_id );
                if ( $thumbnail_path && file_exists( $thumbnail_path ) ) {
                    $thumbnail_size = filesize( $thumbnail_path );
                }
            }
            // Get mime type of image
            $mime_type = explode( '/', get_post_mime_type( get_post_thumbnail_id( $post->ID ) ) );
            $mime = ( isset( $mime_type[1] ) ? $mime_type[1] : 'jpeg' );
            // Add image with <enclosure>
            if ( get_option( 'rss_chimp_defaultfeed_enclosure' ) == '1' ) {
                $output .= '<enclosure url="' . esc_url( $thumbnail_url ) . '" length="' . $thumbnail_size . '" type="image/' . $mime . '"/>';
            }
            // Add image with <media:content>
            $output .= '<media:content url="' . esc_url( $thumbnail_url ) . '" medium="image" type="image/' . $mime . '" />';
            // Get title and description of the featured image
            $post_thumbnail = get_posts( array(
                'p'         => get_post_thumbnail_id( $post->ID ),
                'post_type' => 'attachment',
            ) );
            if ( !empty( $post_thumbnail ) ) {
                $image_title = $post_thumbnail[0]->post_title;
                $image_description = $post_thumbnail[0]->post_content;
            } else {
                $image_title = '';
                $image_description = '';
            }
            // Add image title
            if ( get_option( 'rss_chimp_defaultfeed_image_title' ) == '1' ) {
                if ( !empty( $image_title ) ) {
                    $output .= '<media:title><![CDATA[' . $image_title . ']]></media:title>';
                }
            }
            // Add image description
            if ( get_option( 'rss_chimp_defaultfeed_image_description' ) == '1' ) {
                if ( !empty( $image_description ) ) {
                    $output .= '<media:description><![CDATA[' . $image_description . ']]></media:description>';
                }
            }
        }
        echo $output;
    }

    /**
     * Add URL parameter to  default feed links
     *
     * @since    1.2.2
     */
    function rss_chimp_defaultfeed_add_link_parameter( $post_permalink ) {
        return $post_permalink . get_option( 'rss_chimp_defaultfeed_link_parameter' );
    }

    /**
     * Add channel meta to the RSS feed
     *
     * @since    1.2.0
     */
    function rss_chimp_channel_add_image() {
        // Channel logo
        if ( get_option( 'rss_chimp_channel_choose_image' ) == 'favicon' ) {
            // Favicon
            if ( has_site_icon() ) {
                echo '<image>' . "\n";
                $themefavicon = get_option( 'site_icon' );
                $image = wp_get_attachment_image_url( $themefavicon, 'full' );
                echo '<url>' . $image . '</url>' . "\n";
                echo '<title>' . get_bloginfo( 'name' ) . '</title>' . "\n";
                echo '<link>' . get_bloginfo( 'siteurl' ) . '</link>' . "\n";
                echo '</image>' . "\n";
            }
        } else {
            if ( get_option( 'rss_chimp_channel_choose_image' ) == 'sitelogo' ) {
                // Site logo
                if ( has_custom_logo() ) {
                    echo '<image>' . "\n";
                    $themelogo = get_theme_mod( 'custom_logo' );
                    $image = wp_get_attachment_image_src( $themelogo, 'full' );
                    $image_url = $image[0];
                    echo '<url>' . $image_url . '</url>' . "\n";
                    echo '<title>' . get_bloginfo( 'name' ) . '</title>' . "\n";
                    echo '<link>' . get_bloginfo( 'siteurl' ) . '</link>' . "\n";
                    echo '</image>' . "\n";
                }
            } else {
                if ( get_option( 'rss_chimp_channel_choose_image' ) == 'customimage' ) {
                    // Custm image
                    if ( !empty( get_option( 'rss_chimp_channel_custom_image' ) ) ) {
                        echo '<image>' . "\n";
                        echo '<url>' . get_option( 'rss_chimp_channel_custom_image' ) . '</url>' . "\n";
                        echo '<title>' . get_bloginfo( 'name' ) . '</title>' . "\n";
                        echo '<link>' . get_bloginfo( 'siteurl' ) . '</link>' . "\n";
                        echo '</image>' . "\n";
                    }
                }
            }
        }
        // Copyright
        if ( !empty( get_option( 'rss_chimp_channel_copyright' ) ) ) {
            echo '<copyright>' . get_option( 'rss_chimp_channel_copyright' ) . '</copyright>';
        }
        // Editor
        if ( !empty( get_option( 'rss_chimp_channel_editor' ) ) ) {
            echo '<managingEditor>' . get_option( 'rss_chimp_channel_editor' ) . '</managingEditor>';
        }
        // Webmaster
        if ( !empty( get_option( 'rss_chimp_channel_webmaster' ) ) ) {
            echo '<webMaster>' . get_option( 'rss_chimp_channel_webmaster' ) . '</webMaster>';
        }
    }

    /**
     * Removes default WordPress functionality to add favicon as RSS logo
     *
     * @since  1.2.0
     */
    function rss_chimp_channel_remove_favicon() {
        remove_action( 'rss2_head', 'rss2_site_icon' );
    }

    /**
     * Handles the feed delay
     *
     * @since  1.2.0
     */
    function rss_chimp_feed_delay( $where ) {
        global $wpdb;
        $values = get_option( 'rss_chimp_feed_delay_value' );
        if ( is_feed() ) {
            // timestamp in WP-format
            $now = gmdate( 'Y-m-d H:i:s' );
            // value for wait; + device
            $wait = $values['VALUE'];
            // integer
            // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
            $device = $values['UNIT'];
            //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
            // add SQL-sytax to default $where
            $where .= " AND TIMESTAMPDIFF({$device}, {$wpdb->posts}.post_date_gmt, '{$now}') > {$wait} ";
        }
        return $where;
    }

    /**
     * Add generator tag for RSS Chimp
     *
     * @since  1.0.0
     */
    function rss_chimp_add_generatortag() {
        echo '<generator>https://kerosin.digital/rss-chimp</generator>';
    }

    /**
     * Remove the WordPress generator tag from all feeds
     *
     * @since  1.0.0
     */
    function rss_chimp_remove_generatortag() {
        remove_action( 'rss2_head', 'the_generator' );
        remove_action( 'rss_head', 'the_generator' );
        remove_action( 'rdf_header', 'the_generator' );
        remove_action( 'atom_head', 'the_generator' );
        remove_action( 'commentsrss2_head', 'the_generator' );
        remove_action( 'opml_head', 'the_generator' );
        remove_action( 'app_head', 'the_generator' );
        remove_action( 'comments_atom_head', 'the_generator' );
    }

    /**
     * Disable certain feeds
     *
     * @since  1.0.0
     */
    function rss_chimp_redirect_disabled_feed() {
        wp_redirect( home_url() );
        die;
    }

    /**
     * Flush rewrite rules when saving the options page
     *
     * @since  1.2.6
     */
    function rss_chimp_flush_permalinks() {
        $settings_updated = ( isset( $_GET['settings-updated'] ) ? sanitize_text_field( wp_unslash( $_GET['settings-updated'] ) ) : '' );
        if ( $settings_updated ) {
            flush_rewrite_rules();
        }
    }

    /**
     * Admin notices handling
     *
     * @since  1.2.0
     */
    function rss_chimp_admin_notices() {
        $page = ( isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '' );
        if ( $page === 'rss-chimp' ) {
            $settings_updated = ( isset( $_GET['settings-updated'] ) ? sanitize_text_field( wp_unslash( $_GET['settings-updated'] ) ) : '' );
            if ( $settings_updated ) {
                if ( get_option( 'rss_chimp_channel_choose_image' ) == 'favicon' ) {
                    if ( function_exists( 'wp_site_icon' ) ) {
                        if ( !has_site_icon() ) {
                            echo '<div class="notice notice-warning is-dismissible">';
                            echo '<p>' . esc_html__( 'Warning: You have chosen the site favicon as the default RSS channel image, but it is not set. ', 'rss-chimp' ) . '</p>';
                            echo '</div>';
                        }
                    } else {
                        if ( !function_exists( 'wp_site_icon' ) ) {
                            echo '<div class="notice notice-error is-dismissible">';
                            echo '<p>' . esc_html__( 'Error: You have chosen the site favicon as the default RSS channel image, but your template does not support a favicon. ', 'rss-chimp' ) . '</p>';
                            echo '</div>';
                        }
                    }
                }
                if ( get_option( 'rss_chimp_channel_choose_image' ) == 'sitelogo' ) {
                    if ( function_exists( 'the_custom_logo' ) ) {
                        if ( !has_custom_logo() ) {
                            echo '<div class="notice notice-warning is-dismissible">';
                            echo '<p>' . esc_html__( 'Warning: You have chosen the site logo as the default RSS channel image, but it is not set. ', 'rss-chimp' ) . '</p>';
                            echo '</div>';
                        }
                    } else {
                        if ( !function_exists( 'the_custom_logo' ) ) {
                            echo '<div class="notice notice-error is-dismissible">';
                            echo '<p>' . esc_html__( 'Error: You have chosen the site logo as the default RSS channel image, but your template does not support a site logo. ', 'rss-chimp' ) . '</p>';
                            echo '</div>';
                        }
                    }
                }
                if ( get_option( 'rss_chimp_channel_choose_image' ) == 'customimage' ) {
                    if ( get_option( 'rss_chimp_channel_custom_image' ) == '' ) {
                        echo '<div class="notice notice-warning is-dismissible">';
                        echo '<p>' . esc_html__( 'Warning: You have chosen a custom image as the default RSS channel image, but it is not set. ', 'rss-chimp' ) . '</p>';
                        echo '</div>';
                    }
                }
            }
        }
    }

    /**
     * Save settings.
     *
     * @since  1.3.0
     */
    public function rss_chimp_lazy_images( $content ) {
        $content = preg_replace( '/<img(.*?)src=/i', '<img$1loading="lazy" src=', $content );
        return $content;
    }

    /**
     * Save settings.
     *
     * @since  1.3.0
     */
    public function rss_chimp_save_settings() {
        if ( !isset( $_POST['rss_chimp_nonce'] ) || !wp_verify_nonce( $_POST['rss_chimp_nonce'], 'rss_chimp_save_settings' ) ) {
            wp_die( 'Invalid nonce.' );
        }
        // Sanitize and save settings
        // Example: update_option('rss_chimp_setting_example', sanitize_text_field($_POST['rss_chimp_setting_example']));
        // Redirect to settings page after saving
        wp_redirect( admin_url( 'options-general.php?page=rss-chimp&status=success' ) );
        exit;
    }

    /**
     * Migrate old options to the new structure.
     *
     * @since  1.3.0
     */
    public function rss_chimp_migrate_options() {
        if ( get_option( 'rss_chimp_add_customfeed' ) ) {
            $new_options = array(
                'name'              => array(),
                'imagesize'         => array(),
                'image_title'       => array(),
                'image_description' => array(),
                'enclosure'         => array(),
                'htmlbefore'        => array(),
                'htmlafter'         => array(),
                'parameter'         => array(),
                'length'            => array(),
            );
            $new_options['name'][] = get_option( 'rss_chimp_customfeed_name' );
            $new_options['imagesize'][] = get_option( 'rss_chimp_customfeed_imagesize' );
            $new_options['image_title'][] = get_option( 'rss_chimp_customfeed_image_title' );
            $new_options['image_description'][] = get_option( 'rss_chimp_customfeed_image_description' );
            $new_options['enclosure'][] = get_option( 'rss_chimp_customfeed_enclosure' );
            $new_options['htmlbefore'][] = get_option( 'rss_chimp_customfeed_content_before' );
            $new_options['htmlafter'][] = get_option( 'rss_chimp_customfeed_content_after' );
            $new_options['parameter'][] = get_option( 'rss_chimp_customfeed_link_parameter' );
            $new_options['length'][] = get_option( 'rss_chimp_customfeed_lenght' );
            update_option( 'rss_chimp_customfeed', $new_options );
            delete_option( 'rss_chimp_add_customfeed' );
            delete_option( 'rss_chimp_customfeed_name' );
            delete_option( 'rss_chimp_customfeed_imagesize' );
            delete_option( 'rss_chimp_customfeed_image_title' );
            delete_option( 'rss_chimp_customfeed_image_description' );
            delete_option( 'rss_chimp_customfeed_enclosure' );
            delete_option( 'rss_chimp_customfeed_content_before' );
            delete_option( 'rss_chimp_customfeed_content_after' );
            delete_option( 'rss_chimp_customfeed_link_parameter' );
            delete_option( 'rss_chimp_customfeed_lenght' );
        }
    }

}
