Completed
Push — develop ( 007566...2286c1 )
by Aristeides
02:04
created

Kirki_Control::add_control()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 2
nop 1
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
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     http://opensource.org/licenses/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
		// Add the control.
52
		$this->add_control( $args );
53
54
	}
55
56
	/**
57
	 * Get the class name of the class needed to create tis control.
58
	 *
59
	 * @access private
60
	 * @param array $args The field definition as sanitized in Kirki_Field.
61
	 *
62
	 * @return         string   the name of the class that will be used to create this control.
63
	 */
64
	final private function get_control_class_name( $args ) {
65
66
		// Set a default class name.
67
		$class_name = 'WP_Customize_Control';
68
		// Get the classname from the array of control classnames.
69
		if ( array_key_exists( $args['type'], self::$control_types ) ) {
70
			$class_name = self::$control_types[ $args['type'] ];
71
		}
72
		return $class_name;
73
74
	}
75
76
	/**
77
	 * Adds the control.
78
	 *
79
	 * @access protected
80
	 * @param array $args The field definition as sanitized in Kirki_Field.
81
	 */
82
	final protected function add_control( $args ) {
83
84
		// Get the name of the class we're going to use.
85
		$class_name = $this->get_control_class_name( $args );
86
		// Fixes https://github.com/aristath/kirki/issues/1622
87
		if ( 'kirki-code' === $args['type'] && class_exists( 'WP_Customize_Code_Editor_Control' ) ) {
88
			$this->wp_customize->add_control(
89
				new WP_Customize_Code_Editor_Control(
90
					$this->wp_customize,
91
					$args['settings'],
92
					array(
93
						'label'       => $args['label'],
94
						'section'     => $args['section'],
95
						'settings'    => $args['settings'],
96
						'code_type'   => $args['choices']['language'],
97
						'priority'    => $args['priority'],
98
						'input_attrs' => array(
99
							'aria-describedby' => 'editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4',
100
						),
101
					)
102
				)
103
			);
104
			return;
105
		}
106
107
		// Add the control.
108
		$this->wp_customize->add_control( new $class_name( $this->wp_customize, $args['settings'], $args ) );
109
110
	}
111
112
	/**
113
	 * Sets the $control_types property.
114
	 * Makes sure the kirki/control_types filter is applied
115
	 * and that the defined classes actually exist.
116
	 * If a defined class does not exist, it is removed.
117
	 *
118
	 * @access private
119
	 */
120
	final private function set_control_types() {
121
122
		// Early exit if this has already run.
123
		if ( ! empty( self::$control_types ) ) {
124
			return;
125
		}
126
127
		self::$control_types = apply_filters( 'kirki/control_types', array() );
128
129
		// Make sure the defined classes actually exist.
130
		foreach ( self::$control_types as $key => $classname ) {
131
132
			if ( ! class_exists( $classname ) ) {
133
				unset( self::$control_types[ $key ] );
134
			}
135
		}
136
	}
137
}
138