Changeset 56688
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/fonts/class-wp-font-face-resolver.php
r56500 r56688 51 51 foreach ( $font_families as $definition ) { 52 52 53 // Skip if font-family "name" is not defined.54 if ( empty( $definition['name'] ) ) {55 continue;56 }57 58 53 // Skip if "fontFace" is not defined, meaning there are no variations. 59 54 if ( empty( $definition['fontFace'] ) ) { … … 61 56 } 62 57 63 $font_family = $definition['name']; 58 // Skip if "fontFamily" is not defined. 59 if ( empty( $definition['fontFamily'] ) ) { 60 continue; 61 } 62 63 $font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] ); 64 65 // Skip if no font family is defined. 66 if ( empty( $font_family_name ) ) { 67 continue; 68 } 64 69 65 70 // Prepare the fonts array structure for this font-family. 66 if ( ! array_key_exists( $font_family , $fonts ) ) {67 $fonts[ $font_family ] = array();71 if ( ! array_key_exists( $font_family_name, $fonts ) ) { 72 $fonts[ $font_family_name ] = array(); 68 73 } 69 74 70 $fonts[ $font_family ] = static::convert_font_face_properties( $definition['fontFace'], $font_family);75 $fonts[ $font_family_name ] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name ); 71 76 } 72 77 } 73 78 74 79 return $fonts; 80 } 81 82 /** 83 * Parse font-family name from comma-separated lists. 84 * 85 * If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ), 86 * parse and return the fist font from the list. 87 * 88 * @since 6.4.0 89 * 90 * @param string $font_family Font family `fontFamily' to parse. 91 * @return string Font-family name. 92 */ 93 private static function maybe_parse_name_from_comma_separated_list( $font_family ) { 94 if ( str_contains( $font_family, ',' ) ) { 95 $font_family = explode( ',', $font_family )[0]; 96 } 97 98 return trim( $font_family, "\"'" ); 75 99 } 76 100 -
trunk/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromThemeJson.php
r56500 r56688 97 97 ); 98 98 } 99 100 /** 101 * @dataProvider data_should_get_font_family_name 102 * 103 * @param array $fonts Fonts to test. 104 * @param string $expected_name Expected font-family name. 105 */ 106 public function test_should_get_font_family_name( $fonts, $expected_name ) { 107 switch_theme( static::FONTS_THEME ); 108 109 $replace_fonts = static function ( $theme_json_data ) use ( $fonts ) { 110 $data = $theme_json_data->get_data(); 111 112 // Replace typography.fontFamilies. 113 $data['settings']['typography']['fontFamilies']['theme'] = $fonts; 114 115 return new WP_Theme_JSON_Data( $data ); 116 }; 117 add_filter( 'wp_theme_json_data_theme', $replace_fonts ); 118 $fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json(); 119 remove_filter( 'wp_theme_json_data_theme', $replace_fonts ); 120 121 $this->assertArrayHasKey( $expected_name, $fonts ); 122 } 123 124 /** 125 * Data provider. 126 * 127 * @return array 128 */ 129 public function data_should_get_font_family_name() { 130 $font_face = array( 131 array( 132 'fontFamily' => 'DM Sans', 133 'fontStretch' => 'normal', 134 'fontStyle' => 'normal', 135 'fontWeight' => '400', 136 'src' => array( 137 'file:./assets/fonts/dm-sans/DMSans-Regular.woff2', 138 ), 139 ), 140 array( 141 'fontFamily' => 'DM Sans', 142 'fontStretch' => 'normal', 143 'fontStyle' => 'italic', 144 'fontWeight' => '400', 145 'src' => array( 146 'file:./assets/fonts/dm-sans/DMSans-Regular-Italic.woff2', 147 ), 148 ), 149 array( 150 'fontFamily' => 'DM Sans', 151 'fontStretch' => 'normal', 152 'fontStyle' => 'italic', 153 'fontWeight' => '700', 154 'src' => array( 155 'file:./assets/fonts/dm-sans/DMSans-Bold.woff2', 156 ), 157 ), 158 array( 159 'fontFamily' => 'DM Sans', 160 'fontStretch' => 'normal', 161 'fontStyle' => 'italic', 162 'fontWeight' => '700', 163 'src' => array( 164 'file:./assets/fonts/dm-sans/DMSans-Bold-Italic.woff2', 165 ), 166 ), 167 ); 168 169 return array( 170 'name declared' => array( 171 'fonts' => array( 172 array( 173 'fontFamily' => 'DM Sans', 174 'name' => 'DM Sans Family', 175 'slug' => 'dm-sans', 176 'fontFace' => $font_face, 177 ), 178 ), 179 'expected_name' => 'DM Sans', 180 ), 181 'name not declared' => array( 182 'fonts' => array( 183 array( 184 'fontFamily' => 'DM Sans', 185 'slug' => 'dm-sans', 186 'fontFace' => $font_face, 187 ), 188 ), 189 'expected_name' => 'DM Sans', 190 ), 191 'fontFamily comma-separated list' => array( 192 'fonts' => array( 193 array( 194 'fontFamily' => '"DM Sans", sans-serif', 195 'slug' => 'dm-sans', 196 'fontFace' => $font_face, 197 ), 198 ), 199 'expected_name' => 'DM Sans', 200 ), 201 ); 202 } 99 203 }
Note: See TracChangeset
for help on using the changeset viewer.