Automattic\WooCommerce\Internal\Admin\ImportExport

CSVUploadHelper::filter_woocommerce_check_filetype_for_csvpublicWC 1.0

Filters the WordPress determination of a file's type and extension, specifically to correct CSV files that are misidentified as 'text/html'.

Method of the class: CSVUploadHelper{}

No Hooks.

Returns

Array. Filtered file data.

Usage

$CSVUploadHelper = new CSVUploadHelper();
$CSVUploadHelper->filter_woocommerce_check_filetype_for_csv( $data, $file, $filename, $mimes, $real_mime );
$data(array) (required)
An array of file data: ['ext'] (string), ['type'] (string), ['proper_filename'] (string|false).
$file(string) (required)
Full path to the file.
$filename(string) (required)
The Mime type of the file.
$mimes(array) (required)
Array of mime types.
$real_mime(string) (required)
The actual mime type or empty string.

CSVUploadHelper::filter_woocommerce_check_filetype_for_csv() code WC 10.3.3

public function filter_woocommerce_check_filetype_for_csv( $data, $file, $filename, $mimes, $real_mime ) {
	// Check if the file was misidentified as 'text/html' by PHP.
	if ( 'text/html' === $real_mime ) {
		// Determine the expected file type based on the filename extension.
		// $mimes here is the context-specific list of mimes for the current upload.
		$filename_check = wp_check_filetype( $filename, $mimes );

		$file_ext  = $filename_check['ext'];
		$file_type = $filename_check['type'];

		if ( ( 'csv' === $file_ext && 'text/csv' === $file_type ) ) {
			$data['ext']  = 'csv';
			$data['type'] = 'text/csv';
		}
	}

	return $data;
}