1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Creates and validates field parameters. |
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
|
|
|
* Please do not use this class directly. |
15
|
|
|
* You should instead extend it per-field-type. |
16
|
|
|
*/ |
17
|
|
|
class Kirki_Field { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An array of the field arguments. |
21
|
|
|
* |
22
|
|
|
* @access protected |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $args = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The ID of the kirki_config we're using. |
29
|
|
|
* |
30
|
|
|
* @see Kirki_Config |
31
|
|
|
* @access protected |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $kirki_config = 'global'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Thje capability required so that users can edit this field. |
38
|
|
|
* |
39
|
|
|
* @access protected |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $capability = 'edit_theme_options'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* If we're using options instead of theme_mods |
46
|
|
|
* and we want them serialized, this is the option that |
47
|
|
|
* will saved in the db. |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $option_name = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Custom input attributes (defined as an array). |
56
|
|
|
* |
57
|
|
|
* @access protected |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $input_attrs = array(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Preset choices. |
64
|
|
|
* |
65
|
|
|
* @access protected |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $preset = array(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* CSS Variables. |
72
|
|
|
* |
73
|
|
|
* @access protected |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $css_vars = array(); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Use "theme_mod" or "option". |
80
|
|
|
* |
81
|
|
|
* @access protected |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $option_type = 'theme_mod'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The name of this setting (id for the db). |
88
|
|
|
* |
89
|
|
|
* @access protected |
90
|
|
|
* @var string|array |
91
|
|
|
*/ |
92
|
|
|
protected $settings = ''; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set to true if you want to disable all CSS output for this field. |
96
|
|
|
* |
97
|
|
|
* @access protected |
98
|
|
|
* @var bool |
99
|
|
|
*/ |
100
|
|
|
protected $disable_output = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The field type. |
104
|
|
|
* |
105
|
|
|
* @access protected |
106
|
|
|
* @var string |
107
|
|
|
*/ |
108
|
|
|
protected $type = 'kirki-generic'; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Some fields require options to be set. |
112
|
|
|
* We're whitelisting the property here |
113
|
|
|
* and suggest you validate this in a child class. |
114
|
|
|
* |
115
|
|
|
* @access protected |
116
|
|
|
* @var array |
117
|
|
|
*/ |
118
|
|
|
protected $choices = array(); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Assign this field to a section. |
122
|
|
|
* Fields not assigned to a section will not be displayed in the customizer. |
123
|
|
|
* |
124
|
|
|
* @access protected |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
protected $section = ''; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* The default value for this field. |
131
|
|
|
* |
132
|
|
|
* @access protected |
133
|
|
|
* @var string|array|bool |
134
|
|
|
*/ |
135
|
|
|
protected $default = ''; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Priority determines the position of a control inside a section. |
139
|
|
|
* Lower priority numbers move the control to the top. |
140
|
|
|
* |
141
|
|
|
* @access protected |
142
|
|
|
* @var int |
143
|
|
|
*/ |
144
|
|
|
protected $priority = 10; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Unique ID for this field. |
148
|
|
|
* This is auto-calculated from the $settings argument. |
149
|
|
|
* |
150
|
|
|
* @access protected |
151
|
|
|
* @var string |
152
|
|
|
*/ |
153
|
|
|
protected $id = ''; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Use if you want to automatically generate CSS from this field's value. |
157
|
|
|
* |
158
|
|
|
* @see https://kirki.org/docs/arguments/output |
159
|
|
|
* @access protected |
160
|
|
|
* @var array |
161
|
|
|
*/ |
162
|
|
|
protected $output = array(); |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Use to automatically generate postMessage scripts. |
166
|
|
|
* Not necessary to use if you use 'transport' => 'auto' |
167
|
|
|
* and have already set an array for the 'output' argument. |
168
|
|
|
* |
169
|
|
|
* @see https://kirki.org/docs/arguments/js_vars |
170
|
|
|
* @access protected |
171
|
|
|
* @var array |
172
|
|
|
*/ |
173
|
|
|
protected $js_vars = array(); |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* If you want to use a CSS compiler, then use this to set the variable names. |
177
|
|
|
* |
178
|
|
|
* @see https://kirki.org/docs/arguments/variables |
179
|
|
|
* @access protected |
180
|
|
|
* @var array |
181
|
|
|
*/ |
182
|
|
|
protected $variables = array(); |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Text that will be used in a tooltip to provide extra info for this field. |
186
|
|
|
* |
187
|
|
|
* @access protected |
188
|
|
|
* @var string |
189
|
|
|
*/ |
190
|
|
|
protected $tooltip = ''; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* A custom callback to determine if the field should be visible or not. |
194
|
|
|
* |
195
|
|
|
* @access protected |
196
|
|
|
* @var string|array |
197
|
|
|
*/ |
198
|
|
|
protected $active_callback = '__return_true'; |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* A custom sanitize callback that will be used to properly save the values. |
202
|
|
|
* |
203
|
|
|
* @access protected |
204
|
|
|
* @var string|array |
205
|
|
|
*/ |
206
|
|
|
protected $sanitize_callback = ''; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Use 'refresh', 'postMessage' or 'auto'. |
210
|
|
|
* 'auto' will automatically geberate any 'js_vars' from the 'output' argument. |
211
|
|
|
* |
212
|
|
|
* @access protected |
213
|
|
|
* @var string |
214
|
|
|
*/ |
215
|
|
|
protected $transport = 'refresh'; |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Define dependencies to show/hide this field based on the values of other fields. |
219
|
|
|
* |
220
|
|
|
* @access protected |
221
|
|
|
* @var array |
222
|
|
|
*/ |
223
|
|
|
protected $required = array(); |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Partial Refreshes array. |
227
|
|
|
* |
228
|
|
|
* @access protected |
229
|
|
|
* @var array |
230
|
|
|
*/ |
231
|
|
|
protected $partial_refresh = array(); |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* The class constructor. |
235
|
|
|
* Parses and sanitizes all field arguments. |
236
|
|
|
* Then it adds the field to Kirki::$fields. |
237
|
|
|
* |
238
|
|
|
* @access public |
239
|
|
|
* @param string $config_id The ID of the config we want to use. |
240
|
|
|
* Defaults to "global". |
241
|
|
|
* Configs are handled by the Kirki_Config class. |
242
|
|
|
* @param array $args The arguments of the field. |
243
|
|
|
*/ |
244
|
|
|
public function __construct( $config_id = 'global', $args = array() ) { |
245
|
|
|
|
246
|
|
|
if ( isset( $args['setting'] ) && ! empty( $args['setting'] ) && ( ! isset( $args['settings'] ) || empty( $args['settings'] ) ) ) { |
247
|
|
|
/* translators: %s represents the field ID where the error occurs. */ |
248
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Typo found in field %s - setting instead of settings.', 'kirki' ), esc_attr( $args['settings'] ) ), '3.0.10' ); |
249
|
|
|
$args['settings'] = $args['setting']; |
250
|
|
|
unset( $args['setting'] ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// In case the user only provides 1 argument, |
254
|
|
|
// assume that the provided argument is $args and set $config_id = 'global'. |
255
|
|
|
if ( is_array( $config_id ) && empty( $args ) ) { |
|
|
|
|
256
|
|
|
/* translators: %1$s represents the field ID where the error occurs. %2$s is the URL in the documentation site. */ |
257
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Config not defined for field %1$s - See %2$s for details on how to properly add fields.', 'kirki' ), esc_attr( $args['settings'] ), 'https://aristath.github.io/kirki/docs/getting-started/fields.html' ), '3.0.10' ); |
258
|
|
|
$args = $config_id; |
259
|
|
|
$config_id = 'global'; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$args['kirki_config'] = $config_id; |
263
|
|
|
|
264
|
|
|
$this->kirki_config = trim( esc_attr( $config_id ) ); |
265
|
|
|
if ( '' === $config_id ) { |
266
|
|
|
/* translators: %1$s represents the field ID where the error occurs. %2$s is the URL in the documentation site. */ |
267
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Config not defined for field %1$s - See %2$s for details on how to properly add fields.', 'kirki' ), esc_attr( $args['settings'] ), 'https://aristath.github.io/kirki/docs/getting-started/fields.html' ), '3.0.10' ); |
268
|
|
|
$this->kirki_config = 'global'; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// Get defaults from the class. |
272
|
|
|
$defaults = get_class_vars( __CLASS__ ); |
273
|
|
|
|
274
|
|
|
// Get the config arguments, and merge them with the defaults. |
275
|
|
|
$config_defaults = ( isset( Kirki::$config['global'] ) ) ? Kirki::$config['global'] : array(); |
276
|
|
|
if ( 'global' !== $this->kirki_config && isset( Kirki::$config[ $this->kirki_config ] ) ) { |
277
|
|
|
$config_defaults = Kirki::$config[ $this->kirki_config ]; |
278
|
|
|
} |
279
|
|
|
$config_defaults = ( is_array( $config_defaults ) ) ? $config_defaults : array(); |
280
|
|
|
foreach ( $config_defaults as $key => $value ) { |
281
|
|
|
if ( isset( $defaults[ $key ] ) && ! empty( $value ) && $value !== $defaults[ $key ] ) { |
282
|
|
|
$defaults[ $key ] = $value; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// Merge our args with the defaults. |
287
|
|
|
$args = wp_parse_args( $args, $defaults ); |
288
|
|
|
|
289
|
|
|
// Set the class properties using the parsed args. |
290
|
|
|
foreach ( $args as $key => $value ) { |
291
|
|
|
$this->$key = $value; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$this->args = $args; |
295
|
|
|
|
296
|
|
|
$this->set_field(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Processes the field arguments |
301
|
|
|
* |
302
|
|
|
* @access protected |
303
|
|
|
*/ |
304
|
|
|
protected function set_field() { |
305
|
|
|
|
306
|
|
|
$properties = get_class_vars( __CLASS__ ); |
307
|
|
|
|
308
|
|
|
// Some things must run before the others. |
309
|
|
|
$this->set_option_name(); |
310
|
|
|
$this->set_option_type(); |
311
|
|
|
$this->set_settings(); |
312
|
|
|
|
313
|
|
|
// Sanitize the properties, skipping the ones that have already run above. |
314
|
|
|
foreach ( $properties as $property => $value ) { |
315
|
|
|
if ( in_array( $property, array( 'option_name', 'option_type', 'settings' ), true ) ) { |
316
|
|
|
continue; |
317
|
|
|
} |
318
|
|
|
if ( method_exists( $this, 'set_' . $property ) ) { |
319
|
|
|
$method_name = 'set_' . $property; |
320
|
|
|
$this->$method_name(); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
// Get all arguments with their values. |
325
|
|
|
$args = get_object_vars( $this ); |
326
|
|
|
foreach ( array_keys( $args ) as $key ) { |
327
|
|
|
$args[ $key ] = $this->$key; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// Add the field to the static $fields variable properly indexed. |
331
|
|
|
Kirki::$fields[ $this->settings ] = $args; |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Escape $kirki_config. |
337
|
|
|
* |
338
|
|
|
* @access protected |
339
|
|
|
*/ |
340
|
|
|
protected function set_kirki_config() { |
341
|
|
|
|
342
|
|
|
$this->kirki_config = esc_attr( $this->kirki_config ); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Escape $option_name. |
347
|
|
|
* |
348
|
|
|
* @access protected |
349
|
|
|
*/ |
350
|
|
|
protected function set_option_name() { |
351
|
|
|
|
352
|
|
|
$this->option_name = esc_attr( $this->option_name ); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Escape the $section. |
357
|
|
|
* |
358
|
|
|
* @access protected |
359
|
|
|
*/ |
360
|
|
|
protected function set_section() { |
361
|
|
|
|
362
|
|
|
$this->section = sanitize_key( $this->section ); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Escape the $section. |
367
|
|
|
* |
368
|
|
|
* @access protected |
369
|
|
|
*/ |
370
|
|
|
protected function set_input_attrs() { |
371
|
|
|
|
372
|
|
|
if ( ! is_array( $this->input_attrs ) ) { |
|
|
|
|
373
|
|
|
$this->input_attrs = array(); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Checks the capability chosen is valid. |
379
|
|
|
* If not, then falls back to 'edit_theme_options' |
380
|
|
|
* |
381
|
|
|
* @access protected |
382
|
|
|
*/ |
383
|
|
|
protected function set_capability() { |
384
|
|
|
|
385
|
|
|
// Early exit if we're using 'edit_theme_options'. |
386
|
|
|
if ( 'edit_theme_options' === $this->capability ) { |
387
|
|
|
return; |
388
|
|
|
} |
389
|
|
|
// Escape & trim the capability. |
390
|
|
|
$this->capability = trim( esc_attr( $this->capability ) ); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Make sure we're using the correct option_type |
395
|
|
|
* |
396
|
|
|
* @access protected |
397
|
|
|
*/ |
398
|
|
|
protected function set_option_type() { |
399
|
|
|
|
400
|
|
|
// Take care of common typos. |
401
|
|
|
if ( 'options' === $this->option_type ) { |
402
|
|
|
$this->option_type = 'option'; |
403
|
|
|
} |
404
|
|
|
// Take care of common typos. |
405
|
|
|
if ( 'theme_mods' === $this->option_type ) { |
406
|
|
|
/* translators: %1$s represents the field ID where the error occurs. */ |
407
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Typo found in field %s - "theme_mods" vs "theme_mod"', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
408
|
|
|
$this->option_type = 'theme_mod'; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Modifications for partial refreshes. |
414
|
|
|
* |
415
|
|
|
* @access protected |
416
|
|
|
*/ |
417
|
|
|
protected function set_partial_refresh() { |
418
|
|
|
|
419
|
|
|
if ( ! is_array( $this->partial_refresh ) ) { |
|
|
|
|
420
|
|
|
$this->partial_refresh = array(); |
421
|
|
|
} |
422
|
|
|
foreach ( $this->partial_refresh as $id => $args ) { |
423
|
|
|
if ( ! is_array( $args ) || ! isset( $args['selector'] ) || ! isset( $args['render_callback'] ) || ! is_callable( $args['render_callback'] ) ) { |
424
|
|
|
/* translators: %1$s represents the field ID where the error occurs. */ |
425
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"partial_refresh" invalid entry in field %s', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
426
|
|
|
unset( $this->partial_refresh[ $id ] ); |
427
|
|
|
continue; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
if ( ! empty( $this->partial_refresh ) ) { |
431
|
|
|
$this->transport = 'postMessage'; |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Sets the settings. |
437
|
|
|
* If we're using serialized options it makes sure that settings are properly formatted. |
438
|
|
|
* We'll also be escaping all setting names here for consistency. |
439
|
|
|
* |
440
|
|
|
* @access protected |
441
|
|
|
*/ |
442
|
|
|
protected function set_settings() { |
443
|
|
|
|
444
|
|
|
// If settings is not an array, temporarily convert it to an array. |
445
|
|
|
// This is just to allow us to process everything the same way and avoid code duplication. |
446
|
|
|
// if settings is not an array then it will not be set as an array in the end. |
447
|
|
|
if ( ! is_array( $this->settings ) ) { |
448
|
|
|
$this->settings = array( |
449
|
|
|
'kirki_placeholder_setting' => $this->settings, |
450
|
|
|
); |
451
|
|
|
} |
452
|
|
|
$settings = array(); |
453
|
|
|
foreach ( $this->settings as $setting_key => $setting_value ) { |
454
|
|
|
$settings[ $setting_key ] = $setting_value; |
455
|
|
|
// If we're using serialized options then we need to spice this up. |
456
|
|
|
if ( 'option' === $this->option_type && '' !== $this->option_name && ( false === strpos( $setting_key, '[' ) ) ) { |
457
|
|
|
$settings[ $setting_key ] = "{$this->option_name}[{$setting_value}]"; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
$this->settings = $settings; |
461
|
|
|
if ( isset( $this->settings['kirki_placeholder_setting'] ) ) { |
462
|
|
|
$this->settings = $this->settings['kirki_placeholder_setting']; |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Sets the active_callback |
468
|
|
|
* If we're using the $required argument, |
469
|
|
|
* Then this is where the switch is made to our evaluation method. |
470
|
|
|
* |
471
|
|
|
* @access protected |
472
|
|
|
*/ |
473
|
|
|
protected function set_active_callback() { |
474
|
|
|
|
475
|
|
|
if ( is_array( $this->active_callback ) && ! is_callable( $this->active_callback ) ) { |
476
|
|
|
if ( isset( $this->active_callback[0] ) ) { |
477
|
|
|
$this->required = $this->active_callback; |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
if ( ! empty( $this->required ) ) { |
482
|
|
|
$this->active_callback = '__return_true'; |
483
|
|
|
return; |
484
|
|
|
} |
485
|
|
|
// No need to proceed any further if we're using the default value. |
486
|
|
|
if ( '__return_true' === $this->active_callback ) { |
487
|
|
|
return; |
488
|
|
|
} |
489
|
|
|
// Make sure the function is callable, otherwise fallback to __return_true. |
490
|
|
|
if ( ! is_callable( $this->active_callback ) ) { |
491
|
|
|
$this->active_callback = '__return_true'; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Sets the control type. |
497
|
|
|
* |
498
|
|
|
* @access protected |
499
|
|
|
*/ |
500
|
|
|
protected function set_type() { |
501
|
|
|
|
502
|
|
|
// Escape the control type (it doesn't hurt to be sure). |
503
|
|
|
$this->type = esc_attr( $this->type ); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Sets the $id. |
508
|
|
|
* Setting the ID should happen after the 'settings' sanitization. |
509
|
|
|
* This way we can also properly handle cases where the option_type is set to 'option' |
510
|
|
|
* and we're using an array instead of individual options. |
511
|
|
|
* |
512
|
|
|
* @access protected |
513
|
|
|
*/ |
514
|
|
|
protected function set_id() { |
515
|
|
|
|
516
|
|
|
$this->id = sanitize_key( str_replace( '[', '-', str_replace( ']', '', $this->settings ) ) ); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Sets the $choices. |
521
|
|
|
* |
522
|
|
|
* @access protected |
523
|
|
|
*/ |
524
|
|
|
protected function set_choices() { |
525
|
|
|
|
526
|
|
|
if ( ! is_array( $this->choices ) ) { |
|
|
|
|
527
|
|
|
$this->choices = array(); |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Escapes the $disable_output. |
533
|
|
|
* |
534
|
|
|
* @access protected |
535
|
|
|
*/ |
536
|
|
|
protected function set_disable_output() { |
537
|
|
|
|
538
|
|
|
$this->disable_output = (bool) $this->disable_output; |
539
|
|
|
|
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Sets the $sanitize_callback |
544
|
|
|
* |
545
|
|
|
* @access protected |
546
|
|
|
*/ |
547
|
|
|
protected function set_output() { |
548
|
|
|
|
549
|
|
|
if ( empty( $this->output ) ) { |
550
|
|
|
return; |
551
|
|
|
} |
552
|
|
|
if ( ! is_array( $this->output ) ) { |
|
|
|
|
553
|
|
|
/* translators: The field ID where the error occurs. */ |
554
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
555
|
|
|
$this->output = array( |
556
|
|
|
array( |
557
|
|
|
'element' => $this->output, |
558
|
|
|
), |
559
|
|
|
); |
560
|
|
|
} |
561
|
|
|
// Convert to array of arrays if needed. |
562
|
|
|
if ( isset( $this->output['element'] ) ) { |
563
|
|
|
/* translators: The field ID where the error occurs. */ |
564
|
|
|
_doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
565
|
|
|
$this->output = array( $this->output ); |
566
|
|
|
} |
567
|
|
|
foreach ( $this->output as $key => $output ) { |
568
|
|
|
if ( empty( $output ) || ! isset( $output['element'] ) ) { |
569
|
|
|
unset( $this->output[ $key ] ); |
570
|
|
|
continue; |
571
|
|
|
} |
572
|
|
|
if ( ! isset( $output['sanitize_callback'] ) && isset( $output['callback'] ) ) { |
573
|
|
|
$this->output[ $key ]['sanitize_callback'] = $output['callback']; |
574
|
|
|
} |
575
|
|
|
// Convert element arrays to strings. |
576
|
|
|
if ( isset( $output['element'] ) && is_array( $output['element'] ) ) { |
577
|
|
|
$this->output[ $key ]['element'] = array_unique( $this->output[ $key ]['element'] ); |
578
|
|
|
sort( $this->output[ $key ]['element'] ); |
579
|
|
|
|
580
|
|
|
// Trim each element in the array. |
581
|
|
|
foreach ( $this->output[ $key ]['element'] as $index => $element ) { |
582
|
|
|
$this->output[ $key ]['element'][ $index ] = trim( $element ); |
583
|
|
|
} |
584
|
|
|
$this->output[ $key ]['element'] = implode( ',', $this->output[ $key ]['element'] ); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
// Fix for https://github.com/aristath/kirki/issues/1659#issuecomment-346229751. |
588
|
|
|
$this->output[ $key ]['element'] = str_replace( array( "\t", "\n", "\r", "\0", "\x0B" ), ' ', $this->output[ $key ]['element'] ); |
589
|
|
|
$this->output[ $key ]['element'] = trim( preg_replace( '/\s+/', ' ', $this->output[ $key ]['element'] ) ); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Sets the $js_vars |
595
|
|
|
* |
596
|
|
|
* @access protected |
597
|
|
|
*/ |
598
|
|
|
protected function set_js_vars() { |
599
|
|
|
|
600
|
|
|
if ( ! is_array( $this->js_vars ) ) { |
|
|
|
|
601
|
|
|
$this->js_vars = array(); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
// Check if transport is set to auto. |
605
|
|
|
// If not, then skip the auto-calculations and exit early. |
606
|
|
|
if ( 'auto' !== $this->transport ) { |
607
|
|
|
return; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
// Set transport to refresh initially. |
611
|
|
|
// Serves as a fallback in case we failt to auto-calculate js_vars. |
612
|
|
|
$this->transport = 'refresh'; |
613
|
|
|
|
614
|
|
|
$js_vars = array(); |
615
|
|
|
|
616
|
|
|
// Try to auto-generate js_vars. |
617
|
|
|
// First we need to check if js_vars are empty, and that output is not empty. |
618
|
|
|
if ( empty( $this->js_vars ) && ! empty( $this->output ) ) { |
619
|
|
|
|
620
|
|
|
// Start going through each item in the $output array. |
621
|
|
|
foreach ( $this->output as $output ) { |
622
|
|
|
$output['function'] = ( isset( $output['function'] ) ) ? $output['function'] : 'style'; |
623
|
|
|
|
624
|
|
|
// If 'element' or 'property' are not defined, skip this. |
625
|
|
|
if ( ! isset( $output['element'] ) || ! isset( $output['property'] ) ) { |
626
|
|
|
continue; |
627
|
|
|
} |
628
|
|
|
if ( is_array( $output['element'] ) ) { |
629
|
|
|
$output['element'] = implode( ',', $output['element'] ); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
// If there's a sanitize_callback defined skip this, unless we also have a js_callback defined. |
633
|
|
|
if ( isset( $output['sanitize_callback'] ) && ! empty( $output['sanitize_callback'] ) && ! isset( $output['js_callback'] ) ) { |
634
|
|
|
continue; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
// If we got this far, it's safe to add this. |
638
|
|
|
$js_vars[] = $output; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
// Did we manage to get all the items from 'output'? |
642
|
|
|
// If not, then we're missing something so don't add this. |
643
|
|
|
if ( count( $js_vars ) !== count( $this->output ) ) { |
644
|
|
|
return; |
645
|
|
|
} |
646
|
|
|
$this->js_vars = $js_vars; |
647
|
|
|
$this->transport = 'postMessage'; |
648
|
|
|
|
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Sets the $variables |
654
|
|
|
* |
655
|
|
|
* @access protected |
656
|
|
|
*/ |
657
|
|
|
protected function set_variables() { |
658
|
|
|
|
659
|
|
|
if ( ! is_array( $this->variables ) ) { |
|
|
|
|
660
|
|
|
$variable = ( is_string( $this->variables ) && ! empty( $this->variables ) ) ? $this->variables : false; |
661
|
|
|
$this->variables = array(); |
662
|
|
|
if ( $variable && empty( $this->variables ) ) { |
663
|
|
|
$this->variables[0]['name'] = $variable; |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Sets the $transport |
670
|
|
|
* |
671
|
|
|
* @access protected |
672
|
|
|
*/ |
673
|
|
|
protected function set_transport() { |
674
|
|
|
|
675
|
|
|
if ( 'postmessage' === trim( strtolower( $this->transport ) ) ) { |
676
|
|
|
$this->transport = 'postMessage'; |
677
|
|
|
} |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* Sets the $required |
682
|
|
|
* |
683
|
|
|
* @access protected |
684
|
|
|
*/ |
685
|
|
|
protected function set_required() { |
686
|
|
|
|
687
|
|
|
if ( ! is_array( $this->required ) ) { |
|
|
|
|
688
|
|
|
$this->required = array(); |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Sets the $priority |
694
|
|
|
* |
695
|
|
|
* @access protected |
696
|
|
|
*/ |
697
|
|
|
protected function set_priority() { |
698
|
|
|
|
699
|
|
|
$this->priority = absint( $this->priority ); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* Sets the $css_vars |
704
|
|
|
* |
705
|
|
|
* @access protected |
706
|
|
|
*/ |
707
|
|
|
protected function set_css_vars() { |
708
|
|
|
if ( is_string( $this->css_vars ) ) { |
|
|
|
|
709
|
|
|
$this->css_vars = array( $this->css_vars ); |
710
|
|
|
} |
711
|
|
|
if ( isset( $this->css_vars[0] ) && is_string( $this->css_vars[0] ) ) { |
712
|
|
|
$this->css_vars = array( $this->css_vars ); |
713
|
|
|
} |
714
|
|
|
foreach ( $this->css_vars as $key => $val ) { |
715
|
|
|
if ( ! isset( $val[1] ) ) { |
716
|
|
|
$this->css_vars[ $key ][1] = '$'; |
717
|
|
|
} |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
} |
721
|
|
|
|