1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMB2 field display base. |
4
|
|
|
* |
5
|
|
|
* @since 2.2.2 |
6
|
|
|
* |
7
|
|
|
* @category WordPress_Plugin |
8
|
|
|
* @package CMB2 |
9
|
|
|
* @author CMB2 team |
10
|
|
|
* @license GPL-2.0+ |
11
|
|
|
* @link https://cmb2.io |
12
|
|
|
*/ |
13
|
|
|
class CMB2_Field_Display { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A CMB field object |
17
|
|
|
* |
18
|
|
|
* @var CMB2_Field object |
19
|
|
|
* @since 2.2.2 |
20
|
|
|
*/ |
21
|
|
|
public $field; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The CMB field object's value. |
25
|
|
|
* |
26
|
|
|
* @var mixed |
27
|
|
|
* @since 2.2.2 |
28
|
|
|
*/ |
29
|
|
|
public $value; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the corresponding display class for the field type. |
33
|
|
|
* |
34
|
|
|
* @since 2.2.2 |
35
|
|
|
* @param CMB2_Field $field Requested field type. |
36
|
|
|
* @return CMB2_Field_Display |
37
|
|
|
*/ |
38
|
|
|
public static function get( CMB2_Field $field ) { |
39
|
|
|
$fieldtype = $field->type(); |
40
|
|
|
$display_class_name = $field->args( 'display_class' ); |
41
|
|
|
|
42
|
|
|
if ( empty( $display_class_name ) ) { |
43
|
|
|
switch ( $fieldtype ) { |
44
|
|
|
case 'text_url': |
45
|
|
|
$display_class_name = 'CMB2_Display_Text_Url'; |
46
|
|
|
break; |
47
|
|
|
case 'text_money': |
48
|
|
|
$display_class_name = 'CMB2_Display_Text_Money'; |
49
|
|
|
break; |
50
|
|
|
case 'colorpicker': |
51
|
|
|
$display_class_name = 'CMB2_Display_Colorpicker'; |
52
|
|
|
break; |
53
|
|
|
case 'checkbox': |
54
|
|
|
$display_class_name = 'CMB2_Display_Checkbox'; |
55
|
|
|
break; |
56
|
|
|
case 'wysiwyg': |
57
|
|
|
case 'textarea_small': |
58
|
|
|
$display_class_name = 'CMB2_Display_Textarea'; |
59
|
|
|
break; |
60
|
|
|
case 'textarea_code': |
61
|
|
|
$display_class_name = 'CMB2_Display_Textarea_Code'; |
62
|
|
|
break; |
63
|
|
|
case 'text_time': |
64
|
|
|
$display_class_name = 'CMB2_Display_Text_Time'; |
65
|
|
|
break; |
66
|
|
|
case 'text_date': |
67
|
|
|
case 'text_date_timestamp': |
68
|
|
|
case 'text_datetime_timestamp': |
69
|
|
|
$display_class_name = 'CMB2_Display_Text_Date'; |
70
|
|
|
break; |
71
|
|
|
case 'text_datetime_timestamp_timezone': |
72
|
|
|
$display_class_name = 'CMB2_Display_Text_Date_Timezone'; |
73
|
|
|
break; |
74
|
|
|
case 'select': |
75
|
|
|
case 'radio': |
76
|
|
|
case 'radio_inline': |
77
|
|
|
$display_class_name = 'CMB2_Display_Select'; |
78
|
|
|
break; |
79
|
|
|
case 'multicheck': |
80
|
|
|
case 'multicheck_inline': |
81
|
|
|
$display_class_name = 'CMB2_Display_Multicheck'; |
82
|
|
|
break; |
83
|
|
|
case 'taxonomy_radio': |
84
|
|
|
case 'taxonomy_radio_inline': |
85
|
|
|
case 'taxonomy_select': |
86
|
|
|
case 'taxonomy_select_hierarchical': |
87
|
|
|
case 'taxonomy_radio_hierarchical': |
88
|
|
|
$display_class_name = 'CMB2_Display_Taxonomy_Radio'; |
89
|
|
|
break; |
90
|
|
|
case 'taxonomy_multicheck': |
91
|
|
|
case 'taxonomy_multicheck_inline': |
92
|
|
|
case 'taxonomy_multicheck_hierarchical': |
93
|
|
|
$display_class_name = 'CMB2_Display_Taxonomy_Multicheck'; |
94
|
|
|
break; |
95
|
|
|
case 'file': |
96
|
|
|
$display_class_name = 'CMB2_Display_File'; |
97
|
|
|
break; |
98
|
|
|
case 'file_list': |
99
|
|
|
$display_class_name = 'CMB2_Display_File_List'; |
100
|
|
|
break; |
101
|
|
|
case 'oembed': |
102
|
|
|
$display_class_name = 'CMB2_Display_oEmbed'; |
103
|
|
|
break; |
104
|
|
|
default: |
105
|
|
|
$display_class_name = __CLASS__; |
106
|
|
|
break; |
107
|
|
|
}// End switch. |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ( has_action( "cmb2_display_class_{$fieldtype}" ) ) { |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Filters the custom field display class used for displaying the field. Class is required to extend CMB2_Type_Base. |
114
|
|
|
* |
115
|
|
|
* The dynamic portion of the hook name, $fieldtype, refers to the (custom) field type. |
116
|
|
|
* |
117
|
|
|
* @since 2.2.4 |
118
|
|
|
* |
119
|
|
|
* @param string $display_class_name The custom field display class to use. |
120
|
|
|
* @param object $field The `CMB2_Field` object. |
121
|
|
|
*/ |
122
|
|
|
$display_class_name = apply_filters( "cmb2_display_class_{$fieldtype}", $display_class_name, $field ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return new $display_class_name( $field ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Setup our class vars |
130
|
|
|
* |
131
|
|
|
* @since 2.2.2 |
132
|
|
|
* @param CMB2_Field $field A CMB2 field object. |
133
|
|
|
*/ |
134
|
|
|
public function __construct( CMB2_Field $field ) { |
135
|
|
|
$this->field = $field; |
136
|
|
|
$this->value = $this->field->value; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Catchall method if field's 'display_cb' is NOT defined, or field type does |
141
|
|
|
* not have a corresponding display method |
142
|
|
|
* |
143
|
|
|
* @since 2.2.2 |
144
|
|
|
*/ |
145
|
|
|
public function display() { |
146
|
|
|
// If repeatable. |
147
|
|
|
if ( $this->field->args( 'repeatable' ) ) { |
148
|
|
|
|
149
|
|
|
// And has a repeatable value. |
150
|
|
|
if ( is_array( $this->field->value ) ) { |
151
|
|
|
|
152
|
|
|
// Then loop and output. |
153
|
|
|
echo '<ul class="cmb2-' . esc_attr( sanitize_html_class( str_replace( '_', '-', $this->field->type() ) ) ) . '">'; |
154
|
|
|
foreach ( $this->field->value as $val ) { |
155
|
|
|
$this->value = $val; |
156
|
|
|
echo '<li>', $this->_display(), '</li>'; |
157
|
|
|
; |
158
|
|
|
} |
159
|
|
|
echo '</ul>'; |
160
|
|
|
} |
161
|
|
|
} else { |
162
|
|
|
$this->_display(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Default fallback display method. |
168
|
|
|
* |
169
|
|
|
* @since 2.2.2 |
170
|
|
|
*/ |
171
|
|
|
protected function _display() { |
172
|
|
|
print_r( $this->value ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
class CMB2_Display_Text_Url extends CMB2_Field_Display { |
177
|
|
|
/** |
178
|
|
|
* Display url value. |
179
|
|
|
* |
180
|
|
|
* @since 2.2.2 |
181
|
|
|
*/ |
182
|
|
|
protected function _display() { |
183
|
|
|
echo make_clickable( esc_url( $this->value ) ); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
class CMB2_Display_Text_Money extends CMB2_Field_Display { |
188
|
|
|
/** |
189
|
|
|
* Display text_money value. |
190
|
|
|
* |
191
|
|
|
* @since 2.2.2 |
192
|
|
|
*/ |
193
|
|
|
protected function _display() { |
194
|
|
|
$this->value = $this->value ? $this->value : '0'; |
195
|
|
|
echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
class CMB2_Display_Colorpicker extends CMB2_Field_Display { |
200
|
|
|
/** |
201
|
|
|
* Display color picker value. |
202
|
|
|
* |
203
|
|
|
* @since 2.2.2 |
204
|
|
|
*/ |
205
|
|
|
protected function _display() { |
206
|
|
|
echo '<span class="cmb2-colorpicker-swatch"><span style="background-color:', esc_attr( $this->value ), '"></span> ', esc_html( $this->value ), '</span>'; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
class CMB2_Display_Checkbox extends CMB2_Field_Display { |
211
|
|
|
/** |
212
|
|
|
* Display multicheck value. |
213
|
|
|
* |
214
|
|
|
* @since 2.2.2 |
215
|
|
|
*/ |
216
|
|
|
protected function _display() { |
217
|
|
|
echo $this->value === 'on' ? 'on' : 'off'; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
class CMB2_Display_Select extends CMB2_Field_Display { |
222
|
|
|
/** |
223
|
|
|
* Display select value. |
224
|
|
|
* |
225
|
|
|
* @since 2.2.2 |
226
|
|
|
*/ |
227
|
|
|
protected function _display() { |
228
|
|
|
$options = $this->field->options(); |
229
|
|
|
|
230
|
|
|
$fallback = $this->field->args( 'show_option_none' ); |
231
|
|
|
if ( ! $fallback && isset( $options[''] ) ) { |
232
|
|
|
$fallback = $options['']; |
233
|
|
|
} |
234
|
|
|
if ( ! $this->value && $fallback ) { |
235
|
|
|
echo $fallback; |
236
|
|
|
} elseif ( isset( $options[ $this->value ] ) ) { |
237
|
|
|
echo $options[ $this->value ]; |
238
|
|
|
} else { |
239
|
|
|
echo esc_attr( $this->value ); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
class CMB2_Display_Multicheck extends CMB2_Field_Display { |
245
|
|
|
/** |
246
|
|
|
* Display multicheck value. |
247
|
|
|
* |
248
|
|
|
* @since 2.2.2 |
249
|
|
|
*/ |
250
|
|
|
protected function _display() { |
251
|
|
|
if ( empty( $this->value ) || ! is_array( $this->value ) ) { |
252
|
|
|
return; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$options = $this->field->options(); |
256
|
|
|
|
257
|
|
|
$output = array(); |
258
|
|
|
foreach ( $this->value as $val ) { |
259
|
|
|
if ( isset( $options[ $val ] ) ) { |
260
|
|
|
$output[] = $options[ $val ]; |
261
|
|
|
} else { |
262
|
|
|
$output[] = esc_attr( $val ); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
echo implode( ', ', $output ); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
class CMB2_Display_Textarea extends CMB2_Field_Display { |
271
|
|
|
/** |
272
|
|
|
* Display textarea value. |
273
|
|
|
* |
274
|
|
|
* @since 2.2.2 |
275
|
|
|
*/ |
276
|
|
|
protected function _display() { |
277
|
|
|
echo wpautop( wp_kses_post( $this->value ) ); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
class CMB2_Display_Textarea_Code extends CMB2_Field_Display { |
282
|
|
|
/** |
283
|
|
|
* Display textarea_code value. |
284
|
|
|
* |
285
|
|
|
* @since 2.2.2 |
286
|
|
|
*/ |
287
|
|
|
protected function _display() { |
288
|
|
|
echo '<xmp class="cmb2-code">' . print_r( $this->value, true ) . '</xmp>'; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
class CMB2_Display_Text_Time extends CMB2_Field_Display { |
293
|
|
|
/** |
294
|
|
|
* Display text_time value. |
295
|
|
|
* |
296
|
|
|
* @since 2.2.2 |
297
|
|
|
*/ |
298
|
|
|
protected function _display() { |
299
|
|
|
echo $this->field->get_timestamp_format( 'time_format', $this->value ); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
class CMB2_Display_Text_Date extends CMB2_Field_Display { |
304
|
|
|
/** |
305
|
|
|
* Display text_date value. |
306
|
|
|
* |
307
|
|
|
* @since 2.2.2 |
308
|
|
|
*/ |
309
|
|
|
protected function _display() { |
310
|
|
|
echo $this->field->get_timestamp_format( 'date_format', $this->value ); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
class CMB2_Display_Text_Date_Timezone extends CMB2_Field_Display { |
315
|
|
|
/** |
316
|
|
|
* Display text_datetime_timestamp_timezone value. |
317
|
|
|
* |
318
|
|
|
* @since 2.2.2 |
319
|
|
|
*/ |
320
|
|
|
protected function _display() { |
321
|
|
|
$field = $this->field; |
322
|
|
|
|
323
|
|
|
if ( empty( $this->value ) ) { |
324
|
|
|
return; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$datetime = maybe_unserialize( $this->value ); |
328
|
|
|
$this->value = $tzstring = ''; |
329
|
|
|
|
330
|
|
|
if ( $datetime && $datetime instanceof DateTime ) { |
331
|
|
|
$tz = $datetime->getTimezone(); |
332
|
|
|
$tzstring = $tz->getName(); |
333
|
|
|
$this->value = $datetime->getTimestamp(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$date = $this->field->get_timestamp_format( 'date_format', $this->value ); |
337
|
|
|
$time = $this->field->get_timestamp_format( 'time_format', $this->value ); |
338
|
|
|
|
339
|
|
|
echo $date, ( $time ? ' ' . $time : '' ), ( $tzstring ? ', ' . $tzstring : '' ); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display { |
344
|
|
|
/** |
345
|
|
|
* Display single taxonomy value. |
346
|
|
|
* |
347
|
|
|
* @since 2.2.2 |
348
|
|
|
*/ |
349
|
|
|
protected function _display() { |
350
|
|
|
$taxonomy = $this->field->args( 'taxonomy' ); |
351
|
|
|
$types = new CMB2_Types( $this->field ); |
352
|
|
|
$type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_Taxonomy_Radio' ); |
353
|
|
|
$terms = $type->get_object_terms(); |
354
|
|
|
$term = false; |
355
|
|
|
|
356
|
|
|
if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) { |
357
|
|
|
$term = get_term_by( 'slug', $default, $taxonomy ); |
358
|
|
|
} elseif ( ! empty( $terms ) ) { |
359
|
|
|
$term = $terms[ key( $terms ) ]; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if ( $term ) { |
363
|
|
|
$link = get_edit_term_link( $term->term_id, $taxonomy ); |
364
|
|
|
echo '<a href="', esc_url( $link ), '">', esc_html( $term->name ), '</a>'; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display { |
370
|
|
|
/** |
371
|
|
|
* Display taxonomy values. |
372
|
|
|
* |
373
|
|
|
* @since 2.2.2 |
374
|
|
|
*/ |
375
|
|
|
protected function _display() { |
376
|
|
|
$taxonomy = $this->field->args( 'taxonomy' ); |
377
|
|
|
$types = new CMB2_Types( $this->field ); |
378
|
|
|
$type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_Taxonomy_Multicheck' ); |
379
|
|
|
$terms = $type->get_object_terms(); |
380
|
|
|
|
381
|
|
|
if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) { |
382
|
|
|
$terms = array(); |
383
|
|
|
if ( is_array( $default ) ) { |
384
|
|
|
foreach ( $default as $slug ) { |
385
|
|
|
$terms[] = get_term_by( 'slug', $slug, $taxonomy ); |
386
|
|
|
} |
387
|
|
|
} else { |
388
|
|
|
$terms[] = get_term_by( 'slug', $default, $taxonomy ); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
if ( is_array( $terms ) ) { |
393
|
|
|
|
394
|
|
|
$links = array(); |
395
|
|
|
foreach ( $terms as $term ) { |
396
|
|
|
$link = get_edit_term_link( $term->term_id, $taxonomy ); |
397
|
|
|
$links[] = '<a href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a>'; |
398
|
|
|
} |
399
|
|
|
// Then loop and output. |
400
|
|
|
echo '<div class="cmb2-taxonomy-terms-', esc_attr( sanitize_html_class( $taxonomy ) ), '">'; |
401
|
|
|
echo implode( ', ', $links ); |
402
|
|
|
echo '</div>'; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
class CMB2_Display_File extends CMB2_Field_Display { |
408
|
|
|
/** |
409
|
|
|
* Display file value. |
410
|
|
|
* |
411
|
|
|
* @since 2.2.2 |
412
|
|
|
*/ |
413
|
|
|
protected function _display() { |
414
|
|
|
if ( empty( $this->value ) ) { |
415
|
|
|
return; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
$this->value = esc_url_raw( $this->value ); |
419
|
|
|
|
420
|
|
|
$types = new CMB2_Types( $this->field ); |
421
|
|
|
$type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_File_Base' ); |
422
|
|
|
|
423
|
|
|
$id = $this->field->get_field_clone( array( |
424
|
|
|
'id' => $this->field->_id( '', false ) . '_id', |
425
|
|
|
) )->escaped_value( 'absint' ); |
426
|
|
|
|
427
|
|
|
$this->file_output( $this->value, $id, $type ); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) { |
431
|
|
|
// If there is no ID saved yet, try to get it from the url. |
432
|
|
|
if ( $url_value && ! $id ) { |
433
|
|
|
$id = CMB2_Utils::image_id_from_url( esc_url_raw( $url_value ) ); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if ( $field_type->is_valid_img_ext( $url_value ) ) { |
437
|
|
|
$img_size = $this->field->args( 'preview_size' ); |
438
|
|
|
|
439
|
|
|
if ( $id ) { |
440
|
|
|
$image = wp_get_attachment_image( $id, $img_size, null, array( |
441
|
|
|
'class' => 'cmb-image-display', |
442
|
|
|
) ); |
443
|
|
|
} else { |
444
|
|
|
$size = is_array( $img_size ) ? $img_size[0] : 200; |
445
|
|
|
$image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . esc_url( $url_value ) . '" alt="" />'; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
echo $image; |
449
|
|
|
|
450
|
|
|
} else { |
451
|
|
|
|
452
|
|
|
printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>', |
453
|
|
|
esc_html( $field_type->_text( 'file_text', __( 'File:', 'cmb2' ) ) ), |
454
|
|
|
esc_url( $url_value ), |
455
|
|
|
esc_html( CMB2_Utils::get_file_name_from_path( $url_value ) ) |
456
|
|
|
); |
457
|
|
|
|
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
class CMB2_Display_File_List extends CMB2_Display_File { |
463
|
|
|
/** |
464
|
|
|
* Display file_list value. |
465
|
|
|
* |
466
|
|
|
* @since 2.2.2 |
467
|
|
|
*/ |
468
|
|
|
protected function _display() { |
469
|
|
|
if ( empty( $this->value ) || ! is_array( $this->value ) ) { |
470
|
|
|
return; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$types = new CMB2_Types( $this->field ); |
474
|
|
|
$type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_File_Base' ); |
475
|
|
|
|
476
|
|
|
echo '<ul class="cmb2-display-file-list">'; |
477
|
|
|
foreach ( $this->value as $id => $fullurl ) { |
478
|
|
|
echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $type ), '</li>'; |
479
|
|
|
} |
480
|
|
|
echo '</ul>'; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
class CMB2_Display_oEmbed extends CMB2_Field_Display { |
485
|
|
|
/** |
486
|
|
|
* Display oembed value. |
487
|
|
|
* |
488
|
|
|
* @since 2.2.2 |
489
|
|
|
*/ |
490
|
|
|
protected function _display() { |
491
|
|
|
if ( ! $this->value ) { |
492
|
|
|
return; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
cmb2_do_oembed( array( |
496
|
|
|
'url' => $this->value, |
497
|
|
|
'object_id' => $this->field->object_id, |
498
|
|
|
'object_type' => $this->field->object_type, |
499
|
|
|
'oembed_args' => array( |
500
|
|
|
'width' => '300', |
501
|
|
|
), |
502
|
|
|
'field_id' => $this->field->id(), |
503
|
|
|
) ); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|