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
class CMB2_Type_File_Base extends CMB2_Type_Text {
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 );
}
$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();
return (bool) apply_filters( "cmb2_{$field_id}_is_valid_img_ext", $is_valid, $file, $file_ext );
}
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'] : ''
);
}
public function file_status_output( $args ) {
return sprintf( '<%1$s class="file-status cmb2-media-item"><span>%2$s <strong>%3$s</strong></span> (<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'] : ''
);
}
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> (<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> (<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
}
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'] = '';
if ( $named_size = CMB2_Utils::get_named_size( $img_size ) ) {
$data['name'] = $named_size;
}
} else {
$image_sizes = CMB2_Utils::get_available_image_sizes();
if ( 'thumb' == $img_size ) {
$img_size = 'thumbnail';
}
if ( ! array_key_exists( $img_size, $image_sizes ) ) {
$img_size = $fallback;
}
$data['width'] = intval( $image_sizes[ $img_size ]['width'] );
$data['height'] = intval( $image_sizes[ $img_size ]['height'] );
$data['name'] = $img_size;
}
return $data;
}
public static function prepare_image_sizes_for_js( $response, $attachment, $meta ) {
foreach ( CMB2_Utils::get_available_image_sizes() as $size => $info ) {
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;
}
}