From 4aa03820077f02657e4504904538bbee23aaacf9 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Fri, 22 Sep 2023 18:08:20 -0500 Subject: [PATCH] Font Face: Gets name from "fontFamily" setting as fallback (#54615) Iff the "name" setting does not exist (as it's not required by the schema or documentation), get the font-family name from the required "fontFamily" setting, i.e. within each `typography.fontFamilies`. --- .../font-face/class-wp-font-face-resolver.php | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php b/lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php index 2ef31f3ac33b0..01b9d4566aa67 100644 --- a/lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php +++ b/lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php @@ -56,30 +56,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'] ) ) { - // continue; - // } - // Skip if "fontFace" is not defined, meaning there are no variations. if ( empty( $definition['fontFace'] ) ) { continue; } - $font_family = $definition['name']; + // Skip if "fontFamily" is not defined. + if ( empty( $definition['fontFamily'] ) ) { + continue; + } + + $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. *