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