Tooltips.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Injects tooltips to controls when the 'tooltip' argument is used. |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @category Modules |
| 7 | * @author Themeum |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | * @since 1.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Kirki\Module; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | use Kirki\URL; |
| 20 | |
| 21 | /** |
| 22 | * Adds the tooltips. |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | */ |
| 26 | class Tooltips { |
| 27 | |
| 28 | /** |
| 29 | * The class instance. |
| 30 | * |
| 31 | * @static |
| 32 | * @access private |
| 33 | * @since 1.0 |
| 34 | * @var object |
| 35 | */ |
| 36 | private static $instance; |
| 37 | |
| 38 | /** |
| 39 | * An array containing field identifieds and their tooltips. |
| 40 | * |
| 41 | * @access private |
| 42 | * @since 1.0 |
| 43 | * @var array |
| 44 | */ |
| 45 | private $tooltips_content = []; |
| 46 | |
| 47 | /** |
| 48 | * Get the one, true instance of this class. |
| 49 | * Prevents performance issues since this is instantiated in a filter. |
| 50 | * |
| 51 | * @static |
| 52 | * @access public |
| 53 | * @since 1.0 |
| 54 | * @return object |
| 55 | */ |
| 56 | public static function get_instance() { |
| 57 | if ( null === self::$instance ) { |
| 58 | self::$instance = new self(); |
| 59 | } |
| 60 | return self::$instance; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * The class constructor |
| 65 | * |
| 66 | * @access public |
| 67 | * @since 1.0 |
| 68 | */ |
| 69 | public function __construct() { |
| 70 | |
| 71 | add_action( 'customize_controls_print_footer_scripts', [ $this, 'customize_controls_print_footer_scripts' ] ); |
| 72 | add_filter( 'kirki_field_add_control_args', [ $this, 'filter_control_args' ], 10, 2 ); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Enqueue scripts. |
| 78 | * |
| 79 | * @access public |
| 80 | * @since 1.0 |
| 81 | */ |
| 82 | public function customize_controls_print_footer_scripts() { |
| 83 | |
| 84 | |
| 85 | wp_localize_script( 'kirki-customizer', 'kirkiTooltips', $this->tooltips_content ); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Filter control args. |
| 91 | * |
| 92 | * @access public |
| 93 | * @since 1.0 |
| 94 | * @param array $args The field arguments. |
| 95 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 96 | * @return array |
| 97 | */ |
| 98 | public function filter_control_args( $args, $wp_customize ) { |
| 99 | if ( isset( $args['tooltip'] ) && $args['tooltip'] ) { |
| 100 | $this->tooltips_content[ $args['settings'] ] = [ |
| 101 | 'id' => sanitize_key( $args['settings'] ), |
| 102 | 'content' => wp_kses_post( $args['tooltip'] ), |
| 103 | ]; |
| 104 | } |
| 105 | return $args; |
| 106 | } |
| 107 | } |