From 9122ef68e1093e70b3996457fb64a5d933d9b112 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Mon, 25 Sep 2023 21:29:17 +0000 Subject: [PATCH] Fonts: Get font-family name from 'fontFamily' field. Instead of getting the name from the optional `'name'` field, the font-family name now comes from the required `'fontFamily'` field. This change fixes a back-compat (BC) break in how the font-family name is pulled from the incoming font data in the `WP_Font_Face_Resolver`. Why? WP Core does not require the `'name'` field in theme.json. For themes that do not declare it, that set of font variations is ignored, thus causing a BC break from how the stopgap code worked (see [53282]). However, `WP_Theme_JSON` schema does require the `fontFamily` field in each of the `typography.fontFamilies`. == Other details: Includes a parser to extract the first entry when a `fontFamily` field has a comma-separated list of font-families, e.g. `Inter, sans-serif`. References: * Merge from Gutenberg's PR https://github.com/WordPress/gutenberg/pull/54615. Follow-up to [56500], [53282]. Props ironprogrammer, hellofromTonya, mmaattiiaass, pbking. Fixes #59165. Built from https://develop.svn.wordpress.org/trunk@56688 git-svn-id: http://core.svn.wordpress.org/trunk@56200 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../fonts/class-wp-font-face-resolver.php | 40 +++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/wp-includes/fonts/class-wp-font-face-resolver.php b/wp-includes/fonts/class-wp-font-face-resolver.php index da1ea1c711a..125ff6a2277 100644 --- a/wp-includes/fonts/class-wp-font-face-resolver.php +++ b/wp-includes/fonts/class-wp-font-face-resolver.php @@ -50,30 +50,54 @@ private static function parse_settings( array $settings ) { foreach ( $settings['typography']['fontFamilies'] as $font_families ) { foreach ( $font_families as $definition ) { - // Skip if font-family "name" is not defined. - if ( empty( $definition['name'] ) ) { + // Skip if "fontFace" is not defined, meaning there are no variations. + if ( empty( $definition['fontFace'] ) ) { continue; } - // Skip if "fontFace" is not defined, meaning there are no variations. - if ( empty( $definition['fontFace'] ) ) { + // Skip if "fontFamily" is not defined. + if ( empty( $definition['fontFamily'] ) ) { continue; } - $font_family = $definition['name']; + $font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] ); + + // Skip if no font family is defined. + if ( empty( $font_family_name ) ) { + continue; + } // Prepare the fonts array structure for this font-family. - if ( ! array_key_exists( $font_family, $fonts ) ) { - $fonts[ $font_family ] = array(); + if ( ! array_key_exists( $font_family_name, $fonts ) ) { + $fonts[ $font_family_name ] = array(); } - $fonts[ $font_family ] = static::convert_font_face_properties( $definition['fontFace'], $font_family ); + $fonts[ $font_family_name ] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name ); } } return $fonts; } + /** + * Parse font-family name from comma-separated lists. + * + * If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ), + * parse and return the fist font from the list. + * + * @since 6.4.0 + * + * @param string $font_family Font family `fontFamily' to parse. + * @return string Font-family name. + */ + private static function maybe_parse_name_from_comma_separated_list( $font_family ) { + if ( str_contains( $font_family, ',' ) ) { + $font_family = explode( ',', $font_family )[0]; + } + + return trim( $font_family, "\"'" ); + } + /** * Converts font-face properties from theme.json format. * diff --git a/wp-includes/version.php b/wp-includes/version.php index e822d594897..324873b5b9c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56687'; +$wp_version = '6.4-alpha-56688'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.