Completed
Push — develop ( 53715b...572ce4 )
by Aristeides
02:29
created

Kirki_Init::set_url()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 11
nop 0
dl 0
loc 27
rs 6.7272
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) 2016, 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
		$this->set_url();
32
		add_action( 'after_setup_theme', array( $this, 'set_url' ) );
33
		add_action( 'customize_update_user_meta', array( $this, 'update_user_meta' ), 10, 2 );
34
		add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 );
35
		add_filter( 'kirki/control_types', array( $this, 'default_control_types' ) );
36
		add_filter( 'acf/settings/select2_version', array( $this, 'acf_select2_version' ), 99 );
37
	}
38
39
	/**
40
	 * Properly set the Kirki URL for assets.
41
	 * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme
42
	 * and then does some calculations to get the proper URL for its CSS & JS assets.
43
	 */
44
	public function set_url() {
45
		if ( Kirki::$url ) {
46
			return;
47
		}
48
		Kirki::$path = wp_normalize_path( Kirki::$path );
49
		$parent_theme_path = wp_normalize_path( get_template_directory() );
50
		$child_theme_path  = wp_normalize_path( get_stylesheet_directory() );
51
		if ( false === strpos( Kirki::$path, $child_theme_path ) ) {
52
			Kirki::$url = str_replace( $child_theme_path, get_stylesheet_directory_uri(), Kirki::$path );
53
		} elseif ( false === strpos( Kirki::$path, $parent_theme_path ) ) {
54
			Kirki::$url = str_replace( $parent_theme_path, get_stylesheet_directory_uri(), Kirki::$path );
55
		} elseif ( self::is_plugin() ) {
56
			Kirki::$url = plugin_dir_url( KIRKI_PLUGIN_FILE );
57
		} elseif ( defined( 'ABSPATH' ) ) {
58
			// Replace path with URL.
59
			$kirki_url  = str_replace( ABSPATH, '', Kirki::$path );
60
			Kirki::$url = site_url( $kirki_url );
61
			// Escape the URL.
62
			Kirki::$url = esc_url_raw( Kirki::$url );
63
		}
64
		// Apply the kirki/config filter.
65
		$config = apply_filters( 'kirki/config', array() );
66
		if ( isset( $config['url_path'] ) ) {
67
			Kirki::$url = $config['url_path'];
68
		}
69
		Kirki::$url = esc_url_raw( Kirki::$url );
70
	}
71
72
	/**
73
	 * Add the default Kirki control types.
74
	 *
75
	 * @access public
76
	 * @since 3.0.0
77
	 * @param array $control_types The control types array.
78
	 * @return array
79
	 */
80
	public function default_control_types( $control_types = array() ) {
81
82
		$this->control_types = array(
83
			'checkbox'              => 'WP_Customize_Control',
84
			'kirki-background'      => 'Kirki_Control_Background',
85
			'kirki-code'            => 'Kirki_Control_Code',
86
			'kirki-color'           => 'Kirki_Control_Color',
87
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
88
			'kirki-custom'          => 'Kirki_Control_Custom',
89
			'kirki-date'            => 'Kirki_Control_Date',
90
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
91
			'kirki-dimension'       => 'Kirki_Control_Dimension',
92
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
93
			'kirki-editor'          => 'Kirki_Control_Editor',
94
			'kirki-gradient'        => 'Kirki_Control_Gradient',
95
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
96
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
97
			'kirki-number'          => 'Kirki_Control_Number',
98
			'kirki-palette'         => 'Kirki_Control_Palette',
99
			'kirki-preset'          => 'Kirki_Control_Preset',
100
			'kirki-radio'           => 'Kirki_Control_Radio',
101
			'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet',
102
			'kirki-radio-image'     => 'Kirki_Control_Radio_Image',
103
			'repeater'              => 'Kirki_Control_Repeater',
104
			'kirki-select'          => 'Kirki_Control_Select',
105
			'kirki-slider'          => 'Kirki_Control_Slider',
106
			'kirki-sortable'        => 'Kirki_Control_Sortable',
107
			'kirki-spacing'         => 'Kirki_Control_Dimensions',
108
			'kirki-switch'          => 'Kirki_Control_Switch',
109
			'kirki-generic'         => 'Kirki_Control_Generic',
110
			'kirki-toggle'          => 'Kirki_Control_Toggle',
111
			'kirki-typography'      => 'Kirki_Control_Typography',
112
			'image'                 => 'WP_Customize_Image_Control',
113
			'cropped_image'         => 'WP_Customize_Cropped_Image_Control',
114
			'upload'                => 'WP_Customize_Upload_Control',
115
		);
116
		return array_merge( $control_types, $this->control_types );
117
118
	}
