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