PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-image.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago index.php 1 year ago
class-acf-field-image.php
468 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_image' ) ) :
4
5 class acf_field_image extends acf_field {
6
7
8 /**
9 * This function will setup the field type data
10 *
11 * @type function
12 * @date 5/03/2014
13 * @since 5.0.0
14 *
15 * @param n/a
16 * @return n/a
17 */
18 function initialize() {
19
20 // vars
21 $this->name = 'image';
22 $this->label = __( 'Image', 'acf' );
23 $this->category = 'content';
24 $this->description = __( 'Uses the native WordPress media picker to upload, or choose images.', 'acf' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-image.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/image/';
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 * description
47 *
48 * @type function
49 * @date 16/12/2015
50 * @since 5.3.2
51 *
52 * @param $post_id (int)
53 * @return $post_id (int)
54 */
55 function input_admin_enqueue_scripts() {
56
57 // localize
58 acf_localize_text(
59 array(
60 'Select Image' => __( 'Select Image', 'acf' ),
61 'Edit Image' => __( 'Edit Image', 'acf' ),
62 'Update Image' => __( 'Update Image', 'acf' ),
63 'All images' => __( 'All images', 'acf' ),
64 )
65 );
66 }
67
68 /**
69 * Renders the field HTML.
70 *
71 * @date 23/01/13
72 * @since 3.6.0
73 *
74 * @param array $field The field settings.
75 * @return void
76 */
77 function render_field( $field ) {
78 $uploader = acf_get_setting( 'uploader' );
79
80 // Enqueue uploader scripts
81 if ( $uploader === 'wp' ) {
82 acf_enqueue_uploader();
83 }
84
85 // Elements and attributes.
86 $value = '';
87 $div_attrs = array(
88 'class' => 'acf-image-uploader',
89 'data-preview_size' => $field['preview_size'],
90 'data-library' => $field['library'],
91 'data-mime_types' => $field['mime_types'],
92 'data-uploader' => $uploader,
93 );
94 $img_attrs = array(
95 'src' => '',
96 'alt' => '',
97 'data-name' => 'image',
98 );
99
100 // Detect value.
101 if ( $field['value'] && is_numeric( $field['value'] ) ) {
102 $image = wp_get_attachment_image_src( $field['value'], $field['preview_size'] );
103 if ( $image ) {
104 $value = $field['value'];
105 $img_attrs['src'] = $image[0];
106 $img_attrs['alt'] = get_post_meta( $field['value'], '_wp_attachment_image_alt', true );
107 $div_attrs['class'] .= ' has-value';
108 }
109 }
110
111 // Add "preview size" max width and height style.
112 // Apply max-width to wrap, and max-height to img for max compatibility with field widths.
113 $size = acf_get_image_size( $field['preview_size'] );
114 $size_w = $size['width'] ? $size['width'] . 'px' : '100%';
115 $size_h = $size['height'] ? $size['height'] . 'px' : '100%';
116 $img_attrs['style'] = sprintf( 'max-height: %s;', $size_h );
117
118 // Render HTML.
119 ?>
120 <div <?php echo acf_esc_attrs( $div_attrs ); ?>>
121 <?php
122 acf_hidden_input(
123 array(
124 'name' => $field['name'],
125 'value' => $value,
126 )
127 );
128 ?>
129 <div class="show-if-value image-wrap" style="max-width: <?php echo esc_attr( $size_w ); ?>">
130 <img <?php echo acf_esc_attrs( $img_attrs ); ?> />
131 <div class="acf-actions -hover">
132 <?php if ( $uploader !== 'basic' ) : ?>
133 <a class="acf-icon -pencil dark" data-name="edit" href="#" title="<?php esc_attr_e( 'Edit', 'acf' ); ?>"></a>
134 <?php endif; ?>
135 <a class="acf-icon -cancel dark" data-name="remove" href="#" title="<?php esc_attr_e( 'Remove', 'acf' ); ?>"></a>
136 </div>
137 </div>
138 <div class="hide-if-value">
139 <?php if ( $uploader === 'basic' ) : ?>
140 <?php if ( $field['value'] && ! is_numeric( $field['value'] ) ) : ?>
141 <div class="acf-error-message"><p><?php echo acf_esc_html( $field['value'] ); ?></p></div>
142 <?php endif; ?>
143 <label class="acf-basic-uploader">
144 <?php
145 acf_file_input(
146 array(
147 'name' => $field['name'],
148 'id' => $field['id'],
149 'key' => $field['key'],
150 )
151 );
152 ?>
153 </label>
154 <?php else : ?>
155 <p><?php esc_html_e( 'No image selected', 'acf' ); ?> <a data-name="add" class="acf-button button" href="#"><?php esc_html_e( 'Add Image', 'acf' ); ?></a></p>
156 <?php endif; ?>
157 </div>
158 </div>
159 <?php
160 }
161
162
163 /**
164 * Create extra options for your field. This is rendered when editing a field.
165 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
166 *
167 * @type action
168 * @since 3.6
169 * @date 23/01/13
170 *
171 * @param $field - an array holding all the field's data
172 */
173 function render_field_settings( $field ) {
174 acf_render_field_setting(
175 $field,
176 array(
177 'label' => __( 'Return Format', 'acf' ),
178 'instructions' => '',
179 'type' => 'radio',
180 'name' => 'return_format',
181 'layout' => 'horizontal',
182 'choices' => array(
183 'array' => __( 'Image Array', 'acf' ),
184 'url' => __( 'Image URL', 'acf' ),
185 'id' => __( 'Image ID', 'acf' ),
186 ),
187 )
188 );
189
190 acf_render_field_setting(
191 $field,
192 array(
193 'label' => __( 'Library', 'acf' ),
194 'instructions' => __( 'Limit the media library choice', 'acf' ),
195 'type' => 'radio',
196 'name' => 'library',
197 'layout' => 'horizontal',
198 'choices' => array(
199 'all' => __( 'All', 'acf' ),
200 'uploadedTo' => __( 'Uploaded to post', 'acf' ),
201 ),
202 )
203 );
204 }
205
206 /**
207 * Renders the field settings used in the "Validation" tab.
208 *
209 * @since 6.0
210 *
211 * @param array $field The field settings array.
212 * @return void
213 */
214 function render_field_validation_settings( $field ) {
215 // Clear numeric settings.
216 $clear = array(
217 'min_width',
218 'min_height',
219 'min_size',
220 'max_width',
221 'max_height',
222 'max_size',
223 );
224
225 foreach ( $clear as $k ) {
226 if ( empty( $field[ $k ] ) ) {
227 $field[ $k ] = '';
228 }
229 }
230
231 acf_render_field_setting(
232 $field,
233 array(
234 'label' => __( 'Minimum', 'acf' ),
235 'hint' => __( 'Restrict which images can be uploaded', 'acf' ),
236 'type' => 'text',
237 'name' => 'min_width',
238 'prepend' => __( 'Width', 'acf' ),
239 'append' => 'px',
240 )
241 );
242
243 acf_render_field_setting(
244 $field,
245 array(
246 'label' => '',
247 'type' => 'text',
248 'name' => 'min_height',
249 'prepend' => __( 'Height', 'acf' ),
250 'append' => 'px',
251 '_append' => 'min_width',
252 )
253 );
254
255 acf_render_field_setting(
256 $field,
257 array(
258 'label' => '',
259 'type' => 'text',
260 'name' => 'min_size',
261 'prepend' => __( 'File size', 'acf' ),
262 'append' => 'MB',
263 '_append' => 'min_width',
264 )
265 );
266
267 acf_render_field_setting(
268 $field,
269 array(
270 'label' => __( 'Maximum', 'acf' ),
271 'hint' => __( 'Restrict which images can be uploaded', 'acf' ),
272 'type' => 'text',
273 'name' => 'max_width',
274 'prepend' => __( 'Width', 'acf' ),
275 'append' => 'px',
276 )
277 );
278
279 acf_render_field_setting(
280 $field,
281 array(
282 'label' => '',
283 'type' => 'text',
284 'name' => 'max_height',
285 'prepend' => __( 'Height', 'acf' ),
286 'append' => 'px',
287 '_append' => 'max_width',
288 )
289 );
290
291 acf_render_field_setting(
292 $field,
293 array(
294 'label' => '',
295 'type' => 'text',
296 'name' => 'max_size',
297 'prepend' => __( 'File size', 'acf' ),
298 'append' => 'MB',
299 '_append' => 'max_width',
300 )
301 );
302
303 acf_render_field_setting(
304 $field,
305 array(
306 'label' => __( 'Allowed File Types', 'acf' ),
307 'instructions' => __( 'Comma separated list. Leave blank for all types', 'acf' ),
308 'type' => 'text',
309 'name' => 'mime_types',
310 )
311 );
312 }
313
314 /**
315 * Renders the field settings used in the "Presentation" tab.
316 *
317 * @since 6.0
318 *
319 * @param array $field The field settings array.
320 * @return void
321 */
322 function render_field_presentation_settings( $field ) {
323 acf_render_field_setting(
324 $field,
325 array(
326 'label' => __( 'Preview Size', 'acf' ),
327 'instructions' => '',
328 'type' => 'select',
329 'name' => 'preview_size',
330 'choices' => acf_get_image_sizes(),
331 )
332 );
333 }
334
335 /**
336 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
337 *
338 * @type filter
339 * @since 3.6
340 * @date 23/01/13
341 *
342 * @param $value (mixed) the value which was loaded from the database
343 * @param $post_id (mixed) the post_id from which the value was loaded
344 * @param $field (array) the field array holding all the field options
345 *
346 * @return $value (mixed) the modified value
347 */
348 function format_value( $value, $post_id, $field ) {
349
350 // bail early if no value
351 if ( empty( $value ) ) {
352 return false;
353 }
354
355 // bail early if not numeric (error message)
356 if ( ! is_numeric( $value ) ) {
357 return false;
358 }
359
360 // convert to int
361 $value = intval( $value );
362
363 // format
364 if ( $field['return_format'] == 'url' ) {
365 return wp_get_attachment_url( $value );
366 } elseif ( $field['return_format'] == 'array' ) {
367 return acf_get_attachment( $value );
368 }
369
370 // return
371 return $value;
372 }
373
374
375 /**
376 * description
377 *
378 * @type function
379 * @date 27/01/13
380 * @since 3.6.0
381 *
382 * @param $vars (array)
383 * @return $vars
384 */
385 function get_media_item_args( $vars ) {
386
387 $vars['send'] = true;
388 return( $vars );
389 }
390
391
392 /**
393 * This filter is appied to the $value before it is updated in the db
394 *
395 * @type filter
396 * @since 3.6
397 * @date 23/01/13
398 *
399 * @param $value - the value which will be saved in the database
400 * @param $post_id - the post_id of which the value will be saved
401 * @param $field - the field array holding all the field options
402 *
403 * @return $value - the modified value
404 */
405 function update_value( $value, $post_id, $field ) {
406
407 return acf_get_field_type( 'file' )->update_value( $value, $post_id, $field );
408 }
409
410
411 /**
412 * This function will validate a basic file input
413 *
414 * @type function
415 * @since 5.0.0
416 *
417 * @param boolean $valid The current validity status.
418 * @param mixed $value The field value.
419 * @param array $field The field array.
420 * @param string $input The name of the input in the POST object.
421 * @return boolean The validity status.
422 */
423 public function validate_value( $valid, $value, $field, $input ) {
424 return acf_get_field_type( 'file' )->validate_value( $valid, $value, $field, $input );
425 }
426
427 /**
428 * Additional validation for the image field when submitted via REST.
429 *
430 * @param boolean $valid The current validity booleean
431 * @param integer $value The value of the field
432 * @param array $field The field array
433 * @return boolean|WP_Error
434 */
435 public function validate_rest_value( $valid, $value, $field ) {
436 return acf_get_field_type( 'file' )->validate_rest_value( $valid, $value, $field );
437 }
438
439 /**
440 * Return the schema array for the REST API.
441 *
442 * @param array $field The field array
443 * @return array
444 */
445 public function get_rest_schema( array $field ) {
446 return acf_get_field_type( 'file' )->get_rest_schema( $field );
447 }
448
449 /**
450 * Apply basic formatting to prepare the value for default REST output.
451 *
452 * @param mixed $value The field value
453 * @param string|integer $post_id The post ID
454 * @param array $field The field array
455 * @return mixed
456 */
457 public function format_value_for_rest( $value, $post_id, array $field ) {
458 return acf_format_numerics( $value );
459 }
460 }
461
462
463 // initialize
464 acf_register_field_type( 'acf_field_image' );
465 endif; // class_exists check
466
467 ?>
468