Kirki_Control   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A get_control_class_name() 0 10 2
A set_control_types() 0 13 4
A add_control() 0 7 1
1
<?php
2
/**
3
 * Controls handler
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license    https://opensource.org/licenses/MIT
10
 */
11
12
/**
13
 * Our main Kirki_Control object
14
 */
15
class Kirki_Control {
16
17
	/**
18
	 * The $wp_customize WordPress global.
19
	 *
20
	 * @access protected
21
	 * @var WP_Customize_Manager
22
	 */
23
	protected $wp_customize;
24
25
	/**
26
	 * An array of all available control types.
27
	 *
28
	 * @access protected
29
	 * @var array
30
	 */
31
	protected static $control_types = array();
32
33
	/**
34
	 * The class constructor.
35
	 * Creates the actual controls in the customizer.
36
	 *
37
	 * @access public
38
	 * @param array $args The field definition as sanitized in Kirki_Field.
39
	 */
40
	public function __construct( $args ) {
41
42
		// Set the $wp_customize property.
43
		global $wp_customize;
44
		if ( ! $wp_customize ) {
45
			return;
46
		}
47
		$this->wp_customize = $wp_customize;
48
49
		// Set the control types.
50
		$this->set_control_types();
51
52
		// Add the control.
53
		$this->add_control( $args );
54
55
	}
56
57
	/**
58
	 * Get the class name of the class needed to create tis control.
59
	 *
60
	 * @access private
61
	 * @param array $args The field definition as sanitized in Kirki_Field.
62
	 *
63
	 * @return         string   the name of the class that will be used to create this control.
64
	 */
65
	final private function get_control_class_name( $args ) {
66
67
		// Set a default class name.
68
		$class_name = 'WP_Customize_Control';
69
70
		// Get the classname from the array of control classnames.
71
		if ( array_key_exists( $args['type'], self::$control_types ) ) {
72
			$class_name = self::$control_types[ $args['type'] ];
73
		}
74
		return $class_name;
75
	}
76
77
	/**
78
	 * Adds the control.
79
	 *
80
	 * @access protected
81
	 * @param array $args The field definition as sanitized in Kirki_Field.
82
	 */
83
	final protected function add_control( $args ) {
84
85
		// Get the name of the class we're going to use.
86
		$class_name = $this->get_control_class_name( $args );
87
88
		// Add the control.
89
		$this->wp_customize->add_control( new $class_name( $this->wp_customize, $args['settings'], $args ) );
90
91
	}
92
93
	/**
94
	 * Sets the $control_types property.
95
	 * Makes sure the kirki_control_types filter is applied
96
	 * and that the defined classes actually exist.
97
	 * If a defined class does not exist, it is removed.
98
	 *
99
	 * @access private
100
	 */
101
	final private function set_control_types() {
102
103
		// Early exit if this has already run.
104
		if ( ! empty( self::$control_types ) ) {
105
			return;
106
		}
107
108
		self::$control_types = apply_filters( 'kirki_control_types', array() );
109
110
		// Make sure the defined classes actually exist.
111
		foreach ( self::$control_types as $key => $classname ) {
112
			if ( ! class_exists( $classname ) ) {
113
				unset( self::$control_types[ $key ] );
114
			}
115
		}
116
	}
117
}
118