Completed
Push — develop ( 5139fe...e3ddd5 )
by Aristeides
02:31
created

Kirki_Init::is_plugin()   C

Complexity

Conditions 12
Paths 36

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 18
nc 36
nop 0
dl 0
loc 36
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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