1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fields API Field Class |
4
|
|
|
* |
5
|
|
|
* Handles saving and sanitizing of fields. |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Fields_API |
9
|
|
|
*/ |
10
|
|
|
class WP_Fields_API_Field { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @access public |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
public $id = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Object type. |
20
|
|
|
* |
21
|
|
|
* @access public |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $object_type = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Object name (for post types and taxonomies). |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $object_name = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Capability required to edit this field. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $capability = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Theme feature support for the field. |
43
|
|
|
* |
44
|
|
|
* @access public |
45
|
|
|
* @var string|array |
46
|
|
|
*/ |
47
|
|
|
public $theme_supports = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Default value for field |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $default = ''; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Server-side sanitization callback for the field's value. |
58
|
|
|
* |
59
|
|
|
* @var callback |
60
|
|
|
*/ |
61
|
|
|
public $sanitize_callback = ''; |
62
|
|
|
public $sanitize_js_callback = ''; |
63
|
|
|
|
64
|
|
|
protected $id_data = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Capabilities Callback. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* |
71
|
|
|
* @see WP_Fields_API_Field::check_capabilities() |
72
|
|
|
* |
73
|
|
|
* @var callable Callback is called with one argument, the instance of |
74
|
|
|
* WP_Fields_API_Field, and returns bool to indicate whether |
75
|
|
|
* the field has capabilities to be used. |
76
|
|
|
*/ |
77
|
|
|
public $capabilities_callback = ''; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Value Callback. |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* |
84
|
|
|
* @see WP_Fields_API_Field::value() |
85
|
|
|
* |
86
|
|
|
* @var callable Callback is called with two arguments, the item ID and the instance of |
87
|
|
|
* WP_Fields_API_Field. It returns a string for the value to use. |
88
|
|
|
*/ |
89
|
|
|
public $value_callback = ''; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Pre-Update Value Callback. |
93
|
|
|
* |
94
|
|
|
* @access public |
95
|
|
|
* |
96
|
|
|
* @see WP_Fields_API_Field::save() |
97
|
|
|
* |
98
|
|
|
* @var callable Callback is called with three arguments, the value being saved, the item ID, and the instance of |
99
|
|
|
* WP_Fields_API_Field. It returns a string of the value to save. |
100
|
|
|
*/ |
101
|
|
|
public $pre_update_value_callback = ''; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Update Value Callback. |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
* |
108
|
|
|
* @see WP_Fields_API_Field::update() |
109
|
|
|
* |
110
|
|
|
* @var callable Callback is called with three arguments, the value being saved, the item ID, and the instance of |
111
|
|
|
* WP_Fields_API_Field. |
112
|
|
|
*/ |
113
|
|
|
public $update_value_callback = ''; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Constructor. |
117
|
|
|
* |
118
|
|
|
* Parameters are not set to maintain PHP overloading compatibility (strict standards) |
119
|
|
|
* |
120
|
|
|
* @return WP_Fields_API_Field $field |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
public function __construct() { |
123
|
|
|
|
124
|
|
|
$args = func_get_args(); |
125
|
|
|
|
126
|
|
|
call_user_func_array( array( $this, 'init' ), $args ); |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Secondary constructor; Any supplied $args override class property defaults. |
132
|
|
|
* |
133
|
|
|
* @param string $object_type Object type. |
134
|
|
|
* @param string $id A specific ID of the field. Can be a |
135
|
|
|
* theme mod or option name. |
136
|
|
|
* @param array $args Field arguments. |
137
|
|
|
* |
138
|
|
|
* @return WP_Fields_API_Field $field |
139
|
|
|
*/ |
140
|
|
|
public function init( $object_type, $id, $args = array() ) { |
141
|
|
|
|
142
|
|
|
$this->object_type = $object_type; |
143
|
|
|
|
144
|
|
|
if ( is_array( $id ) ) { |
145
|
|
|
$args = $id; |
146
|
|
|
|
147
|
|
|
$id = ''; |
|
|
|
|
148
|
|
|
} else { |
149
|
|
|
$this->id = $id; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$keys = array_keys( get_object_vars( $this ) ); |
153
|
|
|
|
154
|
|
|
foreach ( $keys as $key ) { |
155
|
|
|
if ( isset( $args[ $key ] ) ) { |
156
|
|
|
$this->$key = $args[ $key ]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Parse the ID for array keys. |
161
|
|
|
$this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) ); |
162
|
|
|
$this->id_data['base'] = array_shift( $this->id_data['keys'] ); |
163
|
|
|
|
164
|
|
|
// Rebuild the ID. |
165
|
|
|
$this->id = $this->id_data['base']; |
166
|
|
|
|
167
|
|
|
if ( ! empty( $this->id_data['keys'] ) ) { |
168
|
|
|
$this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ( $this->sanitize_callback ) { |
172
|
|
|
add_filter( "fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_callback, 10, 2 ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ( $this->sanitize_js_callback ) { |
176
|
|
|
add_filter( "fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_js_callback, 10, 2 ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Check user capabilities and theme supports, and then save |
183
|
|
|
* the value of the field. |
184
|
|
|
* |
185
|
|
|
* @param mixed $value The value to save. |
|
|
|
|
186
|
|
|
* @param int $item_id The Item ID. |
|
|
|
|
187
|
|
|
* |
188
|
|
|
* @return false|mixed False if cap check fails or value isn't set. |
189
|
|
|
*/ |
190
|
|
|
public function save() { |
191
|
|
|
|
192
|
|
|
$value = func_get_arg(0); |
193
|
|
|
$item_id = func_get_arg(1); |
194
|
|
|
|
195
|
|
|
if ( ! $this->check_capabilities() || false === $value ) { |
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ( is_callable( $this->pre_update_value_callback ) ) { |
200
|
|
|
$value = call_user_func( $this->pre_update_value_callback, $value, $item_id, $this ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Fires when the WP_Fields_API_Field::save() method is called. |
205
|
|
|
* |
206
|
|
|
* The dynamic portion of the hook name, `$this->id_data['base']` refers to |
207
|
|
|
* the base slug of the field name. |
208
|
|
|
* |
209
|
|
|
* @param mixed $value The value being saved. |
210
|
|
|
* @param int $item_id The item ID. |
211
|
|
|
* @param WP_Fields_API_Field $this {@see WP_Fields_API_Field} instance. |
212
|
|
|
* |
213
|
|
|
* @return string The value to save |
214
|
|
|
*/ |
215
|
|
|
$value = apply_filters( 'field_save_' . $this->object_type . '_' . $this->id_data[ 'base' ], $value, $item_id, $this ); |
216
|
|
|
|
217
|
|
|
return $this->update( $value, $item_id ); |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Sanitize an input. |
223
|
|
|
* |
224
|
|
|
* @param mixed $value The value to sanitize. |
225
|
|
|
* |
226
|
|
|
* @return mixed Null if an input isn't valid, otherwise the sanitized value. |
227
|
|
|
*/ |
228
|
|
|
public function sanitize( $value ) { |
229
|
|
|
|
230
|
|
|
$value = wp_unslash( $value ); |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Filter a Customize field value in un-slashed form. |
234
|
|
|
* |
235
|
|
|
* @param mixed $value Value of the field. |
236
|
|
|
* @param WP_Fields_API_Field $this WP_Fields_API_Field instance. |
237
|
|
|
*/ |
238
|
|
|
return apply_filters( "fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this ); |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Save the value of the field, using the related API. |
244
|
|
|
* |
245
|
|
|
* @param mixed $value The value to update. |
246
|
|
|
* @param int $item_id Item ID. |
|
|
|
|
247
|
|
|
* |
248
|
|
|
* @return mixed The result of saving the value. |
249
|
|
|
*/ |
250
|
|
|
protected function update( $value ) { |
251
|
|
|
|
252
|
|
|
// @todo Support post / term / user / comment object field updates |
253
|
|
|
|
254
|
|
|
$item_id = func_get_arg(1); |
255
|
|
|
|
256
|
|
|
switch ( $this->object_type ) { |
257
|
|
|
case is_callable( $this->update_value_callback ) : |
258
|
|
|
return call_user_func( $this->update_value_callback, $value, $item_id, $this ); |
259
|
|
|
|
260
|
|
|
case 'customizer' : |
261
|
|
|
return $this->_update_theme_mod( $value ); |
262
|
|
|
|
263
|
|
|
case 'settings' : |
264
|
|
|
return $this->_update_option( $value ); |
265
|
|
|
|
266
|
|
|
case 'post' : |
267
|
|
|
case 'term' : |
268
|
|
|
case 'user' : |
269
|
|
|
case 'comment' : |
270
|
|
|
return $this->_update_meta( $this->object_type, $value, $item_id ); |
271
|
|
|
|
272
|
|
|
default : |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Fires when the {@see WP_Fields_API_Field::update()} method is called for fields |
276
|
|
|
* not handled as theme_mods or options. |
277
|
|
|
* |
278
|
|
|
* The dynamic portion of the hook name, `$this->object_type`, refers to the type of field. |
279
|
|
|
* |
280
|
|
|
* @param mixed $value Value of the field. |
281
|
|
|
* @param int $item_id Item ID. |
282
|
|
|
* @param WP_Fields_API_Field $this WP_Fields_API_Field instance. |
283
|
|
|
*/ |
284
|
|
|
do_action( "fields_update_{$this->object_type}", $value, $item_id, $this ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return null; |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Update the theme mod from the value of the parameter. |
293
|
|
|
* |
294
|
|
|
* @param mixed $value The value to update. |
295
|
|
|
* |
296
|
|
|
* @return null |
297
|
|
|
*/ |
298
|
|
View Code Duplication |
protected function _update_theme_mod( $value ) { |
|
|
|
|
299
|
|
|
|
300
|
|
|
if ( is_null( $value ) ) { |
301
|
|
|
remove_theme_mod( $this->id_data['base'] ); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
// Handle non-array theme mod. |
305
|
|
|
if ( empty( $this->id_data['keys'] ) ) { |
306
|
|
|
set_theme_mod( $this->id_data['base'], $value ); |
307
|
|
|
} else { |
308
|
|
|
// Handle array-based theme mod. |
309
|
|
|
$mods = get_theme_mod( $this->id_data['base'] ); |
310
|
|
|
$mods = $this->multidimensional_replace( $mods, $this->id_data['keys'], $value ); |
311
|
|
|
|
312
|
|
|
if ( isset( $mods ) ) { |
313
|
|
|
set_theme_mod( $this->id_data['base'], $mods ); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return null; |
318
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Update the option from the value of the field. |
323
|
|
|
* |
324
|
|
|
* @param mixed $value The value to update. |
325
|
|
|
* |
326
|
|
|
* @return bool|null The result of saving the value. |
327
|
|
|
*/ |
328
|
|
View Code Duplication |
protected function _update_option( $value ) { |
|
|
|
|
329
|
|
|
|
330
|
|
|
if ( is_null( $value ) ) { |
331
|
|
|
delete_option( $this->id_data['base'] ); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
// Handle non-array option. |
335
|
|
|
if ( empty( $this->id_data['keys'] ) ) { |
336
|
|
|
return update_option( $this->id_data['base'], $value ); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// Handle array-based options. |
340
|
|
|
$options = get_option( $this->id_data['base'] ); |
341
|
|
|
$options = $this->multidimensional_replace( $options, $this->id_data['keys'], $value ); |
342
|
|
|
|
343
|
|
|
if ( isset( $options ) ) { |
344
|
|
|
return update_option( $this->id_data['base'], $options ); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return null; |
348
|
|
|
|
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Update the meta from the value of the field. |
353
|
|
|
* |
354
|
|
|
* @param string $meta_type The meta type. |
355
|
|
|
* @param mixed $value The value to update. |
356
|
|
|
* @param int $item_id Item ID. |
357
|
|
|
* |
358
|
|
|
* @return bool|null The result of saving the value. |
359
|
|
|
*/ |
360
|
|
|
protected function _update_meta( $meta_type, $value, $item_id = 0 ) { |
361
|
|
|
|
362
|
|
|
if ( is_null( $value ) ) { |
363
|
|
|
delete_metadata( $meta_type, $item_id, $this->id_data['base'] ); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
// Handle non-array option. |
367
|
|
|
if ( empty( $this->id_data['keys'] ) ) { |
368
|
|
|
return update_metadata( $meta_type, $item_id, $this->id_data['base'], $value ); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
// Handle array-based keys. |
372
|
|
|
$keys = get_metadata( $meta_type, 0, $this->id_data['base'] ); |
373
|
|
|
$keys = $this->multidimensional_replace( $keys, $this->id_data['keys'], $value ); |
374
|
|
|
|
375
|
|
|
if ( isset( $keys ) ) { |
376
|
|
|
return update_metadata( $meta_type, $item_id, $this->id_data['base'], $keys ); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
return null; |
380
|
|
|
|
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Fetch the value of the field. |
385
|
|
|
* |
386
|
|
|
* @param int $item_id (optional) The Item ID. |
|
|
|
|
387
|
|
|
* |
388
|
|
|
* @return mixed The value. |
389
|
|
|
*/ |
390
|
|
|
public function value() { |
391
|
|
|
|
392
|
|
|
$item_id = func_get_arg(0); |
393
|
|
|
|
394
|
|
|
switch ( $this->object_type ) { |
395
|
|
|
case is_callable( $this->value_callback ) : |
396
|
|
|
$value = call_user_func( $this->value_callback, $item_id, $this ); |
397
|
|
|
$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default ); |
398
|
|
|
|
399
|
|
|
break; |
400
|
|
|
case 'post' : |
401
|
|
|
case 'term' : |
402
|
|
|
case 'user' : |
403
|
|
|
case 'comment' : |
404
|
|
|
$value = $this->get_object_value( $item_id ); |
405
|
|
|
$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default ); |
406
|
|
|
break; |
407
|
|
|
|
408
|
|
|
case 'customizer' : |
409
|
|
|
case 'settings' : |
410
|
|
|
$value = $this->get_option_value(); |
411
|
|
|
$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default ); |
412
|
|
|
break; |
413
|
|
|
|
414
|
|
|
default : |
415
|
|
|
/** |
416
|
|
|
* Filter a field value for a custom object type. |
417
|
|
|
* |
418
|
|
|
* The dynamic portion of the hook name, `$this->id_date['base']`, refers to |
419
|
|
|
* the base slug of the field name. |
420
|
|
|
* |
421
|
|
|
* For fields handled as theme_mods, options, or object fields, see those corresponding |
422
|
|
|
* functions for available hooks. |
423
|
|
|
* |
424
|
|
|
* @param mixed $default The field default value. Default empty. |
425
|
|
|
* @param int $item_id (optional) The Item ID. |
426
|
|
|
*/ |
427
|
|
|
$value = apply_filters( 'fields_value_' . $this->object_type . '_' . $this->object_name . '_' . $this->id_data['base'], $this->default, $item_id ); |
428
|
|
|
break; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return $value; |
432
|
|
|
|
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Get value from meta / object |
437
|
|
|
* |
438
|
|
|
* @param int $item_id |
439
|
|
|
* |
440
|
|
|
* @return mixed|null |
441
|
|
|
*/ |
442
|
|
|
public function get_object_value( $item_id ) { |
443
|
|
|
|
444
|
|
|
$value = null; |
|
|
|
|
445
|
|
|
$object = null; |
446
|
|
|
|
447
|
|
|
$field_key = $this->id_data['base']; |
448
|
|
|
|
449
|
|
|
switch ( $this->object_type ) { |
450
|
|
|
case 'post' : |
451
|
|
|
$object = get_post( $item_id ); |
452
|
|
|
break; |
453
|
|
|
|
454
|
|
|
case 'term' : |
455
|
|
|
$object = get_term( $item_id ); |
456
|
|
|
break; |
457
|
|
|
|
458
|
|
|
case 'user' : |
459
|
|
|
$object = get_userdata( $item_id ); |
460
|
|
|
break; |
461
|
|
|
|
462
|
|
|
case 'comment' : |
463
|
|
|
$object = get_comment( $item_id ); |
464
|
|
|
break; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
if ( $object && ! is_wp_error( $object ) && isset( $object->{$field_key} ) ) { |
468
|
|
|
// Get value from object |
469
|
|
|
$value = $object->{$field_key}; |
470
|
|
|
} else { |
471
|
|
|
// Get value from meta |
472
|
|
|
$value = get_metadata( $this->object_type, $item_id, $field_key ); |
473
|
|
|
|
474
|
|
|
if ( array() === $value ) { |
475
|
|
|
$value = $this->default; |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
return $value; |
480
|
|
|
|
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Get value from option / theme_mod |
485
|
|
|
* |
486
|
|
|
* @return mixed|void |
487
|
|
|
*/ |
488
|
|
|
public function get_option_value() { |
489
|
|
|
|
490
|
|
|
$function = ''; |
491
|
|
|
$value = null; |
492
|
|
|
|
493
|
|
|
switch ( $this->object_type ) { |
494
|
|
|
case 'customizer' : |
495
|
|
|
$function = 'get_theme_mod'; |
496
|
|
|
break; |
497
|
|
|
|
498
|
|
|
case 'settings' : |
499
|
|
|
$function = 'get_option'; |
500
|
|
|
break; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
if ( is_callable( $function ) ) { |
504
|
|
|
// Handle non-array value |
505
|
|
|
if ( empty( $this->id_data['keys'] ) ) { |
506
|
|
|
return $function( $this->id_data['base'], $this->default ); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
// Handle array-based value |
510
|
|
|
$value = $function( $this->id_data['base'] ); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
return $value; |
514
|
|
|
|
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Sanitize the field's value for use in JavaScript. |
519
|
|
|
* |
520
|
|
|
* @return mixed The requested escaped value. |
521
|
|
|
*/ |
522
|
|
|
public function js_value() { |
523
|
|
|
|
524
|
|
|
$value = $this->value(); |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Filter a Customize field value for use in JavaScript. |
528
|
|
|
* |
529
|
|
|
* The dynamic portion of the hook name, `$this->id`, refers to the field ID. |
530
|
|
|
* |
531
|
|
|
* @param mixed $value The field value. |
532
|
|
|
* @param WP_Fields_API_Field $this {@see WP_Fields_API_Field} instance. |
533
|
|
|
*/ |
534
|
|
|
$value = apply_filters( "fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this ); |
535
|
|
|
|
536
|
|
|
if ( is_string( $value ) ) { |
537
|
|
|
return html_entity_decode( $value, ENT_QUOTES, 'UTF-8' ); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
return $value; |
541
|
|
|
|
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Validate user capabilities whether the theme supports the field. |
546
|
|
|
* |
547
|
|
|
* @return bool False if theme doesn't support the section or user can't change section, otherwise true. |
548
|
|
|
*/ |
549
|
|
View Code Duplication |
public function check_capabilities() { |
|
|
|
|
550
|
|
|
|
551
|
|
|
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) { |
552
|
|
|
return false; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) { |
556
|
|
|
return false; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
$access = true; |
560
|
|
|
|
561
|
|
|
if ( is_callable( $this->capabilities_callback ) ) { |
562
|
|
|
$access = call_user_func( $this->capabilities_callback, $this ); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return $access; |
566
|
|
|
|
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Multidimensional helper function. |
571
|
|
|
* |
572
|
|
|
* @param $root |
573
|
|
|
* @param $keys |
574
|
|
|
* @param bool $create Default is false. |
575
|
|
|
* |
576
|
|
|
* @return null|array Keys are 'root', 'node', and 'key'. |
577
|
|
|
*/ |
578
|
|
|
final protected function multidimensional( &$root, $keys, $create = false ) { |
579
|
|
|
|
580
|
|
|
if ( $create && empty( $root ) ) { |
581
|
|
|
$root = array(); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
if ( ! isset( $root ) || empty( $keys ) ) { |
585
|
|
|
return null; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
$last = array_pop( $keys ); |
589
|
|
|
$node = &$root; |
590
|
|
|
|
591
|
|
|
foreach ( $keys as $key ) { |
592
|
|
|
if ( $create && ! isset( $node[ $key ] ) ) { |
593
|
|
|
$node[ $key ] = array(); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
if ( ! is_array( $node ) || ! isset( $node[ $key ] ) ) { |
597
|
|
|
return null; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$node = &$node[ $key ]; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
if ( $create ) { |
604
|
|
|
if ( ! is_array( $node ) ) { |
605
|
|
|
// account for an array overriding a string or object value |
606
|
|
|
$node = array(); |
607
|
|
|
} |
608
|
|
|
if ( ! isset( $node[ $last ] ) ) { |
609
|
|
|
$node[ $last ] = array(); |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
if ( ! isset( $node[ $last ] ) ) { |
614
|
|
|
return null; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
return array( |
618
|
|
|
'root' => &$root, |
619
|
|
|
'node' => &$node, |
620
|
|
|
'key' => $last, |
621
|
|
|
); |
622
|
|
|
|
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Will attempt to replace a specific value in a multidimensional array. |
627
|
|
|
* |
628
|
|
|
* @param $root |
629
|
|
|
* @param $keys |
630
|
|
|
* @param mixed $value The value to update. |
631
|
|
|
* |
632
|
|
|
* @return |
633
|
|
|
*/ |
634
|
|
View Code Duplication |
final protected function multidimensional_replace( $root, $keys, $value ) { |
|
|
|
|
635
|
|
|
|
636
|
|
|
if ( ! isset( $value ) ) { |
637
|
|
|
return $root; |
638
|
|
|
} elseif ( empty( $keys ) ) { |
639
|
|
|
// If there are no keys, we're replacing the root. |
640
|
|
|
return $value; |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
$result = $this->multidimensional( $root, $keys, true ); |
644
|
|
|
|
645
|
|
|
if ( isset( $result ) ) { |
646
|
|
|
$result['node'][ $result['key'] ] = $value; |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
return $root; |
650
|
|
|
|
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Will attempt to fetch a specific value from a multidimensional array. |
655
|
|
|
* |
656
|
|
|
* @param $root |
657
|
|
|
* @param $keys |
658
|
|
|
* @param mixed $default A default value which is used as a fallback. Default is null. |
659
|
|
|
* |
660
|
|
|
* @return mixed The requested value or the default value. |
661
|
|
|
*/ |
662
|
|
View Code Duplication |
final protected function multidimensional_get( $root, $keys, $default = null ) { |
|
|
|
|
663
|
|
|
|
664
|
|
|
// If there are no keys, test the root. |
665
|
|
|
if ( empty( $keys ) ) { |
666
|
|
|
if ( isset( $root ) ) { |
667
|
|
|
return $root; |
668
|
|
|
} |
669
|
|
|
} else { |
670
|
|
|
$result = $this->multidimensional( $root, $keys ); |
671
|
|
|
|
672
|
|
|
if ( isset( $result ) ) { |
673
|
|
|
return $result['node'][ $result['key'] ]; |
674
|
|
|
} |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
return $default; |
678
|
|
|
|
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Will attempt to check if a specific value in a multidimensional array is set. |
683
|
|
|
* |
684
|
|
|
* @param $root |
685
|
|
|
* @param $keys |
686
|
|
|
* |
687
|
|
|
* @return bool True if value is set, false if not. |
688
|
|
|
*/ |
689
|
|
|
final protected function multidimensional_isset( $root, $keys ) { |
690
|
|
|
|
691
|
|
|
$result = $this->multidimensional_get( $root, $keys ); |
692
|
|
|
|
693
|
|
|
return isset( $result ); |
694
|
|
|
|
695
|
|
|
} |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* A field that is used to filter a value, but will not save the results. |
700
|
|
|
* |
701
|
|
|
* Results should be properly handled using another field or callback. |
702
|
|
|
* |
703
|
|
|
* @package WordPress |
704
|
|
|
* @subpackage Fields_API |
705
|
|
|
*/ |
706
|
|
|
class WP_Fields_API_Filter_Field extends WP_Fields_API_Field { |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Save the value of the field, using the related API. |
710
|
|
|
* |
711
|
|
|
* @param mixed $value The value to update. |
712
|
|
|
* |
713
|
|
|
* @return mixed The result of saving the value. |
714
|
|
|
*/ |
715
|
|
|
public function update( $value ) { |
716
|
|
|
|
717
|
|
|
return null; |
718
|
|
|
|
719
|
|
|
} |
720
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.