<?php
/**
 * Injects tooltips to controls when the 'tooltip' argument is used.
 *
 * @package   Kirki
 * @category  Modules
 * @author    Themeum
 * @copyright Copyright (c) 2023, Themeum
 * @license   https://opensource.org/licenses/MIT
 * @since     1.0
 */

namespace Kirki\Module;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Kirki\URL;

/**
 * Adds the tooltips.
 *
 * @since 1.0
 */
class Tooltips {

	/**
	 * The class instance.
	 *
	 * @static
	 * @access private
	 * @since 1.0
	 * @var object
	 */
	private static $instance;

	/**
	 * An array containing field identifieds and their tooltips.
	 *
	 * @access private
	 * @since 1.0
	 * @var array
	 */
	private $tooltips_content = [];

	/**
	 * Get the one, true instance of this class.
	 * Prevents performance issues since this is instantiated in a filter.
	 *
	 * @static
	 * @access public
	 * @since 1.0
	 * @return object
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * The class constructor
	 *
	 * @access public
	 * @since 1.0
	 */
	public function __construct() {

		add_action( 'customize_controls_print_footer_scripts', [ $this, 'customize_controls_print_footer_scripts' ] );
		add_filter( 'kirki_field_add_control_args', [ $this, 'filter_control_args' ], 10, 2 );

	}

	/**
	 * Enqueue scripts.
	 *
	 * @access public
	 * @since 1.0
	 */
	public function customize_controls_print_footer_scripts() {


		wp_localize_script( 'kirki-customizer', 'kirkiTooltips', $this->tooltips_content );

	}

	/**
	 * Filter control args.
	 *
	 * @access public
	 * @since 1.0
	 * @param array                $args         The field arguments.
	 * @param WP_Customize_Manager $wp_customize The customizer instance.
	 * @return array
	 */
	public function filter_control_args( $args, $wp_customize ) {
		if ( isset( $args['tooltip'] ) && $args['tooltip'] ) {
			$this->tooltips_content[ $args['settings'] ] = [
				'id'      => sanitize_key( $args['settings'] ),
				'content' => wp_kses_post( $args['tooltip'] ),
			];
		}
		return $args;
	}
}