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