Completed
Push — develop ( 17758a...cd430d )
by Aristeides
05:46 queued 03:17
created

Kirki_Init::set_url()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 10
nop 0
dl 0
loc 24
rs 8.5125
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
		Kirki::$path = wp_normalize_path( Kirki::$path );
46
		$parent_theme_path = wp_normalize_path( get_template_directory() );
47
		$child_theme_path  = wp_normalize_path( get_stylesheet_directory() );
48
		if ( false === strpos( $child_theme_path, Kirki::$path ) ) {
49
			Kirki::$url = str_replace( $child_theme_path, get_stylesheet_directory_uri(), Kirki::$path );
50
		} elseif ( false === strpos( $parent_theme_path, Kirki::$path ) ) {
51
			Kirki::$url = str_replace( $parent_theme_path, get_stylesheet_directory_uri(), Kirki::$path );
52
		} elseif ( self::is_plugin() ) {
53
			Kirki::$url = plugin_dir_url( KIRKI_PLUGIN_FILE );
54
		} elseif ( defined( 'ABSPATH' ) ) {
55
			// Replace path with URL.
56
			$kirki_url  = str_replace( ABSPATH, '', Kirki::$path );
57
			Kirki::$url = site_url( $kirki_url );
58
			// Escape the URL.
59
			Kirki::$url = esc_url_raw( Kirki::$url );
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
		Kirki::$url = esc_url_raw( Kirki::$url );
67
	}
68
69
	/**
70
	 * Add the default Kirki control types.
71
	 *
72
	 * @access public
73
	 * @since 3.0.0
74
	 * @param array $control_types The control types array.
75
	 * @return array
76
	 */
77
	public function default_control_types( $control_types = array() ) {
78
79
		$this->control_types = array(
80
			'checkbox'              => 'WP_Customize_Control',
81
			'kirki-background'      => 'Kirki_Control_Background',
82
			'kirki-code'            => 'Kirki_Control_Code',
83
			'kirki-color'           => 'Kirki_Control_Color',
84
			'kirki-color-palette'   => 'Kirki_Control_Color_Palette',
85
			'kirki-custom'          => 'Kirki_Control_Custom',
86
			'kirki-date'            => 'Kirki_Control_Date',
87
			'kirki-dashicons'       => 'Kirki_Control_Dashicons',
88
			'kirki-dimension'       => 'Kirki_Control_Dimension',
89
			'kirki-dimensions'      => 'Kirki_Control_Dimensions',
90
			'kirki-editor'          => 'Kirki_Control_Editor',
91
			'kirki-fontawesome'     => 'Kirki_Control_FontAwesome',
92
			'kirki-gradient'        => 'Kirki_Control_Gradient',
93
			'kirki-multicolor'      => 'Kirki_Control_Multicolor',
94
			'kirki-multicheck'      => 'Kirki_Control_MultiCheck',
95
			'kirki-number'          => 'Kirki_Control_Number',
96
			'kirki-palette'         => 'Kirki_Control_Palette',
97
			'kirki-preset'          => 'Kirki_Control_Preset',
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'                 => 'WP_Customize_Image_Control',
111
			'cropped_image'         => 'WP_Customize_Cropped_Image_Control',
112
			'upload'                => 'WP_Customize_Upload_Control',
113
		);
114
		return array_merge( $control_types, $this->control_types );
115
116
	}
117
118
	/**
119
	 * Helper function that adds the fields, sections and panels to the customizer.
120
	 *
121
	 * @return void
122
	 */
123
	public function add_to_customizer() {
124
		$this->fields_from_filters();
125
		add_action( 'customize_register', array( $this, 'register_control_types' ) );
126
		add_action( 'customize_register', array( $this, 'add_panels' ), 97 );
127
		add_action( 'customize_register', array( $this, 'add_sections' ), 98 );
128
		add_action( 'customize_register', array( $this, 'add_fields' ), 99 );
129
		/* new Kirki_Modules_Loading(); */
130
	}
131
132
	/**
133
	 * Register control types
134
	 *
135
	 * @return  void
136
	 */
137
	public function register_control_types() {
138
		global $wp_customize;
139
140
		$section_types = apply_filters( 'kirki/section_types', array() );
141
		foreach ( $section_types as $section_type ) {
142
			$wp_customize->register_section_type( $section_type );
143
		}
144
		if ( empty( $this->control_types ) ) {
145
			$this->control_types = $this->default_control_types();
146
		}
147
		$do_not_register_control_types = apply_filters( 'kirki/control_types/exclude', array(
148
			'Kirki_Control_Repeater',
149
		) );
150
		foreach ( $this->control_types as $control_type ) {
151
			if ( 0 === strpos( $control_type, 'Kirki' ) && ! in_array( $control_type, $do_not_register_control_types ) ) {
152
				$wp_customize->register_control_type( $control_type );
153
			}
154
		}
155
	}
156
157
	/**
158
	 * Register our panels to the WordPress Customizer.
159
	 *
160
	 * @access public
161
	 */
162
	public function add_panels() {
163
		if ( ! empty( Kirki::$panels ) ) {
164
			foreach ( Kirki::$panels as $panel_args ) {
165
				new Kirki_Panel( $panel_args );
166
			}
167
		}
168
	}
