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