Skip to content

Commit 04fe719

Browse files
Testeable mime type conditionals
porting WordPress/gutenberg#54844 Co-authored-by: Jason Crist <[email protected]>
1 parent ae15e18 commit 04fe719

File tree

4 files changed

+114
-14
lines changed

4 files changed

+114
-14
lines changed

src/wp-includes/fonts/class-wp-font-family-utils.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public static function merge_fonts_data( $font1, $font2 ) {
8181
* @return bool True if the file has a font MIME type, false otherwise.
8282
*/
8383
public static function has_font_mime_type( $filepath ) {
84-
$filetype = wp_check_filetype( $filepath, WP_Font_Library::ALLOWED_FONT_MIME_TYPES );
84+
$allowed_mime_types = WP_Font_Library::get_expected_font_mime_types_per_php_version();
85+
$filetype = wp_check_filetype( $filepath, $allowed_mime_types );
8586

86-
return in_array( $filetype['type'], WP_Font_Library::ALLOWED_FONT_MIME_TYPES, true );
87+
return in_array( $filetype['type'], $allowed_mime_types, true );
8788
}
8889
}

src/wp-includes/fonts/class-wp-font-family.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private function get_upload_overrides( $filename ) {
178178
// Seems mime type for files that are not images cannot be tested.
179179
// See wp_check_filetype_and_ext().
180180
'test_type' => true,
181-
'mimes' => WP_Font_Library::ALLOWED_FONT_MIME_TYPES,
181+
'mimes' => WP_Font_Library::get_expected_font_mime_types_per_php_version(),
182182
'unique_filename_callback' => static function () use ( $filename ) {
183183
// Keep the original filename.
184184
return $filename;

src/wp-includes/fonts/class-wp-font-library.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@
1717
class WP_Font_Library {
1818

1919
/**
20-
* Fonts mime types allowed for each file type.
21-
* Each file type can have multiple mime types depending on the PHP version.
22-
* Currently wp_check_filetype_and_ext() only allows one mime type per file extension.
20+
* 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.
21+
*
22+
* This is necessary until a collection of valid mime-types per-file extension can be provided to 'upload_mimes' filter.
23+
*
24+
* @since 6.4.0
25+
*
26+
* @param array $php_version_id The version of PHP to provide mime types for. The default is the current PHP version.
27+
*
28+
* @return Array A collection of mime types keyed by file extension.
2329
*/
24-
const PHP_7_TTF_MIME_TYPE = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf';
30+
public static function get_expected_font_mime_types_per_php_version( $php_version_id = PHP_VERSION_ID ) {
31+
32+
$php_7_ttf_mime_type = $php_version_id >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf';
2533

26-
const ALLOWED_FONT_MIME_TYPES = array(
27-
'otf' => 'font/otf',
28-
'ttf' => PHP_VERSION_ID >= 70400 ? 'font/sfnt' : self::PHP_7_TTF_MIME_TYPE,
29-
'woff' => PHP_VERSION_ID >= 80100 ? 'font/woff' : 'application/font-woff',
30-
'woff2' => PHP_VERSION_ID >= 80100 ? 'font/woff2' : 'application/font-woff2',
31-
);
34+
return array(
35+
'otf' => 'font/otf',
36+
'ttf' => $php_version_id >= 70400 ? 'font/sfnt' : $php_7_ttf_mime_type,
37+
'woff' => $php_version_id >= 80100 ? 'font/woff' : 'application/font-woff',
38+
'woff2' => $php_version_id >= 80100 ? 'font/woff2' : 'application/font-woff2',
39+
);
40+
}
3241

3342
/**
3443
* Font collections.
@@ -131,6 +140,6 @@ public static function set_upload_dir( $defaults ) {
131140
* @return array Modified upload directory.
132141
*/
133142
public static function set_allowed_mime_types( $mime_types ) {
134-
return array_merge( $mime_types, self::ALLOWED_FONT_MIME_TYPES );
143+
return array_merge( $mime_types, self::get_expected_font_mime_types_per_php_version() );
135144
}
136145
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Test WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version().
4+
*
5+
* @package WordPress
6+
* @subpackage Font Library
7+
*
8+
* @group fonts
9+
* @group font-library
10+
*
11+
* @covers WP_Font_Family_Utils::get_expected_font_mime_types_per_php_version
12+
*/
13+
class Tests_Fonts_WpFontsFamilyUtils_GetMimeTypes extends WP_UnitTestCase {
14+
15+
/**
16+
*
17+
* @dataProvider data_should_supply_correct_mime_type_for_php_version
18+
*
19+
* @param array $php_version_id PHP_VERSION_ID value.
20+
* @param array $expected Expected mime types.
21+
*/
22+
public function test_should_supply_correct_mime_type_for_php_version( $php_version_id, $expected ) {
23+
$mimes = WP_Font_Library::get_expected_font_mime_types_per_php_version( $php_version_id );
24+
$this->assertEquals( $mimes, $expected );
25+
}
26+
27+
/**
28+
* Data provider.
29+
*
30+
* @return array[]
31+
*/
32+
public function data_should_supply_correct_mime_type_for_php_version() {
33+
return array(
34+
'version 7.2' => array(
35+
'php_version_id' => 70200,
36+
'expected' => array(
37+
'otf' => 'font/otf',
38+
'ttf' => 'application/x-font-ttf',
39+
'woff' => 'application/font-woff',
40+
'woff2' => 'application/font-woff2',
41+
),
42+
),
43+
'version 7.3' => array(
44+
'php_version_id' => 70300,
45+
'expected' => array(
46+
'otf' => 'font/otf',
47+
'ttf' => 'application/font-sfnt',
48+
'woff' => 'application/font-woff',
49+
'woff2' => 'application/font-woff2',
50+
),
51+
),
52+
'version 7.4' => array(
53+
'php_version_id' => 70400,
54+
'expected' => array(
55+
'otf' => 'font/otf',
56+
'ttf' => 'font/sfnt',
57+
'woff' => 'application/font-woff',
58+
'woff2' => 'application/font-woff2',
59+
),
60+
),
61+
'version 8.0' => array(
62+
'php_version_id' => 80000,
63+
'expected' => array(
64+
'otf' => 'font/otf',
65+
'ttf' => 'font/sfnt',
66+
'woff' => 'application/font-woff',
67+
'woff2' => 'application/font-woff2',
68+
),
69+
),
70+
'version 8.1' => array(
71+
'php_version_id' => 80100,
72+
'expected' => array(
73+
'otf' => 'font/otf',
74+
'ttf' => 'font/sfnt',
75+
'woff' => 'font/woff',
76+
'woff2' => 'font/woff2',
77+
),
78+
),
79+
'version 8.2' => array(
80+
'php_version_id' => 80200,
81+
'expected' => array(
82+
'otf' => 'font/otf',
83+
'ttf' => 'font/sfnt',
84+
'woff' => 'font/woff',
85+
'woff2' => 'font/woff2',
86+
),
87+
),
88+
);
89+
}
90+
}

0 commit comments

Comments
 (0)