<?php
/**
 * Class: Rss_Reader_Widget
 * Name: Lecteur RSS
 * Slug: eac-addon-lecteur-rss
 *
 * Description: Rss_Reader_Widget affiche une liste de médias
 * qui diffuse du contenu au format RSS
 *
 * @since 1.0.0
 */

namespace EACCustomWidgets\Includes\Widgets;

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

use EACCustomWidgets\Core\Eac_Load_Config;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Repeater;
use Elementor\Core\Breakpoints\Manager as Breakpoints_manager;
use Elementor\Plugin;

class Rss_Reader_Widget extends Widget_Base {
	use \EACCustomWidgets\Includes\Traits\Button_Read_More_Trait;

	/**
	 * Le nom de la clé du composant dans le fichier de configuration
	 *
	 * @var $slug
	 *
	 * @access private
	 */
	private $slug = 'lecteur-rss';

	/**
	 * Retrieve widget name.
	 *
	 * @access public
	 *
	 * @return string widget name.
	 */
	public function get_name(): string {
		return Eac_Load_Config::get_widget_name( $this->slug );
	}

	/**
	 * Retrieve widget title.
	 *
	 * @access public
	 *
	 * @return string widget title.
	 */
	public function get_title(): string {
		return Eac_Load_Config::get_widget_title( $this->slug );
	}

	/**
	 * Retrieve widget icon.
	 *
	 * @access public
	 *
	 * @return string widget icon.
	 */
	public function get_icon(): string {
		return Eac_Load_Config::get_widget_icon( $this->slug );
	}

	/**
	 * Affecte le composant à la catégorie définie dans plugin.php
	 *
	 * @access public
	 *
	 * @return array widget category.
	 */
	public function get_categories(): array {
		return Eac_Load_Config::get_widget_categories( $this->slug );
	}

	/**
	 * Load dependent libraries
	 *
	 * @access public
	 *
	 * @return array libraries list.
	 */
	public function get_script_depends(): array {
		return array( 'eac-rss-reader' );
	}

	/**
	 * Load dependent styles
	 * Les styles sont chargés dans le footer
	 *
	 * @access public
	 *
	 * @return array CSS list.
	 */
	public function get_style_depends(): array {
		return array( 'eac-rss-reader' );
	}

	/**
	 * Get widget keywords.
	 *
	 * Retrieve the list of keywords the widget belongs to.
	 *
	 * @access public
	 *
	 * @return array Widget keywords.
	 */
	public function get_keywords(): array {
		return Eac_Load_Config::get_widget_keywords( $this->slug );
	}

	/**
	 * Get help widget get_custom_help_url.
	 *
	 * @access public
	 *
	 * @return string URL help center
	 */
	public function get_custom_help_url(): string {
		return Eac_Load_Config::get_widget_help_url( $this->slug );
	}

	/**
	 * has_widget_inner_wrapper
	 *
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return false;
	}

	/**
	 * is_dynamic_content
	 *
	 * @return bool
	 */
	protected function is_dynamic_content(): bool {
		return true;
	}

	/**
	 * Get Frontend Settings
	 *
	 * In the RSS widget, this implementation is used to pass a pre-rendered version of the icon to the front end,
	 * which is required in case the FontAwesome SVG experiment is active.
	 *
	 * @since 2.3.3
	 *
	 * @return array
	 */
	public function get_frontend_settings(): array {
		$frontend_settings = parent::get_frontend_settings();

		if ( Plugin::$instance->experiments->is_feature_active( 'e_font_icon_svg' ) && ! empty( $frontend_settings['button_more_picto']['value'] ) ) {
			$frontend_settings['button_more_picto']['rendered_tag'] = Icons_Manager::render_font_icon( $frontend_settings['button_more_picto'] );
		}
		return $frontend_settings;
	}