119
120
	/**
121
	 * Helper function that adds the fields, sections and panels to the customizer.
122
	 *
123
	 * @return void
124
	 */
125
	public function add_to_customizer() {
126
		$this->fields_from_filters();
127
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
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
		/* new Kirki_Modules_Loading(); */
132
	}
133
134
	/**
135
	 * Register control types
136
	 *
137
	 * @return  void
138
	 */
139
	public function register_control_types() {
140
		global $wp_customize;
141
142
		$section_types = apply_filters( 'kirki/section_types', array() );
143
		foreach ( $section_types as $section_type ) {
144
			$wp_customize->register_section_type( $section_type );
145
		}
146
		if ( empty( $this->control_types ) ) {
147
			$this->control_types = $this->default_control_types();
148
		}
149
		$do_not_register_control_types = apply_filters( 'kirki/control_types/exclude', array(
150
			'Kirki_Control_Repeater',
151
		) );
152
		foreach ( $this->control_types as $control_type ) {
153
			if ( 0 === strpos( $control_type, 'Kirki' ) && ! in_array( $control_type, $do_not_register_control_types ) ) {
154
				$wp_customize->register_control_type( $control_type );
155
			}
156
		}
157
	}
158
159
	/**
160
	 * Register our panels to the WordPress Customizer.
161
	 *
162
	 * @access public
163
	 */
164
	public function add_panels() {
165
		if ( ! empty( Kirki::$panels ) ) {
166
			foreach ( Kirki::$panels as $panel_args ) {
167
				new Kirki_Panel( $panel_args );
168
			}
169
		}
170
	}
171
172
	/**
173
	 * Register our sections to the WordPress Customizer.
174
	 *
175
	 * @var	object	The WordPress Customizer object
176
	 * @return  void
177
	 */
178
	public function add_sections() {
179
		if ( ! empty( Kirki::$sections ) ) {
180
			foreach ( Kirki::$sections as $section_args ) {
181
				new Kirki_Section( $section_args );
182
			}
183
		}
184
	}
185
186
	/**
187
	 * Create the settings and controls from the $fields array and register them.
188
	 *
189
	 * @var	object	The WordPress Customizer object
190
	 * @return  void
191
	 */
192
	public function add_fields() {
193
194
		global $wp_customize;
195
		foreach ( Kirki::$fields as $args ) {
196
197
			// Create the settings.
198
			new Kirki_Settings( $args );
199
200
			// Check if we're on the customizer.
201
			// If we are, then we will create the controls, add the scripts needed for the customizer
202
			// and any other tweaks that this field may require.
203
			if ( $wp_customize ) {
204
205
				// Create the control.
206
				new Kirki_Control( $args );
207
208
			}
209
		}
210
	}
211
212
	/**
213
	 * Build the variables.
214
	 *
215
	 * @return array 	('variable-name' => value)
216
	 */
