Completed
Push — develop ( 656b88...85b7b9 )
by Aristeides
06:46
created

Kirki_Init   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 300
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 300
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 5
cbo 7

11 Methods

Rating   Name   Duplication   Size   Complexity  
C set_url() 0 47 8
B default_control_types() 0 41 1
A add_to_customizer() 0 7 1
A __construct() 0 12 2
C register_control_types() 0 28 7
B add_panels() 0 15 5
B add_sections() 0 19 6
A add_fields() 0 19 3
A fields_from_filters() 0 11 3
A is_plugin() 0 4 1
A get_variables() 0 6 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     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
		global $wp_customize;
33
		if ( $wp_customize ) {
34
			$this->set_url();
35
			add_action( 'after_setup_theme', array( $this, 'set_url' ) );
36
		}
37
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
38
		add_filter( 'kirki/control_types', array( $this, 'default_control_types' ) );
39
40
		new Kirki_Values();
41
	}
42
43
	/**
44
	 * Properly set the Kirki URL for assets.
45
	 * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme
46
	 * and then does some calculations to get the proper URL for its CSS & JS assets.
47
	 */
48
	public function set_url() {
49
50
		Kirki::$path = wp_normalize_path( dirname( KIRKI_PLUGIN_FILE ) );
51
52
		// Works in most cases.
53
		// Serves as a fallback in case all other checks fail.
54
		if ( defined( 'WP_CONTENT_DIR' ) ) {
55
			$content_dir = wp_normalize_path( WP_CONTENT_DIR );
56
			if ( false !== strpos( Kirki::$path, $content_dir ) ) {
57
				$relative_path = str_replace( $content_dir, '', Kirki::$path );
58
				Kirki::$url = content_url( $relative_path );
59
			}
60
		}
61
62
		// If Kirki is installed as a plugin, use that for the URL.
63
		if ( Kirki_Util::is_plugin() ) {
64
			Kirki::$url = plugin_dir_url( KIRKI_PLUGIN_FILE );
65
		}
66
67
		// Get the path to the theme.
68
		$theme_path = wp_normalize_path( get_template_directory() );
69
70
		// Is Kirki included in the theme?
71
		if ( false !== strpos( Kirki::$path, $theme_path ) ) {
72
			Kirki::$url = get_template_directory_uri() . str_replace( $theme_path, '', Kirki::$path );
73
		}
74
75
		// Is there a child-theme?
76
		$child_theme_path = wp_normalize_path( get_stylesheet_directory_uri() );
77
		if ( $child_theme_path !== $theme_path ) {
78
			// Is Kirki included in a child theme?
79
			if ( false !== strpos( Kirki::$path, $child_theme_path ) ) {
80
				Kirki::$url = get_template_directory_uri() . str_replace( $child_theme_path, '', Kirki::$path );
81
			}
82
		}
83
84
		// Apply the kirki/config filter.
85
		$config = apply_filters( 'kirki/config', array() );
86
		if ( isset( $config['url_path'] ) ) {
87
			Kirki::$url = $config['url_path'];
88
		}
89
90
		// Escapes the URL.
91
		Kirki::$url = esc_url_raw( Kirki::$url );
92
		// Make sure the right protocol is used.
93
		Kirki::$url = set_url_scheme( Kirki::$url );
94
	}
95
96
	/**
97
	 * Add the default Kirki control types.
98
	 *
99
	 * @access public
100
	 * @since 3.0.0
101
	 * @param array $control_types The control types array.
102
	 * @return array
103
	 */
104
	public function default_control_types( $control_types = array() ) {
105
106
		$this->control_types = array(
107
			'checkbox'              => 'WP_Customize_Control',
108
			'kirki-background'      => 'Kirki_Control_Background',
109
			'kirki-code'            => 'Kirki_Control_Code',
110
			'kirki-color'           => 'Kirki_Control_Color',
111
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
112
			'kirki-custom'          => 'Kirki_Control_Custom',
113
			'kirki-date'            => 'Kirki_Control_Date',
114
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
115
			'kirki-dimension'       => 'Kirki_Control_Dimension',
116
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
117
			'kirki-editor'          => 'Kirki_Control_Editor',
118
			'kirki-fontawesome'     => 'Kirki_Control_FontAwesome',
119
			'kirki-gradient'        => 'Kirki_Control_Gradient',
120
			'kirki-image'           => 'Kirki_Control_Image',
121
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
122
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
123
			'kirki-number'          => 'Kirki_Control_Number',
124
			'kirki-palette'         => 'Kirki_Control_Palette',
125
			'kirki-preset'          => 'Kirki_Control_Preset',
126
			'kirki-radio'           => 'Kirki_Control_Radio',
127
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
128
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
129
			'repeater'              => 'Kirki_Control_Repeater',
130
			'kirki-select'          => 'Kirki_Control_Select',
131
			'kirki-slider'          => 'Kirki_Control_Slider',
132
			'kirki-sortable'        => 'Kirki_Control_Sortable',
133
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
134
			'kirki-switch'          => 'Kirki_Control_Switch',
135
			'kirki-generic'         => 'Kirki_Control_Generic',
136
			'kirki-toggle'          => 'Kirki_Control_Toggle',
137
			'kirki-typography'      => 'Kirki_Control_Typography',
138
			'image'                 => 'Kirki_Control_Image',
139
			'cropped_image'         => 'WP_Customize_Cropped_Image_Control',
140
			'upload'                => 'WP_Customize_Upload_Control',
141
		);
142
		return array_merge( $this->control_types, $control_types );
143
144
	}
