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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 
<?php
/**
 * CMB File base 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_Base extends CMB2_Type_Text {

    /**
     * Determines if a file has a valid image extension
     *
     * @since  1.0.0
     * @param  string $file File url
     * @return bool         Whether file has a valid image extension
     */
    public function is_valid_img_ext( $file, $blah = false ) {
        $file_ext = CMB2_Utils::get_file_ext( $file );

        $valid_types = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'ico', 'icon' );

        $allowed = get_allowed_mime_types();
        if ( ! empty( $allowed ) ) {
            foreach ( (array) $allowed as $type => $mime) {
                if ( 0 === strpos( $mime, 'image/' ) ) {
                    $types = explode( '|', $type );
                    $valid_types = array_merge( $valid_types, $types );
                }
            }
            $valid_types = array_unique( $valid_types );
        }

        /**
         * Which image types are considered valid image file extensions.
         *
         * @since 2.0.9
         *
         * @param array $valid_types The valid image file extensions.
         */
        $is_valid_types = apply_filters( 'cmb2_valid_img_types', $valid_types );
        $is_valid = $file_ext && in_array( $file_ext, (array) $is_valid_types );
        $field_id = $this->field->id();

        /**
         * Filter for determining if a field value has a valid image file-type extension.
         *
         * The dynamic portion of the hook name, $field_id, refers to the field id attribute.
         *
         * @since 2.0.9
         *
         * @param bool   $is_valid Whether field value has a valid image file-type extension.
         * @param string $file     File url.
         * @param string $file_ext File extension.
         */
        return (bool) apply_filters( "cmb2_{$field_id}_is_valid_img_ext", $is_valid, $file, $file_ext );
    }

    /**
     * file/file_list image wrap
     *
     * @since  2.0.2
     * @param  array $args Array of arguments for output
     * @return string       Image wrap output
     */
    public function img_status_output( $args ) {
        return sprintf( '<%1$s class="img-status cmb2-media-item">%2$s<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button"%3$s>%4$s</a></p>%5$s</%1$s>',
            $args['tag'],
            $args['image'],
            isset( $args['cached_id'] ) ? ' rel="' . esc_attr( $args['cached_id'] ) . '"' : '',
            esc_html( $this->_text( 'remove_image_text', esc_html__( 'Remove Image', 'cmb2' ) ) ),
            isset( $args['id_input'] ) ? $args['id_input'] : ''
        );
    }

    /**
     * file/file_list file wrap
     *
     * @since  2.0.2
     * @param  array $args Array of arguments for output
     * @return string       File wrap output
     */
    public function file_status_output( $args ) {
        return sprintf( '<%1$s class="file-status cmb2-media-item"><span>%2$s <strong>%3$s</strong></span>&nbsp;&nbsp; (<a href="%4$s" target="_blank" rel="external">%5$s</a> / <a href="#" class="cmb2-remove-file-button"%6$s>%7$s</a>)%8$s</%1$s>',
            $args['tag'],
            esc_html( $this->_text( 'file_text', esc_html__( 'File:', 'cmb2' ) ) ),
            esc_html( CMB2_Utils::get_file_name_from_path( $args['value'] ) ),
            esc_url( $args['value'] ),
            esc_html( $this->_text( 'file_download_text', esc_html__( 'Download', 'cmb2' ) ) ),
            isset( $args['cached_id'] ) ? ' rel="' . esc_attr( $args['cached_id'] ) . '"' : '',
            esc_html( $this->_text( 'remove_text', esc_html__( 'Remove', 'cmb2' ) ) ),
            isset( $args['id_input'] ) ? $args['id_input'] : ''
        );
    }

    /**
     * Outputs the file/file_list underscore Javascript templates in the footer.
     *
     * @since  2.2.4
     * @return void
     */
    public static function output_js_underscore_templates() {
        ?>
        <script type="text/html" id="tmpl-cmb2-single-image">
            <div class="img-status cmb2-media-item">
                <img width="{{ data.sizeWidth }}" height="{{ data.sizeHeight }}" src="{{ data.sizeUrl }}" class="cmb-file-field-image" alt="{{ data.filename }}" title="{{ data.filename }}" />
                <p><a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}">{{ data.stringRemoveImage }}</a></p>
            </div>
        </script>
        <script type="text/html" id="tmpl-cmb2-single-file">
            <div class="file-status cmb2-media-item">
                <span>{{ data.stringFile }} <strong>{{ data.filename }}</strong></span>&nbsp;&nbsp; (<a href="{{ data.url }}" target="_blank" rel="external">{{ data.stringDownload }}</a> / <a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}">{{ data.stringRemoveFile }}</a>)
            </div>
        </script>
        <script type="text/html" id="tmpl-cmb2-list-image">
            <li class="img-status cmb2-media-item">
                <img width="{{ data.sizeWidth }}" height="{{ data.sizeHeight }}" src="{{ data.sizeUrl }}" class="cmb-file_list-field-image" alt="{{ data.filename }}">
                <p><a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}[{{ data.id }}]">{{ data.stringRemoveImage }}</a></p>
                <input type="hidden" id="filelist-{{ data.id }}" data-id="{{ data.id }}" name="{{ data.mediaFieldName }}[{{ data.id }}]" value="{{ data.url }}">
            </li>
        </script>
        <script type="text/html" id="tmpl-cmb2-list-file">
            <li class="file-status cmb2-media-item">
                <span>{{ data.stringFile }} <strong>{{ data.filename }}</strong></span>&nbsp;&nbsp; (<a href="{{ data.url }}" target="_blank" rel="external">{{ data.stringDownload }}</a> / <a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}[{{ data.id }}]">{{ data.stringRemoveFile }}</a>)
                <input type="hidden" id="filelist-{{ data.id }}" data-id="{{ data.id }}" name="{{ data.mediaFieldName }}[{{ data.id }}]" value="{{ data.url }}">
            </li>
        </script>
        <?php
    }

    /**
     * Utility method to return an array of meta data for a registered image size
     *
     * Uses CMB2_Utils::get_named_size() to get the closest available named size
     * from an array of width and height values and CMB2_Utils::get_available_image_sizes()
     * to get the meta data associated with a named size.
     *
     * @since  2.2.4
     * @param  array|string $img_size  Image size. Accepts an array of width and height (in that order)
     * @param  string       $fallback  Size to use if the supplied named size doesn't exist
     * @return array                   Array containing the image size meta data
     *    $size = (
     *      'width'  => (int) image size width
     *      'height' => (int) image size height
     *      'name'   => (string) e.g. 'thumbnail'
     *    )
     */
    static function get_image_size_data( $img_size = '', $fallback = 'thumbnail' ) {
        $data = array();

        if ( is_array( $img_size ) ) {
            $data['width']  = intval( $img_size[0] );
            $data['height'] = intval( $img_size[1] );
            $data['name']   = '';

            // Try and get the closest named size from our array of dimensions
            if ( $named_size = CMB2_Utils::get_named_size( $img_size ) ) {
                $data['name'] = $named_size;
            }
        } else {

            $image_sizes = CMB2_Utils::get_available_image_sizes();

            // The 'thumb' alias, which works elsewhere, doesn't work in the wp.media uploader
            if ( 'thumb' == $img_size ) {
                $img_size = 'thumbnail';
            }

            // Named size doesn't exist, use $fallback
            if ( ! array_key_exists( $img_size, $image_sizes ) ) {
                $img_size = $fallback;
            }

            // Get image dimensions from named sizes
            $data['width']  = intval( $image_sizes[ $img_size ]['width'] );
            $data['height'] = intval( $image_sizes[ $img_size ]['height'] );
            $data['name']   = $img_size;
        }

        return $data;
    }

    /**
     * Filters attachment data prepared for JavaScript.
     *
     * Adds the url, width, height, and orientation for custom sizes to the JavaScript
     * object returned by the wp.media uploader. Hooked to 'wp_prepare_attachment_for_js'.
     *
     * @since  2.2.4
     * @param  array      $response   Array of prepared attachment data
     * @param  int|object $attachment Attachment ID or object
     * @param  array      $meta       Array of attachment meta data ( from wp_get_attachment_metadata() )
     * @return array      filtered $response array
     */
    public static function prepare_image_sizes_for_js( $response, $attachment, $meta ) {
        foreach ( CMB2_Utils::get_available_image_sizes() as $size => $info ) {

            // registered image size exists for this attachment
            if ( isset( $meta['sizes'][ $size ] ) ) {

                $attachment_url = wp_get_attachment_url( $attachment->ID );
                $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
                $size_meta = $meta['sizes'][ $size ];

                $response['sizes'][ $size ] = array(
                    'url'         => $base_url . $size_meta['file'],
                    'height'      => $size_meta['height'],
                    'width'       => $size_meta['width'],
                    'orientation' => $size_meta['height'] > $size_meta['width'] ? 'portrait' : 'landscape',
                );
            }
        }

        return $response;
    }

}
CMB2 Documentation API documentation generated by ApiGen