Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions includes/classes/class-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -1123,19 +1123,21 @@ public static function validate_csv_file( $file ) {
return new WP_Error( 'invalid_csv_file', 'Invalid file path or file does not exists.' );
}

if ( ! wp_check_filetype( $file )['ext'] === 'csv' ) {
return new WP_Error( 'invalid_csv_file', 'The file must be a CSV file.' );
}
$filename = wp_basename( $file );

$mime_type = mime_content_type( $file );
if ( ! in_array( $mime_type, [ 'text/csv','text/plain', 'application/csv' ], true ) ) {
return new WP_Error(
'invalid_csv_file',
sprintf(
'Invalid file type. Only text/plain, text/csv, and application/csv are supported, given "%s".',
$mime_type
)
);
// WordPress appends .txt and may add a numeric suffix to importer uploads.
$filename = preg_replace( '/\.csv(?:-\d+)?\.txt$/i', '.csv', $filename );

$file_type = wp_check_filetype_and_ext(
$file,
$filename,
[
'csv' => 'text/csv',
]
);

if ( 'csv' !== $file_type['ext'] || 'text/csv' !== $file_type['type'] ) {
return new WP_Error( 'invalid_csv_file', 'The file must be a valid CSV file.' );
}

return $file;
Expand Down
Loading