Automattic\WooCommerce\Internal\Admin\ImportExport
CSVUploadHelper::handle_csv_upload
Handles a CSV file upload.
Method of the class: CSVUploadHelper{}
No Hooks.
Returns
Array
. Details for the uploaded file.
Usage
$CSVUploadHelper = new CSVUploadHelper(); $CSVUploadHelper->handle_csv_upload( $import_type, $files_index, ?array $allowed_mime_types ): array;
- $import_type(string) (required)
- Type of upload or context.
- $files_index(string)
- $_FILES index that contains the file to upload.
Default: 'import' - ?array $allowed_mime_types
- .
Default: null
CSVUploadHelper::handle_csv_upload() CSVUploadHelper::handle csv upload code WC 9.9.3
public function handle_csv_upload( string $import_type, string $files_index = 'import', ?array $allowed_mime_types = null ): array { $import_type = sanitize_key( $import_type ); if ( ! $import_type ) { throw new \Exception( 'Import type is invalid.' ); } if ( ! $allowed_mime_types ) { $allowed_mime_types = array( 'csv' => 'text/csv', 'txt' => 'text/plain', ); } $file = $_FILES[ $files_index ] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Missing if ( ! isset( $file['tmp_name'] ) || ! is_uploaded_file( $file['tmp_name'] ) ) { throw new \Exception( esc_html__( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.', 'woocommerce' ) ); } if ( ! function_exists( 'wp_import_handle_upload' ) ) { require_once ABSPATH . 'wp-admin/includes/import.php'; } // Make sure upload dir exists. $this->get_import_dir(); // Add prefix. $file['name'] = $import_type . '-' . $file['name']; $overrides_callback = function ( $overrides_ ) use ( $allowed_mime_types ) { $overrides_['test_form'] = false; $overrides_['test_type'] = true; $overrides_['mimes'] = $allowed_mime_types; return $overrides_; }; add_filter( 'upload_dir', array( $this, 'override_upload_dir' ) ); add_filter( 'wp_unique_filename', array( $this, 'override_unique_filename' ), 0, 2 ); add_filter( 'wp_handle_upload_overrides', $overrides_callback, 999 ); add_filter( 'wp_handle_upload_prefilter', array( $this, 'remove_txt_from_uploaded_file' ), 0 ); $orig_files_import = $_FILES['import'] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Missing $_FILES['import'] = $file; // wp_import_handle_upload() expects the file to be in 'import'. $upload = wp_import_handle_upload(); remove_filter( 'upload_dir', array( $this, 'override_upload_dir' ) ); remove_filter( 'wp_unique_filename', array( $this, 'override_unique_filename' ), 0 ); remove_filter( 'wp_handle_upload_overrides', $overrides_callback, 999 ); remove_filter( 'wp_handle_upload_prefilter', array( $this, 'remove_txt_from_uploaded_file' ), 0 ); if ( $orig_files_import ) { $_FILES['import'] = $orig_files_import; } else { unset( $_FILES['import'] ); } if ( ! empty( $upload['error'] ) ) { throw new \Exception( esc_html( $upload['error'] ) ); } if ( ! wc_is_file_valid_csv( $upload['file'], false ) ) { wp_delete_attachment( $file['id'], true ); throw new \Exception( esc_html__( 'Invalid file type for a CSV import.', 'woocommerce' ) ); } return $upload; }