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
<?php
if ( ! class_exists( 'WCQD_METABOX_Field ' ) )
{
class WCQD_METABOX_Field
{
/**
* Add actions
*
* @return void
*/
static function add_actions()
{
}
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
}
/**
* 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
* @param bool $saved
*
* @return string
*/
static function show( $field, $saved )
{
global $post;
$field_class = RW_Meta_Box::get_class_name( $field );
$meta = call_user_func( array( $field_class, 'meta' ), $post->ID, $saved, $field );
// Apply filter to field meta value
// 1st filter applies to all fields
// 2nd filter applies to all fields with the same type
// 3rd filter applies to current field only
$meta = apply_filters( 'wcqd_metabox_field_meta', $meta, $field, $saved );
$meta = apply_filters( "wcqd_metabox_{$field['type']}_meta", $meta, $field, $saved );
$meta = apply_filters( "wcqd_metabox_{$field['id']}_meta", $meta, $field, $saved );
$type = $field['type'];
$id = $field['id'];
$begin = call_user_func( array( $field_class, 'begin_html' ), $meta, $field );
// Apply filter to field begin HTML
// 1st filter applies to all fields
// 2nd filter applies to all fields with the same type
// 3rd filter applies to current field only
$begin = apply_filters( 'wcqd_metabox_begin_html', $begin, $field, $meta );
$begin = apply_filters( "wcqd_metabox_{$type}_begin_html", $begin, $field, $meta );
$begin = apply_filters( "wcqd_metabox_{$id}_begin_html", $begin, $field, $meta );
// Separate code for cloneable and non-cloneable fields to make easy to maintain
// Cloneable fields
if ( $field['clone'] )
{
$field_html = '';
/**
* Note: $meta must contain value so that the foreach loop runs!
* @see self::meta()
*/
foreach ( $meta as $index => $sub_meta )
{
$sub_field = $field;
$sub_field['field_name'] = $field['field_name'] . "[{$index}]";
if ( $index > 0 )
{
if ( isset( $sub_field['address_field'] ) )
$sub_field['address_field'] = $field['address_field'] . "_{$index}";
$sub_field['id'] = $field['id'] . "_{$index}";
}
if ( $field['multiple'] )
$sub_field['field_name'] .= '[]';
// Wrap field HTML in a div with class="rwmb-clone" if needed
$input_html = '<div class="rwmb-clone">';
// Drag clone icon
if ( $field['sort_clone'] )
$input_html .= "<a href='javascript:;' class='rwmb-clone-icon'></a>";
// Call separated methods for displaying each type of field
$input_html .= call_user_func( array( $field_class, 'html' ), $sub_meta, $sub_field );
// Apply filter to field HTML
// 1st filter applies to all fields with the same type
// 2nd filter applies to current field only
$input_html = apply_filters( "wcqd_metabox_{$type}_html", $input_html, $field, $sub_meta );
$input_html = apply_filters( "wcqd_metabox_{$id}_html", $input_html, $field, $sub_meta );
// Remove clone button
$input_html .= call_user_func( array( $field_class, 'remove_clone_button' ), $sub_field );
$input_html .= '</div>';
$field_html .= $input_html;
}
}
// Non-cloneable fields
else
{
// Call separated methods for displaying each type of field
$field_html = call_user_func( array( $field_class, 'html' ), $meta, $field );
// Apply filter to field HTML
// 1st filter applies to all fields with the same type
// 2nd filter applies to current field only
$field_html = apply_filters( "wcqd_metabox_{$type}_html", $field_html, $field, $meta );
$field_html = apply_filters( "wcqd_metabox_{$id}_html", $field_html, $field, $meta );
}
$end = call_user_func( array( $field_class, 'end_html' ), $meta, $field );
// Apply filter to field end HTML
// 1st filter applies to all fields
// 2nd filter applies to all fields with the same type
// 3rd filter applies to current field only
$end = apply_filters( 'wcqd_metabox_end_html', $end, $field, $meta );
$end = apply_filters( "wcqd_metabox_{$type}_end_html", $end, $field, $meta );
$end = apply_filters( "wcqd_metabox_{$id}_end_html", $end, $field, $meta );
// Apply filter to field wrapper
// This allow users to change whole HTML markup of the field wrapper (i.e. table row)
// 1st filter applies to all fields
// 1st filter applies to all fields with the same type
// 2nd filter applies to current field only
$html = apply_filters( 'wcqd_metabox_wrapper_html', "{$begin}{$field_html}{$end}", $field, $meta );
$html = apply_filters( "wcqd_metabox_{$type}_wrapper_html", $html, $field, $meta );
$html = apply_filters( "wcqd_metabox_{$id}_wrapper_html", $html, $field, $meta );
// Display label and input in DIV and allow user-defined classes to be appended
$classes = array( 'rwmb-field', "rwmb-{$type}-wrapper" );
if ( 'hidden' === $field['type'] )
$classes[] = 'hidden';
if ( ! empty( $field['required'] ) )
$classes[] = 'required';
if ( ! empty( $field['class'] ) )
$classes[] = $field['class'];
$outer_html = sprintf(
$field['before'] . '<div class="%s">%s</div>' . $field['after'],
implode( ' ', $classes ),
$html
);
// Allow to change output of outer div
// 1st filter applies to all fields
// 1st filter applies to all fields with the same type
// 2nd filter applies to current field only
$outer_html = apply_filters( 'wcqd_metabox_outer_html', $outer_html, $field, $meta );
$outer_html = apply_filters( "wcqd_metabox_{$type}_outer_html", $outer_html, $field, $meta );
$outer_html = apply_filters( "wcqd_metabox_{$id}_outer_html", $outer_html, $field, $meta );
echo $outer_html;
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
return '';
}
/**
* Show begin HTML markup for fields
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function begin_html( $meta, $field )
{
$field_label = '';
if ( $field['name'] )
{
$field_label = sprintf(
'<div class="rwmb-label"><label for="%s">%s</label></div>',
$field['id'],
$field['name']
);
}
$data_max_clone = '';
if ( is_numeric( $field['max_clone'] ) && $field['max_clone'] > 1 )
{
$data_max_clone .= ' 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
* @param array $field
*
* @return string
*/
static function end_html( $meta, $field )
{
$button = $field['clone'] ? call_user_func( array( RW_Meta_Box::get_class_name( $field ), 'add_clone_button' ), $field ) : '';
$desc = $field['desc'] ? "<p id='{$field['id']}_description' class='description'>{$field['desc']}</p>" : '';
// Closes the container
$html = "{$button}{$desc}</div>";
return $html;
}
/**
* Add clone button
*
* @param array $field Field parameter
*
* @return string $html
*/
static function add_clone_button( $field )
{
$text = apply_filters( 'wcqd_metabox_add_clone_button_text', __( '+', 'meta-box' ), $field );
return "<a href='#' class='rwmb-button button-primary add-clone'>$text</a>";
}
/**
* Remove clone button
*
* @param array $field Field parameter
*
* @return string $html
*/
static function remove_clone_button( $field )
{
$text = apply_filters( 'wcqd_metabox_remove_clone_button_text', __( '–', 'meta-box' ), $field );
return "<a href='#' class='rwmb-button button remove-clone'>$text</a>";
}
/**
* Get meta value
*
* @param int $post_id
* @param bool $saved
* @param array $field
*
* @return mixed
*/
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 '';
$single = $field['clone'] || ! $field['multiple'];
$meta = get_post_meta( $post_id, $field['id'], $single );
// Use $field['std'] only when the meta box hasn't been saved (i.e. the first time we run)
$meta = ( ! $saved && '' === $meta || array() === $meta ) ? $field['std'] : $meta;
// Escape attributes
$meta = call_user_func( array( RW_Meta_Box::get_class_name( $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 ) )
{
/**
* Note: 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
*
* @return mixed
*/
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
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return int
*/
static function value( $new, $old, $post_id, $field )
{
return $new;
}
/**
* Save meta value
*
* @param $new
* @param $old
* @param $post_id
* @param $field
*/
static function save( $new, $old, $post_id, $field )
{
$name = $field['id'];
// Remove post meta if it's empty
if ( '' === $new || array() === $new )
{
delete_post_meta( $post_id, $name );
return;
}
// If field is cloneable, value is saved as a single entry in the database
if ( $field['clone'] )
{
$new = (array) $new;
foreach ( $new as $k => $v )
{
if ( '' === $v )
unset( $new[$k] );
}
update_post_meta( $post_id, $name, $new );
return;
}
// If field is multiple, value is saved as multiple entries in the database (WordPress behaviour)
if ( $field['multiple'] )
{
foreach ( $new as $new_value )
{
if ( ! in_array( $new_value, $old ) )
add_post_meta( $post_id, $name, $new_value, false );
}
foreach ( $old as $old_value )
{
if ( ! in_array( $old_value, $new ) )
delete_post_meta( $post_id, $name, $old_value );
}
return;
}
// Default: just update post meta
update_post_meta( $post_id, $name, $new );
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize_field( $field )
{
return $field;
}
/**
* 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
*/
static function get_value( $field, $args = array(), $post_id = null )
{
if ( ! $post_id )
$post_id = get_the_ID();
/**
* Get raw meta value in the database, no escape
* Very similar to self::meta() function
*/
/**
* For special fields like 'divider', 'heading' which don't have ID, just return empty string
* to prevent notice error when display in fields
*/
$value = '';
if ( ! empty( $field['id'] ) )
{
$single = $field['clone'] || ! $field['multiple'];
$value = get_post_meta( $post_id, $field['id'], $single );
// Make sure meta value is an array for clonable and multiple fields
if ( $field['clone'] || $field['multiple'] )
{
$value = is_array( $value ) && $value ? $value : array();
}
}
/**
* Return the meta value by default.
* For specific fields, the returned value might be different. See each field class for details
*/
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
* wcqd_metabox_the_field function later.
*
* @use self::get_value()
* @see wcqd_metabox_the_field()
*
* @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
*/
static function the_value( $field, $args = array(), $post_id = null )
{
$value = call_user_func( array( RW_Meta_Box::get_class_name( $field ), 'get_value' ), $field, $args, $post_id );
$output = $value;
if ( is_array( $value ) )
{
$output = '<ul>';
foreach ( $value as $subvalue )
{
$output .= '<li>' . $subvalue . '</li>';
}
$output .= '</ul>';
}
return $output;
}
}
}