Skip to content
Closed
Prev Previous commit
Next Next commit
Restore the get_allowed_font_mime_types function
  • Loading branch information
youknowriad committed Feb 5, 2024
commit bbf2f6b59454a3147f6d9467678ca6df0e1d81c6
20 changes: 20 additions & 0 deletions src/wp-includes/fonts/class-wp-font-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,24 @@ 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',
);
}
}
15 changes: 2 additions & 13 deletions tests/phpunit/tests/fonts/font-library/fontLibraryHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,18 @@ protected function create_font_face_with_file( $filename ) {
return array( $font_face_id, $font_path );
}

public function get_expected_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',
);
}

protected function upload_font_file( $font_filename ) {
$font_file_path = DIR_TESTDATA . '/fonts/' . $font_filename;

add_filter( 'upload_mimes', array( $this, 'get_expected_font_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( $this, 'get_expected_font_mime_types' ) );
remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) );

return $font_file;
}
Expand Down