From 04fe719b817b0c57e34442333a9c3744625cd2c4 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 27 Sep 2023 14:24:17 -0300 Subject: [PATCH] Testeable mime type conditionals porting https://github.com/WordPress/gutenberg/pull/54844 Co-authored-by: Jason Crist <146530+pbking@users.noreply.github.com> --- .../fonts/class-wp-font-family-utils.php | 5 +- .../fonts/class-wp-font-family.php | 2 +- .../fonts/class-wp-font-library.php | 31 ++++--- .../wpFontLibrary/getMimeTypes.php | 90 +++++++++++++++++++ 4 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 tests/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php diff --git a/src/wp-includes/fonts/class-wp-font-family-utils.php b/src/wp-includes/fonts/class-wp-font-family-utils.php index 0191290e394ae..e025e0d542c2f 100644 --- a/src/wp-includes/fonts/class-wp-font-family-utils.php +++ b/src/wp-includes/fonts/class-wp-font-family-utils.php @@ -81,8 +81,9 @@ public static function merge_fonts_data( $font1, $font2 ) { * @return bool True if the file has a font MIME type, false otherwise. */ public static function has_font_mime_type( $filepath ) { - $filetype = wp_check_filetype( $filepath, WP_Font_Library::ALLOWED_FONT_MIME_TYPES ); + $allowed_mime_types = WP_Font_Library::get_expected_font_mime_types_per_php_version(); + $filetype = wp_check_filetype( $filepath, $allowed_mime_types ); - return in_array( $filetype['type'], WP_Font_Library::ALLOWED_FONT_MIME_TYPES, true ); + return in_array( $filetype['type'], $allowed_mime_types, true ); } } diff --git a/src/wp-includes/fonts/class-wp-font-family.php b/src/wp-includes/fonts/class-wp-font-family.php index f9b122b77e955..247f5333056e6 100644 --- a/src/wp-includes/fonts/class-wp-font-family.php +++ b/src/wp-includes/fonts/class-wp-font-family.php @@ -178,7 +178,7 @@ private function get_upload_overrides( $filename ) { // Seems mime type for files that are not images cannot be tested. // See wp_check_filetype_and_ext(). 'test_type' => true, - 'mimes' => WP_Font_Library::ALLOWED_FONT_MIME_TYPES, + 'mimes' => WP_Font_Library::get_expected_font_mime_types_per_php_version(), 'unique_filename_callback' => static function () use ( $filename ) { // Keep the original filename. return $filename; diff --git a/src/wp-includes/fonts/class-wp-font-library.php b/src/wp-includes/fonts/class-wp-font-library.php index 97da963e5136a..45ba253027937 100644 --- a/src/wp-includes/fonts/class-wp-font-library.php +++ b/src/wp-includes/fonts/class-wp-font-library.php @@ -17,18 +17,27 @@ class WP_Font_Library { /** - * Fonts mime types allowed for each file type. - * Each file type can have multiple mime types depending on the PHP version. - * Currently wp_check_filetype_and_ext() only allows one mime type per file extension. + * Provide the expected mime-type value for font files per-PHP release. Due to differences in the values returned these values differ between PHP versions. + * + * This is necessary until a collection of valid mime-types per-file extension can be provided to 'upload_mimes' filter. + * + * @since 6.4.0 + * + * @param array $php_version_id The version of PHP to provide mime types for. The default is the current PHP version. + * + * @return Array A collection of mime types keyed by file extension. */ - const PHP_7_TTF_MIME_TYPE = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; + public static function get_expected_font_mime_types_per_php_version( $php_version_id = PHP_VERSION_ID ) { + + $php_7_ttf_mime_type = $php_version_id >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; - const ALLOWED_FONT_MIME_TYPES = array( - 'otf' => 'font/otf', - 'ttf' => PHP_VERSION_ID >= 70400 ? 'font/sfnt' : self::PHP_7_TTF_MIME_TYPE, - 'woff' => PHP_VERSION_ID >= 80100 ? 'font/woff' : 'application/font-woff', - 'woff2' => PHP_VERSION_ID >= 80100 ? 'font/woff2' : 'application/font-woff2', - ); + return array( + 'otf' => 'font/otf', + 'ttf' => $php_version_id >= 70400 ? 'font/sfnt' : $php_7_ttf_mime_type, + 'woff' => $php_version_id >= 80100 ? 'font/woff' : 'application/font-woff', + 'woff2' => $php_version_id >= 80100 ? 'font/woff2' : 'application/font-woff2', + ); + } /** * Font collections. @@ -131,6 +140,6 @@ public static function set_upload_dir( $defaults ) { * @return array Modified upload directory. */ public static function set_allowed_mime_types( $mime_types ) { - return array_merge( $mime_types, self::ALLOWED_FONT_MIME_TYPES ); + return array_merge( $mime_types, self::get_expected_font_mime_types_per_php_version() ); } } diff --git a/tests/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/tests/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php new file mode 100644 index 0000000000000..720695d8fe5ef --- /dev/null +++ b/tests/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php @@ -0,0 +1,90 @@ +assertEquals( $mimes, $expected ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_supply_correct_mime_type_for_php_version() { + return array( + 'version 7.2' => array( + 'php_version_id' => 70200, + 'expected' => array( + 'otf' => 'font/otf', + 'ttf' => 'application/x-font-ttf', + 'woff' => 'application/font-woff', + 'woff2' => 'application/font-woff2', + ), + ), + 'version 7.3' => array( + 'php_version_id' => 70300, + 'expected' => array( + 'otf' => 'font/otf', + 'ttf' => 'application/font-sfnt', + 'woff' => 'application/font-woff', + 'woff2' => 'application/font-woff2', + ), + ), + 'version 7.4' => array( + 'php_version_id' => 70400, + 'expected' => array( + 'otf' => 'font/otf', + 'ttf' => 'font/sfnt', + 'woff' => 'application/font-woff', + 'woff2' => 'application/font-woff2', + ), + ), + 'version 8.0' => array( + 'php_version_id' => 80000, + 'expected' => array( + 'otf' => 'font/otf', + 'ttf' => 'font/sfnt', + 'woff' => 'application/font-woff', + 'woff2' => 'application/font-woff2', + ), + ), + 'version 8.1' => array( + 'php_version_id' => 80100, + 'expected' => array( + 'otf' => 'font/otf', + 'ttf' => 'font/sfnt', + 'woff' => 'font/woff', + 'woff2' => 'font/woff2', + ), + ), + 'version 8.2' => array( + 'php_version_id' => 80200, + 'expected' => array( + 'otf' => 'font/otf', + 'ttf' => 'font/sfnt', + 'woff' => 'font/woff', + 'woff2' => 'font/woff2', + ), + ), + ); + } +}