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