217
	public static function get_variables() {
218
219
		$variables = array();
220
221
		// Loop through all fields.
222
		foreach ( Kirki::$fields as $field ) {
223
224
			// Check if we have variables for this field.
225
			if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) {
226
227
				// Loop through the array of variables.
228
				foreach ( $field['variables'] as $field_variable ) {
229
230
					// Is the variable ['name'] defined? If yes, then we can proceed.
231
					if ( isset( $field_variable['name'] ) ) {
232
233
						// Sanitize the variable name.
234
						$variable_name = esc_attr( $field_variable['name'] );
235
236
						// Do we have a callback function defined? If not then set $variable_callback to false.
237
						$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false;
238
239
						// If we have a variable_callback defined then get the value of the option
240
						// and run it through the callback function.
241
						// If no callback is defined (false) then just get the value.
242
						if ( $variable_callback ) {
243
							$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) );
244
						} else {
245
							$variables[ $variable_name ] = Kirki::get_option( $field['settings'] );
246
						}
247
					}
248
				}
249
			}
250
		}
251
252
		// Pass the variables through a filter ('kirki/variable') and return the array of variables.
253
		return apply_filters( 'kirki/variable', $variables );
254
255
	}
256
257
	/**
258
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
259
	 * These filters are no longer used, this is simply for backwards-compatibility.
260
	 */
261
	public function fields_from_filters() {
262
263
		$fields = apply_filters( 'kirki/controls', array() );
264
		$fields = apply_filters( 'kirki/fields', $fields );
265
266
		if ( ! empty( $fields ) ) {
267
			foreach ( $fields as $field ) {
268
				Kirki::add_field( 'global', $field );
269
			}
270
		}
271
272
	}
273
274
	/**
275
	 * Handle saving of settings with "user_meta" storage type.
276
	 *
277
	 * @param string $value The value being saved.
278
	 * @param object $wp_customize_setting $WP_Customize_Setting The WP_Customize_Setting instance when saving is happening.
279
	 */
280
	public function update_user_meta( $value, $wp_customize_setting ) {
281
		update_user_meta( get_current_user_id(), $wp_customize_setting->id, $value );
282
	}
283
284
	/**
285
	 * Changes select2 version in ACF.
286
	 * Fixes a plugin conflict that was causing select fields to crash
287
	 * because of a version mismatch between ACF's and Kirki's select2 scripts.
288
	 * Props @hellor0bot
289
	 *
290
	 * @see https://github.com/aristath/kirki/issues/1302
291
	 * @access public
292
	 * @since 3.0.0
293
	 * @param string $ver The Select2 script version.
294
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
295
	 */
296
	public function acf_select2_version( $ver ) {
297
		if ( is_customize_preview() ) {
298
			return 4;
299
		}
300
		return $ver;
301
	}
302
303
	/**
304
	 * Determine if Kirki is installed as a plugin.
305
	 *
306
	 * @static
307
	 * @access public
308
	 * @since 3.0.0
309
	 * @return bool
310
	 */
311
	public static function is_plugin() {
312
313
		$is_plugin = false;
314
		if ( ! function_exists( 'get_plugins' ) ) {
315
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
316
		}
317
318
		// Get all plugins.
319
		$plugins = get_plugins();
320
		$_plugin = '';
321
		foreach ( $plugins as $plugin => $args ) {
322
			if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) {
323
				$is_plugin = true;
324
				$_plugin   = $plugin;
325
			}
326
		}
327
328
		// No need to proceed any further if Kirki wasn't found in the list of plugins.
329
		if ( ! $is_plugin ) {
330
			return false;
331
		}
332
333
		// Extra logic in case the plugin is installed but not activated.
334
		if ( is_customize_preview() ) {
335
336
			// Make sure the is_plugins_loaded function is loaded.
337
			if ( ! function_exists( 'is_plugin_active' ) ) {
338
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
339
			}
340
341
			if ( $_plugin && ! is_plugin_active( $_plugin ) ) {
342
				return false;
343
			}
344
		}
345
		return $is_plugin;
346
	}
347
}
348