Passed
Push — develop ( fe986b...95ca01 )
by Aristeides
04:19
created

Kirki_Init::register_control_types()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 8
nc 36
nop 0
dl 0
loc 28
rs 8.4444
c 2
b 2
f 0
1
<?php
2
/**
3
 * Initializes Kirki
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       1.0
11
 */
12
13
/**
14
 * Initialize Kirki
15
 */
16
class Kirki_Init {
17
18
	/**
19
	 * Control types.
20
	 *
21
	 * @access private
22
	 * @since 3.0.0
23
	 * @var array
24
	 */
25
	private $control_types = array();
26
27
	/**
28
	 * The class constructor.
29
	 */
30
	public function __construct() {
31
32
		self::set_url();
33
		add_action( 'after_setup_theme', array( $this, 'set_url' ) );
34
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
35
		add_filter( 'kirki_control_types', array( $this, 'default_control_types' ) );
36
37
		add_action( 'customize_register', array( $this, 'remove_panels' ), 99999 );
38
		add_action( 'customize_register', array( $this, 'remove_sections' ), 99999 );
39
		add_action( 'customize_register', array( $this, 'remove_controls' ), 99999 );
40
41
		new Kirki_Values();
42
		new Kirki_Sections();
43
	}
44
45
	/**
46
	 * Properly set the Kirki URL for assets.
47
	 *
48
	 * @static
49
	 * @access public
50
	 */
51
	public static function set_url() {
52
53
		if ( Kirki_Util::is_plugin() ) {
54
			return;
55
		}
56
57
		// Get correct URL and path to wp-content.
58
		$content_url = untrailingslashit( dirname( dirname( get_stylesheet_directory_uri() ) ) );
59
		$content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
60
61
		Kirki::$url = str_replace( $content_dir, $content_url, wp_normalize_path( Kirki::$path ) );
62
63
		// Apply the kirki_config filter.
64
		$config = apply_filters( 'kirki_config', array() );
65
		if ( isset( $config['url_path'] ) ) {
66
			Kirki::$url = $config['url_path'];
67
		}
68
69
		// Make sure the right protocol is used.
70
		Kirki::$url = set_url_scheme( Kirki::$url );
71
	}
72
73
	/**
74
	 * Add the default Kirki control types.
75
	 *
76
	 * @access public
77
	 * @since 3.0.0
78
	 * @param array $control_types The control types array.
79
	 * @return array
80
	 */
81
	public function default_control_types( $control_types = array() ) {
82
83
		$this->control_types = array(
84
			'checkbox'              => 'Kirki_Control_Checkbox',
85
			'kirki-background'      => 'Kirki_Control_Background',
86
			'code_editor'           => 'Kirki_Control_Code',
87
			'kirki-color'           => 'Kirki_Control_Color',
88
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
89
			'kirki-custom'          => 'Kirki_Control_Custom',
90
			'kirki-date'            => 'Kirki_Control_Date',
91
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
92
			'kirki-dimension'       => 'Kirki_Control_Dimension',
93
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
94
			'kirki-editor'          => 'Kirki_Control_Editor',
95
			'kirki-fontawesome'     => 'Kirki_Control_FontAwesome',
96
			'kirki-image'           => 'Kirki_Control_Image',
97
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
98
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
99
			'kirki-number'          => 'Kirki_Control_Number',
100
			'kirki-palette'         => 'Kirki_Control_Palette',
101
			'kirki-radio'           => 'Kirki_Control_Radio',
102
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
103
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
104
			'repeater'              => 'Kirki_Control_Repeater',
105
			'kirki-select'          => 'Kirki_Control_Select',
106
			'kirki-slider'          => 'Kirki_Control_Slider',
107
			'kirki-sortable'        => 'Kirki_Control_Sortable',
108
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
109
			'kirki-switch'          => 'Kirki_Control_Switch',
110
			'kirki-generic'         => 'Kirki_Control_Generic',
111
			'kirki-toggle'          => 'Kirki_Control_Toggle',
112
			'kirki-typography'      => 'Kirki_Control_Typography',
113
			'image'                 => 'Kirki_Control_Image',
114
			'cropped_image'         => 'Kirki_Control_Cropped_Image',
115
			'upload'                => 'Kirki_Control_Upload',
116
		);
117
		return array_merge( $this->control_types, $control_types );
118
119
	}
120
121
	/**
122
	 * Helper function that adds the fields, sections and panels to the customizer.
123
	 */
124
	public function add_to_customizer() {
125
		$this->fields_from_filters();
126
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
127
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
128
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
129
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
130
	}
131
132
	/**
133
	 * Register control types
134
	 */
135
	public function register_control_types() {
136
		global $wp_customize;
137
138
		$section_types = apply_filters( 'kirki_section_types', array() );
139
		foreach ( $section_types as $section_type ) {
140
			$wp_customize->register_section_type( $section_type );
141
		}
142
143
		$this->control_types = $this->default_control_types();
144
		if ( ! class_exists( 'WP_Customize_Code_Editor_Control' ) ) {
145
			unset( $this->control_types['code_editor'] );
146
		}
147
		foreach ( $this->control_types as $key => $classname ) {
148
			if ( ! class_exists( $classname ) ) {
149
				unset( $this->control_types[ $key ] );
150
			}
151
		}
152
153
		$skip_control_types = apply_filters(
154
			'kirki_control_types_exclude', array(
155
				'Kirki_Control_Repeater',
156
				'WP_Customize_Control',
157
			)
158
		);
159
160
		foreach ( $this->control_types as $control_type ) {
161
			if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) {
162
				$wp_customize->register_control_type( $control_type );
163
			}
164
		}
165
	}
