<?php
/**
 * Elementor Dynamic Cookie Name tag.
 *
 * @link       https://chrisjayden.com
 * @since      1.0.0
 *
 * @package    Yummy_Cookies
 * @subpackage Yummy_Cookies/Yummy_Cookies_Elementor_Cookie_Name
 */
class Yummy_Cookies_Elementor_Cookie_Name extends \Elementor\Core\DynamicTags\Tag {

	/**
	 * Get Name
	 *
	 * Returns the Name of the tag
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string
	 */
	public function get_name() {
		return 'yummy-cookies-cookie-name';
	}

	/**
	 * Get Title
	 *
	 * Returns the title of the Tag
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string
	 */
	public function get_title() {
		return __( 'Cookie Name', 'yummy-cookies' );
	}

	/**
	 * Get Group
	 *
	 * Returns the Group of the tag
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string
	 */
	public function get_group() {
		return 'yummy-cookies';
	}

	/**
	 * Get Categories
	 *
	 * Returns an array of tag categories
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return array
	 */
	public function get_categories() {
		return [ \Elementor\Modules\DynamicTags\Module::TEXT_CATEGORY ];
	}

	/**
	 * Register Controls
	 *
	 * @since 1.0.0
	 * @access protected
	 *
	 * @return void
	 */
	protected function _register_controls() {

		$variables = [
			'' => __( 'Select...', 'yummy-cookies' ),
		];

		foreach ( array_keys( $_COOKIE ) as $variable ) {
			$variables[ $variable ] = $variable;
		}

		$this->add_control(
			'key',
			[
				'label'   => __( 'Grab Cookie Name', 'yummy-cookies' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'options' => $variables,
			]
		);

		$this->add_control(
			'custom_key',
			[
				'label'       => __( 'Custom Cookie Name', 'ymmuie-cookies' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => 'Cookie name',
				'condition'   => [
					'key' => '',
				],
			]
		);
	}

	/**
	 * Render
	 *
	 * Prints out the value of the cookie name.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function render() {
		$cookie_name_settings = $this->get_settings( 'key' );

		if ( empty( $cookie_name_settings ) ) {
			$cookie_name_settings = $this->get_settings( 'custom_key' );
		}

		if ( ! $cookie_name_settings ) {
			return;
		}

		echo wp_kses_post( $cookie_name_settings );
	}
}
