PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.1-beta7
Secure Custom Fields v6.4.1-beta7
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-clone.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-flexible-content.php 1 year ago class-acf-field-gallery.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-repeater.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 class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-file.php
527 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' => $field['library'],
99 'data-mime_types' => $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">
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 ( $uploader != 'basic' ) : ?>
152 <a class="acf-icon -pencil dark" data-name="edit" href="#" title="<?php esc_attr_e( 'Edit', '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' ); ?>"></a>
155 </div>
156 </div>
157 <div class="hide-if-value">
158 <?php if ( $uploader == 'basic' ) : ?>
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 acf_file_input(
169 array(
170 'name' => $field['name'],
171 'id' => $field['id'],
172 'key' => $field['key'],
173 )
174 );
175 ?>
176 </label>
177
178 <?php else : ?>
179
180 <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>
181
182 <?php endif; ?>
183
184 </div>
185 </div>
186 <?php
187 }
188
189 /**
190 * Create extra options for your field. This is rendered when editing a field.
191 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
192 *
193 * @type action
194 * @since ACF 3.6
195 * @date 23/01/13
196 *
197 * @param $field - an array holding all the field's data
198 */
199 function render_field_settings( $field ) {
200 acf_render_field_setting(
201 $field,
202 array(
203 'label' => __( 'Return Value', 'secure-custom-fields' ),
204 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ),
205 'type' => 'radio',
206 'name' => 'return_format',
207 'layout' => 'horizontal',
208 'choices' => array(
209 'array' => __( 'File Array', 'secure-custom-fields' ),
210 'url' => __( 'File URL', 'secure-custom-fields' ),
211 'id' => __( 'File ID', 'secure-custom-fields' ),
212 ),
213 )
214 );
215
216 acf_render_field_setting(
217 $field,
218 array(
219 'label' => __( 'Library', 'secure-custom-fields' ),
220 'instructions' => __( 'Limit the media library choice', 'secure-custom-fields' ),
221 'type' => 'radio',
222 'name' => 'library',
223 'layout' => 'horizontal',
224 'choices' => array(
225 'all' => __( 'All', 'secure-custom-fields' ),
226 'uploadedTo' => __( 'Uploaded to post', 'secure-custom-fields' ),
227 ),
228 )
229 );
230 }
231
232 /**
233 * Renders the field settings used in the "Validation" tab.
234 *
235 * @since ACF 6.0
236 *
237 * @param array $field The field settings array.
238 * @return void
239 */
240 function render_field_validation_settings( $field ) {
241 // Clear numeric settings.
242 $clear = array(
243 'min_size',
244 'max_size',
245 );
246
247 foreach ( $clear as $k ) {
248 if ( empty( $field[ $k ] ) ) {
249 $field[ $k ] = '';
250 }
251 }
252
253 acf_render_field_setting(
254 $field,
255 array(
256 'label' => __( 'Minimum', 'secure-custom-fields' ),
257 'instructions' => __( 'Restrict which files can be uploaded', 'secure-custom-fields' ),
258 'type' => 'text',
259 'name' => 'min_size',
260 'prepend' => __( 'File size', 'secure-custom-fields' ),
261 'append' => 'MB',
262 )
263 );
264
265 acf_render_field_setting(
266 $field,
267 array(
268 'label' => __( 'Maximum', 'secure-custom-fields' ),
269 'instructions' => __( 'Restrict which files can be uploaded', 'secure-custom-fields' ),
270 'type' => 'text',
271 'name' => 'max_size',
272 'prepend' => __( 'File size', 'secure-custom-fields' ),
273 'append' => 'MB',
274 )
275 );
276
277 acf_render_field_setting(
278 $field,
279 array(
280 'label' => __( 'Allowed File Types', 'secure-custom-fields' ),
281 'hint' => __( 'Comma separated list. Leave blank for all types', 'secure-custom-fields' ),
282 'type' => 'text',
283 'name' => 'mime_types',
284 )
285 );
286 }
287
288 /**
289 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
290 *
291 * @type filter
292 * @since ACF 3.6
293 * @date 23/01/13
294 *
295 * @param $value (mixed) the value which was loaded from the database
296 * @param $post_id (mixed) the post_id from which the value was loaded
297 * @param $field (array) the field array holding all the field options
298 *
299 * @return $value (mixed) the modified value
300 */
301 function format_value( $value, $post_id, $field ) {
302
303 // bail early if no value
304 if ( empty( $value ) ) {
305 return false;
306 }
307
308 // bail early if not numeric (error message)
309 if ( ! is_numeric( $value ) ) {
310 return false;
311 }
312
313 // convert to int
314 $value = intval( $value );
315
316 // format
317 if ( $field['return_format'] == 'url' ) {
318 return wp_get_attachment_url( $value );
319 } elseif ( $field['return_format'] == 'array' ) {
320 return acf_get_attachment( $value );
321 }
322
323 // return
324 return $value;
325 }
326
327
328 /**
329 * description
330 *
331 * @type function
332 * @date 27/01/13
333 * @since ACF 3.6.0
334 *
335 * @param $vars (array)
336 * @return $vars
337 */
338 function get_media_item_args( $vars ) {
339
340 $vars['send'] = true;
341 return ( $vars );
342 }
343
344
345 /**
346 * This filter is appied to the $value before it is updated in the db
347 *
348 * @type filter
349 * @since ACF 3.6
350 * @date 23/01/13
351 *
352 * @param $value - the value which will be saved in the database
353 * @param $post_id - the post_id of which the value will be saved
354 * @param $field - the field array holding all the field options
355 *
356 * @return $value - the modified value
357 */
358 function update_value( $value, $post_id, $field ) {
359
360 // Bail early if no value.
361 if ( empty( $value ) ) {
362 return $value;
363 }
364
365 // Parse value for id.
366 $attachment_id = acf_idval( $value );
367
368 // Connect attacment to post.
369 acf_connect_attachment_to_post( $attachment_id, $post_id );
370
371 // Return id.
372 return $attachment_id;
373 }
374
375 /**
376 * validate_value
377 *
378 * This function will validate a basic file input
379 *
380 * @type function
381 * @date 11/02/2014
382 * @since ACF 5.0.0
383 *
384 * @param $post_id (int)
385 * @return $post_id (int)
386 */
387 function validate_value( $valid, $value, $field, $input ) {
388
389 // bail early if empty
390 if ( empty( $value ) ) {
391 return $valid;
392 }
393
394 // bail early if is numeric
395 if ( is_numeric( $value ) ) {
396 return $valid;
397 }
398
399 // bail early if not basic string
400 if ( ! is_string( $value ) ) {
401 return $valid;
402 }
403
404 // decode value
405 $file = null;
406 parse_str( $value, $file );
407
408 // bail early if no attachment
409 if ( empty( $file ) ) {
410 return $valid;
411 }
412
413 // get errors
414 $errors = acf_validate_attachment( $file, $field, 'basic_upload' );
415
416 // append error
417 if ( ! empty( $errors ) ) {
418 $valid = implode( "\n", $errors );
419 }
420
421 // return
422 return $valid;
423 }
424
425 /**
426 * Validates file fields updated via the REST API.
427 *
428 * @param boolean $valid The current validity booleean
429 * @param integer $value The value of the field
430 * @param array $field The field array
431 * @return boolean|WP_Error
432 */
433 public function validate_rest_value( $valid, $value, $field ) {
434 if ( is_null( $value ) && empty( $field['required'] ) ) {
435 return $valid;
436 }
437
438 /**
439 * A bit of a hack, but we use `wp_prepare_attachment_for_js()` here
440 * since it returns all the data we need to validate the file, and we use this anyways
441 * to validate fields updated via UI.
442 */
443 $attachment = wp_prepare_attachment_for_js( $value );
444 $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
445 $data = array(
446 'param' => $param,
447 'value' => (int) $value,
448 );
449
450 if ( ! $attachment ) {
451 /* translators: %s: field value */
452 $error = sprintf( __( '%s requires a valid attachment ID.', 'secure-custom-fields' ), $param );
453 return new WP_Error( 'rest_invalid_param', $error, $data );
454 }
455
456 $errors = acf_validate_attachment( $attachment, $field, 'prepare' );
457
458 if ( ! empty( $errors ) ) {
459 $error = $param . ' - ' . implode( ' ', $errors );
460 return new WP_Error( 'rest_invalid_param', $error, $data );
461 }
462
463 return $valid;
464 }
465
466 /**
467 * Return the schema array for the REST API.
468 *
469 * @param array $field
470 * @return array
471 */
472 public function get_rest_schema( array $field ) {
473 $schema = array(
474 'type' => array( 'integer', 'null' ),
475 'required' => isset( $field['required'] ) && $field['required'],
476 );
477
478 if ( ! empty( $field['min_width'] ) ) {
479 $schema['minWidth'] = (int) $field['min_width'];
480 }
481
482 if ( ! empty( $field['min_height'] ) ) {
483 $schema['minHeight'] = (int) $field['min_height'];
484 }
485
486 if ( ! empty( $field['min_size'] ) ) {
487 $schema['minSize'] = $field['min_size'];
488 }
489
490 if ( ! empty( $field['max_width'] ) ) {
491 $schema['maxWidth'] = (int) $field['max_width'];
492 }
493
494 if ( ! empty( $field['max_height'] ) ) {
495 $schema['maxHeight'] = (int) $field['max_height'];
496 }
497
498 if ( ! empty( $field['max_size'] ) ) {
499 $schema['maxSize'] = $field['max_size'];
500 }
501
502 if ( ! empty( $field['mime_types'] ) ) {
503 $schema['mimeTypes'] = $field['mime_types'];
504 }
505
506 return $schema;
507 }
508
509 /**
510 * Apply basic formatting to prepare the value for default REST output.
511 *
512 * @param mixed $value
513 * @param string|integer $post_id
514 * @param array $field
515 * @return mixed
516 */
517 public function format_value_for_rest( $value, $post_id, array $field ) {
518 return acf_format_numerics( $value );
519 }
520 }
521
522
523 // initialize
524 acf_register_field_type( 'acf_field_file' );
525 endif; // class_exists check
526
527 ?>