Kirki_Modules_Tooltips   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 96
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 0 5 2
A customize_controls_print_footer_scripts() 0 6 1
A add_tooltip() 0 4 1
A parse_fields() 0 10 4
1
<?php
2
/**
3
 * Injects tooltips to controls when the 'tooltip' argument is used.
4
 *
5
 * @package     Kirki
6
 * @category    Modules
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license    https://opensource.org/licenses/MIT
10
 * @since       3.0.0
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Adds script for tooltips.
20
 */
21
class Kirki_Modules_Tooltips {
22
23
	/**
24
	 * The object instance.
25
	 *
26
	 * @static
27
	 * @access private
28
	 * @since 3.0.0
29
	 * @var object
30
	 */
31
	private static $instance;
32
33
	/**
34
	 * An array containing field identifieds and their tooltips.
35
	 *
36
	 * @access private
37
	 * @since 3.0.0
38
	 * @var array
39
	 */
40
	private $tooltips_content = array();
41
42
	/**
43
	 * The class constructor
44
	 *
45
	 * @access protected
46
	 * @since 3.0.0
47
	 */
48
	protected function __construct() {
49
		add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_controls_print_footer_scripts' ) );
50
	}
51
52
	/**
53
	 * Gets an instance of this object.
54
	 * Prevents duplicate instances which avoid artefacts and improves performance.
55
	 *
56
	 * @static
57
	 * @access public
58
	 * @since 3.0.0
59
	 * @return object
60
	 */
61
	public static function get_instance() {
62
		if ( ! self::$instance ) {
63
			self::$instance = new self();
64
		}
65
		return self::$instance;
66
	}
67
68
	/**
69
	 * Parses fields and if any tooltips are found, they are added to the
70
	 * object's $tooltips_content property.
71
	 *
72
	 * @access private
73
	 * @since 3.0.0
74
	 */
75
	private function parse_fields() {
76
		$fields = Kirki::$fields;
77
		foreach ( $fields as $field ) {
78
			if ( isset( $field['tooltip'] ) && ! empty( $field['tooltip'] ) ) {
79
				// Get the control ID and properly format it for the tooltips.
80
				$id = str_replace( '[', '-', str_replace( ']', '', $field['settings'] ) );
81
				// Add the tooltips content.
82
				$this->tooltips_content[ $id ] = array(
83
					'id'      => $id,
84
					'content' => $field['tooltip'],
85
				);
86
			}
87
		}
88
	}
89
90
	/**
91
	 * Allows us to add a tooltip to any control.
92
	 *
93
	 * @access public
94
	 * @since 4.2.0
95
	 * @param string $field_id The field-ID.
96
	 * @param string $tooltip  The tooltip content.
97
	 */
98
	public function add_tooltip( $field_id, $tooltip ) {
99
		$this->tooltips_content[ $field_id ] = array(
100
			'id'      => sanitize_key( $field_id ),
101
			'content' => wp_kses_post( $tooltip ),
102
		);
103
	}
104
105
	/**
106
	 * Enqueue scripts.
107
	 *
108
	 * @access public
109
	 * @since 3.0.0
110
	 */
111
	public function customize_controls_print_footer_scripts() {
112
		$this->parse_fields();
113
114
		wp_enqueue_script( 'kirki-tooltip', trailingslashit( Kirki::$url ) . 'modules/tooltips/tooltip.js', array( 'jquery' ), KIRKI_VERSION, false );
115
		wp_localize_script( 'kirki-tooltip', 'kirkiTooltips', $this->tooltips_content );
116
		wp_enqueue_style( 'kirki-tooltip', trailingslashit( Kirki::$url ) . 'modules/tooltips/tooltip.css', array(), KIRKI_VERSION );
117
	}
118
}
119