diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index dfe8af4335..e0c859cc8f 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3150,9 +3150,22 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { $real_mime = finfo_file( $finfo, $file ); finfo_close( $finfo ); - // finfo_file() returns redudant mime type for Google docs, see #57898. - if ( 'application/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.openxmlformats-officedocument.wordprocessingml.document' === $real_mime ) { - $real_mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; + $google_docs_types = array( + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + ); + + foreach ( $google_docs_types as $google_docs_type ) { + /* + * finfo_file() can return duplicate mime type for Google docs, + * this conditional reduces it to a single instance. + * + * @see https://bugs.php.net/bug.php?id=77784 + * @see https://core.trac.wordpress.org/ticket/57898 + */ + if ( 2 === substr_count( $real_mime, $google_docs_type ) ) { + $real_mime = $google_docs_type; + } } // fileinfo often misidentifies obscure files as one of these types. diff --git a/tests/phpunit/data/uploads/double-mime-type.docx b/tests/phpunit/data/uploads/double-mime-type.docx new file mode 100644 index 0000000000..44988f24da Binary files /dev/null and b/tests/phpunit/data/uploads/double-mime-type.docx differ diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 32fa2bd1e6..1451117b91 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -1643,6 +1643,16 @@ class Tests_Functions extends WP_UnitTestCase { 'proper_filename' => false, ), ), + // Google Docs file for which finfo_file() returns a duplicate mime type. + array( + DIR_TESTDATA . '/uploads/double-mime-type.docx', + 'double-mime-type.docx', + array( + 'ext' => 'docx', + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'proper_filename' => false, + ), + ), // Non-image file with wrong sub-type. array( DIR_TESTDATA . '/uploads/pages-to-word.docx',