Kirki_Init   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 300
Duplicated Lines 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
eloc 110
dl 0
loc 300
rs 9.28
c 3
b 3
f 0
wmc 39

14 Methods

Rating   Name   Duplication   Size   Complexity  
A default_control_types() 0 36 1
B register_control_types() 0 28 8
A remove_controls() 0 3 2
A remove_panels() 0 3 2
A add_fields() 0 14 3
A set_url() 0 19 3
A fields_from_filters() 0 7 3
A remove_sections() 0 3 2
A add_sections() 0 16 6
A is_plugin() 0 2 1
A add_panels() 0 12 5
A add_to_customizer() 0 6 1
A __construct() 0 12 1
A get_variables() 0 5 1
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    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
		self::set_url();
32
		add_action( 'after_setup_theme', array( $this, 'set_url' ) );
33
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
34
		add_filter( 'kirki_control_types', array( $this, 'default_control_types' ) );
35
36
		add_action( 'customize_register', array( $this, 'remove_panels' ), 99999 );
37
		add_action( 'customize_register', array( $this, 'remove_sections' ), 99999 );
38
		add_action( 'customize_register', array( $this, 'remove_controls' ), 99999 );
39
40
		new Kirki_Values();
41
		new Kirki_Sections();
42
	}
43
44
	/**
45
	 * Properly set the Kirki URL for assets.
46
	 *
47
	 * @static
48
	 * @access public
49
	 */
50
	public static function set_url() {
51
		if ( Kirki_Util::is_plugin() ) {
52
			return;
53
		}
54
55
		// Get correct URL and path to wp-content.
56
		$content_url = untrailingslashit( dirname( dirname( get_stylesheet_directory_uri() ) ) );
57
		$content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
58
59
		Kirki::$url = str_replace( $content_dir, $content_url, wp_normalize_path( Kirki::$path ) );
60
61
		// Apply the kirki_config filter.
62
		$config = apply_filters( 'kirki_config', array() );
63
		if ( isset( $config['url_path'] ) ) {
64
			Kirki::$url = $config['url_path'];
65
		}
66
67
		// Make sure the right protocol is used.
68
		Kirki::$url = set_url_scheme( Kirki::$url );
69
	}
70
71
	/**
72
	 * Add the default Kirki control types.
73
	 *
74
	 * @access public
75
	 * @since 3.0.0
76
	 * @param array $control_types The control types array.
77
	 * @return array
78
	 */
79
	public function default_control_types( $control_types = array() ) {
80
		$this->control_types = array(
81
			'checkbox'              => 'Kirki_Control_Checkbox',
82
			'kirki-background'      => 'Kirki_Control_Background',
83
			'code_editor'           => 'Kirki_Control_Code',
84
			'kirki-color'           => 'Kirki_Control_Color',
85
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
86
			'kirki-custom'          => 'Kirki_Control_Custom',
87
			'kirki-date'            => 'Kirki_Control_Date',
88
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
89
			'kirki-dimension'       => 'Kirki_Control_Dimension',
90
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
91
			'kirki-editor'          => 'Kirki_Control_Editor',
92
			'kirki-fontawesome'     => 'Kirki_Control_FontAwesome',
93
			'kirki-image'           => 'Kirki_Control_Image',
94
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
95
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
96
			'kirki-number'          => 'Kirki_Control_Number',
97
			'kirki-palette'         => 'Kirki_Control_Palette',
98
			'kirki-radio'           => 'Kirki_Control_Radio',
99
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
100
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
101
			'repeater'              => 'Kirki_Control_Repeater',
102
			'kirki-select'          => 'Kirki_Control_Select',
103
			'kirki-slider'          => 'Kirki_Control_Slider',
104
			'kirki-sortable'        => 'Kirki_Control_Sortable',
105
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
106
			'kirki-switch'          => 'Kirki_Control_Switch',
107
			'kirki-generic'         => 'Kirki_Control_Generic',
108
			'kirki-toggle'          => 'Kirki_Control_Toggle',
109
			'kirki-typography'      => 'Kirki_Control_Typography',
110
			'image'                 => 'Kirki_Control_Image',
111
			'cropped_image'         => 'Kirki_Control_Cropped_Image',
112
			'upload'                => 'Kirki_Control_Upload',
113
		);
114
		return array_merge( $this->control_types, $control_types );
115
	}
116
117
	/**
118
	 * Helper function that adds the fields, sections and panels to the customizer.
119
	 */
120
	public function add_to_customizer() {
121
		$this->fields_from_filters();
122
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
123
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
124
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
125
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
126
	}
127
128
	/**
129
	 * Register control types
130
	 */
131
	public function register_control_types() {
132
		global $wp_customize;
133
134
		$section_types = apply_filters( 'kirki_section_types', array() );
135
		foreach ( $section_types as $section_type ) {
136
			$wp_customize->register_section_type( $section_type );
137
		}
138
139
		$this->control_types = $this->default_control_types();
140
		if ( ! class_exists( 'WP_Customize_Code_Editor_Control' ) ) {
141
			unset( $this->control_types['code_editor'] );
142
		}
143
		foreach ( $this->control_types as $key => $classname ) {
144
			if ( ! class_exists( $classname ) ) {
145
				unset( $this->control_types[ $key ] );
146
			}
147
		}
148
149
		$skip_control_types = apply_filters(
150
			'kirki_control_types_exclude', array(
0 ignored issues
show
Coding Style introduced by Aristeides Stathopoulos
For multi-line function calls, each argument should be on a separate line.

For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
);
Loading history...
151
				'Kirki_Control_Repeater',
152
				'WP_Customize_Control',
153
			)
154
		);
