PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.9.1
Secure Custom Fields v6.9.1
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
FlexibleContent 2 months ago class-acf-field-accordion.php 2 months ago class-acf-field-button-group.php 2 months ago class-acf-field-checkbox.php 2 days ago class-acf-field-clone.php 2 months ago class-acf-field-color_picker.php 2 months ago class-acf-field-date_picker.php 2 months ago class-acf-field-date_time_picker.php 2 months ago class-acf-field-email.php 2 months ago class-acf-field-file.php 2 months ago class-acf-field-flexible-content.php 1 week ago class-acf-field-gallery.php 3 weeks ago class-acf-field-google-map.php 2 months ago class-acf-field-group.php 2 months ago class-acf-field-icon_picker.php 7 months ago class-acf-field-image.php 2 months ago class-acf-field-link.php 2 months ago class-acf-field-message.php 1 year ago class-acf-field-nav-menu.php 1 week ago class-acf-field-number.php 2 months ago class-acf-field-oembed.php 3 weeks ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 3 weeks ago class-acf-field-password.php 2 months ago class-acf-field-post_object.php 3 weeks ago class-acf-field-radio.php 2 days ago class-acf-field-range.php 2 months ago class-acf-field-relationship.php 3 weeks ago class-acf-field-repeater.php 3 weeks ago class-acf-field-select.php 2 days ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 3 weeks ago class-acf-field-text.php 3 weeks ago class-acf-field-textarea.php 3 weeks ago class-acf-field-time_picker.php 2 months ago class-acf-field-true_false.php 2 months ago class-acf-field-url.php 3 weeks ago class-acf-field-user.php 3 weeks ago class-acf-field-wysiwyg.php 2 months ago class-acf-field.php 2 months ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-image.php
551 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 ACF 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', 'secure-custom-fields' );
23 $this->category = 'content';
24 $this->description = __( 'Uses the native WordPress media picker to upload, or choose images.', 'secure-custom-fields' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-image.png';
26 $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/image/';
27 $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/image/image-tutorial/';
28 $this->defaults = array(
29 'return_format' => 'array',
30 'preview_size' => 'medium',
31 'library' => 'all',
32 'min_width' => 0,
33 'min_height' => 0,
34 'min_size' => 0,
35 'max_width' => 0,
36 'max_height' => 0,
37 'max_size' => 0,
38 'mime_types' => '',
39 );
40
41 // filters
42 add_filter( 'get_media_item_args', array( $this, 'get_media_item_args' ) );
43 }
44
45
46 /**
47 * description
48 *
49 * @type function
50 * @date 16/12/2015
51 * @since ACF 5.3.2
52 *
53 * @param $post_id (int)
54 * @return $post_id (int)
55 */
56 function input_admin_enqueue_scripts() {
57
58 // localize
59 acf_localize_text(
60 array(
61 'Select Image' => __( 'Select Image', 'secure-custom-fields' ),
62 'Edit Image' => __( 'Edit Image', 'secure-custom-fields' ),
63 'Update Image' => __( 'Update Image', 'secure-custom-fields' ),
64 'All images' => __( 'All images', 'secure-custom-fields' ),
65 )
66 );
67 }
68
69 /**
70 * Renders the field HTML.
71 *
72 * @date 23/01/13
73 * @since ACF 3.6.0
74 *
75 * @param array $field The field settings.
76 * @return void
77 */
78 function render_field( $field ) {
79 $uploader = acf_get_setting( 'uploader' );
80
81 // Enqueue uploader scripts
82 if ( $uploader === 'wp' ) {
83 acf_enqueue_uploader();
84 }
85
86 // Elements and attributes.
87 $value = '';
88 $div_attrs = array(
89 'class' => 'acf-image-uploader',
90 'data-preview_size' => $field['preview_size'],
91 'data-library' => $field['library'],
92 'data-mime_types' => $field['mime_types'],
93 'data-uploader' => $uploader,
94 );
95 $img_attrs = array(
96 'src' => '',
97 'alt' => '',
98 'data-name' => 'image',
99 );
100
101 // Detect value.
102 if ( $field['value'] && is_numeric( $field['value'] ) ) {
103 $image = wp_get_attachment_image_src( $field['value'], $field['preview_size'] );
104 if ( $image ) {
105 $value = $field['value'];
106 $img_attrs['src'] = $image[0];
107 $img_attrs['alt'] = get_post_meta( $field['value'], '_wp_attachment_image_alt', true );
108 $div_attrs['class'] .= ' has-value';
109 }
110 }
111
112 // Add "preview size" max width and height style.
113 // Apply max-width to wrap, and max-height to img for max compatibility with field widths.
114 $size = acf_get_image_size( $field['preview_size'] );
115 $size_w = $size['width'] ? $size['width'] . 'px' : '100%';
116 $size_h = $size['height'] ? $size['height'] . 'px' : '100%';
117 $img_attrs['style'] = sprintf( 'max-height: %s;', $size_h );
118
119 // Render HTML.
120 ?>
121 <div <?php echo acf_esc_attrs( $div_attrs ); ?>>
122 <?php
123 acf_hidden_input(
124 array(
125 'name' => $field['name'],
126 'value' => $value,
127 )
128 );
129 ?>
130 <div class="show-if-value image-wrap" style="max-width: <?php echo esc_attr( $size_w ); ?>" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Selected image. Press tab to access image options.', 'secure-custom-fields' ); ?>">
131 <img <?php echo acf_esc_attrs( $img_attrs ); ?> />
132 <div class="acf-actions -hover">
133 <?php if ( 'basic' !== $uploader ) : ?>
134 <a class="acf-icon -pencil dark" data-name="edit" href="#" title="<?php esc_attr_e( 'Edit', 'secure-custom-fields' ); ?>" aria-label="<?php esc_attr_e( 'Edit image', 'secure-custom-fields' ); ?>"></a>
135 <?php endif; ?>
136 <a class="acf-icon -cancel dark" data-name="remove" href="#" title="<?php esc_attr_e( 'Remove', 'secure-custom-fields' ); ?>" aria-label="<?php esc_attr_e( 'Remove image', 'secure-custom-fields' ); ?>"></a>
137 </div>
138 </div>
139 <div class="hide-if-value">
140 <?php if ( 'basic' === $uploader ) : ?>
141 <?php if ( $field['value'] && ! is_numeric( $field['value'] ) ) : ?>
142 <div class="acf-error-message"><p><?php echo acf_esc_html( $field['value'] ); ?></p></div>
143 <?php endif; ?>
144 <label class="acf-basic-uploader">
145 <?php
146 acf_file_input(
147 array(
148 'name' => $field['name'],
149 'id' => $field['id'],
150 'key' => $field['key'],
151 )
152 );
153 ?>
154 </label>
155 <?php else : ?>
156 <p><?php esc_html_e( 'No image selected', 'secure-custom-fields' ); ?> <a data-name="add" class="acf-button button" href="#"><?php esc_html_e( 'Add Image', 'secure-custom-fields' ); ?></a></p>
157 <?php endif; ?>
158 </div>
159 </div>
160 <?php
161 }
162
163
164 /**
165 * Create extra options for your field. This is rendered when editing a field.
166 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
167 *
168 * @type action
169 * @since ACF 3.6
170 * @date 23/01/13
171 *
172 * @param $field - an array holding all the field's data
173 */
174 function render_field_settings( $field ) {
175 acf_render_field_setting(
176 $field,
177 array(
178 'label' => __( 'Return Format', 'secure-custom-fields' ),
179 'instructions' => '',
180 'type' => 'radio',
181 'name' => 'return_format',
182 'layout' => 'horizontal',
183 'choices' => array(
184 'array' => __( 'Image Array', 'secure-custom-fields' ),
185 'url' => __( 'Image URL', 'secure-custom-fields' ),
186 'id' => __( 'Image ID', 'secure-custom-fields' ),
187 ),
188 )
189 );
190
191 acf_render_field_setting(
192 $field,
193 array(
194 'label' => __( 'Library', 'secure-custom-fields' ),
195 'instructions' => __( 'Limit the media library choice', 'secure-custom-fields' ),
196 'type' => 'radio',
197 'name' => 'library',
198 'layout' => 'horizontal',
199 'choices' => array(
200 'all' => __( 'All', 'secure-custom-fields' ),
201 'uploadedTo' => __( 'Uploaded to post', 'secure-custom-fields' ),
202 ),
203 )
204 );
205 }
206
207 /**
208 * Renders the field settings used in the "Validation" tab.
209 *
210 * @since ACF 6.0
211 *
212 * @param array $field The field settings array.
213 * @return void
214 */
215 function render_field_validation_settings( $field ) {
216 // Clear numeric settings.
217 $clear = array(
218 'min_width',
219 'min_height',
220 'min_size',
221 'max_width',
222 'max_height',
223 'max_size',
224 );
225
226 foreach ( $clear as $k ) {
227 if ( empty( $field[ $k ] ) ) {
228 $field[ $k ] = '';
229 }
230 }
231
232 acf_render_field_setting(
233 $field,
234 array(
235 'label' => __( 'Minimum', 'secure-custom-fields' ),
236 'hint' => __( 'Restrict which images can be uploaded', 'secure-custom-fields' ),
237 'type' => 'text',
238 'name' => 'min_width',
239 'prepend' => __( 'Width', 'secure-custom-fields' ),
240 'append' => 'px',
241 )
242 );
243
244 acf_render_field_setting(
245 $field,
246 array(
247 'label' => '',
248 'type' => 'text',
249 'name' => 'min_height',
250 'prepend' => __( 'Height', 'secure-custom-fields' ),
251 'append' => 'px',
252 '_append' => 'min_width',
253 )
254 );
255
256 acf_render_field_setting(
257 $field,
258 array(
259 'label' => '',
260 'type' => 'text',
261 'name' => 'min_size',
262 'prepend' => __( 'File size', 'secure-custom-fields' ),
263 'append' => 'MB',
264 '_append' => 'min_width',
265 )
266 );
267
268 acf_render_field_setting(
269 $field,
270 array(
271 'label' => __( 'Maximum', 'secure-custom-fields' ),
272 'hint' => __( 'Restrict which images can be uploaded', 'secure-custom-fields' ),
273 'type' => 'text',
274 'name' => 'max_width',
275 'prepend' => __( 'Width', 'secure-custom-fields' ),
276 'append' => 'px',
277 )
278 );
279
280 acf_render_field_setting(
281 $field,
282 array(
283 'label' => '',
284 'type' => 'text',
285 'name' => 'max_height',
286 'prepend' => __( 'Height', 'secure-custom-fields' ),
287 'append' => 'px',
288 '_append' => 'max_width',
289 )
290 );
291
292 acf_render_field_setting(
293 $field,
294 array(
295 'label' => '',
296 'type' => 'text',
297 'name' => 'max_size',
298 'prepend' => __( 'File size', 'secure-custom-fields' ),
299 'append' => 'MB',
300 '_append' => 'max_width',
301 )
302 );
303
304 acf_render_field_setting(
305 $field,
306 array(
307 'label' => __( 'Allowed File Types', 'secure-custom-fields' ),
308 'instructions' => __( 'Comma separated list. Leave blank for all types', 'secure-custom-fields' ),
309 'type' => 'text',
310 'name' => 'mime_types',
311 )
312 );
313 }
314
315 /**
316 * Renders the field settings used in the "Presentation" tab.
317 *
318 * @since ACF 6.0
319 *
320 * @param array $field The field settings array.
321 * @return void
322 */
323 function render_field_presentation_settings( $field ) {
324 acf_render_field_setting(
325 $field,
326 array(
327 'label' => __( 'Preview Size', 'secure-custom-fields' ),
328 'instructions' => '',
329 'type' => 'select',
330 'name' => 'preview_size',
331 'choices' => acf_get_image_sizes(),
332 )
333 );
334 }
335
336 /**
337 * This filter is applied to the $value after it is loaded from the db and before it is returned to the template
338 *
339 * @type filter
340 * @since ACF 3.6
341 * @date 23/01/13
342 *
343 * @param $value (mixed) the value which was loaded from the database
344 * @param $post_id (mixed) the post_id from which the value was loaded
345 * @param $field (array) the field array holding all the field options
346 *
347 * @return $value (mixed) the modified value
348 */
349 function format_value( $value, $post_id, $field ) {
350
351 // bail early if no value
352 if ( empty( $value ) ) {
353 return false;
354 }
355
356 // bail early if not numeric (error message)
357 if ( ! is_numeric( $value ) ) {
358 return false;
359 }
360
361 // convert to int
362 $value = intval( $value );
363
364 // format
365 if ( $field['return_format'] == 'url' ) {
366 return wp_get_attachment_url( $value );
367 } elseif ( $field['return_format'] == 'array' ) {
368 return acf_get_attachment( $value );
369 }
370
371 // return
372 return $value;
373 }
374
375
376 /**
377 * description
378 *
379 * @type function
380 * @date 27/01/13
381 * @since ACF 3.6.0
382 *
383 * @param $vars (array)
384 * @return $vars
385 */
386 function get_media_item_args( $vars ) {
387
388 $vars['send'] = true;
389 return( $vars );
390 }
391
392
393 /**
394 * This filter is applied to the $value before it is updated in the db
395 *
396 * @type filter
397 * @since ACF 3.6
398 * @date 23/01/13
399 *
400 * @param $value - the value which will be saved in the database
401 * @param $post_id - the post_id of which the value will be saved
402 * @param $field - the field array holding all the field options
403 *
404 * @return $value - the modified value
405 */
406 function update_value( $value, $post_id, $field ) {
407
408 return acf_get_field_type( 'file' )->update_value( $value, $post_id, $field );
409 }
410
411
412 /**
413 * This function will validate a basic file input
414 *
415 * @type function
416 * @since ACF 5.0.0
417 *
418 * @param boolean $valid The current validity status.
419 * @param mixed $value The field value.
420 * @param array $field The field array.
421 * @param string $input The name of the input in the POST object.
422 * @return boolean The validity status.
423 */
424 public function validate_value( $valid, $value, $field, $input ) {
425 return acf_get_field_type( 'file' )->validate_value( $valid, $value, $field, $input );
426 }
427
428 /**
429 * Additional validation for the image field when submitted via REST.
430 *
431 * @param boolean $valid The current validity boolean.
432 * @param integer $value The value of the field.
433 * @param array $field The field array.
434 * @return boolean|WP_Error
435 */
436 public function validate_rest_value( $valid, $value, $field ) {
437 return acf_get_field_type( 'file' )->validate_rest_value( $valid, $value, $field );
438 }
439
440 /**
441 * Return the schema array for the REST API.
442 *
443 * @param array $field The field array
444 * @return array
445 */
446 public function get_rest_schema( array $field ) {
447 return acf_get_field_type( 'file' )->get_rest_schema( $field );
448 }
449
450 /**
451 * Apply basic formatting to prepare the value for default REST output.
452 *
453 * @since ACF 5.0.0
454 * @since SCF 6.8.0 Now respects the return_format field setting.
455 *
456 * @param mixed $value The field value
457 * @param string|integer $post_id The post ID
458 * @param array $field The field array
459 * @return mixed The formatted value based on return_format setting.
460 */
461 public function format_value_for_rest( $value, $post_id, array $field ) {
462 return $this->format_value( $value, $post_id, $field );
463 }
464
465 /**
466 * Formats the field value for JSON-LD output.
467 *
468 * @since 6.8.0
469 *
470 * @param mixed $value The value of the field.
471 * @param integer|string $post_id The ID of the post.
472 * @param array $field The field array.
473 * @return mixed
474 */
475 public function format_value_for_jsonld( $value, $post_id, $field ) {
476 if ( empty( $value ) ) {
477 return null;
478 }
479
480 // Get output format with fallback.
481 $output_format = $field['schema_output_format'] ?? '';
482 if ( empty( $output_format ) ) {
483 $property = $field['schema_property'] ?? '';
484 $output_format = \SCF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
485 }
486
487 // Get the attachment ID.
488 $attachment_id = is_array( $value ) ? ( $value['ID'] ?? 0 ) : (int) $value;
489 if ( ! $attachment_id ) {
490 return null;
491 }
492
493 $url = wp_get_attachment_url( $attachment_id );
494 if ( ! $url ) {
495 return null;
496 }
497
498 // URL format - just return the URL string.
499 if ( 'URL' === $output_format ) {
500 return $url;
501 }
502
503 // ImageObject format - return structured object.
504 $image_object = array(
505 '@type' => 'ImageObject',
506 'url' => $url,
507 );
508
509 // Add dimensions if available.
510 $metadata = wp_get_attachment_metadata( $attachment_id );
511 if ( ! empty( $metadata['width'] ) ) {
512 $image_object['width'] = (int) $metadata['width'];
513 }
514 if ( ! empty( $metadata['height'] ) ) {
515 $image_object['height'] = (int) $metadata['height'];
516 }
517
518 // Add caption/alt text if available.
519 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
520 if ( $alt ) {
521 $image_object['caption'] = $alt;
522 }
523
524 // Add name from attachment title.
525 $attachment = get_post( $attachment_id );
526 if ( $attachment && $attachment->post_title ) {
527 $image_object['name'] = $attachment->post_title;
528 }
529
530 return $image_object;
531 }
532
533 /**
534 * Returns an array of JSON-LD Property output types that are supported by this field type.
535 *
536 * @since 6.8
537 *
538 * @return string[]
539 */
540 public function get_jsonld_output_types(): array {
541 return array( 'ImageObject', 'URL' );
542 }
543 }
544
545
546 // initialize
547 acf_register_field_type( 'acf_field_image' );
548 endif; // class_exists check
549
550 ?>
551