<?php
/**
 * Class: Cookies_Tag
 *
 * @return affiche la liste des cookies et leurs valeurs
 * @since 1.6.5
 * @since 1.7.0 Transformer SELECT en SELECT2
 *              Affiche cookie name::cookie value
 */

namespace EACCustomWidgets\Includes\Elementor\DynamicTags\Tags;

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

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module as TagsModule;
use Elementor\Controls_Manager;

class Cookies_Tag extends Tag {

	public function get_name(): string {
		return 'eac-addon-site-cookies';
	}

	public function get_title(): string {
		return esc_html( 'Cookies' );
	}

	public function get_group(): array {
		return array( 'eac-site-groupe' );
	}

	public function get_categories(): array {
		return array( TagsModule::TEXT_CATEGORY );
	}

	public function get_panel_template_setting_key(): string {
		return 'cookie_name';
	}

	protected function register_controls(): void {

		$this->add_control(
			'cookie_name',
			array(
				'label'       => esc_html__( 'Cookie name', 'eac-components' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'default'     => array(),
				'options'     => $this->get_cookies_names(),
				'multiple'    => true,

			)
		);
	}

	public function render(): void {
		$cookies_array = array();
		$settings      = $this->get_settings();
		$keys          = $settings['cookie_name'];
		if ( empty( $keys ) ) {
			return;
		}

		// @since 1.7.0
		foreach ( $keys as $key ) {
			$cookies_array[] = '<b>' . $key . '</b>::' . filter_input( INPUT_COOKIE, $key );
		}
		echo wp_kses_post( implode( '<br/>', $cookies_array ) );
	}

	private function get_cookies_names() {
		$cookies_names = array();
		foreach ( $_COOKIE as $key => $val ) {
			$key                   = esc_attr( $key );
			$cookies_names[ $key ] = $key;
		}
		return $cookies_names;
	}
}