155
156
		foreach ( $this->control_types as $control_type ) {
157
			if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) {
158
				$wp_customize->register_control_type( $control_type );
159
			}
160
		}
161
	}
162
163
	/**
164
	 * Register our panels to the WordPress Customizer.
165
	 *
166
	 * @access public
167
	 */
168
	public function add_panels() {
169
		if ( ! empty( Kirki::$panels ) ) {
170
			foreach ( Kirki::$panels as $panel_args ) {
171
				// Extra checks for nested panels.
172
				if ( isset( $panel_args['panel'] ) ) {
173
					if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) {
174
						// Set the type to nested.
175
						$panel_args['type'] = 'kirki-nested';
176
					}
177
				}
178
179
				new Kirki_Panel( $panel_args );
180
			}
181
		}
182
	}
183
184
	/**
185
	 * Register our sections to the WordPress Customizer.
186
	 *
187
	 * @var object The WordPress Customizer object
188
	 */
189
	public function add_sections() {
190
		if ( ! empty( Kirki::$sections ) ) {
191
			foreach ( Kirki::$sections as $section_args ) {
192
				// Extra checks for nested sections.
193
				if ( isset( $section_args['section'] ) ) {
194
					if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) {
195
						// Set the type to nested.
196
						$section_args['type'] = 'kirki-nested';
197
						// We need to check if the parent section is nested inside a panel.
198
						$parent_section = Kirki::$sections[ $section_args['section'] ];
199
						if ( isset( $parent_section['panel'] ) ) {
200
							$section_args['panel'] = $parent_section['panel'];
201
						}
202
					}
203
				}
204
				new Kirki_Section( $section_args );
205
			}
206
		}
207
	}
208
209
	/**
210
	 * Create the settings and controls from the $fields array and register them.
211
	 *
212
	 * @var object The WordPress Customizer object.
213
	 */
214
	public function add_fields() {
215
		global $wp_customize;
216
		foreach ( Kirki::$fields as $args ) {
217
218
			// Create the settings.
219
			new Kirki_Settings( $args );
220
221
			// Check if we're on the customizer.
222
			// If we are, then we will create the controls, add the scripts needed for the customizer
223
			// and any other tweaks that this field may require.
224
			if ( $wp_customize ) {
225
226
				// Create the control.
227
				new Kirki_Control( $args );
228
229
			}
230
		}
231
	}
232
233
	/**
234
	 * Process fields added using the 'kirki_fields' and 'kirki_controls' filter.
235
	 * These filters are no longer used, this is simply for backwards-compatibility.
236
	 *
237
	 * @access private
238
	 * @since 2.0.0
239
	 */
240
	private function fields_from_filters() {
241
		$fields = apply_filters( 'kirki_controls', array() );
242
		$fields = apply_filters( 'kirki_fields', $fields );
243
244
		if ( ! empty( $fields ) ) {
245
			foreach ( $fields as $field ) {
246
				Kirki::add_field( 'global', $field );
247
			}
248
		}
249
	}
250
251
	/**
252
	 * Alias for the is_plugin static method in the Kirki_Util class.
253
	 * This is here for backwards-compatibility purposes.
254
	 *
255
	 * @static
256
	 * @access public
257
	 * @since 3.0.0
258
	 * @return bool
259
	 */
260
	public static function is_plugin() {
261
		return Kirki_Util::is_plugin();
262
	}
263
264
	/**
265
	 * Alias for the get_variables static method in the Kirki_Util class.
266
	 * This is here for backwards-compatibility purposes.
267
	 *
268
	 * @static
269
	 * @access public
270
	 * @since 2.0.0
271
	 * @return array Formatted as array( 'variable-name' => value ).
272
	 */
273
	public static function get_variables() {
274
275
		// Log error for developers.
276
		_doing_it_wrong( __METHOD__, esc_html__( 'We detected you\'re using Kirki_Init::get_variables(). Please use Kirki_Util::get_variables() instead.', 'kirki' ), '3.0.10' );
277
		return Kirki_Util::get_variables();
278
	}
279
280
	/**
281
	 * Remove panels.
282
	 *
283
	 * @since 3.0.17
284
	 * @param object $wp_customize The customizer object.
285
	 * @return void
286
	 */
287
	public function remove_panels( $wp_customize ) {
288
		foreach ( Kirki::$panels_to_remove as $panel ) {
289
			$wp_customize->remove_panel( $panel );
290
		}
291
	}
292
293
	/**
294
	 * Remove sections.
295
	 *
296
	 * @since 3.0.17
297
	 * @param object $wp_customize The customizer object.
298
	 * @return void
299
	 */
300
	public function remove_sections( $wp_customize ) {
301
		foreach ( Kirki::$sections_to_remove as $section ) {
302
			$wp_customize->remove_section( $section );
303
		}
304
	}
305
306
	/**
307
	 * Remove controls.
308
	 *
309
	 * @since 3.0.17
310
	 * @param object $wp_customize The customizer object.
311
	 * @return void
312
	 */
313
	public function remove_controls( $wp_customize ) {
314
		foreach ( Kirki::$controls_to_remove as $control ) {
315
			$wp_customize->remove_control( $control );
316
		}
317
	}
318
}
319