Completed
Push — develop ( ecadb5...8032db )
by Aristeides
02:23
created

Kirki_Init::is_plugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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' ) );
34
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
35
		add_filter( 'kirki/control_types', array( $this, 'default_control_types' ) );
36
37
		new Kirki_Values();
38
	}
39
40
	/**
41
	 * Properly set the Kirki URL for assets.
42
	 *
43
	 * @static
44
	 * @access public
45
	 * @return void
46
	 */
47
	public static function set_url() {
48
49
		// Get correct URL and path to wp-content
50
		$content_url = untrailingslashit( dirname( dirname( get_stylesheet_directory_uri() ) ) );
51
		$content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
52
53
		Kirki::$url = str_replace( $content_dir, $content_url, wp_normalize_path( Kirki::$path ) );
54
55
		// Apply the kirki/config filter.
56
		$config = apply_filters( 'kirki/config', array() );
57
		if ( isset( $config['url_path'] ) ) {
58
			Kirki::$url = $config['url_path'];
59
		}
60
61
		// Make sure the right protocol is used.
62
		Kirki::$url = set_url_scheme( Kirki::$url );
63
	}
64
65
	/**
66
	 * Add the default Kirki control types.
67
	 *
68
	 * @access public
69
	 * @since 3.0.0
70
	 * @param array $control_types The control types array.
71
	 * @return array
72
	 */
73
	public function default_control_types( $control_types = array() ) {
74
75
		$this->control_types = array(
76
			'checkbox'              => 'WP_Customize_Control',
77
			'kirki-background'      => 'Kirki_Control_Background',
78
			'kirki-code'            => 'Kirki_Control_Code',
79
			'kirki-color'           => 'Kirki_Control_Color',
80
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
81
			'kirki-custom'          => 'Kirki_Control_Custom',
82
			'kirki-date'            => 'Kirki_Control_Date',
83
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
84
			'kirki-dimension'       => 'Kirki_Control_Dimension',
85
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
86
			'kirki-editor'          => 'Kirki_Control_Editor',
87
			'kirki-fontawesome'     => 'Kirki_Control_FontAwesome',
88
			'kirki-gradient'        => 'Kirki_Control_Gradient',
89
			'kirki-image'           => 'Kirki_Control_Image',
90
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
91
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
92
			'kirki-number'          => 'Kirki_Control_Number',
93
			'kirki-palette'         => 'Kirki_Control_Palette',
94
			'kirki-preset'          => 'Kirki_Control_Preset',
95
			'kirki-radio'           => 'Kirki_Control_Radio',
96
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
97
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
98
			'repeater'              => 'Kirki_Control_Repeater',
99
			'kirki-select'          => 'Kirki_Control_Select',
100
			'kirki-slider'          => 'Kirki_Control_Slider',
101
			'kirki-sortable'        => 'Kirki_Control_Sortable',
102
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
103
			'kirki-switch'          => 'Kirki_Control_Switch',
104
			'kirki-generic'         => 'Kirki_Control_Generic',
105
			'kirki-toggle'          => 'Kirki_Control_Toggle',
106
			'kirki-typography'      => 'Kirki_Control_Typography',
107
			'image'                 => 'Kirki_Control_Image',
108
			'cropped_image'         => 'WP_Customize_Cropped_Image_Control',
109
			'upload'                => 'WP_Customize_Upload_Control',
110
		);
111
		return array_merge( $this->control_types, $control_types );
112
113
	}
114
115
	/**
116
	 * Helper function that adds the fields, sections and panels to the customizer.
117
	 *
118
	 * @return void
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
	 * @return  void
132
	 */
133
	public function register_control_types() {
134
		global $wp_customize;
135
136
		$section_types = apply_filters( 'kirki/section_types', array() );
137
		foreach ( $section_types as $section_type ) {
138
			$wp_customize->register_section_type( $section_type );
139
		}
140
141
		$this->control_types = $this->default_control_types();
142
		foreach ( $this->control_types as $key => $classname ) {
143
			if ( ! class_exists( $classname ) ) {
144
				unset( $this->control_types[ $key ] );
145
			}
146
		}
147
148
		$skip_control_types = apply_filters(
149
			'kirki/control_types/exclude', array(
150
				'Kirki_Control_Repeater',
151
				'WP_Customize_Control',
152
			)
153
		);
154
155
		foreach ( $this->control_types as $control_type ) {
156
			if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) {
157
				$wp_customize->register_control_type( $control_type );
158
			}
159
		}
160
	}
161
162
	/**
163
	 * Register our panels to the WordPress Customizer.
164
	 *
165
	 * @access public
166
	 */
167
	public function add_panels() {
168
		if ( ! empty( Kirki::$panels ) ) {
169
			foreach ( Kirki::$panels as $panel_args ) {
170
				// Extra checks for nested panels.
171
				if ( isset( $panel_args['panel'] ) ) {
172
					if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) {
173
						// Set the type to nested.
174
						$panel_args['type'] = 'kirki-nested';
175
					}
176
				}
177
178
				new Kirki_Panel( $panel_args );
179
			}
180
		}
181
	}
182
183
	/**
184
	 * Register our sections to the WordPress Customizer.
185
	 *
186
	 * @var object The WordPress Customizer object
187
	 * @return void
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
	 * @return void
214
	 */
215
	public function add_fields() {
216
217
		global $wp_customize;
218
		foreach ( Kirki::$fields as $args ) {
219
220
			// Create the settings.
221
			new Kirki_Settings( $args );
222
223
			// Check if we're on the customizer.
224
			// If we are, then we will create the controls, add the scripts needed for the customizer
225
			// and any other tweaks that this field may require.
226
			if ( $wp_customize ) {
227
228
				// Create the control.
229
				new Kirki_Control( $args );
230
231
			}
232
		}
233
	}
234
235
	/**
236
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
237
	 * These filters are no longer used, this is simply for backwards-compatibility.
238
	 *
239
	 * @access private
240
	 * @since 2.0.0
241
	 */
242
	private function fields_from_filters() {
243
244
		$fields = apply_filters( 'kirki/controls', array() );
245
		$fields = apply_filters( 'kirki/fields', $fields );
246
247
		if ( ! empty( $fields ) ) {
248
			foreach ( $fields as $field ) {
249
				Kirki::add_field( 'global', $field );
250
			}
251
		}
252
	}
253
254
	/**
255
	 * Alias for the is_plugin static method in the Kirki_Util class.
256
	 * This is here for backwards-compatibility purposes.
257
	 *
258
	 * @static
259
	 * @access public
260
	 * @since 3.0.0
261
	 * @return bool
262
	 */
263
	public static function is_plugin() {
264
		// Return result using the Kirki_Util class.
265
		return Kirki_Util::is_plugin();
266
	}
267
268
	/**
269
	 * Alias for the get_variables static method in the Kirki_Util class.
270
	 * This is here for backwards-compatibility purposes.
271
	 *
272
	 * @static
273
	 * @access public
274
	 * @since 2.0.0
275
	 * @return array Formatted as array( 'variable-name' => value ).
276
	 */
277
	public static function get_variables() {
278
		// Log error for developers.
279
		_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' );
280
		// Return result using the Kirki_Util class.
281
		return Kirki_Util::get_variables();
282
	}
283
284
}
285