diff --git a/lib/experimental/fonts/font-library/class-wp-font-family-utils.php b/lib/experimental/fonts/font-library/class-wp-font-family-utils.php index 200cfef22289f..f6166f3b13a3a 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-family-utils.php @@ -85,8 +85,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/lib/experimental/fonts/font-library/class-wp-font-family.php b/lib/experimental/fonts/font-library/class-wp-font-family.php index ad7b0d207cd0d..68e980ced05ce 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family.php +++ b/lib/experimental/fonts/font-library/class-wp-font-family.php @@ -182,7 +182,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/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index d488e330486a7..6d2ad5de43a35 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -20,14 +20,28 @@ */ class WP_Font_Library { - const PHP_7_TTF_MIME_TYPE = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; + /** + * 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. + */ + public static function get_expected_font_mime_types_per_php_version( $php_version_id = PHP_VERSION_ID ) { - 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', - ); + $php_7_ttf_mime_type = $php_version_id >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; + + 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. @@ -130,6 +144,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/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php new file mode 100644 index 0000000000000..720695d8fe5ef --- /dev/null +++ b/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', + ), + ), + ); + } +}