| @@ -1,0 +1,507 @@ | ||
| 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 | +#[AllowDynamicProperties] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes -- Back-compat: allow dynamic props (PHP 8.2+) on this class + subclasses. | |
| 14 | +class CMB2_Field_Display { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * A CMB field object | |
| 18 | + * | |
| 19 | + * @var CMB2_Field object | |
| 20 | + * @since 2.2.2 | |
| 21 | + */ | |
| 22 | + public $field; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * The CMB field object's value. | |
| 26 | + * | |
| 27 | + * @var mixed | |
| 28 | + * @since 2.2.2 | |
| 29 | + */ | |
| 30 | + public $value; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Get the corresponding display class for the field type. | |
| 34 | + * | |
| 35 | + * @since 2.2.2 | |
| 36 | + * @param CMB2_Field $field Requested field type. | |
| 37 | + * @return CMB2_Field_Display | |
| 38 | + */ | |
| 39 | + public static function get( CMB2_Field $field ) { | |
| 40 | + $fieldtype = $field->type(); | |
| 41 | + $display_class_name = $field->args( 'display_class' ); | |
| 42 | + | |
| 43 | + if ( empty( $display_class_name ) ) { | |
| 44 | + switch ( $fieldtype ) { | |
| 45 | + case 'text_url': | |
| 46 | + $display_class_name = 'CMB2_Display_Text_Url'; | |
| 47 | + break; | |
| 48 | + case 'text_money': | |
| 49 | + $display_class_name = 'CMB2_Display_Text_Money'; | |
| 50 | + break; | |
| 51 | + case 'colorpicker': | |
| 52 | + $display_class_name = 'CMB2_Display_Colorpicker'; | |
| 53 | + break; | |
| 54 | + case 'checkbox': | |
| 55 | + $display_class_name = 'CMB2_Display_Checkbox'; | |
| 56 | + break; | |
| 57 | + case 'wysiwyg': | |
| 58 | + case 'textarea_small': | |
| 59 | + $display_class_name = 'CMB2_Display_Textarea'; | |
| 60 | + break; | |
| 61 | + case 'textarea_code': | |
| 62 | + $display_class_name = 'CMB2_Display_Textarea_Code'; | |
| 63 | + break; | |
| 64 | + case 'text_time': | |
| 65 | + $display_class_name = 'CMB2_Display_Text_Time'; | |
| 66 | + break; | |
| 67 | + case 'text_date': | |
| 68 | + case 'text_date_timestamp': | |
| 69 | + case 'text_datetime_timestamp': | |
| 70 | + $display_class_name = 'CMB2_Display_Text_Date'; | |
| 71 | + break; | |
| 72 | + case 'text_datetime_timestamp_timezone': | |
| 73 | + $display_class_name = 'CMB2_Display_Text_Date_Timezone'; | |
| 74 | + break; | |
| 75 | + case 'select': | |
| 76 | + case 'radio': | |
| 77 | + case 'radio_inline': | |
| 78 | + $display_class_name = 'CMB2_Display_Select'; | |
| 79 | + break; | |
| 80 | + case 'multicheck': | |
| 81 | + case 'multicheck_inline': | |
| 82 | + $display_class_name = 'CMB2_Display_Multicheck'; | |
| 83 | + break; | |
| 84 | + case 'taxonomy_radio': | |
| 85 | + case 'taxonomy_radio_inline': | |
| 86 | + case 'taxonomy_select': | |
| 87 | + case 'taxonomy_select_hierarchical': | |
| 88 | + case 'taxonomy_radio_hierarchical': | |
| 89 | + $display_class_name = 'CMB2_Display_Taxonomy_Radio'; | |
| 90 | + break; | |
| 91 | + case 'taxonomy_multicheck': | |
| 92 | + case 'taxonomy_multicheck_inline': | |
| 93 | + case 'taxonomy_multicheck_hierarchical': | |
| 94 | + $display_class_name = 'CMB2_Display_Taxonomy_Multicheck'; | |
| 95 | + break; | |
| 96 | + case 'file': | |
| 97 | + $display_class_name = 'CMB2_Display_File'; | |
| 98 | + break; | |
| 99 | + case 'file_list': | |
| 100 | + $display_class_name = 'CMB2_Display_File_List'; | |
| 101 | + break; | |
| 102 | + case 'oembed': | |
| 103 | + $display_class_name = 'CMB2_Display_oEmbed'; | |
| 104 | + break; | |
| 105 | + default: | |
| 106 | + $display_class_name = __CLASS__; | |
| 107 | + break; | |
| 108 | + }// End switch. | |
| 109 | + } | |
| 110 | + | |
| 111 | + if ( has_action( "cmb2_display_class_{$fieldtype}" ) ) { | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Filters the custom field display class used for displaying the field. Class is required to extend CMB2_Type_Base. | |
| 115 | + * | |
| 116 | + * The dynamic portion of the hook name, $fieldtype, refers to the (custom) field type. | |
| 117 | + * | |
| 118 | + * @since 2.2.4 | |
| 119 | + * | |
| 120 | + * @param string $display_class_name The custom field display class to use. | |
| 121 | + * @param object $field The `CMB2_Field` object. | |
| 122 | + */ | |
| 123 | + $display_class_name = apply_filters( "cmb2_display_class_{$fieldtype}", $display_class_name, $field ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + return new $display_class_name( $field ); | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * Setup our class vars | |
| 131 | + * | |
| 132 | + * @since 2.2.2 | |
| 133 | + * @param CMB2_Field $field A CMB2 field object. | |
| 134 | + */ | |
| 135 | + public function __construct( CMB2_Field $field ) { | |
| 136 | + $this->field = $field; | |
| 137 | + $this->value = $this->field->value; | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Catchall method if field's 'display_cb' is NOT defined, or field type does | |
| 142 | + * not have a corresponding display method | |
| 143 | + * | |
| 144 | + * @since 2.2.2 | |
| 145 | + */ | |
| 146 | + public function display() { | |
| 147 | + // If repeatable. | |
| 148 | + if ( $this->field->args( 'repeatable' ) ) { | |
| 149 | + | |
| 150 | + // And has a repeatable value. | |
| 151 | + if ( is_array( $this->field->value ) ) { | |
| 152 | + | |
| 153 | + // Then loop and output. | |
| 154 | + echo '<ul class="cmb2-' . esc_attr( sanitize_html_class( str_replace( '_', '-', $this->field->type() ) ) ) . '">'; | |
| 155 | + foreach ( $this->field->value as $val ) { | |
| 156 | + $this->value = $val; | |
| 157 | + echo '<li>', $this->_display(), '</li>'; | |
| 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 | + if ( empty( $this->value ) ) { | |
| 322 | + return; | |
| 323 | + } | |
| 324 | + | |
| 325 | + $datetime = CMB2_Utils::get_datetime_from_value( $this->value ); | |
| 326 | + if ( ! $datetime || ! $datetime instanceof DateTime ) { | |
| 327 | + return; | |
| 328 | + } | |
| 329 | + | |
| 330 | + $date = $datetime->format( stripslashes( $this->field->args( 'date_format' ) ) ); | |
| 331 | + $time = $datetime->format( stripslashes( $this->field->args( 'time_format' ) ) ); | |
| 332 | + $timezone = $datetime->getTimezone()->getName(); | |
| 333 | + | |
| 334 | + echo $date; | |
| 335 | + if ( $time ) { | |
| 336 | + echo ' ' . $time; | |
| 337 | + } | |
| 338 | + if ( $timezone ) { | |
| 339 | + echo ', ' . $timezone; | |
| 340 | + } | |
| 341 | + } | |
| 342 | +} | |
| 343 | + | |
| 344 | +class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display { | |
| 345 | + /** | |
| 346 | + * Display single taxonomy value. | |
| 347 | + * | |
| 348 | + * @since 2.2.2 | |
| 349 | + */ | |
| 350 | + protected function _display() { | |
| 351 | + $taxonomy = $this->field->args( 'taxonomy' ); | |
| 352 | + $types = new CMB2_Types( $this->field ); | |
| 353 | + $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_Taxonomy_Radio' ); | |
| 354 | + $terms = $type->get_object_terms(); | |
| 355 | + $term = false; | |
| 356 | + | |
| 357 | + if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) { | |
| 358 | + $term = get_term_by( 'slug', $default, $taxonomy ); | |
| 359 | + } elseif ( ! empty( $terms ) ) { | |
| 360 | + $term = $terms[ key( $terms ) ]; | |
| 361 | + } | |
| 362 | + | |
| 363 | + if ( $term ) { | |
| 364 | + $link = get_edit_term_link( $term->term_id, $taxonomy ); | |
| 365 | + echo '<a href="', esc_url( $link ? $link : '' ), '">', esc_html( $term->name ), '</a>'; | |
| 366 | + } | |
| 367 | + } | |
| 368 | +} | |
| 369 | + | |
| 370 | +class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display { | |
| 371 | + /** | |
| 372 | + * Display taxonomy values. | |
| 373 | + * | |
| 374 | + * @since 2.2.2 | |
| 375 | + */ | |
| 376 | + protected function _display() { | |
| 377 | + $taxonomy = $this->field->args( 'taxonomy' ); | |
| 378 | + $types = new CMB2_Types( $this->field ); | |
| 379 | + $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_Taxonomy_Multicheck' ); | |
| 380 | + $terms = $type->get_object_terms(); | |
| 381 | + | |
| 382 | + if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) { | |
| 383 | + $terms = array(); | |
| 384 | + if ( is_array( $default ) ) { | |
| 385 | + foreach ( $default as $slug ) { | |
| 386 | + $terms[] = get_term_by( 'slug', $slug, $taxonomy ); | |
| 387 | + } | |
| 388 | + } else { | |
| 389 | + $terms[] = get_term_by( 'slug', $default, $taxonomy ); | |
| 390 | + } | |
| 391 | + } | |
| 392 | + | |
| 393 | + if ( is_array( $terms ) ) { | |
| 394 | + | |
| 395 | + $links = array(); | |
| 396 | + foreach ( $terms as $term ) { | |
| 397 | + $link = get_edit_term_link( $term->term_id, $taxonomy ); | |
| 398 | + $links[] = '<a href="' . esc_url( $link ? $link : '' ) . '">' . esc_html( $term->name ) . '</a>'; | |
| 399 | + } | |
| 400 | + // Then loop and output. | |
| 401 | + echo '<div class="cmb2-taxonomy-terms-', esc_attr( sanitize_html_class( $taxonomy ) ), '">'; | |
| 402 | + echo implode( ', ', $links ); | |
| 403 | + echo '</div>'; | |
| 404 | + } | |
| 405 | + } | |
| 406 | +} | |
| 407 | + | |
| 408 | +class CMB2_Display_File extends CMB2_Field_Display { | |
| 409 | + /** | |
| 410 | + * Display file value. | |
| 411 | + * | |
| 412 | + * @since 2.2.2 | |
| 413 | + */ | |
| 414 | + protected function _display() { | |
| 415 | + if ( empty( $this->value ) ) { | |
| 416 | + return; | |
| 417 | + } | |
| 418 | + | |
| 419 | + $this->value = esc_url_raw( $this->value ); | |
| 420 | + | |
| 421 | + $types = new CMB2_Types( $this->field ); | |
| 422 | + $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_File_Base' ); | |
| 423 | + | |
| 424 | + $id = $this->field->get_field_clone( array( | |
| 425 | + 'id' => $this->field->_id( '', false ) . '_id', | |
| 426 | + ) )->escaped_value( 'absint' ); | |
| 427 | + | |
| 428 | + $this->file_output( $this->value, $id, $type ); | |
| 429 | + } | |
| 430 | + | |
| 431 | + protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) { | |
| 432 | + // If there is no ID saved yet, try to get it from the url. | |
| 433 | + if ( $url_value && ! $id ) { | |
| 434 | + $id = CMB2_Utils::image_id_from_url( esc_url_raw( $url_value ) ); | |
| 435 | + } | |
| 436 | + | |
| 437 | + if ( $field_type->is_valid_img_ext( $url_value ) ) { | |
| 438 | + $img_size = $this->field->args( 'preview_size' ); | |
| 439 | + | |
| 440 | + if ( $id ) { | |
| 441 | + $image = wp_get_attachment_image( $id, $img_size, null, array( | |
| 442 | + 'class' => 'cmb-image-display', | |
| 443 | + ) ); | |
| 444 | + } else { | |
| 445 | + $size = is_array( $img_size ) ? $img_size[0] : 200; | |
| 446 | + $image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . esc_url( $url_value ) . '" alt="" />'; | |
| 447 | + } | |
| 448 | + | |
| 449 | + echo $image; | |
| 450 | + | |
| 451 | + } else { | |
| 452 | + | |
| 453 | + printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>', | |
| 454 | + esc_html( $field_type->_text( 'file_text', __( 'File:', 'cmb2' ) ) ), | |
| 455 | + esc_url( $url_value ), | |
| 456 | + esc_html( CMB2_Utils::get_file_name_from_path( $url_value ) ) | |
| 457 | + ); | |
| 458 | + | |
| 459 | + } | |
| 460 | + } | |
| 461 | +} | |
| 462 | + | |
| 463 | +class CMB2_Display_File_List extends CMB2_Display_File { | |
| 464 | + /** | |
| 465 | + * Display file_list value. | |
| 466 | + * | |
| 467 | + * @since 2.2.2 | |
| 468 | + */ | |
| 469 | + protected function _display() { | |
| 470 | + if ( empty( $this->value ) || ! is_array( $this->value ) ) { | |
| 471 | + return; | |
| 472 | + } | |
| 473 | + | |
| 474 | + $types = new CMB2_Types( $this->field ); | |
| 475 | + $type = $types->get_new_render_type( $this->field->type(), 'CMB2_Type_File_Base' ); | |
| 476 | + | |
| 477 | + echo '<ul class="cmb2-display-file-list">'; | |
| 478 | + foreach ( $this->value as $id => $fullurl ) { | |
| 479 | + echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $type ), '</li>'; | |
| 480 | + } | |
| 481 | + echo '</ul>'; | |
| 482 | + } | |
| 483 | +} | |
| 484 | + | |
| 485 | +// phpcs:ignore PEAR.NamingConventions.ValidClassName.Invalid -- "oEmbed" is the correct brand casing; renaming this public class is a BC break. | |
| 486 | +class CMB2_Display_oEmbed extends CMB2_Field_Display { | |
| 487 | + /** | |
| 488 | + * Display oembed value. | |
| 489 | + * | |
| 490 | + * @since 2.2.2 | |
| 491 | + */ | |
| 492 | + protected function _display() { | |
| 493 | + if ( ! $this->value ) { | |
| 494 | + return; | |
| 495 | + } | |
| 496 | + | |
| 497 | + cmb2_do_oembed( array( | |
| 498 | + 'url' => $this->value, | |
| 499 | + 'object_id' => $this->field->object_id, | |
| 500 | + 'object_type' => $this->field->object_type, | |
| 501 | + 'oembed_args' => array( | |
| 502 | + 'width' => '300', | |
| 503 | + ), | |
| 504 | + 'field_id' => $this->field->id(), | |
| 505 | + ) ); | |
| 506 | + } | |
| 507 | +} | |