Uploads a new file for a specific form submission and element.
Description
See also
Parameters
- $file_id
-
(string) (Required) Identifier to look for in $_FILES.
- $submission
-
(awsmug\Torro_Forms\DB_Objects\Submissions\Submission) (Required) Submission object.
- $form
-
(awsmug\Torro_Forms\DB_Objects\Forms\Form) (Required) Form object.
- $element_id
-
(int) (Required) Element ID.
- $field
-
(string) (Optional) Element field slug. Default is '_main'.
Default value: '_main'
- $allowed_mimes
-
(array) (Optional) Allowed MIME types. Default are all MIME types that WordPress core allows.
Default value: null
- $allowed_filesize
-
(int) (Optional) Allowed maximum file size. Default is no limit other than WordPress core restrictions.
Default value: null
Return
(int|WP_Error) Attachment ID for the new file, or error object on failure.
Source
File: src/components/form-upload-manager.php
public function upload_file( $file_id, $submission, $form, $element_id, $field = '_main', $allowed_mimes = null, $allowed_filesize = null ) { if ( ! isset( $_FILES[ $file_id ] ) ) { return new WP_Error( 'missing_file', __( 'No file was provided to upload.', 'torro-forms' ) ); } $prefix = $this->get_prefix(); if ( ! $field ) { $field = '_main'; } $attachment_data = array( 'post_title' => sprintf( __( 'Form upload for submission #%1$s (form “%2$s”)', 'torro-forms' ), $submission->id, $form->title ), 'meta_input' => array( $prefix . 'parent_submission_id' => $submission->id, $prefix . 'parent_form_id' => $form->id, $prefix . 'parent_element_id' => $element->id, $prefix . 'parent_element_field' => $field, ), ); $post_id = 0; if ( $this->should_set_parent_form( $form->id ) ) { $post_id = $form->id; } $overrides = array( 'mimes' => $allowed_mimes, 'test_form' => false, 'test_type' => true, 'test_size' => true, ); if ( $allowed_filesize ) { $filesize = isset( $_FILES[ $file_id ]['size'] ) ? $_FILES[ $file_id ]['size'] : filesize( $_FILES[ $file_id ]['tmp_name'] ); if ( (int) $filesize > (int) $allowed_filesize ) { return new WP_Error( 'upload_error', __( 'The file exceeds the maximum allowed size.', 'torro-forms' ) ); } } if ( ! function_exists( 'wp_handle_upload' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! function_exists( 'wp_read_image_metadata' ) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; } if ( ! function_exists( 'media_handle_upload' ) ) { require_once ABSPATH . 'wp-admin/includes/media.php'; } $added_filter = false; if ( ! $this->should_generate_image_sizes( $form->id ) ) { $added_filter = true; add_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', 9999 ); } $attachment_id = media_handle_upload( $file_id, $post_id, $attachment_data, $overrides ); if ( $added_filter ) { remove_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', 9999 ); } if ( is_wp_error( $attachment_id ) ) { // The following line has no textdomain on purpose as it's a WP core message. if ( $allowed_mimes && 'upload_error' === $attachment_id->get_error_code() && __( 'Sorry, this file type is not permitted for security reasons.' ) === $attachment_id->get_error_message() ) { return new WP_Error( 'upload_error', __( 'The file type is not permitted.', 'torro-forms' ) ); } return $attachment_id; } if ( ! $attachment_id ) { return new WP_Error( 'upload_error', __( 'The file could not be registered with the database.', 'torro-forms' ) ); } $attachment_id = (int) $attachment_id; // Set the default attachment taxonomy term for form uploads. $taxonomy_slug = $this->taxonomies()->get_attachment_taxonomy_slug(); if ( ! empty( $taxonomy_slug ) ) { $taxonomy_term_id = $this->taxonomies()->get_attachment_taxonomy_term_id(); if ( ! empty( $taxonomy_term_id ) ) { wp_set_post_terms( $attachment_id, array( $taxonomy_term_id ), $taxonomy_slug ); } } return $attachment_id; }
Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |