From 528d2b7d9805e7191884e46ea0abe092e6a7c9c2 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 5 Feb 2024 13:11:27 +0100 Subject: [PATCH] Move getAllowedMimeTypes to FontUtils (#58667) Co-authored-by: youknowriad Co-authored-by: getdave --- .../fonts/class-wp-font-library.php | 35 -------- .../fonts/class-wp-font-utils.php | 20 +++++ .../class-wp-rest-font-faces-controller.php | 6 +- .../fonts/font-library/fontLibraryHooks.php | 4 +- .../wpFontLibrary/getMimeTypes.php | 89 ------------------- 5 files changed, 25 insertions(+), 129 deletions(-) delete mode 100644 phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php index b09e2c5a7b0f38..dec2421cb94dff 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php @@ -18,29 +18,6 @@ */ class WP_Font_Library { - /** - * 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.5.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 ) { - - $php_7_ttf_mime_type = $php_version_id >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; - - return array( - 'otf' => 'application/vnd.ms-opentype', - '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. * @@ -142,17 +119,5 @@ public static function get_font_collection( $slug ) { } return new WP_Error( 'font_collection_not_found', 'Font collection not found.' ); } - - /** - * Sets the allowed mime types for fonts. - * - * @since 6.5.0 - * - * @param array $mime_types List of allowed mime types. - * @return array Modified upload directory. - */ - public static function set_allowed_mime_types( $mime_types ) { - return array_merge( $mime_types, self::get_expected_font_mime_types_per_php_version() ); - } } } diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php index 2f810640c55f05..b75ce0ec41cf69 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php @@ -213,5 +213,25 @@ private static function apply_sanitizer( $value, $sanitizer ) { } return call_user_func( $sanitizer, $value ); } + + /** + * 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.5.0 + * + * @return Array A collection of mime types keyed by file extension. + */ + public static function get_allowed_font_mime_types() { + $php_7_ttf_mime_type = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf'; + + return array( + 'otf' => 'application/vnd.ms-opentype', + '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', + ); + } } } diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index 22a843e7e69ed0..60f04e9ff23df3 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -847,7 +847,7 @@ protected function sanitize_src( $value ) { * @return array Array containing uploaded file attributes on success, or error on failure. */ protected function handle_font_file_upload( $file ) { - add_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) ); + add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); add_filter( 'upload_dir', 'wp_get_font_dir' ); $overrides = array( @@ -861,13 +861,13 @@ protected function handle_font_file_upload( $file ) { // See wp_check_filetype_and_ext(). 'test_type' => true, // Only allow uploading font files for this request. - 'mimes' => WP_Font_Library::get_expected_font_mime_types_per_php_version(), + 'mimes' => WP_Font_Utils::get_allowed_font_mime_types(), ); $uploaded_file = wp_handle_upload( $file, $overrides ); remove_filter( 'upload_dir', 'wp_get_font_dir' ); - remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) ); + remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); return $uploaded_file; } diff --git a/phpunit/tests/fonts/font-library/fontLibraryHooks.php b/phpunit/tests/fonts/font-library/fontLibraryHooks.php index 550f3992c9f73e..85d631ecaa45f2 100644 --- a/phpunit/tests/fonts/font-library/fontLibraryHooks.php +++ b/phpunit/tests/fonts/font-library/fontLibraryHooks.php @@ -73,7 +73,7 @@ protected function upload_font_file( $font_filename ) { // @core-merge Use `DIR_TESTDATA` instead of `GUTENBERG_DIR_TESTDATA`. $font_file_path = GUTENBERG_DIR_TESTDATA . 'fonts/' . $font_filename; - add_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) ); + add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); add_filter( 'upload_dir', 'wp_get_font_dir' ); $font_file = wp_upload_bits( $font_filename, @@ -81,7 +81,7 @@ protected function upload_font_file( $font_filename ) { file_get_contents( $font_file_path ) ); remove_filter( 'upload_dir', 'wp_get_font_dir' ); - remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) ); + remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); return $font_file; } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php deleted file mode 100644 index 4fb97e754611ff..00000000000000 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php +++ /dev/null @@ -1,89 +0,0 @@ -assertSame( $expected, $mimes ); - } - - /** - * 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' => 'application/vnd.ms-opentype', - 'ttf' => 'application/x-font-ttf', - 'woff' => 'application/font-woff', - 'woff2' => 'application/font-woff2', - ), - ), - 'version 7.3' => array( - 'php_version_id' => 70300, - 'expected' => array( - 'otf' => 'application/vnd.ms-opentype', - 'ttf' => 'application/font-sfnt', - 'woff' => 'application/font-woff', - 'woff2' => 'application/font-woff2', - ), - ), - 'version 7.4' => array( - 'php_version_id' => 70400, - 'expected' => array( - 'otf' => 'application/vnd.ms-opentype', - 'ttf' => 'font/sfnt', - 'woff' => 'application/font-woff', - 'woff2' => 'application/font-woff2', - ), - ), - 'version 8.0' => array( - 'php_version_id' => 80000, - 'expected' => array( - 'otf' => 'application/vnd.ms-opentype', - 'ttf' => 'font/sfnt', - 'woff' => 'application/font-woff', - 'woff2' => 'application/font-woff2', - ), - ), - 'version 8.1' => array( - 'php_version_id' => 80100, - 'expected' => array( - 'otf' => 'application/vnd.ms-opentype', - 'ttf' => 'font/sfnt', - 'woff' => 'font/woff', - 'woff2' => 'font/woff2', - ), - ), - 'version 8.2' => array( - 'php_version_id' => 80200, - 'expected' => array( - 'otf' => 'application/vnd.ms-opentype', - 'ttf' => 'font/sfnt', - 'woff' => 'font/woff', - 'woff2' => 'font/woff2', - ), - ), - ); - } -}