CMB2_Display_File

The CMB2 CMB2 Display File class.

Defined (1)

The class is defined in the following location(s).

/includes/CMB2_Field_Display.php  
  1. class CMB2_Display_File extends CMB2_Field_Display { 
  2. /** 
  3. * Display file value. 
  4. * @since 2.2.2 
  5. */ 
  6. protected function _display() { 
  7. if ( empty( $this->value ) ) { 
  8. return; 
  9.  
  10. $this->value = esc_url_raw( $this->value ); 
  11.  
  12. $field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) ); 
  13.  
  14. $id = $this->field->get_field_clone( array( 
  15. 'id' => $field_type->_id() . '_id',  
  16. ) )->escaped_value( 'absint' ); 
  17.  
  18. $this->file_output( $this->value, $id, $field_type ); 
  19.  
  20. protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) { 
  21. // If there is no ID saved yet, try to get it from the url 
  22. if ( $url_value && ! $id ) { 
  23. $id = CMB2_Utils::image_id_from_url( esc_url_raw( $url_value ) ); 
  24.  
  25. if ( $field_type->is_valid_img_ext( $url_value ) ) { 
  26. $img_size = $this->field->args( 'preview_size' ); 
  27.  
  28. if ( $id ) { 
  29. $image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display' ) ); 
  30. } else { 
  31. $size = is_array( $img_size ) ? $img_size[0] : 200; 
  32. $image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />'; 
  33.  
  34. echo $image; 
  35.  
  36. } else { 
  37.  
  38. printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>',  
  39. esc_html( $field_type->_text( 'file_text', esc_html__( 'File:', 'cmb2' ) ) ),  
  40. $url_value,  
  41. CMB2_Utils::get_file_name_from_path( $url_value ) 
  42. ); 
  43.