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
|
|
|
$this->set_url(); |
32
|
|
|
add_action( 'after_setup_theme', array( $this, 'set_url' ) ); |
33
|
|
|
add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 ); |
34
|
|
|
add_filter( 'kirki/control_types', array( $this, 'default_control_types' ) ); |
35
|
|
|
add_action( 'after_setup_theme', array( $this, 'acf_pro_compatibility' ) ); |
36
|
|
|
|
37
|
|
|
new Kirki_Custom_Build(); |
38
|
|
|
|
39
|
|
|
add_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
40
|
|
|
add_filter( 'option_active_plugins', array( $this, 'is_plugin_active' ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Properly set the Kirki URL for assets. |
45
|
|
|
* Determines if Kirki is installed as a plugin, in a child theme, or a parent theme |
46
|
|
|
* and then does some calculations to get the proper URL for its CSS & JS assets. |
47
|
|
|
*/ |
48
|
|
|
public function set_url() { |
49
|
|
|
|
50
|
|
|
Kirki::$path = wp_normalize_path( dirname( KIRKI_PLUGIN_FILE ) ); |
51
|
|
|
|
52
|
|
|
// Works in most cases. |
53
|
|
|
// Serves as a fallback in case all other checks fail. |
54
|
|
|
if ( defined( 'WP_CONTENT_DIR' ) ) { |
55
|
|
|
$content_dir = wp_normalize_path( WP_CONTENT_DIR ); |
56
|
|
|
if ( false !== strpos( Kirki::$path, $content_dir ) ) { |
57
|
|
|
$relative_path = str_replace( $content_dir, '', Kirki::$path ); |
58
|
|
|
Kirki::$url = content_url( $relative_path ); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If Kirki is installed as a plugin, use that for the URL. |
63
|
|
|
if ( self::is_plugin() ) { |
64
|
|
|
Kirki::$url = plugin_dir_url( KIRKI_PLUGIN_FILE ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Get the path to the theme. |
68
|
|
|
$theme_path = wp_normalize_path( get_template_directory() ); |
69
|
|
|
|
70
|
|
|
// Is Kirki included in the theme? |
71
|
|
|
if ( false !== strpos( Kirki::$path, $theme_path ) ) { |
72
|
|
|
Kirki::$url = get_template_directory_uri() . str_replace( $theme_path, '', Kirki::$path ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Is there a child-theme? |
76
|
|
|
$child_theme_path = wp_normalize_path( get_stylesheet_directory_uri() ); |
77
|
|
|
if ( $child_theme_path !== $theme_path ) { |
78
|
|
|
// Is Kirki included in a child theme? |
79
|
|
|
if ( false !== strpos( Kirki::$path, $child_theme_path ) ) { |
80
|
|
|
Kirki::$url = get_template_directory_uri() . str_replace( $child_theme_path, '', Kirki::$path ); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Apply the kirki/config filter. |
85
|
|
|
$config = apply_filters( 'kirki/config', array() ); |
86
|
|
|
if ( isset( $config['url_path'] ) ) { |
87
|
|
|
Kirki::$url = $config['url_path']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Escapes the URL. |
91
|
|
|
Kirki::$url = esc_url_raw( Kirki::$url ); |
92
|
|
|
// Make sure the right protocol is used. |
93
|
|
|
Kirki::$url = set_url_scheme( Kirki::$url ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add the default Kirki control types. |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* @since 3.0.0 |
101
|
|
|
* @param array $control_types The control types array. |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function default_control_types( $control_types = array() ) { |
105
|
|
|
|
106
|
|
|
$this->control_types = array( |
107
|
|
|
'checkbox' => 'WP_Customize_Control', |
108
|
|
|
'kirki-background' => 'Kirki_Control_Background', |
109
|
|
|
'kirki-code' => 'Kirki_Control_Code', |
110
|
|
|
'kirki-color' => 'Kirki_Control_Color', |
111
|
|
|
'kirki-color-palette' => 'Kirki_Control_Color_Palette', |
112
|
|
|
'kirki-custom' => 'Kirki_Control_Custom', |
113
|
|
|
'kirki-date' => 'Kirki_Control_Date', |
114
|
|
|
'kirki-dashicons' => 'Kirki_Control_Dashicons', |
115
|
|
|
'kirki-dimension' => 'Kirki_Control_Dimension', |
116
|
|
|
'kirki-dimensions' => 'Kirki_Control_Dimensions', |
117
|
|
|
'kirki-editor' => 'Kirki_Control_Editor', |
118
|
|
|
'kirki-fontawesome' => 'Kirki_Control_FontAwesome', |
119
|
|
|
'kirki-gradient' => 'Kirki_Control_Gradient', |
120
|
|
|
'kirki-image' => 'Kirki_Control_Image', |
121
|
|
|
'kirki-multicolor' => 'Kirki_Control_Multicolor', |
122
|
|
|
'kirki-multicheck' => 'Kirki_Control_MultiCheck', |
123
|
|
|
'kirki-number' => 'Kirki_Control_Number', |
124
|
|
|
'kirki-palette' => 'Kirki_Control_Palette', |
125
|
|
|
'kirki-preset' => 'Kirki_Control_Preset', |
126
|
|
|
'kirki-radio' => 'Kirki_Control_Radio', |
127
|
|
|
'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet', |
128
|
|
|
'kirki-radio-image' => 'Kirki_Control_Radio_Image', |
129
|
|
|
'repeater' => 'Kirki_Control_Repeater', |
130
|
|
|
'kirki-select' => 'Kirki_Control_Select', |
131
|
|
|
'kirki-slider' => 'Kirki_Control_Slider', |
132
|
|
|
'kirki-sortable' => 'Kirki_Control_Sortable', |
133
|
|
|
'kirki-spacing' => 'Kirki_Control_Dimensions', |
134
|
|
|
'kirki-switch' => 'Kirki_Control_Switch', |
135
|
|
|
'kirki-generic' => 'Kirki_Control_Generic', |
136
|
|
|
'kirki-toggle' => 'Kirki_Control_Toggle', |
137
|
|
|
'kirki-typography' => 'Kirki_Control_Typography', |
138
|
|
|
'image' => 'Kirki_Control_Image', |
139
|
|
|
'cropped_image' => 'WP_Customize_Cropped_Image_Control', |
140
|
|
|
'upload' => 'WP_Customize_Upload_Control', |
141
|
|
|
); |
142
|
|
|
return array_merge( $control_types, $this->control_types ); |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Helper function that adds the fields, sections and panels to the customizer. |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
public function add_to_customizer() { |
152
|
|
|
$this->fields_from_filters(); |
153
|
|
|
add_action( 'customize_register', array( $this, 'register_control_types' ) ); |
154
|
|
|
add_action( 'customize_register', array( $this, 'add_panels' ), 97 ); |
155
|
|
|
add_action( 'customize_register', array( $this, 'add_sections' ), 98 ); |
156
|
|
|
add_action( 'customize_register', array( $this, 'add_fields' ), 99 ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Register control types |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function register_control_types() { |
165
|
|
|
global $wp_customize; |
166
|
|
|
|
167
|
|
|
$section_types = apply_filters( 'kirki/section_types', array() ); |
168
|
|
|
foreach ( $section_types as $section_type ) { |
169
|
|
|
$wp_customize->register_section_type( $section_type ); |
170
|
|
|
} |
171
|
|
|
if ( empty( $this->control_types ) ) { |
172
|
|
|
$this->control_types = $this->default_control_types(); |
173
|
|
|
} |
174
|
|
|
$do_not_register_control_types = apply_filters( 'kirki/control_types/exclude', array( |
175
|
|
|
'Kirki_Control_Repeater', |
176
|
|
|
) ); |
177
|
|
|
foreach ( $this->control_types as $control_type ) { |
178
|
|
|
if ( 0 === strpos( $control_type, 'Kirki' ) && ! in_array( $control_type, $do_not_register_control_types, true ) && class_exists( $control_type ) ) { |
179
|
|
|
$wp_customize->register_control_type( $control_type ); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Register our panels to the WordPress Customizer. |
186
|
|
|
* |
187
|
|
|
* @access public |
188
|
|
|
*/ |
189
|
|
|
public function add_panels() { |
190
|
|
|
if ( ! empty( Kirki::$panels ) ) { |
191
|
|
|
foreach ( Kirki::$panels as $panel_args ) { |
192
|
|
|
// Extra checks for nested panels. |
193
|
|
|
if ( isset( $panel_args['panel'] ) ) { |
194
|
|
|
if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) { |
195
|
|
|
// Set the type to nested. |
196
|
|
|
$panel_args['type'] = 'kirki-nested'; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
new Kirki_Panel( $panel_args ); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Register our sections to the WordPress Customizer. |
207
|
|
|
* |
208
|
|
|
* @var object The WordPress Customizer object |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
|
|
public function add_sections() { |
212
|
|
|
if ( ! empty( Kirki::$sections ) ) { |
213
|
|
|
foreach ( Kirki::$sections as $section_args ) { |
214
|
|
|
// Extra checks for nested sections. |
215
|
|
|
if ( isset( $section_args['section'] ) ) { |
216
|
|
|
if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) { |
217
|
|
|
// Set the type to nested. |
218
|
|
|
$section_args['type'] = 'kirki-nested'; |
219
|
|
|
// We need to check if the parent section is nested inside a panel. |
220
|
|
|
$parent_section = Kirki::$sections[ $section_args['section'] ]; |
221
|
|
|
if ( isset( $parent_section['panel'] ) ) { |
222
|
|
|
$section_args['panel'] = $parent_section['panel']; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
new Kirki_Section( $section_args ); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Create the settings and controls from the $fields array and register them. |
233
|
|
|
* |
234
|
|
|
* @var object The WordPress Customizer object |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function add_fields() { |
238
|
|
|
|
239
|
|
|
global $wp_customize; |
240
|
|
|
foreach ( Kirki::$fields as $args ) { |
241
|
|
|
|
242
|
|
|
// Create the settings. |
243
|
|
|
new Kirki_Settings( $args ); |
244
|
|
|
|
245
|
|
|
// Check if we're on the customizer. |
246
|
|
|
// If we are, then we will create the controls, add the scripts needed for the customizer |
247
|
|
|
// and any other tweaks that this field may require. |
248
|
|
|
if ( $wp_customize ) { |
249
|
|
|
|
250
|
|
|
// Create the control. |
251
|
|
|
new Kirki_Control( $args ); |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Build the variables. |
259
|
|
|
* |
260
|
|
|
* @return array ('variable-name' => value) |
261
|
|
|
*/ |
262
|
|
|
public static function get_variables() { |
263
|
|
|
|
264
|
|
|
$variables = array(); |
265
|
|
|
|
266
|
|
|
// Loop through all fields. |
267
|
|
|
foreach ( Kirki::$fields as $field ) { |
268
|
|
|
|
269
|
|
|
// Check if we have variables for this field. |
270
|
|
|
if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) { |
271
|
|
|
|
272
|
|
|
// Loop through the array of variables. |
273
|
|
|
foreach ( $field['variables'] as $field_variable ) { |
274
|
|
|
|
275
|
|
|
// Is the variable ['name'] defined? If yes, then we can proceed. |
276
|
|
|
if ( isset( $field_variable['name'] ) ) { |
277
|
|
|
|
278
|
|
|
// Sanitize the variable name. |
279
|
|
|
$variable_name = esc_attr( $field_variable['name'] ); |
280
|
|
|
|
281
|
|
|
// Do we have a callback function defined? If not then set $variable_callback to false. |
282
|
|
|
$variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false; |
283
|
|
|
|
284
|
|
|
// If we have a variable_callback defined then get the value of the option |
285
|
|
|
// and run it through the callback function. |
286
|
|
|
// If no callback is defined (false) then just get the value. |
287
|
|
|
if ( $variable_callback ) { |
288
|
|
|
$variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) ); |
289
|
|
|
} else { |
290
|
|
|
$variables[ $variable_name ] = Kirki::get_option( $field['settings'] ); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// Pass the variables through a filter ('kirki/variable') and return the array of variables. |
298
|
|
|
return apply_filters( 'kirki/variable', $variables ); |
299
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Process fields added using the 'kirki/fields' and 'kirki/controls' filter. |
304
|
|
|
* These filters are no longer used, this is simply for backwards-compatibility. |
305
|
|
|
* |
306
|
|
|
* @access private |
307
|
|
|
* @since 2.0.0 |
308
|
|
|
*/ |
309
|
|
|
private function fields_from_filters() { |
310
|
|
|
|
311
|
|
|
$fields = apply_filters( 'kirki/controls', array() ); |
312
|
|
|
$fields = apply_filters( 'kirki/fields', $fields ); |
313
|
|
|
|
314
|
|
|
if ( ! empty( $fields ) ) { |
315
|
|
|
foreach ( $fields as $field ) { |
316
|
|
|
Kirki::add_field( 'global', $field ); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Changes select2 version in ACF. |
323
|
|
|
* Fixes a plugin conflict that was causing select fields to crash |
324
|
|
|
* because of a version mismatch between ACF's and Kirki's select2 scripts. |
325
|
|
|
* Props @hellor0bot |
326
|
|
|
* |
327
|
|
|
* @see https://github.com/aristath/kirki/issues/1302 |
328
|
|
|
* @access public |
329
|
|
|
* @since 3.0.0 |
330
|
|
|
*/ |
331
|
|
|
public function acf_pro_compatibility() { |
332
|
|
|
if ( is_customize_preview() ) { |
333
|
|
|
add_filter( 'acf/settings/enqueue_select2', '__return_false', 99 ); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Determine if Kirki is installed as a plugin. |
339
|
|
|
* |
340
|
|
|
* @static |
341
|
|
|
* @access public |
342
|
|
|
* @since 3.0.0 |
343
|
|
|
* @return bool |
344
|
|
|
*/ |
345
|
|
|
public static function is_plugin() { |
346
|
|
|
|
347
|
|
|
$is_plugin = false; |
348
|
|
|
if ( ! function_exists( 'get_plugins' ) ) { |
349
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// Get all plugins. |
353
|
|
|
$plugins = get_plugins(); |
354
|
|
|
$_plugin = ''; |
355
|
|
|
foreach ( $plugins as $plugin => $args ) { |
356
|
|
|
if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) { |
357
|
|
|
$is_plugin = true; |
358
|
|
|
$_plugin = $plugin; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// No need to proceed any further if Kirki wasn't found in the list of plugins. |
363
|
|
|
if ( ! $is_plugin ) { |
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
// Extra logic in case the plugin is installed but not activated. |
368
|
|
|
// Make sure the is_plugins_loaded function is loaded. |
369
|
|
|
if ( ! function_exists( 'is_plugin_active' ) ) { |
370
|
|
|
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if ( $_plugin && ! is_plugin_active( $_plugin ) ) { |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
return $is_plugin; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* HTTP Request injection. |
381
|
|
|
* |
382
|
|
|
* @access public |
383
|
|
|
* @since 3.0.0 |
384
|
|
|
* @param array $r The request params. |
385
|
|
|
* @param string $url The request URL. |
386
|
|
|
* @return array |
387
|
|
|
*/ |
388
|
|
|
public function http_request( $r = array(), $url = '' ) { |
|
|
|
|
389
|
|
|
// Early exit if not a request to wordpress.org. |
390
|
|
|
if ( false === strpos( $url, 'wordpress.org' ) ) { |
391
|
|
|
return $r; |
392
|
|
|
} |
393
|
|
|
// Early exit if Kirki is installed as a plugin. |
394
|
|
|
if ( self::is_plugin() ) { |
395
|
|
|
return $r; |
396
|
|
|
} |
397
|
|
|
// Early exit if we don't have everything we need. |
398
|
|
|
if ( ! isset( $r['body'] ) || ! isset( $r['body']['plugins'] ) || ! isset( $r['body']['translations'] ) || ! isset( $r['body']['locale'] ) || ! isset( $r['body']['all'] ) ) { |
399
|
|
|
return $r; |
400
|
|
|
} |
401
|
|
|
// Inject data. |
402
|
|
|
$plugins = json_decode( $r['body']['plugins'], true ); |
403
|
|
|
if ( isset( $plugins['plugins'] ) ) { |
404
|
|
|
$exists = false; |
405
|
|
|
foreach ( $plugins['plugins'] as $plugin ) { |
406
|
|
|
if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
407
|
|
|
$exists = true; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
411
|
|
|
$plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
412
|
|
|
} |
413
|
|
|
$r['body']['plugins'] = json_encode( $plugins ); |
414
|
|
|
return $r; |
415
|
|
|
} |
416
|
|
|
return $r; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Plugin is active. |
421
|
|
|
* |
422
|
|
|
* @since 3.0.0 |
423
|
|
|
* @access public |
424
|
|
|
* @param array $plugins An array of active plugins. |
425
|
|
|
* @return array Active plugins. |
426
|
|
|
*/ |
427
|
|
|
public function is_plugin_active( $plugins ) { |
|
|
|
|
428
|
|
|
global $pagenow; |
429
|
|
|
$exclude = array( |
430
|
|
|
'plugins.php', |
431
|
|
|
'plugin-install.php', |
432
|
|
|
); |
433
|
|
|
$referer = ( isset( $_SERVER ) && isset( $_SERVER['HTTP_REFERER'] ) ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; |
|
|
|
|
434
|
|
|
$refered = false; |
435
|
|
|
foreach ( $exclude as $exception ) { |
436
|
|
|
if ( false !== strpos( $referer, $exception ) ) { |
437
|
|
|
$refered = true; |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
if ( is_array( $plugins ) && ! in_array( $pagenow, $exclude, true ) && ! $refered ) { |
441
|
|
|
$exists = false; |
442
|
|
|
foreach ( $plugins as $plugin ) { |
443
|
|
|
if ( false !== strpos( $plugin, 'kirki.php' ) ) { |
444
|
|
|
$exists = true; |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
if ( ! $exists ) { |
448
|
|
|
$plugins[] = 'kirki/kirki.php'; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
return $plugins; |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.