	/**
	 * Register widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @access protected
	 */
	protected function register_controls(): void {

		$this->start_controls_section(
			'rss_galerie_settings',
			array(
				'label' => esc_html__( 'List of RSS feeds', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			$repeater = new Repeater();

			$repeater->add_control(
				'rss_item_title',
				array(
					'label'       => esc_html__( 'Title', 'eac-components' ),
					'type'        => Controls_Manager::TEXT,
					'dynamic'     => array( 'active' => true ),
					'ai'          => array( 'active' => false ),
					'label_block' => true,
				)
			);

			$repeater->add_control(
				'rss_item_url',
				array(
					'label'       => esc_html( 'URL' ),
					'type'        => Controls_Manager::URL,
					'placeholder' => 'https://your-link.com/xml/',
					'render_type' => 'none',
				)
			);

			$this->add_control(
				'rss_image_list',
				array(
					'type'        => Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'default'     => array(
						array(
							'rss_item_title' => 'EAC - Components feed',
							'rss_item_url'   => array( 'url' => 'https://elementor-addon-components.com/feed/' ),
						),
						array(
							'rss_item_title' => 'Reuters - News en-US',
							'rss_item_url'   => array( 'url' => 'https://news.google.com/rss/search?q=when:24h+allinurl:reuters.com&ceid=US:en&hl=en-US&gl=US' ),
						),
						array(
							'rss_item_title' => 'Nasa - Image of the day',
							'rss_item_url'   => array( 'url' => 'https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss' ),
						),
						array(
							'rss_item_title' => 'Youtube Channel - Arte cinéma',
							'rss_item_url'   => array( 'url' => 'https://www.youtube.com/feeds/videos.xml?channel_id=UClo03hULFynpoX3w1Jv7fhw' ),
						),
						array(
							'rss_item_title' => 'Youtube Channel - Euronews',
							'rss_item_url'   => array( 'url' => 'https://www.youtube.com/feeds/videos.xml?channel_id=UCW2QcKZiU8aUGg4yxCIditg' ),
						),
						array(
							'rss_item_title' => 'Youtube Channel - France Inter',
							'rss_item_url'   => array( 'url' => 'https://www.youtube.com/feeds/videos.xml?channel_id=UCJldRgT_D7Am-ErRHQZ90uw' ),
						),
						array(
							'rss_item_title' => 'Youtube Channel - France Musique',
							'rss_item_url'   => array( 'url' => 'https://www.youtube.com/feeds/videos.xml?channel_id=UCiPYZkZeNbYTQZKNMlB0Gsw' ),
						),
						array(
							'rss_item_title' => 'Youtube Channel - FIP',
							'rss_item_url'   => array( 'url' => 'https://www.youtube.com/feeds/videos.xml?channel_id=UCFLkFeQEuU9CHQISq_rp0_Q' ),
						),
						array(
							'rss_item_title' => 'Le monde',
							'rss_item_url'   => array( 'url' => 'https://www.lemonde.fr/rss/en_continu.xml' ),
						),
						array(
							'rss_item_title' => 'Courrier International',
							'rss_item_url'   => array( 'url' => 'https://www.courrierinternational.com/feed/all/rss.xml' ),
						),
						array(
							'rss_item_title' => "L'Équipe",
							'rss_item_url'   => array( 'url' => 'https://dwh.lequipe.fr/api/edito/rss?path=/' ),
						),
						array(
							'rss_item_title' => 'France TV - Info',
							'rss_item_url'   => array( 'url' => 'https://www.francetvinfo.fr/titres.rss' ),
						),
						array(
							'rss_item_title' => 'BBC News - World',
							'rss_item_url'   => array( 'url' => 'https://feeds.bbci.co.uk/news/world/rss.xml' ),
						),
						array(
							'rss_item_title' => 'Corriere della Sera',
							'rss_item_url'   => array( 'url' => 'https://xml2.corriereobjects.it/rss/homepage.xml' ),
						),
						array(
							'rss_item_title' => 'Die Welt',
							'rss_item_url'   => array( 'url' => 'https://www.welt.de/feeds/latest.rss' ),
						),
						array(
							'rss_item_title' => 'WP Marmite',
							'rss_item_url'   => array( 'url' => 'https://feedpress.me/WPMarmite' ),
						),
						array(
							'rss_item_title' => 'CSS Tips',
							'rss_item_url'   => array( 'url' => 'https://css-tip.com/feed/feed.xml' ),
						),
						array(
							'rss_item_title' => 'Smashing Magazine',
							'rss_item_url'   => array( 'url' => 'https://www.smashingmagazine.com/feed/' ),
						),
						array(
							'rss_item_title' => 'Podcast France Inter - Le 7/9',
							'rss_item_url'   => array( 'url' => 'https://radiofrance-podcast.net/podcast09/rss_10241.xml' ),
						),
						array(
							'rss_item_title' => 'Podcast France Culture - La méthode scientifique',
							'rss_item_url'   => array( 'url' => 'https://radiofrance-podcast.net/podcast09/rss_14312.xml' ),
						),
						array(
							'rss_item_title' => 'Podcast Collège de France - Tous les podcasts',
							'rss_item_url'   => array( 'url' => 'https://podcast.college-de-france.fr/xml/general.xml' ),
						),
						array(
							'rss_item_title' => 'Podcast BBC - BBC World Service',
							'rss_item_url'   => array( 'url' => 'https://podcasts.files.bbci.co.uk/p02nq0gn.rss' ),
						),
					),
					'title_field' => '{{{ rss_item_title }}}',
					'button_text' => esc_html__( 'Add feed', 'eac-components' ),
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'rss_layout_type_settings',
			array(
				'label' => esc_html__( 'Layout', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			$active_breakpoints  = Plugin::$instance->breakpoints->get_active_breakpoints();
			$columns_device_args = array();
		foreach ( $active_breakpoints as $breakpoint_name => $breakpoint_instance ) {
			if ( Breakpoints_manager::BREAKPOINT_KEY_WIDESCREEN === $breakpoint_name ) {
				$columns_device_args[ $breakpoint_name ] = array( 'default' => '4' );
			} elseif ( Breakpoints_manager::BREAKPOINT_KEY_LAPTOP === $breakpoint_name ) {
				$columns_device_args[ $breakpoint_name ] = array( 'default' => '3' );
			} elseif ( Breakpoints_manager::BREAKPOINT_KEY_TABLET_EXTRA === $breakpoint_name ) {
					$columns_device_args[ $breakpoint_name ] = array( 'default' => '3' );
			} elseif ( Breakpoints_manager::BREAKPOINT_KEY_TABLET === $breakpoint_name ) {
					$columns_device_args[ $breakpoint_name ] = array( 'default' => '3' );
			} elseif ( Breakpoints_manager::BREAKPOINT_KEY_MOBILE_EXTRA === $breakpoint_name ) {
				$columns_device_args[ $breakpoint_name ] = array( 'default' => '2' );
			} elseif ( Breakpoints_manager::BREAKPOINT_KEY_MOBILE === $breakpoint_name ) {
				$columns_device_args[ $breakpoint_name ] = array( 'default' => '1' );
			}
		}

			$this->add_responsive_control(
				'rss_columns',
				array(
					'label'        => esc_html__( 'Columns count', 'eac-components' ),
					'type'         => Controls_Manager::SELECT,
					'default'      => '3',
					'device_args'  => $columns_device_args,
					'options'      => array(
						'1' => '1',
						'2' => '2',
						'3' => '3',
						'4' => '4',
						'5' => '5',
						'6' => '6',
					),
					'prefix_class' => 'responsive%s-',
				)
			);

			$this->add_control(
				'rss_image_settings',
				array(
					'label'     => esc_html__( 'Image layout', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'condition' => array( 'rss_item_image' => 'yes' ),
					'separator' => 'before',
				)
			);

			$this->add_control(
				'rss_item_image_height_auto',
				array(
					'label'        => esc_html__( 'Auto height', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_responsive_control(
				'rss_item_image_height',
				array(
					'label'          => esc_html__( 'Image height', 'eac-components' ),
					'type'           => Controls_Manager::SLIDER,
					'size_units'     => array( 'px' ),
					'default'        => array(
						'size' => 200,
						'unit' => 'px',
					),
					'laptop_default' => array(
						'size' => 200,
						'unit' => 'px',
					),
					'tablet_default' => array(
						'size' => 200,
						'unit' => 'px',
					),
					'mobile_default' => array(
						'size' => 150,
						'unit' => 'px',
					),
					'range'          => array(
						'px' => array(
							'min'  => 0,
							'max'  => 500,
							'step' => 50,
						),
					),
					'selectors'      => array( '{{WRAPPER}} .rss-galerie__item-image img' => 'block-size: {{SIZE}}{{UNIT}};' ),
					'condition'      => array( 'rss_item_image_height_auto!' => 'yes' ),
				)
			);

			$this->add_responsive_control(
				'rss_item_image_position_y',
				array(
					'label'       => esc_html__( 'Vertical position', 'eac-components' ),
					'type'        => Controls_Manager::SLIDER,
					'size_units'  => array( '%' ),
					'default'     => array(
						'size' => 50,
						'unit' => '%',
					),
					'range'       => array(
						'%' => array(
							'min'  => 0,
							'max'  => 100,
							'step' => 5,
						),
					),
					'selectors'   => array( '{{WRAPPER}} .rss-galerie__item-image img' => 'object-position: 50% {{SIZE}}%;' ),
					'render_type' => 'ui',
					'condition'   => array( 'rss_item_image_height_auto!' => 'yes' ),
				)
			);

			$this->add_control(
				'rss_content_layout',
				array(
					'label'     => esc_html__( 'Content layout', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
				)
			);

			$this->add_responsive_control(
				'rss_item_content_align_v',
				array(
					'label'       => esc_html__( 'Vertical alignment', 'eac-components' ),
					'type'        => Controls_Manager::CHOOSE,
					'options'     => array(
						'flex-start'    => array(
							'title' => esc_html__( 'Top', 'eac-components' ),
							'icon'  => 'eicon-justify-start-v',
						),
						'center'        => array(
							'title' => esc_html__( 'Center', 'eac-components' ),
							'icon'  => 'eicon-justify-center-v',
						),
						'flex-end'      => array(
							'title' => esc_html__( 'Bottom', 'eac-components' ),
							'icon'  => 'eicon-justify-end-v',
						),
						'space-between' => array(
							'title' => esc_html__( 'Space between', 'eac-components' ),
							'icon'  => 'eicon-justify-space-between-v',
						),
						'space-around'  => array(
							'title' => esc_html__( 'Space around', 'eac-components' ),
							'icon'  => 'eicon-justify-space-around-v',
						),
						'space-evenly'  => array(
							'title' => esc_html__( 'Space evenly', 'eac-components' ),
							'icon'  => 'eicon-justify-space-evenly-v',
						),
					),
					'default'     => 'flex-start',
					'label_block' => true,
					'selectors'   => array( '{{WRAPPER}} .rss-galerie__content-inner' => 'justify-content: {{VALUE}};' ),
				)
			);

			$start = is_rtl() ? 'right' : 'left';
			$end   = is_rtl() ? 'left' : 'right';
			$this->add_responsive_control(
				'rss_item_content_align_h',
				array(
					'label'     => esc_html__( 'Horizontal alignment', 'eac-components' ),
					'type'      => Controls_Manager::CHOOSE,
					'options'   => array(
						'start'  => array(
							'title' => is_rtl() ? esc_html__( 'Right', 'eac-components' ) : esc_html__( 'Left', 'eac-components' ),
							'icon'  => "eicon-h-align-{$start}",
						),
						'center' => array(
							'title' => esc_html__( 'Center', 'eac-components' ),
							'icon'  => 'eicon-h-align-center',
						),
						'end'    => array(
							'title' => is_rtl() ? esc_html__( 'Left', 'eac-components' ) : esc_html__( 'Right', 'eac-components' ),
							'icon'  => "eicon-h-align-{$end}",
						),
					),
					'default'   => 'center',
					'toggle'    => false,
					'selectors' => array(
						'{{WRAPPER}} .rss-galerie__content-inner' => 'align-items: {{VALUE}}; text-align: {{VALUE}};',
						'{{WRAPPER}} .rss-galerie__content-inner .buttons-wrapper' => 'justify-content: {{VALUE}};',
					),
				)
			);

		$this->end_controls_section();

		$this->start_controls_section(
			'rss_items_content',
			array(
				'label' => esc_html__( 'Content', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			/** Les articles */
			$this->add_control(
				'rss_item_posts',
				array(
					'label' => esc_html__( 'Post', 'eac-components' ),
					'type'  => Controls_Manager::HEADING,
				)
			);

			$this->add_control(
				'rss_item_image',
				array(
					'label'        => esc_html__( 'Image', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'rss_content_item_title',
				array(
					'label'        => esc_html__( 'Title', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'rss_item_excerpt',
				array(
					'label'        => esc_html__( 'Excerpt', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'rss_item_date',
				array(
					'label'        => esc_html__( 'Publication/Author date', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
				)
			);

			/** Les nombres */
			$this->add_control(
				'rss_item_numbers',
				array(
					'label'     => esc_html__( 'Density', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
				)
			);

			$this->add_control(
				'rss_item_nombre',
				array(
					'label'       => esc_html__( 'Post count', 'eac-components' ),
					'type'        => Controls_Manager::NUMBER,
					'min'         => 5,
					'max'         => 50,
					'step'        => 5,
					'default'     => 20,
				)
			);

			$this->add_control(
				'rss_item_length',
				array(
					'label'       => esc_html__( 'Number of words', 'eac-components' ),
					'description' => esc_html__( 'Excerpt', 'eac-components' ),
					'type'        => Controls_Manager::NUMBER,
					'min'         => 3,
					'max'         => 100,
					'step'        => 5,
					'default'     => 25,
					'render_type' => 'none',
					'condition'   => array( 'rss_item_excerpt' => 'yes' ),
				)
			);

			/** Les liens */
			$this->add_control(
				'rss_item_links',
				array(
					'label'     => esc_html__( 'Link', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
				)
			);

			$this->add_control(
				'rss_item_readmore',
				array(
					'label'        => esc_html__( 'Button', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => 'yes',
				)
			);

			$this->add_control(
				'rss_item_title_link',
				array(
					'label'        => esc_html__( 'Post link on title', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
					'condition'    => array(
						'rss_content_item_title' => 'yes',
					),
				)
			);

			$this->add_control(
				'rss_item_image_link',
				array(
					'label'        => esc_html__( 'Post link on image', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
					'condition'    => array(
						'rss_item_image'     => 'yes',
						'rss_item_lightbox!' => 'yes',
					),
				)
			);

			$this->add_control(
				'rss_item_article_link',
				array(
					'label'        => esc_html__( 'Apply the link globally', 'eac-components' ),
					'description'  => esc_html__( 'The link wraps each item', 'eac-components' ),
					'type'      => Controls_Manager::CHOOSE,
					'options'   => array(
						'yes' => array(
							'title' => esc_html__( 'Yes', 'eac-components' ),
							'icon'  => 'eicon-check',
						),
						'no'  => array(
							'title' => esc_html__( 'No', 'eac-components' ),
							'icon'  => 'eicon-ban',
						),
					),
					'default'   => 'no',
					'toggle'    => false,
					'conditions' => array(
						'relation' => 'or',
						'terms'    => array(
							array(
								'name'     => 'rss_item_readmore',
								'operator' => '===',
								'value'    => 'yes',
							),
							array(
								'relation' => 'and',
								'terms'    => array(
									array(
										'name'     => 'rss_content_item_title',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'rss_item_title_link',
										'operator' => '===',
										'value'    => 'yes',
									),
								),
							),
							array(
								'relation' => 'and',
								'terms'    => array(
									array(
										'name'     => 'rss_item_image',
										'operator' => '===',
										'value'    => 'yes',
									),
									array(
										'name'     => 'rss_item_image_link',
										'operator' => '===',
										'value'    => 'yes',
									),
								),
							),
						),
					),
				)
			);

			$this->add_control(
				'rss_item_lightbox',
				array(
					'label'        => esc_html__( 'Lightbox', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'eac-components' ),
					'label_off'    => esc_html__( 'No', 'eac-components' ),
					'return_value' => 'yes',
					'default'      => '',
					'condition'    => array(
						'rss_item_image'         => 'yes',
						'rss_item_image_link!'   => 'yes',
						'rss_item_article_link!' => 'yes',
					),
				)
			);

			/** Le bouton */
			$this->add_control(
				'rss_item_buttons',
				array(
					'label'     => esc_html__( 'Button Read the feed', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
				)
			);

			$this->add_control(
				'rss_item_button_label',
				array(
					'label'       => esc_html__( 'Label', 'eac-components' ),
					'type'        => Controls_Manager::TEXT,
					'default'     => esc_html__( 'Read the feed', 'eac-components' ),
					'dynamic'     => array( 'active' => true ),
					'ai'          => array( 'active' => false ),
					'label_block' => true,
				)
			);

			$this->add_control(
				'rss_more_settings',
				array(
					'label'     => esc_html__( 'Button', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'condition' => array( 'rss_item_readmore' => 'yes' ),
					'separator' => 'before',
				)
			);

			// Trait du contenu du bouton read more
			$this->register_button_more_content_controls( array( 'control_condition' => array( 'rss_item_readmore' => 'yes' ) ) );

		$this->end_controls_section();

		$this->start_controls_section(
			'rss_general_style',
			array(
				'label' => esc_html__( 'General', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

			/** Conteneur */
			$this->add_control(
				'rss_container_style',
				array(
					'label'     => esc_html__( 'Container', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'      => 'rss_list_options_typo',
					'label'     => esc_html__( 'List typography', 'eac-components' ),
					'selector'  => '.rss-options__items-list .select__options-items',
				)
			);

			$this->add_control(
				'rss_wrapper_bg_color',
				array(
					'label'     => esc_html__( 'Background color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( '{{WRAPPER}} .eac-rss-galerie' => 'background-color: {{VALUE}};' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'      => 'rss_wrapper_border',
					'selector'  => '{{WRAPPER}} .eac-rss-galerie',
				)
			);

			$this->add_control(
				'rss_wrapper_radius',
				array(
					'label'      => esc_html__( 'Border radius', 'eac-components' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array(
						'{{WRAPPER}} .eac-rss-galerie' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'      => 'rss_wrapper_shadow',
					'label'     => esc_html__( 'Shadow', 'eac-components' ),
					'selector'  => '{{WRAPPER}} .eac-rss-galerie',
				)
			);

			/** La liste select */
			$this->add_control(
				'rss_select_style',
				array(
					'label'     => esc_html__( 'List RSS feeds', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => 'rss_select_typography',
					'label'    => esc_html__( 'Typography', 'eac-components' ),
					'selector' => '{{WRAPPER}} .select__options-items',
				)
			);

			/** Articles */
			$this->add_control(
				'rss_items_style',
				array(
					'label'     => esc_html__( 'Post', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
				)
			);

			$this->add_control(
				'rss_wrapper_style',
				array(
					'label'        => esc_html__( 'Style', 'eac-components' ),
					'type'         => Controls_Manager::SELECT,
					'default'      => 'style-0',
					'options'      => array(
						'style-0' => esc_html__( 'Default', 'eac-components' ),
						'style-1' => 'Style 1',
						'style-2' => 'Style 2',
						'style-3' => 'Style 3',
						'style-4' => 'Style 4',
						'style-5' => 'Style 5',
						'style-6' => 'Style 6',
						'style-7' => 'Style 7',
						'style-8' => 'Style 8',
						'style-9' => 'Style 9',
					),
					'prefix_class' => 'rss-galerie_wrapper-',
				)
			);

			$this->add_responsive_control(
				'rss_wrapper_gap',
				array(
					'label'      => esc_html__( 'Margin between items', 'eac-components' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px' ),
					'default'    => array(
						'size' => 20,
						'unit' => 'px',
					),
					'range'      => array(
						'px' => array(
							'min'  => 0,
							'max'  => 50,
							'step' => 1,
						),
					),
					'selectors'  => array(
						'{{WRAPPER}} .rss-galerie' => 'gap: {{SIZE}}{{UNIT}}; padding-block: calc({{SIZE}}{{UNIT}} / 2); padding-inline: calc({{SIZE}}{{UNIT}} / 2);',
						'{{WRAPPER}} .rss-item__header' => 'margin-inline: calc({{SIZE}}{{UNIT}} / 2);',
					),
				)
			);

			$this->add_control(
				'rss_items_bg_color',
				array(
					'label'     => esc_html__( 'Background color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( '{{WRAPPER}} .rss-galerie__item' => 'background-color: {{VALUE}};' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'      => 'rss_items_border',
					'selector'  => '{{WRAPPER}} .rss-galerie__item',
					'condition' => array( 'rss_wrapper_style' => 'style-0' ),
				)
			);

			$this->add_control(
				'rss_items_radius',
				array(
					'label'      => esc_html__( 'Border radius', 'eac-components' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array(
						'{{WRAPPER}} .rss-galerie__item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
					'condition'  => array( 'rss_wrapper_style' => 'style-0' ),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'      => 'rss_items_shadow',
					'label'     => esc_html__( 'Shadow', 'eac-components' ),
					'selector'  => '{{WRAPPER}} .rss-galerie__item',
					'condition' => array( 'rss_wrapper_style' => 'style-0' ),
				)
			);

			/** Image */
			$this->add_control(
				'rss_image_style',
				array(
					'label'     => esc_html__( 'Image', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => array( 'rss_item_image' => 'yes' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'      => 'rss_image_border',
					'selector'  => '{{WRAPPER}} .rss-galerie__item-image img',
					'condition' => array( 'rss_item_image' => 'yes' ),
				)
			);

			$this->add_control(
				'rss_image_border_radius',
				array(
					'label'              => esc_html__( 'Border radius', 'eac-components' ),
					'type'               => Controls_Manager::DIMENSIONS,
					'size_units'         => array( 'px', '%' ),
					'allowed_dimensions' => array( 'top', 'right', 'bottom', 'left' ),
					'default'            => array(
						'top'      => 0,
						'right'    => 0,
						'bottom'   => 0,
						'left'     => 0,
						'unit'     => 'px',
						'isLinked' => true,
					),
					'selectors'          => array(
						'{{WRAPPER}} .rss-galerie__item-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
					'condition' => array( 'rss_item_image' => 'yes' ),
				)
			);

			/** Titre */
			$this->add_control(
				'rss_title_style',
				array(
					'label'     => esc_html__( 'Title', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => array( 'rss_content_item_title' => 'yes' ),
				)
			);

			$this->add_control(
				'rss_titre_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( '{{WRAPPER}} .rss-galerie__item-titre' => 'color: {{VALUE}};' ),
					'condition' => array( 'rss_content_item_title' => 'yes' ),
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => 'rss_titre_typography',
					'label'    => esc_html__( 'Typography', 'eac-components' ),
					'selector' => '{{WRAPPER}} .rss-galerie__item-titre',
					'condition' => array( 'rss_content_item_title' => 'yes' ),
				)
			);

			/** Résumé */
			$this->add_control(
				'rss_excerpt_style',
				array(
					'label'     => esc_html__( 'Excerpt', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => array( 'rss_item_excerpt' => 'yes' ),
				)
			);

			$this->add_control(
				'rss_excerpt_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( '{{WRAPPER}} .rss-galerie__item-description p' => 'color: {{VALUE}};' ),
					'condition' => array( 'rss_item_excerpt' => 'yes' ),
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'      => 'rss_excerpt_typography',
					'label'     => esc_html__( 'Typography', 'eac-components' ),
					'selector'  => '{{WRAPPER}} .rss-galerie__item-description p',
					'condition' => array( 'rss_item_excerpt' => 'yes' ),
				)
			);

			/** Date/Auteur */
			$this->add_control(
				'rss_icone_style',
				array(
					'label'     => esc_html__( 'Publication/Author date', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => array( 'rss_item_date' => 'yes' ),
				)
			);

			$this->add_control(
				'rss_icone_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .rss-galerie__item-date, {{WRAPPER}} .rss-galerie__item-auteur' => 'color: {{VALUE}};',
					),
					'condition' => array( 'rss_item_date' => 'yes' ),
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => 'rss_icone_typography',
					'label'    => esc_html__( 'Typography', 'eac-components' ),
					'selector' => '{{WRAPPER}} .rss-galerie__item-date, {{WRAPPER}} .rss-galerie__item-auteur',
					'condition' => array( 'rss_item_date' => 'yes' ),
				)
			);

			$this->add_control(
				'rss_more_style',
				array(
					'label'     => esc_html__( 'Button', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'condition' => array( 'rss_item_readmore' => 'yes' ),
					'separator' => 'before',
				)
			);

			// Trait Style du bouton read more
			$this->register_button_more_style_controls( array( 'control_condition' => array( 'rss_item_readmore' => 'yes' ) ) );

		$this->end_controls_section();
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @access protected
	 */
	protected function render(): void {
		$settings = $this->get_settings_for_display();
		if ( ! $settings['rss_image_list'] ) {
			return;
		}
		$this->add_render_attribute( 'rss_galerie', 'class', 'rss-galerie' );
		$this->add_render_attribute( 'rss_galerie', 'id', 'rss-galerie' );
		$this->add_render_attribute( 'rss_galerie', 'role', 'feed' );
		$this->add_render_attribute( 'rss_galerie', 'aria-busy', 'false' );
		$this->add_render_attribute( 'rss_galerie', 'data-settings', $this->get_settings_json() );
		?>
		<div class='eac-rss-galerie'>
			<input type='hidden' id='rss_nonce' name='rss_nonce' value="<?php echo wp_create_nonce( 'eac_rss_feed_' . esc_attr( $this->get_id() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" />
			<?php $this->render_galerie(); ?>
			<div <?php $this->print_render_attribute_string( 'rss_galerie' ); ?>></div>
		</div>
		<?php
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @access protected
	 */
	protected function render_galerie(): void {
		$settings = $this->get_settings_for_display();
		$id       = $this->get_id();
		?>
		<div class='rss-select__item-list'>
			<div class='rss-options__items-list'>
				<label id="label_<?php echo esc_attr( $id ); ?>" class='visually-hidden' for="listbox_<?php echo esc_attr( $id ); ?>"><?php esc_html_e( 'List of RSS feeds', 'eac-components' ); ?></label>
				<select id="listbox_<?php echo esc_attr( $id ); ?>" class='select__options-items' aria-labelledby="label_<?php echo esc_attr( $id ); ?>">
					<?php foreach ( $settings['rss_image_list'] as $item ) {
						if ( ! empty( $item['rss_item_url']['url'] ) && $this->is_valid_url( $item['rss_item_url']['url'] ) ) : ?>
							<option value="<?php echo esc_url( $item['rss_item_url']['url'] ); ?>"><?php echo esc_html( $item['rss_item_title'] ); ?></option>
						<?php endif;
					} ?>
				</select>
			</div>
			<?php
			$this->add_render_attribute( 'read_button', 'class', 'eac-accessible-link button__readmore-wrapper' );
			$this->add_render_attribute( 'read_button', 'id', 'rss__read-button' );
			$this->add_render_attribute( 'read_button', 'type', 'button' );
			$this->add_render_attribute( 'read_button', 'aria-expanded', 'false' );
			$this->add_render_attribute( 'read_button', 'aria-controls', 'rss-galerie' );
			$this->add_render_attribute( 'read_button', 'aria-label', esc_attr__( 'View content of selected feed', 'eac-components' ) );
			$this->add_inline_editing_attributes( 'rss_item_button_label', 'none' );
			$this->add_render_attribute( 'rss_item_button_label', 'class', 'label-icon' );
			$label        = ! empty( $settings['rss_item_button_label'] ) ? esc_html( $settings['rss_item_button_label'] ) : esc_html__( 'Read the feed', 'eac-components' );
			$label_button = '<span ' . $this->get_render_attribute_string( 'rss_item_button_label' ) . '>' . trim( $label ) . '</span>';
			$button       = '<button ' . $this->get_render_attribute_string( 'read_button' ) . '>' . trim( $label_button ) . '</button>';
			?>
			<div class='buttons-wrapper'><?php echo wp_kses_post( $button ); ?></div>
			<div id='rss__loader-wheel' class='eac__loader-spin'></div>
		</div>
		<div class='rss-item__header'></div>
		<?php
	}

	/**
	 * is_valid_url
	 *
	 * @param mixed $url
	 *
	 * @return bool
	 */
	private function is_valid_url( $url ): bool {
		return ! preg_match( '/\bjavascript\b/i', $url ) && filter_var( $url, FILTER_VALIDATE_URL );
	}

	/**
	 * get_settings_json()
	 *
	 * Retrieve fields values to pass at the widget container
	 * Convert on JSON format
	 *
	 * @uses      wp_json_encode()
	 *
	 * @return    JSON oject
	 */

	protected function get_settings_json(): string {
		$settings = $this->get_settings_for_display();

		$module_settings = array(
			'data_id'             => esc_attr( $this->get_id() ),
			'data_nombre'         => ! empty( $settings['rss_item_nombre'] ) ? absint( $settings['rss_item_nombre'] ) : 20,
			'data_date'           => 'yes' === $settings['rss_item_date'] ? true : false,
			'data_img'            => 'yes' === $settings['rss_item_image'] ? true : false,
			'data_lightbox'       => 'yes' === $settings['rss_item_lightbox'] ? true : false,
			'data_image_link'     => 'yes' === $settings['rss_item_image_link'] ? true : false,
			'data_title'          => 'yes' === $settings['rss_content_item_title'] ? true : false,
			'data_title_link'     => 'yes' === $settings['rss_item_title_link'] ? true : false,
			'data_excerpt'        => 'yes' === $settings['rss_item_excerpt'] ? true : false,
			'data_excerpt_lenght' => 'yes' === $settings['rss_item_excerpt'] && ! empty( $settings['rss_item_length'] ) ? absint( $settings['rss_item_length'] ) : 25,
			'data_readmore'       => 'yes' === $settings['rss_item_readmore'] ? true : false,
			'data_readmore_label' => 'yes' === $settings['rss_item_readmore'] ? esc_html( trim( $settings['button_more_label'] ) ) : '',
			'data_icon_pos'       => isset( $settings['button_more_position'] ) ? $settings['button_more_position'] : '',
			'data_global_link'    => isset( $settings['rss_item_article_link'] ) && 'yes' === $settings['rss_item_article_link'] ? true : false,
			'data_locale'         => str_replace( '_', '-', get_user_locale() ),
		);

		return wp_json_encode( $module_settings );
	}

	protected function content_template(): void {}
}
