Skip to content

Commit

Permalink
Move getAllowedMimeTypes to FontUtils (#58667)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: getdave <get_dave@git.wordpress.org>
  • Loading branch information
3 people authored Feb 5, 2024
1 parent 714e88c commit 528d2b7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 129 deletions.
35 changes: 0 additions & 35 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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() );
}
}
}
20 changes: 20 additions & 0 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions phpunit/tests/fonts/font-library/fontLibraryHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ 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,
null,
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;
}
Expand Down
89 changes: 0 additions & 89 deletions phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php

This file was deleted.

0 comments on commit 528d2b7

Please sign in to comment.