169
170
	/**
171
	 * Register our sections to the WordPress Customizer.
172
	 *
173
	 * @var	object	The WordPress Customizer object
174
	 * @return  void
175
	 */
176
	public function add_sections() {
177
		if ( ! empty( Kirki::$sections ) ) {
178
			foreach ( Kirki::$sections as $section_args ) {
179
				new Kirki_Section( $section_args );
180
			}
181
		}
182
	}
183
184
	/**
185
	 * Create the settings and controls from the $fields array and register them.
186
	 *
187
	 * @var	object	The WordPress Customizer object
188
	 * @return  void
189
	 */
190
	public function add_fields() {
191
192
		global $wp_customize;
193
		foreach ( Kirki::$fields as $args ) {
194
195
			// Create the settings.
196
			new Kirki_Settings( $args );
197
198
			// Check if we're on the customizer.
199
			// If we are, then we will create the controls, add the scripts needed for the customizer
200
			// and any other tweaks that this field may require.
201
			if ( $wp_customize ) {
202
203
				// Create the control.
204
				new Kirki_Control( $args );
205
206
			}
207
		}
208
	}
209
210
	/**
211
	 * Build the variables.
212
	 *
213
	 * @return array 	('variable-name' => value)
214
	 */
215
	public static function get_variables() {
216
217
		$variables = array();
218
219
		// Loop through all fields.
220
		foreach ( Kirki::$fields as $field ) {
221
222
			// Check if we have variables for this field.
223
			if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) {
224
225
				// Loop through the array of variables.
226
				foreach ( $field['variables'] as $field_variable ) {
227
228
					// Is the variable ['name'] defined? If yes, then we can proceed.
229
					if ( isset( $field_variable['name'] ) ) {
230
231
						// Sanitize the variable name.
232
						$variable_name = esc_attr( $field_variable['name'] );
233
234
						// Do we have a callback function defined? If not then set $variable_callback to false.
235
						$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false;
236
237
						// If we have a variable_callback defined then get the value of the option
238
						// and run it through the callback function.
239
						// If no callback is defined (false) then just get the value.
240
						if ( $variable_callback ) {
241
							$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) );
242
						} else {
243
							$variables[ $variable_name ] = Kirki::get_option( $field['settings'] );
244
						}
245
					}
246
				}
247
			}
248
		}
249
250
		// Pass the variables through a filter ('kirki/variable') and return the array of variables.
251
		return apply_filters( 'kirki/variable', $variables );
252
253
	}
254
255
	/**
256
	 * Process fields added using the 'kirki/fields' and 'kirki/controls' filter.
257
	 * These filters are no longer used, this is simply for backwards-compatibility.
258
	 */
259
	public function fields_from_filters() {
260
261
		$fields = apply_filters( 'kirki/controls', array() );
262
		$fields = apply_filters( 'kirki/fields', $fields );
263
264
		if ( ! empty( $fields ) ) {
265
			foreach ( $fields as $field ) {
266
				Kirki::add_field( 'global', $field );
267
			}
268
		}
269
270
	}
271
272
	/**
273
	 * Handle saving of settings with "user_meta" storage type.
274
	 *
275
	 * @param string $value The value being saved.
276
	 * @param object $wp_customize_setting $WP_Customize_Setting The WP_Customize_Setting instance when saving is happening.
277
	 */
278
	public function update_user_meta( $value, $wp_customize_setting ) {
279
		update_user_meta( get_current_user_id(), $wp_customize_setting->id, $value );
280
	}
281
282
	/**
283
	 * Changes select2 version in ACF.
284
	 * Fixes a plugin conflict that was causing select fields to crash
285
	 * because of a version mismatch between ACF's and Kirki's select2 scripts.
286
	 * Props @hellor0bot
287
	 *
288
	 * @see https://github.com/aristath/kirki/issues/1302
289
	 * @access public
290
	 * @since 3.0.0
291
	 * @param string $ver The Select2 script version.
292
	 * @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...
293
	 */
294
	public function acf_select2_version( $ver ) {
295
		if ( is_customize_preview() ) {
296
			return 4;
297
		}
298
		return $ver;
299
	}
300
301
	/**
302
	 * Determine if Kirki is installed as a plugin.
303
	 *
304
	 * @static
305
	 * @access public
306
	 * @since 3.0.0
307
	 * @return bool
308
	 */
309
	public static function is_plugin() {
310
311
		$is_plugin = false;
312
		if ( ! function_exists( 'get_plugins' ) ) {
313
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
314
		}
315
316
		// Get all plugins.
317
		$plugins = get_plugins();
318
		$_plugin = '';
319
		foreach ( $plugins as $plugin => $args ) {
320
			if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) {
321
				$is_plugin = true;
322
				$_plugin   = $plugin;
323
			}
324
		}
325
326
		// No need to proceed any further if Kirki wasn't found in the list of plugins.
327
		if ( ! $is_plugin ) {
328
			return false;
329
		}
330
331
		// Extra logic in case the plugin is installed but not activated.
332
		if ( is_customize_preview() ) {
333
334
			// Make sure the is_plugins_loaded function is loaded.
335
			if ( ! function_exists( 'is_plugin_active' ) ) {
336
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
337
			}
338
339
			if ( $_plugin && ! is_plugin_active( $_plugin ) ) {
340
				return false;
341
			}
342
		}
343
		return $is_plugin;
344
	}
345
}
346