<?php
/**
 * Class: Site_Server
 *
 * @return affiche la valeur d'un variable SERVER
 * @since 1.6.0
 */

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 Site_Server extends Tag {

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

	public function get_title(): string {
		return esc_html__( 'Server variables', 'eac-components' );
	}

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


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

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

	protected function register_controls(): void {
		$variables = array( '' => esc_html__( 'Select...', 'eac-components' ) );

		foreach ( array_keys( $_SERVER ) as $variable ) {
			$variables[ $variable ] = ucfirst( str_replace( '_', ' ', $variable ) );
		}

		$this->add_control(
			'param_name',
			array(
				'label'   => esc_html__( 'Key', 'eac-components' ),
				'type'    => Controls_Manager::SELECT,
				'options' => $variables,
			)
		);
	}

	public function render(): void {
		$param_name = $this->get_settings( 'param_name' );

		if ( ! $param_name ) {
			return;
		}

		if ( ! isset( $_SERVER[ $param_name ] ) ) {
			return;
		}

		$value = sanitize_text_field( wp_unslash( $_SERVER[ $param_name ] ) );
		echo wp_kses_post( $value );
	}
}
