Skip to content

Commit

Permalink
Testeable mime type conditionals
Browse files Browse the repository at this point in the history
porting WordPress/gutenberg#54844

Co-authored-by: Jason Crist <146530+pbking@users.noreply.github.com>
  • Loading branch information
matiasbenedetto and pbking committed Sep 27, 2023
1 parent ae15e18 commit 04fe719
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/wp-includes/fonts/class-wp-font-family-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
2 changes: 1 addition & 1 deletion src/wp-includes/fonts/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 20 additions & 11 deletions src/wp-includes/fonts/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Test WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version
*/
class Tests_Fonts_WpFontsFamilyUtils_GetMimeTypes extends WP_UnitTestCase {

/**
*
* @dataProvider data_should_supply_correct_mime_type_for_php_version
*
* @param array $php_version_id PHP_VERSION_ID value.
* @param array $expected Expected mime types.
*/
public function test_should_supply_correct_mime_type_for_php_version( $php_version_id, $expected ) {
$mimes = WP_Font_Library::get_expected_font_mime_types_per_php_version( $php_version_id );
$this->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',
),
),
);
}
}

0 comments on commit 04fe719

Please sign in to comment.