class-acf-field-image.php
| 1 | <?php |
| 2 | |
| 3 | if( ! class_exists('acf_field_image') ) : |
| 4 | |
| 5 | class acf_field_image extends acf_field { |
| 6 | |
| 7 | |
| 8 | /* |
| 9 | * __construct |
| 10 | * |
| 11 | * This function will setup the field type data |
| 12 | * |
| 13 | * @type function |
| 14 | * @date 5/03/2014 |
| 15 | * @since 5.0.0 |
| 16 | * |
| 17 | * @param n/a |
| 18 | * @return n/a |
| 19 | */ |
| 20 | |
| 21 | function initialize() { |
| 22 | |
| 23 | // vars |
| 24 | $this->name = 'image'; |
| 25 | $this->label = __("Image",'acf'); |
| 26 | $this->category = 'content'; |
| 27 | $this->defaults = array( |
| 28 | 'return_format' => 'array', |
| 29 | 'preview_size' => 'medium', |
| 30 | 'library' => 'all', |
| 31 | 'min_width' => 0, |
| 32 | 'min_height' => 0, |
| 33 | 'min_size' => 0, |
| 34 | 'max_width' => 0, |
| 35 | 'max_height' => 0, |
| 36 | 'max_size' => 0, |
| 37 | 'mime_types' => '' |
| 38 | ); |
| 39 | |
| 40 | // filters |
| 41 | add_filter('get_media_item_args', array($this, 'get_media_item_args')); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * input_admin_enqueue_scripts |
| 48 | * |
| 49 | * description |
| 50 | * |
| 51 | * @type function |
| 52 | * @date 16/12/2015 |
| 53 | * @since 5.3.2 |
| 54 | * |
| 55 | * @param $post_id (int) |
| 56 | * @return $post_id (int) |
| 57 | */ |
| 58 | |
| 59 | function input_admin_enqueue_scripts() { |
| 60 | |
| 61 | // localize |
| 62 | acf_localize_text(array( |
| 63 | 'Select Image' => __('Select Image', 'acf'), |
| 64 | 'Edit Image' => __('Edit Image', 'acf'), |
| 65 | 'Update Image' => __('Update Image', 'acf'), |
| 66 | 'All images' => __('All images', 'acf'), |
| 67 | )); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Renders the field HTML. |
| 72 | * |
| 73 | * @date 23/01/13 |
| 74 | * @since 3.6.0 |
| 75 | * |
| 76 | * @param array $field The field settings. |
| 77 | * @return void |
| 78 | */ |
| 79 | function render_field( $field ) { |
| 80 | $uploader = acf_get_setting('uploader'); |
| 81 | |
| 82 | // Enqueue uploader scripts |
| 83 | if( $uploader === 'wp' ) { |
| 84 | acf_enqueue_uploader(); |
| 85 | } |
| 86 | |
| 87 | // Elements and attributes. |
| 88 | $value = ''; |
| 89 | $div_attrs = array( |
| 90 | 'class' => 'acf-image-uploader', |
| 91 | 'data-preview_size' => $field['preview_size'], |
| 92 | 'data-library' => $field['library'], |
| 93 | 'data-mime_types' => $field['mime_types'], |
| 94 | 'data-uploader' => $uploader |
| 95 | ); |
| 96 | $img_attrs = array( |
| 97 | 'src' => '', |
| 98 | 'alt' => '', |
| 99 | 'data-name' => 'image' |
| 100 | ); |
| 101 | |
| 102 | // Detect value. |
| 103 | if( $field['value'] && is_numeric($field['value']) ) { |
| 104 | $image = wp_get_attachment_image_src( $field['value'], $field['preview_size'] ); |
| 105 | if( $image ) { |
| 106 | $value = $field['value']; |
| 107 | $img_attrs['src'] = $image[0]; |
| 108 | $img_attrs['alt'] = get_post_meta( $field['value'], '_wp_attachment_image_alt', true ); |
| 109 | $div_attrs['class'] .= ' has-value'; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Add "preview size" max width and height style. |
| 114 | // Apply max-width to wrap, and max-height to img for max compatibility with field widths. |
| 115 | $size = acf_get_image_size( $field['preview_size'] ); |
| 116 | $size_w = $size['width'] ? $size['width'] . 'px' : '100%'; |
| 117 | $size_h = $size['height'] ? $size['height'] . 'px' : '100%'; |
| 118 | $img_attrs['style'] = sprintf( 'max-height: %s;', $size_h ); |
| 119 | |
| 120 | // Render HTML. |
| 121 | ?> |
| 122 | <div <?php echo acf_esc_attrs( $div_attrs ); ?>> |
| 123 | <?php acf_hidden_input(array( |
| 124 | 'name' => $field['name'], |
| 125 | 'value' => $value |
| 126 | )); ?> |
| 127 | <div class="show-if-value image-wrap" style="max-width: <?php echo esc_attr( $size_w ); ?>"> |
| 128 | <img <?php echo acf_esc_attrs( $img_attrs ); ?> /> |
| 129 | <div class="acf-actions -hover"> |
| 130 | <?php if( $uploader !== 'basic' ): ?> |
| 131 | <a class="acf-icon -pencil dark" data-name="edit" href="#" title="<?php _e( 'Edit', 'acf' ); ?>"></a> |
| 132 | <?php endif; ?> |
| 133 | <a class="acf-icon -cancel dark" data-name="remove" href="#" title="<?php _e( 'Remove', 'acf' ); ?>"></a> |
| 134 | </div> |
| 135 | </div> |
| 136 | <div class="hide-if-value"> |
| 137 | <?php if( $uploader === 'basic' ): ?> |
| 138 | <?php if( $field['value'] && !is_numeric($field['value']) ): ?> |
| 139 | <div class="acf-error-message"><p><?php echo acf_esc_html( $field['value'] ); ?></p></div> |
| 140 | <?php endif; ?> |
| 141 | <label class="acf-basic-uploader"> |
| 142 | <?php acf_file_input(array( |
| 143 | 'name' => $field['name'], |
| 144 | 'id' => $field['id'] |
| 145 | )); ?> |
| 146 | </label> |
| 147 | <?php else: ?> |
| 148 | <p><?php _e( 'No image selected', 'acf' ); ?> <a data-name="add" class="acf-button button" href="#"><?php _e( 'Add Image', 'acf' ); ?></a></p> |
| 149 | <?php endif; ?> |
| 150 | </div> |
| 151 | </div> |
| 152 | <?php |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /* |
| 157 | * render_field_settings() |
| 158 | * |
| 159 | * Create extra options for your field. This is rendered when editing a field. |
| 160 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 161 | * |
| 162 | * @type action |
| 163 | * @since 3.6 |
| 164 | * @date 23/01/13 |
| 165 | * |
| 166 | * @param $field - an array holding all the field's data |
| 167 | */ |
| 168 | |
| 169 | function render_field_settings( $field ) { |
| 170 | |
| 171 | // clear numeric settings |
| 172 | $clear = array( |
| 173 | 'min_width', |
| 174 | 'min_height', |
| 175 | 'min_size', |
| 176 | 'max_width', |
| 177 | 'max_height', |
| 178 | 'max_size' |
| 179 | ); |
| 180 | |
| 181 | foreach( $clear as $k ) { |
| 182 | |
| 183 | if( empty($field[$k]) ) { |
| 184 | |
| 185 | $field[$k] = ''; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | |
| 192 | // return_format |
| 193 | acf_render_field_setting( $field, array( |
| 194 | 'label' => __('Return Format','acf'), |
| 195 | 'instructions' => '', |
| 196 | 'type' => 'radio', |
| 197 | 'name' => 'return_format', |
| 198 | 'layout' => 'horizontal', |
| 199 | 'choices' => array( |
| 200 | 'array' => __("Image Array",'acf'), |
| 201 | 'url' => __("Image URL",'acf'), |
| 202 | 'id' => __("Image ID",'acf') |
| 203 | ) |
| 204 | )); |
| 205 | |
| 206 | |
| 207 | // preview_size |
| 208 | acf_render_field_setting( $field, array( |
| 209 | 'label' => __('Preview Size','acf'), |
| 210 | 'instructions' => '', |
| 211 | 'type' => 'select', |
| 212 | 'name' => 'preview_size', |
| 213 | 'choices' => acf_get_image_sizes() |
| 214 | )); |
| 215 | |
| 216 | |
| 217 | // library |
| 218 | acf_render_field_setting( $field, array( |
| 219 | 'label' => __('Library','acf'), |
| 220 | 'instructions' => __('Limit the media library choice','acf'), |
| 221 | 'type' => 'radio', |
| 222 | 'name' => 'library', |
| 223 | 'layout' => 'horizontal', |
| 224 | 'choices' => array( |
| 225 | 'all' => __('All', 'acf'), |
| 226 | 'uploadedTo' => __('Uploaded to post', 'acf') |
| 227 | ) |
| 228 | )); |
| 229 | |
| 230 | |
| 231 | // min |
| 232 | acf_render_field_setting( $field, array( |
| 233 | 'label' => __('Minimum','acf'), |
| 234 | 'instructions' => __('Restrict which images can be uploaded','acf'), |
| 235 | 'type' => 'text', |
| 236 | 'name' => 'min_width', |
| 237 | 'prepend' => __('Width', 'acf'), |
| 238 | 'append' => 'px', |
| 239 | )); |
| 240 | |
| 241 | acf_render_field_setting( $field, array( |
| 242 | 'label' => '', |
| 243 | 'type' => 'text', |
| 244 | 'name' => 'min_height', |
| 245 | 'prepend' => __('Height', 'acf'), |
| 246 | 'append' => 'px', |
| 247 | '_append' => 'min_width' |
| 248 | )); |
| 249 | |
| 250 | acf_render_field_setting( $field, array( |
| 251 | 'label' => '', |
| 252 | 'type' => 'text', |
| 253 | 'name' => 'min_size', |
| 254 | 'prepend' => __('File size', 'acf'), |
| 255 | 'append' => 'MB', |
| 256 | '_append' => 'min_width' |
| 257 | )); |
| 258 | |
| 259 | |
| 260 | // max |
| 261 | acf_render_field_setting( $field, array( |
| 262 | 'label' => __('Maximum','acf'), |
| 263 | 'instructions' => __('Restrict which images can be uploaded','acf'), |
| 264 | 'type' => 'text', |
| 265 | 'name' => 'max_width', |
| 266 | 'prepend' => __('Width', 'acf'), |
| 267 | 'append' => 'px', |
| 268 | )); |
| 269 | |
| 270 | acf_render_field_setting( $field, array( |
| 271 | 'label' => '', |
| 272 | 'type' => 'text', |
| 273 | 'name' => 'max_height', |
| 274 | 'prepend' => __('Height', 'acf'), |
| 275 | 'append' => 'px', |
| 276 | '_append' => 'max_width' |
| 277 | )); |
| 278 | |
| 279 | acf_render_field_setting( $field, array( |
| 280 | 'label' => '', |
| 281 | 'type' => 'text', |
| 282 | 'name' => 'max_size', |
| 283 | 'prepend' => __('File size', 'acf'), |
| 284 | 'append' => 'MB', |
| 285 | '_append' => 'max_width' |
| 286 | )); |
| 287 | |
| 288 | |
| 289 | // allowed type |
| 290 | acf_render_field_setting( $field, array( |
| 291 | 'label' => __('Allowed file types','acf'), |
| 292 | 'instructions' => __('Comma separated list. Leave blank for all types','acf'), |
| 293 | 'type' => 'text', |
| 294 | 'name' => 'mime_types', |
| 295 | )); |
| 296 | |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /* |
| 301 | * format_value() |
| 302 | * |
| 303 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
| 304 | * |
| 305 | * @type filter |
| 306 | * @since 3.6 |
| 307 | * @date 23/01/13 |
| 308 | * |
| 309 | * @param $value (mixed) the value which was loaded from the database |
| 310 | * @param $post_id (mixed) the $post_id from which the value was loaded |
| 311 | * @param $field (array) the field array holding all the field options |
| 312 | * |
| 313 | * @return $value (mixed) the modified value |
| 314 | */ |
| 315 | |
| 316 | function format_value( $value, $post_id, $field ) { |
| 317 | |
| 318 | // bail early if no value |
| 319 | if( empty($value) ) return false; |
| 320 | |
| 321 | |
| 322 | // bail early if not numeric (error message) |
| 323 | if( !is_numeric($value) ) return false; |
| 324 | |
| 325 | |
| 326 | // convert to int |
| 327 | $value = intval($value); |
| 328 | |
| 329 | |
| 330 | // format |
| 331 | if( $field['return_format'] == 'url' ) { |
| 332 | |
| 333 | return wp_get_attachment_url( $value ); |
| 334 | |
| 335 | } elseif( $field['return_format'] == 'array' ) { |
| 336 | |
| 337 | return acf_get_attachment( $value ); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | |
| 342 | // return |
| 343 | return $value; |
| 344 | |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /* |
| 349 | * get_media_item_args |
| 350 | * |
| 351 | * description |
| 352 | * |
| 353 | * @type function |
| 354 | * @date 27/01/13 |
| 355 | * @since 3.6.0 |
| 356 | * |
| 357 | * @param $vars (array) |
| 358 | * @return $vars |
| 359 | */ |
| 360 | |
| 361 | function get_media_item_args( $vars ) { |
| 362 | |
| 363 | $vars['send'] = true; |
| 364 | return($vars); |
| 365 | |
| 366 | } |
| 367 | |
| 368 | |
| 369 | /* |
| 370 | * update_value() |
| 371 | * |
| 372 | * This filter is appied to the $value before it is updated in the db |
| 373 | * |
| 374 | * @type filter |
| 375 | * @since 3.6 |
| 376 | * @date 23/01/13 |
| 377 | * |
| 378 | * @param $value - the value which will be saved in the database |
| 379 | * @param $post_id - the $post_id of which the value will be saved |
| 380 | * @param $field - the field array holding all the field options |
| 381 | * |
| 382 | * @return $value - the modified value |
| 383 | */ |
| 384 | |
| 385 | function update_value( $value, $post_id, $field ) { |
| 386 | |
| 387 | return acf_get_field_type('file')->update_value( $value, $post_id, $field ); |
| 388 | |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /* |
| 393 | * validate_value |
| 394 | * |
| 395 | * This function will validate a basic file input |
| 396 | * |
| 397 | * @type function |
| 398 | * @date 11/02/2014 |
| 399 | * @since 5.0.0 |
| 400 | * |
| 401 | * @param $post_id (int) |
| 402 | * @return $post_id (int) |
| 403 | */ |
| 404 | |
| 405 | function validate_value( $valid, $value, $field, $input ){ |
| 406 | |
| 407 | return acf_get_field_type('file')->validate_value( $valid, $value, $field, $input ); |
| 408 | |
| 409 | } |
| 410 | |
| 411 | } |
| 412 | |
| 413 | |
| 414 | // initialize |
| 415 | acf_register_field_type( 'acf_field_image' ); |
| 416 | |
| 417 | endif; // class_exists check |
| 418 | |
| 419 | ?> |