Completed
Push — develop ( 2d809e...4b580b )
by Aristeides
03:12
created

Kirki_Modules_Icons   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get_instance() 0 6 2
A add_icon() 0 5 1
B customize_controls_enqueue_scripts() 0 23 5
1
<?php
2
/**
3
 * Try to automatically generate the script necessary for adding icons to panels & section
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/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 scripts for icons in sections & panels.
20
 */
21
class Kirki_Modules_Icons {
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 of panels and sections with icons.
35
	 *
36
	 * @static
37
	 * @access private
38
	 * @var string
39
	 */
40
	private static $icons = array();
41
42
	/**
43
	 * The class constructor.
44
	 *
45
	 * @access protected
46
	 */
47
	protected function __construct() {
48
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ), 99 );
49
	}
50
51
	/**
52
	 * Gets an instance of this object.
53
	 * Prevents duplicate instances which avoid artefacts and improves performance.
54
	 *
55
	 * @static
56
	 * @access public
57
	 * @since 3.0.0
58
	 * @return object
59
	 */
60
	public static function get_instance() {
61
		if ( ! self::$instance ) {
62
			self::$instance = new self();
63
		}
64
		return self::$instance;
65
	}
66
67
	/**
68
	 * Adds icon for a section/panel.
69
	 *
70
	 * @access public
71
	 * @since 3.0.0
72
	 * @param string $id      The panel or section ID.
73
	 * @param string $icon    The icon to add.
74
	 * @param string $context Lowercase 'section' or 'panel'.
75
	 */
76
	public function add_icon( $id, $icon, $context = 'section' ) {
77
78
		self::$icons[ $context ][ $id ] = trim( $icon );
79
80
	}
81
82
	/**
83
	 * Format the script in a way that will be compatible with WordPress.
84
	 */
85
	public function customize_controls_enqueue_scripts() {
86
87
		$sections = Kirki::$sections;
88
		$panels   = Kirki::$panels;
89
90
		// Parse sections and find ones with icons.
91
		foreach ( $sections as $section ) {
92
			if ( isset( $section['icon'] ) ) {
93
				$this->add_icon( $section['id'], $section['icon'], 'section' );
94
			}
95
		}
96
97
		// Parse panels and find ones with icons.
98
		foreach ( $panels as $panel ) {
99
			if ( isset( $panel['icon'] ) ) {
100
				$this->add_icon( $section['id'], $section['icon'], 'panel' );
101
			}
102
		}
103
104
		wp_enqueue_script( 'kirki_panel_and_section_icons', trailingslashit( Kirki::$url ) . 'modules/icons/icons.js', array( 'jquery', 'customize-base', 'customize-controls' ), false, true );
105
		wp_localize_script( 'kirki_panel_and_section_icons', 'kirkiIcons', self::$icons );
106
107
	}
108
}
109