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