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-file.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-file.php
616 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_file' ) ) :
4
5 class acf_field_file extends acf_field {
6
7
8
9 /**
10 * This function will setup the field type data
11 *
12 * @type function
13 * @date 5/03/2014
14 * @since ACF 5.0.0
15 *
16 * @param n/a
17 * @return n/a
18 */
19 function initialize() {
20
21 // vars
22 $this->name = 'file';
23 $this->label = __( 'File', 'secure-custom-fields' );
24 $this->category = 'content';
25 $this->description = __( 'Uses the native WordPress media picker to upload, or choose files.', 'secure-custom-fields' );
26 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-file.png';
27 $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/file/';
28 $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/file/file-tutorial/';
29 $this->defaults = array(
30 'return_format' => 'array',
31 'library' => 'all',
32 'min_size' => 0,
33 'max_size' => 0,
34 'mime_types' => '',
35 );
36
37 // filters
38 add_filter( 'get_media_item_args', array( $this, 'get_media_item_args' ) );
39 }
40
41
42 /**
43 * description
44 *
45 * @type function
46 * @date 16/12/2015
47 * @since ACF 5.3.2
48 *
49 * @param $post_id (int)
50 * @return $post_id (int)
51 */
52 function input_admin_enqueue_scripts() {
53
54 // localize
55 acf_localize_text(
56 array(
57 'Select File' => __( 'Select File', 'secure-custom-fields' ),
58 'Edit File' => __( 'Edit File', 'secure-custom-fields' ),
59 'Update File' => __( 'Update File', 'secure-custom-fields' ),
60 )
61 );
62 }
63
64
65 /**
66 * Create the HTML interface for your field
67 *
68 * @param $field - an array holding all the field's data
69 *
70 * @type action
71 * @since ACF 3.6
72 * @date 23/01/13
73 */
74 function render_field( $field ) {
75
76 // vars
77 $uploader = acf_get_setting( 'uploader' );
78
79 // allow custom uploader
80 $uploader = acf_maybe_get( $field, 'uploader', $uploader );
81
82 // enqueue
83 if ( $uploader == 'wp' ) {
84 acf_enqueue_uploader();
85 }
86
87 // vars
88 $o = array(
89 'icon' => '',
90 'title' => '',
91 'url' => '',
92 'filename' => '',
93 'filesize' => '',
94 );
95
96 $div = array(
97 'class' => 'acf-file-uploader',
98 'data-library' => acf_maybe_get( $field, 'library', '' ),
99 'data-mime_types' => acf_maybe_get( $field, 'mime_types', '' ),
100 'data-uploader' => $uploader,
101 );
102
103 // has value?
104 if ( $field['value'] ) {
105 $attachment = acf_get_attachment( $field['value'] );
106 if ( $attachment ) {
107
108 // has value
109 $div['class'] .= ' has-value';
110
111 // update
112 $o['icon'] = $attachment['icon'];
113 $o['title'] = $attachment['title'];
114 $o['url'] = $attachment['url'];
115 $o['filename'] = $attachment['filename'];
116 if ( $attachment['filesize'] ) {
117 $o['filesize'] = size_format( $attachment['filesize'] );
118 }
119 }
120 }
121
122 ?>
123 <div <?php echo acf_esc_attrs( $div ); ?>>
124 <?php
125 acf_hidden_input(
126 array(
127 'name' => $field['name'],
128 'value' => $field['value'],
129 'data-name' => 'id',
130 )
131 );
132 ?>
133 <div class="show-if-value file-wrap" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Selected file. Press tab to access file options.', 'secure-custom-fields' ); ?>">
134 <div class="file-icon">
135 <img data-name="icon" src="<?php echo esc_url( $o['icon'] ); ?>" alt="" />
136 </div>
137 <div class="file-info">
138 <p>
139 <strong data-name="title"><?php echo esc_html( $o['title'] ); ?></strong>
140 </p>
141 <p>
142 <strong><?php esc_html_e( 'File name', 'secure-custom-fields' ); ?>:</strong>
143 <a data-name="filename" href="<?php echo esc_url( $o['url'] ); ?>" target="_blank"><?php echo esc_html( $o['filename'] ); ?></a>
144 </p>
145 <p>
146 <strong><?php esc_html_e( 'File size', 'secure-custom-fields' ); ?>:</strong>
147 <span data-name="filesize"><?php echo esc_html( $o['filesize'] ); ?></span>
148 </p>
149 </div>
150 <div class="acf-actions -hover">
151 <?php if ( 'basic' !== $uploader ) : ?>
152 <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 file', 'secure-custom-fields' ); ?>"></a>
153 <?php endif; ?>
154 <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 file', 'secure-custom-fields' ); ?>"></a>
155 </div>
156 </div>
157 <div class="hide-if-value">
158 <?php if ( 'basic' === $uploader ) : ?>
159
160 <?php if ( $field['value'] && ! is_numeric( $field['value'] ) ) : ?>
161 <div class="acf-error-message">
162 <p><?php echo acf_esc_html( $field['value'] ); ?></p>
163 </div>
164 <?php endif; ?>
165
166 <label class="acf-basic-uploader">
167 <?php
168 $args = array(
169 'name' => $field['name'],
170 'id' => $field['id'],
171 'key' => $field['key'],
172 );
173
174 if ( ! empty( $field['mime_types'] ) ) {
175 $args['accept'] = $field['mime_types'];
176 }
177
178 acf_file_input( $args );
179 ?>
180 </label>
181
182 <?php else : ?>
183
184 <p><?php esc_html_e( 'No file selected', 'secure-custom-fields' ); ?> <a data-name="add" class="acf-button button" href="#"><?php esc_html_e( 'Add File', 'secure-custom-fields' ); ?></a></p>
185
186 <?php endif; ?>
187
188 </div>
189 </div>
190 <?php
191 }
192
193 /**
194 * Create extra options for your field. This is rendered when editing a field.
195 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
196 *
197 * @type action
198 * @since ACF 3.6
199 * @date 23/01/13
200 *
201 * @param $field - an array holding all the field's data
202 */
203 function render_field_settings( $field ) {
204 acf_render_field_setting(
205 $field,
206 array(
207 'label' => __( 'Return Value', 'secure-custom-fields' ),
208 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ),
209 'type' => 'radio',
210 'name' => 'return_format',
211 'layout' => 'horizontal',
212 'choices' => array(
213 'array' => __( 'File Array', 'secure-custom-fields' ),
214 'url' => __( 'File URL', 'secure-custom-fields' ),
215 'id' => __( 'File ID', 'secure-custom-fields' ),
216 ),
217 )
218 );
219
220 acf_render_field_setting(
221 $field,
222 array(
223 'label' => __( 'Library', 'secure-custom-fields' ),
224 'instructions' => __( 'Limit the media library choice', 'secure-custom-fields' ),
225 'type' => 'radio',
226 'name' => 'library',
227 'layout' => 'horizontal',
228 'choices' => array(
229 'all' => __( 'All', 'secure-custom-fields' ),
230 'uploadedTo' => __( 'Uploaded to post', 'secure-custom-fields' ),
231 ),
232 )
233 );
234 }
235
236 /**
237 * Renders the field settings used in the "Validation" tab.
238 *
239 * @since ACF 6.0
240 *
241 * @param array $field The field settings array.
242 * @return void
243 */
244 function render_field_validation_settings( $field ) {
245 // Clear numeric settings.
246 $clear = array(
247 'min_size',
248 'max_size',
249 );
250
251 foreach ( $clear as $k ) {
252 if ( empty( $field[ $k ] ) ) {
253 $field[ $k ] = '';
254 }
255 }
256
257 acf_render_field_setting(
258 $field,
259 array(
260 'label' => __( 'Minimum', 'secure-custom-fields' ),
261 'instructions' => __( 'Restrict which files can be uploaded', 'secure-custom-fields' ),
262 'type' => 'text',
263 'name' => 'min_size',
264 'prepend' => __( 'File size', 'secure-custom-fields' ),
265 'append' => 'MB',
266 )
267 );
268
269 acf_render_field_setting(
270 $field,
271 array(
272 'label' => __( 'Maximum', 'secure-custom-fields' ),
273 'instructions' => __( 'Restrict which files can be uploaded', 'secure-custom-fields' ),
274 'type' => 'text',
275 'name' => 'max_size',
276 'prepend' => __( 'File size', 'secure-custom-fields' ),
277 'append' => 'MB',
278 )
279 );
280
281 acf_render_field_setting(
282 $field,
283 array(
284 'label' => __( 'Allowed File Types', 'secure-custom-fields' ),
285 'hint' => __( 'Comma separated list. Leave blank for all types', 'secure-custom-fields' ),
286 'type' => 'text',
287 'name' => 'mime_types',
288 )
289 );
290 }
291
292 /**
293 * This filter is applied to the $value after it is loaded from the db and before it is returned to the template
294 *
295 * @type filter
296 * @since ACF 3.6
297 * @date 23/01/13
298 *
299 * @param $value (mixed) the value which was loaded from the database
300 * @param $post_id (mixed) the post_id from which the value was loaded
301 * @param $field (array) the field array holding all the field options
302 *
303 * @return $value (mixed) the modified value
304 */
305 function format_value( $value, $post_id, $field ) {
306
307 // bail early if no value
308 if ( empty( $value ) ) {
309 return false;
310 }
311
312 // bail early if not numeric (error message)
313 if ( ! is_numeric( $value ) ) {
314 return false;
315 }
316
317 // convert to int
318 $value = intval( $value );
319
320 // format
321 if ( $field['return_format'] == 'url' ) {
322 return wp_get_attachment_url( $value );
323 } elseif ( $field['return_format'] == 'array' ) {
324 return acf_get_attachment( $value );
325 }
326
327 // return
328 return $value;
329 }
330
331
332 /**
333 * description
334 *
335 * @type function
336 * @date 27/01/13
337 * @since ACF 3.6.0
338 *
339 * @param $vars (array)
340 * @return $vars
341 */
342 function get_media_item_args( $vars ) {
343
344 $vars['send'] = true;
345 return ( $vars );
346 }
347
348
349 /**
350 * This filter is applied to the $value before it is updated in the db
351 *
352 * @type filter
353 * @since ACF 3.6
354 * @date 23/01/13
355 *
356 * @param $value - the value which will be saved in the database
357 * @param $post_id - the post_id of which the value will be saved
358 * @param $field - the field array holding all the field options
359 *
360 * @return $value - the modified value
361 */
362 function update_value( $value, $post_id, $field ) {
363
364 // Bail early if no value.
365 if ( empty( $value ) ) {
366 return $value;
367 }
368
369 // Parse value for id.
370 $attachment_id = acf_idval( $value );
371
372 // Connect attachment to post.
373 acf_connect_attachment_to_post( $attachment_id, $post_id );
374
375 // Return id.
376 return $attachment_id;
377 }
378
379 /**
380 * validate_value
381 *
382 * This function will validate a basic file input
383 *
384 * @type function
385 * @date 11/02/2014
386 * @since ACF 5.0.0
387 *
388 * @param $post_id (int)
389 * @return $post_id (int)
390 */
391 function validate_value( $valid, $value, $field, $input ) {
392
393 // bail early if empty
394 if ( empty( $value ) ) {
395 return $valid;
396 }
397
398 // bail early if is numeric
399 if ( is_numeric( $value ) ) {
400 return $valid;
401 }
402
403 // bail early if not basic string
404 if ( ! is_string( $value ) ) {
405 return $valid;
406 }
407
408 // decode value
409 $file = null;
410 parse_str( $value, $file );
411
412 // bail early if no attachment
413 if ( empty( $file ) ) {
414 return $valid;
415 }
416
417 // get errors
418 $errors = acf_validate_attachment( $file, $field, 'basic_upload' );
419
420 // append error
421 if ( ! empty( $errors ) ) {
422 $valid = implode( "\n", $errors );
423 }
424
425 // return
426 return $valid;
427 }
428
429 /**
430 * Validates file fields updated via the REST API.
431 *
432 * @param boolean $valid The current validity boolean.
433 * @param integer $value The value of the field.
434 * @param array $field The field array.
435 * @return boolean|WP_Error
436 */
437 public function validate_rest_value( $valid, $value, $field ) {
438 if ( is_null( $value ) && empty( $field['required'] ) ) {
439 return $valid;
440 }
441
442 /**
443 * A bit of a hack, but we use `wp_prepare_attachment_for_js()` here
444 * since it returns all the data we need to validate the file, and we use this anyways
445 * to validate fields updated via UI.
446 */
447 $attachment = wp_prepare_attachment_for_js( $value );
448 $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
449 $data = array(
450 'param' => $param,
451 'value' => (int) $value,
452 );
453
454 if ( ! $attachment ) {
455 /* translators: %s: field value */
456 $error = sprintf( __( '%s requires a valid attachment ID.', 'secure-custom-fields' ), $param );
457 return new WP_Error( 'rest_invalid_param', $error, $data );
458 }
459
460 $errors = acf_validate_attachment( $attachment, $field, 'prepare' );
461
462 if ( ! empty( $errors ) ) {
463 $error = $param . ' - ' . implode( ' ', $errors );
464 return new WP_Error( 'rest_invalid_param', $error, $data );
465 }
466
467 return $valid;
468 }
469
470 /**
471 * Return the schema array for the REST API.
472 *
473 * @param array $field
474 * @return array
475 */
476 public function get_rest_schema( array $field ) {
477 $schema = array(
478 'type' => array( 'integer', 'null' ),
479 'required' => isset( $field['required'] ) && $field['required'],
480 );
481
482 if ( ! empty( $field['min_width'] ) ) {
483 $schema['minWidth'] = (int) $field['min_width'];
484 }
485
486 if ( ! empty( $field['min_height'] ) ) {
487 $schema['minHeight'] = (int) $field['min_height'];
488 }
489
490 if ( ! empty( $field['min_size'] ) ) {
491 $schema['minSize'] = $field['min_size'];
492 }
493
494 if ( ! empty( $field['max_width'] ) ) {
495 $schema['maxWidth'] = (int) $field['max_width'];
496 }
497
498 if ( ! empty( $field['max_height'] ) ) {
499 $schema['maxHeight'] = (int) $field['max_height'];
500 }
501
502 if ( ! empty( $field['max_size'] ) ) {
503 $schema['maxSize'] = $field['max_size'];
504 }
505
506 if ( ! empty( $field['mime_types'] ) ) {
507 $schema['mimeTypes'] = $field['mime_types'];
508 }
509
510 return $schema;
511 }
512
513 /**
514 * Apply basic formatting to prepare the value for default REST output.
515 *
516 * @since ACF 5.0.0
517 * @since SCF 6.8.0 Now respects the return_format field setting.
518 *
519 * @param mixed $value The field value.
520 * @param string|integer $post_id The post ID.
521 * @param array $field The field array.
522 * @return mixed The formatted value based on return_format setting.
523 */
524 public function format_value_for_rest( $value, $post_id, array $field ) {
525 return $this->format_value( $value, $post_id, $field );
526 }
527
528 /**
529 * Formats the field value for JSON-LD output.
530 *
531 * @since 6.8.0
532 *
533 * @param mixed $value The value of the field.
534 * @param integer|string $post_id The ID of the post.
535 * @param array $field The field array.
536 * @return mixed
537 */
538 public function format_value_for_jsonld( $value, $post_id, $field ) {
539 if ( empty( $value ) ) {
540 return null;
541 }
542
543 // Get output format with fallback.
544 $output_format = $field['schema_output_format'] ?? '';
545 if ( empty( $output_format ) ) {
546 $property = $field['schema_property'] ?? '';
547 $output_format = \SCF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
548 }
549
550 // Get attachment ID.
551 $attachment_id = is_array( $value ) ? ( $value['ID'] ?? 0 ) : (int) $value;
552 if ( ! $attachment_id ) {
553 return null;
554 }
555
556 $url = wp_get_attachment_url( $attachment_id );
557 if ( ! $url ) {
558 return null;
559 }
560
561 // URL format - just return the URL string.
562 if ( 'URL' === $output_format ) {
563 return $url;
564 }
565
566 // MediaObject or DataDownload format.
567 $file_object = array(
568 '@type' => $output_format ? $output_format : 'MediaObject',
569 'contentUrl' => $url,
570 );
571
572 // Add name from attachment title.
573 $attachment = get_post( $attachment_id );
574 if ( $attachment && $attachment->post_title ) {
575 $file_object['name'] = $attachment->post_title;
576 }
577
578 // Add description if available.
579 if ( $attachment && $attachment->post_content ) {
580 $file_object['description'] = $attachment->post_content;
581 }
582
583 // Add file metadata.
584 $file_path = get_attached_file( $attachment_id );
585 if ( $file_path && file_exists( $file_path ) ) {
586 $file_object['contentSize'] = size_format( filesize( $file_path ) );
587 }
588
589 // Add MIME type.
590 $mime_type = get_post_mime_type( $attachment_id );
591 if ( $mime_type ) {
592 $file_object['encodingFormat'] = $mime_type;
593 }
594
595 return $file_object;
596 }
597
598 /**
599 * Returns an array of JSON-LD Property output types that are supported by this field type.
600 *
601 * @since 6.8
602 *
603 * @return string[]
604 */
605 public function get_jsonld_output_types(): array {
606 return array( 'MediaObject', 'DataDownload', 'URL' );
607 }
608 }
609
610
611 // initialize
612 acf_register_field_type( 'acf_field_file' );
613 endif; // class_exists check
614
615 ?>
616