CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_Integration_Box
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils

Hooks

  • Hook Reference
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 
<?php
/**
 * CMB file field type
 *
 * @since  2.2.2
 *
 * @category  WordPress_Plugin
 * @package   CMB2
 * @author    CMB2 team
 * @license   GPL-2.0+
 * @link      https://cmb2.io
 */
class CMB2_Type_File extends CMB2_Type_File_Base {

    /**
     * Handles outputting an 'file' field
     *
     * @param  array $args Override arguments.
     * @return string      Form input element
     */
    public function render( $args = array() ) {
        $args    = empty( $args ) ? $this->args : $args;
        $field   = $this->field;
        $options = (array) $field->options();

        $a = $this->args = $this->parse_args( 'file', array(
            'class'           => 'cmb2-upload-file regular-text',
            'id'              => $this->_id(),
            'name'            => $this->_name(),
            'value'           => $field->escaped_value(),
            'id_value'        => null,
            'desc'            => $this->_desc( true ),
            'size'            => 45,
            'js_dependencies' => 'media-editor',
            'preview_size'    => $field->args( 'preview_size' ),
            'query_args'      => $field->args( 'query_args' ),

            // if options array and 'url' => false, then hide the url field.
            'type'            => array_key_exists( 'url', $options ) && false === $options['url']
                ? 'hidden'
                : 'text',
        ), $args );

        // get an array of image size meta data, fallback to 'large'.
        $this->args['img_size_data'] = $img_size_data = parent::get_image_size_data(
            $a['preview_size'],
            'large'
        );

        $output = '';

        $output .= parent::render( array(
            'type'             => $a['type'],
            'class'            => $a['class'],
            'value'            => $a['value'],
            'id'               => $a['id'],
            'name'             => $a['name'],
            'size'             => $a['size'],
            'desc'             => '',
            'data-previewsize' => sprintf( '[%d,%d]', $img_size_data['width'], $img_size_data['height'] ),
            'data-sizename'    => $img_size_data['name'],
            'data-queryargs'   => ! empty( $a['query_args'] ) ? json_encode( $a['query_args'] ) : '',
            'js_dependencies'  => $a['js_dependencies'],
        ) );

        // Now remove the data-iterator attribute if it exists.
        // (Possible if being used within a custom field)
        // This is not elegant, but compensates for CMB2_Types::_id
        // automagically & inelegantly adding the data-iterator attribute.
        // Single responsibility principle? pffft.
        $parts            = explode( '"', $this->args['id'] );
        $this->args['id'] = $parts[0];

        $output .= sprintf(
            '<input class="cmb2-upload-button button-secondary" type="button" value="%1$s" />',
            esc_attr( $this->_text( 'add_upload_file_text', esc_html__( 'Add or Upload File', 'cmb2' ) ) )
        );

        $output .= $a['desc'];
        $output .= $this->get_id_field_output();

        $output .= '<div id="' . esc_attr( $field->id() ) . '-status" class="cmb2-media-status">';
        if ( ! empty( $a['value'] ) ) {
            $output .= $this->get_file_preview_output();
        }
        $output .= '</div>';

        return $this->rendered( $output );
    }

    /**
     * Return attempted file preview output for a provided file.
     *
     * @since 2.2.5
     *
     * @return string
     */
    public function get_file_preview_output() {
        if ( ! $this->is_valid_img_ext( $this->args['value'] ) ) {

            return $this->file_status_output( array(
                'value'     => $this->args['value'],
                'tag'       => 'div',
                'cached_id' => $this->args['id'],
            ) );
        }

        if ( $this->args['id_value'] ) {
            $image = wp_get_attachment_image( $this->args['id_value'], $this->args['preview_size'], null, array(
                'class' => 'cmb-file-field-image',
            ) );
        } else {
            $image = '<img style="max-width: ' . absint( $this->args['img_size_data']['width'] ) . 'px; width: 100%;" src="' . esc_url( $this->args['value'] ) . '" class="cmb-file-field-image" alt="" />';
        }

        return $this->img_status_output( array(
            'image'     => $image,
            'tag'       => 'div',
            'cached_id' => $this->args['id'],
        ) );
    }

    /**
     * Return field ID output as a hidden field.
     *
     * @since 2.2.5
     *
     * @return string
     */
    public function get_id_field_output() {
        $field = $this->field;

        /*
         * A little bit of magic (tsk tsk) replacing the $this->types->field object,
         * So that the render function is using the proper field object.
         */
        $this->types->field = $this->get_id_field();

        $output = parent::render( array(
            'type'  => 'hidden',
            'class' => 'cmb2-upload-file-id',
            'value' => $this->types->field->value,
            'desc'  => '',
        ) );

        // We need to put the original field object back
        // or other fields in a custom field will be broken.
        $this->types->field = $field;

        return $output;
    }

    /**
     * Return field ID data.
     *
     * @since 2.2.5
     *
     * @return mixed
     */
    public function get_id_field() {

        // reset field args for attachment id.
        $args = array(
            // if we're looking at a file in a group, we need to get the non-prefixed id.
            'id' => ( $this->field->group ? $this->field->args( '_id' ) : $this->args['id'] ) . '_id',
            'disable_hash_data_attribute' => true,
        );

        // and get new field object
        // (need to set it to the types field property).
        $id_field = $this->field->get_field_clone( $args );

        $id_value = absint( null !== $this->args['id_value'] ? $this->args['id_value'] : $id_field->escaped_value() );

        // we don't want to output "0" as a value.
        if ( ! $id_value ) {
            $id_value = '';
        }

        // if there is no id saved yet, try to get it from the url.
        if ( $this->args['value'] && ! $id_value ) {
            $id_value = CMB2_Utils::image_id_from_url( esc_url_raw( $this->args['value'] ) );
        }

        $id_field->value = $id_value;

        return $id_field;
    }

}
CMB2 Documentation API documentation generated by ApiGen