CMB2_Type_File

CMB file field type.

Defined (1)

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

/includes/types/CMB2_Type_File.php  
  1. class CMB2_Type_File extends CMB2_Type_File_Base { 
  2.  
  3. public function render() { 
  4. $field = $this->field; 
  5. $meta_value = $field->escaped_value(); 
  6. $options = (array) $field->options(); 
  7. $img_size = $field->args( 'preview_size' ); 
  8. $query_args = $field->args( 'query_args' ); 
  9. $output = ''; 
  10.  
  11. // if options array and 'url' => false, then hide the url field 
  12. $input_type = array_key_exists( 'url', $options ) && false === $options['url'] ? 'hidden' : 'text'; 
  13.  
  14. $output .= parent::render( array( 
  15. 'type' => $input_type,  
  16. 'class' => 'cmb2-upload-file regular-text',  
  17. 'size' => 45,  
  18. 'desc' => '',  
  19. 'data-previewsize' => is_array( $img_size ) ? '[' . implode( ', ', $img_size ) . ']' : 350,  
  20. 'data-queryargs' => ! empty( $query_args ) ? json_encode( $query_args ) : '',  
  21. 'js_dependencies' => 'media-editor',  
  22. ) ); 
  23.  
  24. $output .= sprintf( '<input class="cmb2-upload-button button" type="button" value="%s" />', esc_attr( $this->_text( 'add_upload_file_text', esc_html__( 'Add or Upload File', 'cmb2' ) ) ) ); 
  25.  
  26. $output .= $this->_desc( true ); 
  27.  
  28. $cached_id = $this->_id(); 
  29.  
  30. // Reset field args for attachment ID 
  31. $args = array( 
  32. // If we're looking at a file in a group, we need to get the non-prefixed id 
  33. 'id' => ( $field->group ? $field->args( '_id' ) : $cached_id ) . '_id',  
  34. ); 
  35.  
  36. // And get new field object 
  37. // (Need to set it to the types field property) 
  38. $this->types->field = $field = $field->get_field_clone( $args ); 
  39.  
  40. // Get ID value 
  41. $_id_value = $field->escaped_value( 'absint' ); 
  42.  
  43. // If there is no ID saved yet, try to get it from the url 
  44. if ( $meta_value && ! $_id_value ) { 
  45. $_id_value = CMB2_Utils::image_id_from_url( esc_url_raw( $meta_value ) ); 
  46.  
  47. $output .= parent::render( array( 
  48. 'type' => 'hidden',  
  49. 'class' => 'cmb2-upload-file-id',  
  50. 'value' => $_id_value,  
  51. 'desc' => '',  
  52. ) ); 
  53. $output .= '<div id="' . $this->_id( '-status' ) . '" class="cmb2-media-status">'; 
  54. if ( ! empty( $meta_value ) ) { 
  55.  
  56. if ( $this->is_valid_img_ext( $meta_value ) ) { 
  57.  
  58. if ( $_id_value ) { 
  59. $image = wp_get_attachment_image( $_id_value, $img_size, null, array( 'class' => 'cmb-file-field-image' ) ); 
  60. } else { 
  61. $size = is_array( $img_size ) ? $img_size[0] : 350; 
  62. $image = '<img style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $meta_value . '" alt="" />'; 
  63.  
  64. $output .= $this->img_status_output( array( 
  65. 'image' => $image,  
  66. 'tag' => 'div',  
  67. 'cached_id' => $cached_id,  
  68. ) ); 
  69.  
  70. } else { 
  71.  
  72. $output .= $this->file_status_output( array( 
  73. 'value' => $meta_value,  
  74. 'tag' => 'div',  
  75. 'cached_id' => $cached_id,  
  76. ) ); 
  77.  
  78. $output .= '</div>'; 
  79.  
  80. return $this->rendered( $output ); 
  81.