1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
<?php /** * The field base class. * This is the parent class of all custom fields defined by the plugin, which defines all the common methods. * Fields must inherit this class and overwrite methods with its own. * * @package Meta Box */
/** * The field base class. */ abstract class RWMB_Field { /** * Add actions. */ public static function add_actions() { }
/** * Enqueue scripts and styles. */ public static function admin_enqueue_scripts() { }
/** * Localize scripts with prevention of loading localized data twice. * * @link https://github.com/rilwis/meta-box/issues/850 * * @param string $handle Script handle. * @param string $name Object name. * @param mixed $data Localized data. */ public static function localize_script( $handle, $name, $data ) { /* * Check with function_exists to make it work in WordPress 4.1. * @link https://github.com/rilwis/meta-box/issues/1009 */ if ( ! function_exists( 'wp_scripts' ) || ! wp_scripts()->get_data( $handle, 'data' ) ) { wp_localize_script( $handle, $name, $data ); } }
/** * Show field HTML * Filters are put inside this method, not inside methods such as "meta", "html", "begin_html", etc. * That ensures the returned value are always been applied filters. * This method is not meant to be overwritten in specific fields. * * @param array $field Field parameters. * @param bool $saved Whether the meta box is saved at least once. * @param int $post_id Post ID. */ public static function show( $field, $saved, $post_id = 0 ) { $meta = self::call( $field, 'meta', $post_id, $saved ); $meta = self::filter( 'field_meta', $meta, $field, $saved );
$begin = self::call( $field, 'begin_html', $meta ); $begin = self::filter( 'begin_html', $begin, $field, $meta );
// Separate code for cloneable and non-cloneable fields to make easy to maintain. if ( $field['clone'] ) { $field_html = RWMB_Clone::html( $meta, $field ); } else { // Call separated methods for displaying each type of field. $field_html = self::call( $field, 'html', $meta ); $field_html = self::filter( 'html', $field_html, $field, $meta ); }
$end = self::call( $field, 'end_html', $meta ); $end = self::filter( 'end_html', $end, $field, $meta );
$html = self::filter( 'wrapper_html', "$begin$field_html$end", $field, $meta );
// Display label and input in DIV and allow user-defined classes to be appended. $classes = "rwmb-field rwmb-{$field['type']}-wrapper " . $field['class']; if ( 'hidden' === $field['type'] ) { $classes .= ' hidden'; } if ( ! empty( $field['required'] ) ) { $classes .= ' required'; }
$outer_html = sprintf( $field['before'] . '<div class="%s">%s</div>' . $field['after'], trim( $classes ), $html ); $outer_html = self::filter( 'outer_html', $outer_html, $field, $meta );
echo $outer_html; // WPCS: XSS OK. }
/** * Get field HTML. * * @param mixed $meta Meta value. * @param array $field Field parameters. * * @return string */ public static function html( $meta, $field ) { return ''; }
/** * Show begin HTML markup for fields. * * @param mixed $meta Meta value. * @param array $field Field parameters. * * @return string */ public static function begin_html( $meta, $field ) { $field_label = ''; if ( $field['name'] ) { $field_label = sprintf( '<div class="rwmb-label"> <label for="%s">%s</label> %s </div>', esc_attr( $field['id'] ), $field['name'], self::label_description( $field ) ); }
$data_max_clone = is_numeric( $field['max_clone'] ) && $field['max_clone'] > 1 ? ' data-max-clone=' . $field['max_clone'] : '';
$input_open = sprintf( '<div class="rwmb-input"%s>', $data_max_clone );
return $field_label . $input_open; }
/** * Show end HTML markup for fields. * * @param mixed $meta Meta value. * @param array $field Field parameters. * * @return string */ public static function end_html( $meta, $field ) { return RWMB_Clone::add_clone_button( $field ) . self::call( 'input_description', $field ) . '</div>'; }
/** * Display field label description. * * @param array $field Field parameters. * @return string */ protected static function label_description( $field ) { $id = $field['id'] ? ' id="' . esc_attr( $field['id'] ) . '-label-description"' : ''; return $field['label_description'] ? "<p{$id} class='description'>{$field['label_description']}</p>" : ''; }
/** * Display field description. * * @param array $field Field parameters. * @return string */ protected static function input_description( $field ) { $id = $field['id'] ? ' id="' . esc_attr( $field['id'] ) . '-description"' : ''; return $field['desc'] ? "<p{$id} class='description'>{$field['desc']}</p>" : ''; }
/** * Get raw meta value. * * @param int $object_id Object ID. * @param array $field Field parameters. * @param array $args Arguments of {@see rwmb_meta()} helper. * * @return mixed */ public static function raw_meta( $object_id, $field, $args = array() ) { if ( empty( $field['id'] ) ) { return ''; }
if ( isset( $args['object_type'] ) ) { $storage = rwmb_get_storage( $args['object_type'] ); } elseif ( isset( $field['storage'] ) ) { $storage = $field['storage']; } else { $storage = rwmb_get_storage( 'post' ); }
if ( ! isset( $args['single'] ) ) { $args['single'] = $field['clone'] || ! $field['multiple']; }
return $storage->get( $object_id, $field['id'], $args ); }
/** * Get meta value. * * @param int $post_id Post ID. * @param bool $saved Whether the meta box is saved at least once. * @param array $field Field parameters. * * @return mixed */ public static function meta( $post_id, $saved, $field ) { /** * For special fields like 'divider', 'heading' which don't have ID, just return empty string * to prevent notice error when displaying fields. */ if ( empty( $field['id'] ) ) { return ''; }
// Get raw meta. $meta = self::call( $field, 'raw_meta', $post_id );
// Use $field['std'] only when the meta box hasn't been saved (i.e. the first time we run). $meta = ! $saved ? $field['std'] : $meta;
// Ensure multiple fields are arrays. if ( $field['multiple'] ) { if ( $field['clone'] ) { $meta = (array) $meta; foreach ( $meta as $key => $m ) { $meta[ $key ] = (array) $m; } } else { $meta = (array) $meta; } } // Escape attributes. $meta = self::call( $field, 'esc_meta', $meta );
// Make sure meta value is an array for clonable and multiple fields. if ( $field['clone'] || $field['multiple'] ) { if ( empty( $meta ) || ! is_array( $meta ) ) { /** * If field is clonable, $meta must be an array with values so that the foreach loop in self::show() runs properly. * * @see self::show() */ $meta = $field['clone'] ? array( '' ) : array(); } }
return $meta; }
/** * Escape meta for field output. * * @param mixed $meta Meta value. * * @return mixed */ public static function esc_meta( $meta ) { return is_array( $meta ) ? array_map( __METHOD__, $meta ) : esc_attr( $meta ); }
/** * Set value of meta before saving into database. * * @param mixed $new The submitted meta value. * @param mixed $old The existing meta value. * @param int $post_id The post ID. * @param array $field The field parameters. * * @return int */ public static function value( $new, $old, $post_id, $field ) { return $new; }
/** * Save meta value. * * @param mixed $new The submitted meta value. * @param mixed $old The existing meta value. * @param int $post_id The post ID. * @param array $field The field parameters. */ public static function save( $new, $old, $post_id, $field ) { $name = $field['id']; $storage = $field['storage'];
// Remove post meta if it's empty. if ( '' === $new || array() === $new ) { $storage->delete( $post_id, $name ); return; }
// If field is cloneable, value is saved as a single entry in the database. if ( $field['clone'] ) { // Remove empty values. $new = (array) $new; foreach ( $new as $k => $v ) { if ( '' === $v || array() === $v ) { unset( $new[ $k ] ); } } // Reset indexes. $new = array_values( $new ); $storage->update( $post_id, $name, $new ); return; }
// If field is multiple, value is saved as multiple entries in the database (WordPress behaviour). if ( $field['multiple'] ) { $old = (array) $old; $new = (array) $new; $new_values = array_diff( $new, $old ); foreach ( $new_values as $new_value ) { $storage->add( $post_id, $name, $new_value, false ); } $old_values = array_diff( $old, $new ); foreach ( $old_values as $old_value ) { $storage->delete( $post_id, $name, $old_value ); } return; }
// Default: just update post meta. $storage->update( $post_id, $name, $new ); }
/** * Normalize parameters for field. * * @param array $field Field parameters. * * @return array */ public static function normalize( $field ) { $field = wp_parse_args( $field, array( 'id' => '', 'name' => '', 'label_description' => '', 'multiple' => false, 'std' => '', 'desc' => '', 'format' => '', 'before' => '', 'after' => '', 'field_name' => isset( $field['id'] ) ? $field['id'] : '', 'placeholder' => '',
'clone' => false, 'max_clone' => 0, 'sort_clone' => false, 'add_button' => __( '+ Add more', 'meta-box' ), 'clone_default' => false,
'class' => '', 'disabled' => false, 'required' => false, 'autofocus' => false, 'attributes' => array(), ) );
if ( $field['clone_default'] ) { $field['attributes'] = wp_parse_args( $field['attributes'], array( 'data-default' => $field['std'], 'data-clone-default' => 'true', ) ); }
return $field; }
/** * Get the attributes for a field. * * @param array $field Field parameters. * @param mixed $value Meta value. * * @return array */ public static function get_attributes( $field, $value = null ) { $attributes = wp_parse_args( $field['attributes'], array( 'disabled' => $field['disabled'], 'autofocus' => $field['autofocus'], 'required' => $field['required'], 'id' => $field['id'], 'class' => '', 'name' => $field['field_name'], ) );
$attributes['class'] = implode( ' ', array_merge( array( "rwmb-{$field['type']}" ), (array) $attributes['class'] ) );
return $attributes; }
/** * Renders an attribute array into an html attributes string. * * @param array $attributes HTML attributes. * * @return string */ public static function render_attributes( $attributes ) { $output = '';
foreach ( $attributes as $key => $value ) { if ( false === $value || '' === $value ) { continue; }
if ( is_array( $value ) ) { $value = wp_json_encode( $value ); }
$output .= sprintf( true === $value ? ' %s' : ' %s="%s"', $key, esc_attr( $value ) ); }
return $output; }
/** * Get the field value. * The difference between this function and 'meta' function is 'meta' function always returns the escaped value * of the field saved in the database, while this function returns more meaningful value of the field, for ex.: * for file/image: return array of file/image information instead of file/image IDs. * * Each field can extend this function and add more data to the returned value. * See specific field classes for details. * * @param array $field Field parameters. * @param array $args Additional arguments. Rarely used. See specific fields for details. * @param int|null $post_id Post ID. null for current post. Optional. * * @return mixed Field value */ public static function get_value( $field, $args = array(), $post_id = null ) { // Some fields does not have ID like heading, custom HTML, etc. if ( empty( $field['id'] ) ) { return ''; }
if ( ! $post_id ) { $post_id = get_the_ID(); }
// Get raw meta value in the database, no escape. $value = self::call( $field, 'raw_meta', $post_id, $args );
// Make sure meta value is an array for cloneable and multiple fields. if ( $field['clone'] || $field['multiple'] ) { $value = is_array( $value ) && $value ? $value : array(); }
return $value; }
/** * Output the field value. * Depends on field value and field types, each field can extend this method to output its value in its own way * See specific field classes for details. * * Note: we don't echo the field value directly. We return the output HTML of field, which will be used in * rwmb_the_field function later. * * @use self::get_value() * @see rwmb_the_value() * * @param array $field Field parameters. * @param array $args Additional arguments. Rarely used. See specific fields for details. * @param int|null $post_id Post ID. null for current post. Optional. * * @return string HTML output of the field */ public static function the_value( $field, $args = array(), $post_id = null ) { $value = self::call( 'get_value', $field, $args, $post_id );
if ( false === $value ) { return ''; }
return self::call( 'format_value', $field, $value ); }
/** * Format value for the helper functions. * * @param array $field Field parameters. * @param string|array $value The field meta value. * @return string */ public static function format_value( $field, $value ) { if ( ! is_array( $value ) ) { return self::call( 'format_single_value', $field, $value ); } $output = '<ul>'; foreach ( $value as $subvalue ) { $output .= '<li>' . self::call( 'format_value', $field, $subvalue ) . '</li>'; } $output .= '</ul>'; return $output; }
/** * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary. * * @param array $field Field parameters. * @param string $value The value. * @return string */ public static function format_single_value( $field, $value ) { return $value; }
/** * Call a method of a field. * This should be replaced by static::$method( $args ) in PHP 5.3. * * @return mixed */ public static function call() { $args = func_get_args();
$check = reset( $args );
// Params: method name, field, other params. if ( is_string( $check ) ) { $method = array_shift( $args ); $field = reset( $args ); // Keep field as 1st param. } else { $field = array_shift( $args ); $method = array_shift( $args );
if ( 'raw_meta' === $method ) { // Add field param after object id. array_splice( $args, 1, 0, array( $field ) ); } else { $args[] = $field; // Add field as last param. } }
return call_user_func_array( array( self::get_class_name( $field ), $method ), $args ); }
/** * Map field types. * * @param array $field Field parameters. * @return string Field mapped type. */ public static function map_types( $field ) { $type = isset( $field['type'] ) ? $field['type'] : 'input'; $type_map = apply_filters( 'rwmb_type_map', array( 'file_advanced' => 'media', 'plupload_image' => 'image_upload', 'url' => 'text', ) );
return isset( $type_map[ $type ] ) ? $type_map[ $type ] : $type; }
/** * Get field class name. * * @param array $field Field parameters. * @return string Field class name. */ public static function get_class_name( $field ) { $type = self::map_types( $field ); $type = str_replace( array( '-', '_' ), ' ', $type ); $class = 'RWMB_' . ucwords( $type ) . '_Field'; $class = str_replace( ' ', '_', $class ); return class_exists( $class ) ? $class : 'RWMB_Input_Field'; }
/** * Apply various filters based on field type, id. * Filters: * - rwmb_{$name} * - rwmb_{$field['type']}_{$name} * - rwmb_{$field['id']}_{$name} * * @return mixed */ public static function filter() { $args = func_get_args();
// 3 first params must be: filter name, value, field. Other params will be used for filters. $name = array_shift( $args ); $value = array_shift( $args ); $field = array_shift( $args );
// List of filters. $filters = array( 'rwmb_' . $name, 'rwmb_' . $field['type'] . '_' . $name, ); if ( isset( $field['id'] ) ) { $filters[] = 'rwmb_' . $field['id'] . '_' . $name; }
// Filter params: value, field, other params. Note: value is changed after each run. array_unshift( $args, $field ); foreach ( $filters as $filter ) { $filter_args = $args; array_unshift( $filter_args, $value ); $value = apply_filters_ref_array( $filter, $filter_args ); }
return $value; } }
|