145
146
	/**
147
	 * Helper function that adds the fields, sections and panels to the customizer.
148
	 *
149
	 * @return void
150
	 */
151
	public function add_to_customizer() {
152
		$this->fields_from_filters();
153
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
154
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
155
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
156
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
157
	}
158
159
	/**
160
	 * Register control types
161
	 *
162
	 * @return  void
163
	 */
164
	public function register_control_types() {
165
		global $wp_customize;
166
167
		$section_types = apply_filters( 'kirki/section_types', array() );
168
		foreach ( $section_types as $section_type ) {
169
			$wp_customize->register_section_type( $section_type );
170
		}
171
172
		$this->control_types = $this->default_control_types();
173
		foreach ( $this->control_types as $key => $classname ) {
174
			if ( ! class_exists( $classname ) ) {
175
				unset( $this->control_types[ $key ] );
176
			}
177
		}
178
179
		$skip_control_types = apply_filters(
180
			'kirki/control_types/exclude', array(
181
				'Kirki_Control_Repeater',
182
				'WP_Customize_Control',
183
			)
184
		);
185
186
		foreach ( $this->control_types as $control_type ) {
187
			if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) {
188
				$wp_customize->register_control_type( $control_type );
189
			}
190
		}
191
	}
192
193
	/**
194
	 * Register our panels to the WordPress Customizer.
195
	 *
196
	 * @access public
197
	 */
198
	public function add_panels() {
199
		if ( ! empty( Kirki::$panels ) ) {
200
			foreach ( Kirki::$panels as $panel_args ) {
201
				// Extra checks for nested panels.
202
				if ( isset( $panel_args['panel'] ) ) {
203
					if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) {
204
						// Set the type to nested.
205
						$panel_args['type'] = 'kirki-nested';
206
					}
207
				}
208
209
				new Kirki_Panel( $panel_args );
210
			}
211
		}
212
	}
213
214
	/**
215
	 * Register our sections to the WordPress Customizer.
216
	 *
217
	 * @var object The WordPress Customizer object
218
	 * @return void
219
	 */
220
	public function add_sections() {
221
		if ( ! empty( Kirki::$sections ) ) {
222
			foreach ( Kirki::$sections as $section_args ) {
223
				// Extra checks for nested sections.
224
				if ( isset( $section_args['section'] ) ) {
225
					if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) {
226
						// Set the type to nested.
227
						$section_args['type'] = 'kirki-nested';
228
						// We need to check if the parent section is nested inside a panel.
229
						$parent_section = Kirki::$sections[ $section_args['section'] ];
230
						if ( isset( $parent_section['panel'] ) ) {
231
							$section_args['panel'] = $parent_section['panel'];
232
						}
233
					}
234
				}
235
				new Kirki_Section( $section_args );
236
			}
237
		}
238
	}
239
240
	/**
241
	 * Create the settings and controls from the $fields array and register them.
242
	 *
243
	 * @var object The WordPress Customizer object.
244
	 * @return void
245
	 */
246
	public function add_fields() {
247
248
		global $wp_customize;
249
		foreach ( Kirki::$fields as $args ) {
250
251
			// Create the settings.
252
			new Kirki_Settings( $args );
253
254
			// Check if we're on the customizer.
255
			// If we are, then we will create the controls, add the scripts needed for the customizer
256
			// and any other tweaks that this field may require.
257
			if ( $wp_customize ) {
258
259
				// Create the control.
260
				new Kirki_Control( $args );
261
262
			}
263
		}
264
	}
265
266
	/**
267
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
268
	 * These filters are no longer used, this is simply for backwards-compatibility.
269
	 *
270
	 * @access private
271
	 * @since 2.0.0
272
	 */
273
	private function fields_from_filters() {
274
275
		$fields = apply_filters( 'kirki/controls', array() );
276
		$fields = apply_filters( 'kirki/fields', $fields );
277
278
		if ( ! empty( $fields ) ) {
279
			foreach ( $fields as $field ) {
280
				Kirki::add_field( 'global', $field );
281
			}
282
		}
283
	}
284
285
	/**
286
	 * Alias for the is_plugin static method in the Kirki_Util class.
287
	 * This is here for backwards-compatibility purposes.
288
	 *
289
	 * @static
290
	 * @access public
291
	 * @since 3.0.0
292
	 * @return bool
293
	 */
294
	public static function is_plugin() {
295
		// Return result using the Kirki_Util class.
296
		return Kirki_Util::is_plugin();
297
	}
298
299
	/**
300
	 * Alias for the get_variables static method in the Kirki_Util class.
301
	 * This is here for backwards-compatibility purposes.
302
	 *
303
	 * @static
304
	 * @access public
305
	 * @since 2.0.0
306
	 * @return array Formatted as array( 'variable-name' => value ).
307
	 */
308
	public static function get_variables() {
309
		// Log error for developers.
310
		_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' );
311
		// Return result using the Kirki_Util class.
312
		return Kirki_Util::get_variables();
313
	}
314
315
}
316