166
167
	/**
168
	 * Register our panels to the WordPress Customizer.
169
	 *
170
	 * @access public
171
	 */
172
	public function add_panels() {
173
		if ( ! empty( Kirki::$panels ) ) {
174
			foreach ( Kirki::$panels as $panel_args ) {
175
				// Extra checks for nested panels.
176
				if ( isset( $panel_args['panel'] ) ) {
177
					if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) {
178
						// Set the type to nested.
179
						$panel_args['type'] = 'kirki-nested';
180
					}
181
				}
182
183
				new Kirki_Panel( $panel_args );
184
			}
185
		}
186
	}
187
188
	/**
189
	 * Register our sections to the WordPress Customizer.
190
	 *
191
	 * @var object The WordPress Customizer object
192
	 */
193
	public function add_sections() {
194
		if ( ! empty( Kirki::$sections ) ) {
195
			foreach ( Kirki::$sections as $section_args ) {
196
				// Extra checks for nested sections.
197
				if ( isset( $section_args['section'] ) ) {
198
					if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) {
199
						// Set the type to nested.
200
						$section_args['type'] = 'kirki-nested';
201
						// We need to check if the parent section is nested inside a panel.
202
						$parent_section = Kirki::$sections[ $section_args['section'] ];
203
						if ( isset( $parent_section['panel'] ) ) {
204
							$section_args['panel'] = $parent_section['panel'];
205
						}
206
					}
207
				}
208
				new Kirki_Section( $section_args );
209
			}
210
		}
211
	}
212
213
	/**
214
	 * Create the settings and controls from the $fields array and register them.
215
	 *
216
	 * @var object The WordPress Customizer object.
217
	 */
218
	public function add_fields() {
219
220
		global $wp_customize;
221
		foreach ( Kirki::$fields as $args ) {
222
223
			// Create the settings.
224
			new Kirki_Settings( $args );
225
226
			// Check if we're on the customizer.
227
			// If we are, then we will create the controls, add the scripts needed for the customizer
228
			// and any other tweaks that this field may require.
229
			if ( $wp_customize ) {
230
231
				// Create the control.
232
				new Kirki_Control( $args );
233
234
			}
235
		}
236
	}
237
238
	/**
239
	 * Process fields added using the 'kirki_fields' and 'kirki_controls' filter.
240
	 * These filters are no longer used, this is simply for backwards-compatibility.
241
	 *
242
	 * @access private
243
	 * @since 2.0.0
244
	 */
245
	private function fields_from_filters() {
246
247
		$fields = apply_filters( 'kirki_controls', array() );
248
		$fields = apply_filters( 'kirki_fields', $fields );
249
250
		if ( ! empty( $fields ) ) {
251
			foreach ( $fields as $field ) {
252
				Kirki::add_field( 'global', $field );
253
			}
254
		}
255
	}
256
257
	/**
258
	 * Alias for the is_plugin static method in the Kirki_Util class.
259
	 * This is here for backwards-compatibility purposes.
260
	 *
261
	 * @static
262
	 * @access public
263
	 * @since 3.0.0
264
	 * @return bool
265
	 */
266
	public static function is_plugin() {
267
		// Return result using the Kirki_Util class.
268
		return Kirki_Util::is_plugin();
269
	}
270
271
	/**
272
	 * Alias for the get_variables static method in the Kirki_Util class.
273
	 * This is here for backwards-compatibility purposes.
274
	 *
275
	 * @static
276
	 * @access public
277
	 * @since 2.0.0
278
	 * @return array Formatted as array( 'variable-name' => value ).
279
	 */
280
	public static function get_variables() {
281
		// Log error for developers.
282
		_doing_it_wrong( __METHOD__, esc_attr__( 'We detected you\'re using Kirki_Init::get_variables(). Please use Kirki_Util::get_variables() instead.', 'kirki' ), '3.0.10' );
283
		// Return result using the Kirki_Util class.
284
		return Kirki_Util::get_variables();
285
	}
286
287
	/**
288
	 * Remove panels.
289
	 *
290
	 * @since 3.0.17
291
	 * @param object $wp_customize The customizer object.
292
	 * @return void
293
	 */
294
	public function remove_panels( $wp_customize ) {
295
		foreach ( Kirki::$panels_to_remove as $panel ) {
296
			$wp_customize->remove_panel( $panel );
297
		}
298
	}
299
300
	/**
301
	 * Remove sections.
302
	 *
303
	 * @since 3.0.17
304
	 * @param object $wp_customize The customizer object.
305
	 * @return void
306
	 */
307
	public function remove_sections( $wp_customize ) {
308
		foreach ( Kirki::$sections_to_remove as $section ) {
309
			$wp_customize->remove_section( $section );
310
		}
311
	}
312
313
	/**
314
	 * Remove controls.
315
	 *
316
	 * @since 3.0.17
317
	 * @param object $wp_customize The customizer object.
318
	 * @return void
319
	 */
320
	public function remove_controls( $wp_customize ) {
321
		foreach ( Kirki::$controls_to_remove as $control ) {
322
			$wp_customize->remove_control( $control );
323
		}
324
	}
325
}
326