Completed
Branch develop (98485c)
by Aristeides
12:56 queued 06:30
created

Kirki_Init::set_url()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
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' ) );
1 ignored issue
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
		/** @scrutinizer ignore-call */ 
34
  add_action( 'after_setup_theme', array( $this, 'set_url' ) );
Loading history...
34
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
35
		add_filter( 'kirki_control_types', array( $this, 'default_control_types' ) );
1 ignored issue
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
		/** @scrutinizer ignore-call */ 
36
  add_filter( 'kirki_control_types', array( $this, 'default_control_types' ) );
Loading history...
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() ) ) );
2 ignored issues
show
Bug introduced by
The function get_stylesheet_directory_uri was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
		$content_url = untrailingslashit( dirname( dirname( /** @scrutinizer ignore-call */ get_stylesheet_directory_uri() ) ) );
Loading history...
Bug introduced by
The function untrailingslashit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
		$content_url = /** @scrutinizer ignore-call */ untrailingslashit( dirname( dirname( get_stylesheet_directory_uri() ) ) );
Loading history...
59
		$content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
2 ignored issues
show
Bug introduced by
The constant WP_CONTENT_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function wp_normalize_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
		$content_dir = /** @scrutinizer ignore-call */ wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
Loading history...
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() );
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
		$config = /** @scrutinizer ignore-call */ apply_filters( 'kirki_config', array() );
Loading history...
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 );
1 ignored issue
show
Bug introduced by
The function set_url_scheme was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
		Kirki::$url = /** @scrutinizer ignore-call */ set_url_scheme( Kirki::$url );
Loading history...
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'              => 'WP_Customize_Control',
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-preset'          => 'Kirki_Control_Preset',
102
			'kirki-radio'           => 'Kirki_Control_Radio',
103
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
104
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
105
			'repeater'              => 'Kirki_Control_Repeater',
106
			'kirki-select'          => 'Kirki_Control_Select',
107
			'kirki-slider'          => 'Kirki_Control_Slider',
108
			'kirki-sortable'        => 'Kirki_Control_Sortable',
109
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
110
			'kirki-switch'          => 'Kirki_Control_Switch',
111
			'kirki-generic'         => 'Kirki_Control_Generic',
112
			'kirki-toggle'          => 'Kirki_Control_Toggle',
113
			'kirki-typography'      => 'Kirki_Control_Typography',
114
			'image'                 => 'Kirki_Control_Image',
115
			'cropped_image'         => 'Kirki_Control_Cropped_Image',
116
			'upload'                => 'Kirki_Control_Upload',
117
		);
118
		return array_merge( $this->control_types, $control_types );
119
120
	}
121
122
	/**
123
	 * Helper function that adds the fields, sections and panels to the customizer.
124
	 */
125
	public function add_to_customizer() {
126
		$this->fields_from_filters();
127
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
1 ignored issue
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
		/** @scrutinizer ignore-call */ 
128
  add_action( 'customize_register', array( $this, 'register_control_types' ) );
Loading history...
128
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
129
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
130
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
131
	}
132
133
	/**
134
	 * Register control types
135
	 */
136
	public function register_control_types() {
137
		global $wp_customize;
138
139
		$section_types = apply_filters( 'kirki_section_types', array() );
140
		foreach ( $section_types as $section_type ) {
141
			$wp_customize->register_section_type( $section_type );
142
		}
143
144
		$this->control_types = $this->default_control_types();
145
		foreach ( $this->control_types as $key => $classname ) {
146
			if ( ! class_exists( $classname ) ) {
147
				unset( $this->control_types[ $key ] );
148
			}
149
		}
150
151
		$skip_control_types = apply_filters(
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
		$skip_control_types = /** @scrutinizer ignore-call */ apply_filters(
Loading history...
152
			'kirki_control_types_exclude', array(
153
				'Kirki_Control_Repeater',
154
				'WP_Customize_Control',
155
			)
156
		);
157
158
		foreach ( $this->control_types as $control_type ) {
159
			if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) {
160
				$wp_customize->register_control_type( $control_type );
161
			}
162
		}
163
	}
164
165
	/**
166
	 * Register our panels to the WordPress Customizer.
167
	 *
168
	 * @access public
169
	 */
170
	public function add_panels() {
171
		if ( ! empty( Kirki::$panels ) ) {
172
			foreach ( Kirki::$panels as $panel_args ) {
173
				// Extra checks for nested panels.
174
				if ( isset( $panel_args['panel'] ) ) {
175
					if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) {
176
						// Set the type to nested.
177
						$panel_args['type'] = 'kirki-nested';
178
					}
179
				}
180
181
				new Kirki_Panel( $panel_args );
182
			}
183
		}
184
	}
185
186
	/**
187
	 * Register our sections to the WordPress Customizer.
188
	 *
189
	 * @var object The WordPress Customizer object
190
	 */
