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