191
	public function add_sections() {
192
		if ( ! empty( Kirki::$sections ) ) {
193
			foreach ( Kirki::$sections as $section_args ) {
194
				// Extra checks for nested sections.
195
				if ( isset( $section_args['section'] ) ) {
196
					if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) {
197
						// Set the type to nested.
198
						$section_args['type'] = 'kirki-nested';
199
						// We need to check if the parent section is nested inside a panel.
200
						$parent_section = Kirki::$sections[ $section_args['section'] ];
201
						if ( isset( $parent_section['panel'] ) ) {
202
							$section_args['panel'] = $parent_section['panel'];
203
						}
204
					}
205
				}
206
				new Kirki_Section( $section_args );
207
			}
208
		}
209
	}
210
211
	/**
212
	 * Create the settings and controls from the $fields array and register them.
213
	 *
214
	 * @var object The WordPress Customizer object.
215
	 */
216
	public function add_fields() {
217
218
		global $wp_customize;
219
		foreach ( Kirki::$fields as $args ) {
220
221
			// Create the settings.
222
			new Kirki_Settings( $args );
223
224
			// Check if we're on the customizer.
225
			// If we are, then we will create the controls, add the scripts needed for the customizer
226
			// and any other tweaks that this field may require.
227
			if ( $wp_customize ) {
228
229
				// Create the control.
230
				new Kirki_Control( $args );
231
232
			}
233
		}
234
	}
235
236
	/**
237
	 * Process fields added using the 'kirki_fields' and 'kirki_controls' filter.
238
	 * These filters are no longer used, this is simply for backwards-compatibility.
239
	 *
240
	 * @access private
241
	 * @since 2.0.0
242
	 */
243
	private function fields_from_filters() {
244
245
		$fields = apply_filters( 'kirki_controls', array() );
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

245
		$fields = /** @scrutinizer ignore-call */ apply_filters( 'kirki_controls', array() );
Loading history...
246
		$fields = apply_filters( 'kirki_fields', $fields );
247
248
		if ( ! empty( $fields ) ) {
249
			foreach ( $fields as $field ) {
250
				Kirki::add_field( 'global', $field );
251
			}
252
		}
253
	}
254
255
	/**
256
	 * Alias for the is_plugin static method in the Kirki_Util class.
257
	 * This is here for backwards-compatibility purposes.
258
	 *
259
	 * @static
260
	 * @access public
261
	 * @since 3.0.0
262
	 * @return bool
263
	 */
264
	public static function is_plugin() {
265
		// Return result using the Kirki_Util class.
266
		return Kirki_Util::is_plugin();
267
	}
268
269
	/**
270
	 * Alias for the get_variables static method in the Kirki_Util class.
271
	 * This is here for backwards-compatibility purposes.
272
	 *
273
	 * @static
274
	 * @access public
275
	 * @since 2.0.0
276
	 * @return array Formatted as array( 'variable-name' => value ).
277
	 */
278
	public static function get_variables() {
279
		// Log error for developers.
280
		_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' );
2 ignored issues
show
Bug introduced by
The function esc_attr__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

280
		_doing_it_wrong( __METHOD__, /** @scrutinizer ignore-call */ esc_attr__( 'We detected you\'re using Kirki_Init::get_variables(). Please use Kirki_Util::get_variables() instead.', 'kirki' ), '3.0.10' );
Loading history...
Bug introduced by
The function _doing_it_wrong was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

280
		/** @scrutinizer ignore-call */ 
281
  _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' );
Loading history...
281
		// Return result using the Kirki_Util class.
282
		return Kirki_Util::get_variables();
283
	}
284
285
	/**
286
	 * Remove panels.
287
	 *
288
	 * @since 3.0.17
289
	 * @param object $wp_customize The customizer object.
290
	 * @return void
291
	 */
292
	public function remove_panels( $wp_customize ) {
293
		foreach ( Kirki::$panels_to_remove as $panel ) {
294
			$wp_customize->remove_panel( $panel );
295
		}
296
	}
297
298
	/**
299
	 * Remove sections.
300
	 *
301
	 * @since 3.0.17
302
	 * @param object $wp_customize The customizer object.
303
	 * @return void
304
	 */
305
	public function remove_sections( $wp_customize ) {
306
		foreach ( Kirki::$sections_to_remove as $section ) {
307
			$wp_customize->remove_section( $section );
308
		}
309
	}
310
311
	/**
312
	 * Remove controls.
313
	 *
314
	 * @since 3.0.17
315
	 * @param object $wp_customize The customizer object.
316
	 * @return void
317
	 */
318
	public function remove_controls( $wp_customize ) {
319
		foreach ( Kirki::$controls_to_remove as $control ) {
320
			$wp_customize->remove_control( $control );
321
		}
322
	}
323
}
324