From 201d1cf2cfaf069b85af9727c4dfb4cf9be5e504 Mon Sep 17 00:00:00 2001 From: Sandeep Dahiya Date: Sun, 15 Jun 2025 15:54:03 +0530 Subject: [PATCH 1/6] fix:build-font-face-css single inverted comma in name issue --- src/wp-includes/fonts/class-wp-font-face.php | 27 +++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index 07cd3d6de9002..cdeac73ef8537 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -346,6 +346,25 @@ private function order_src( array $font_face ) { return $font_face; } + /** + * Wraps font-family in quotes if needed. + * + * @since 6.x.x + * + * @param string $item Font-family name. + * @return string Quoted font-family if needed. + */ + + private function maybe_add_quotes( $item ) { + $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; + $item = trim( $item ); + if ( preg_match( $regex, $item ) ) { + $item = trim( $item, "\"'" ); + return '"' . $item . '"'; + } + return $item; + } + /** * Builds the font-family's CSS. @@ -362,13 +381,7 @@ private function build_font_face_css( array $font_face ) { * Wrap font-family in quotes if it contains spaces * and is not already wrapped in quotes. */ - if ( - str_contains( $font_face['font-family'], ' ' ) && - ! str_contains( $font_face['font-family'], '"' ) && - ! str_contains( $font_face['font-family'], "'" ) - ) { - $font_face['font-family'] = '"' . $font_face['font-family'] . '"'; - } + $font_face['font-family'] = self::maybe_add_quotes( $font_face['font-family'] ); foreach ( $font_face as $key => $value ) { // Compile the "src" parameter. From 08ad65bf20252589525599709fd975b7abe29a10 Mon Sep 17 00:00:00 2001 From: Sandeep Dahiya Date: Sun, 15 Jun 2025 17:30:47 +0530 Subject: [PATCH 2/6] removed whitespace from the end --- src/wp-includes/fonts/class-wp-font-face.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index cdeac73ef8537..68b8159164902 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -354,7 +354,6 @@ private function order_src( array $font_face ) { * @param string $item Font-family name. * @return string Quoted font-family if needed. */ - private function maybe_add_quotes( $item ) { $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; $item = trim( $item ); @@ -365,7 +364,6 @@ private function maybe_add_quotes( $item ) { return $item; } - /** * Builds the font-family's CSS. * From f09ea757565105b3734ae187bf2c43561f2de635 Mon Sep 17 00:00:00 2001 From: Sandeep Dahiya Date: Mon, 16 Jun 2025 12:14:25 +0530 Subject: [PATCH 3/6] using WP_Font_Utils::maybe_add_quotes() after exposing it public --- src/wp-includes/fonts/class-wp-font-face.php | 21 ++----------------- src/wp-includes/fonts/class-wp-font-utils.php | 2 +- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index 68b8159164902..e7aa8b9d1e538 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -346,24 +346,7 @@ private function order_src( array $font_face ) { return $font_face; } - /** - * Wraps font-family in quotes if needed. - * - * @since 6.x.x - * - * @param string $item Font-family name. - * @return string Quoted font-family if needed. - */ - private function maybe_add_quotes( $item ) { - $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; - $item = trim( $item ); - if ( preg_match( $regex, $item ) ) { - $item = trim( $item, "\"'" ); - return '"' . $item . '"'; - } - return $item; - } - + /** * Builds the font-family's CSS. * @@ -379,7 +362,7 @@ private function build_font_face_css( array $font_face ) { * Wrap font-family in quotes if it contains spaces * and is not already wrapped in quotes. */ - $font_face['font-family'] = self::maybe_add_quotes( $font_face['font-family'] ); + $font_face['font-family'] = WP_Font_Utils::maybe_add_quotes( $font_face['font-family'] ); foreach ( $font_face as $key => $value ) { // Compile the "src" parameter. diff --git a/src/wp-includes/fonts/class-wp-font-utils.php b/src/wp-includes/fonts/class-wp-font-utils.php index 0ec36abc3f64b..c783bebfe6bc9 100644 --- a/src/wp-includes/fonts/class-wp-font-utils.php +++ b/src/wp-includes/fonts/class-wp-font-utils.php @@ -29,7 +29,7 @@ class WP_Font_Utils { * @param string $item A font family name. * @return string The font family name with surrounding quotes, if necessary. */ - private static function maybe_add_quotes( $item ) { + public static function maybe_add_quotes( $item ) { // Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens). $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; $item = trim( $item ); From 9f2953761ad7ed85284a46acc13c266258799a0d Mon Sep 17 00:00:00 2001 From: Sandeep Dahiya Date: Tue, 17 Jun 2025 09:18:09 +0530 Subject: [PATCH 4/6] renamed to normalize_quoted_font_family_name and added more edge cases --- src/wp-includes/fonts/class-wp-font-face.php | 2 +- src/wp-includes/fonts/class-wp-font-utils.php | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index e7aa8b9d1e538..e62a76e853d0e 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -362,7 +362,7 @@ private function build_font_face_css( array $font_face ) { * Wrap font-family in quotes if it contains spaces * and is not already wrapped in quotes. */ - $font_face['font-family'] = WP_Font_Utils::maybe_add_quotes( $font_face['font-family'] ); + $font_face['font-family'] = WP_Font_Utils::normalize_quoted_font_family_name( $font_face['font-family'] ); foreach ( $font_face as $key => $value ) { // Compile the "src" parameter. diff --git a/src/wp-includes/fonts/class-wp-font-utils.php b/src/wp-includes/fonts/class-wp-font-utils.php index c783bebfe6bc9..93c842d9d976b 100644 --- a/src/wp-includes/fonts/class-wp-font-utils.php +++ b/src/wp-includes/fonts/class-wp-font-utils.php @@ -29,12 +29,15 @@ class WP_Font_Utils { * @param string $item A font family name. * @return string The font family name with surrounding quotes, if necessary. */ - public static function maybe_add_quotes( $item ) { + public static function normalize_quoted_font_family_name( $item ) { // Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens). $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; $item = trim( $item ); if ( preg_match( $regex, $item ) ) { $item = trim( $item, "\"'" ); + $item = str_replace( [ "\r\n", "\r", "\n" ], '\\A', $item ); + $item = str_replace( '\\', '\\\\', $item ); + $item = str_replace( '"', '\\"', $item ); return '"' . $item . '"'; } return $item; @@ -67,14 +70,14 @@ public static function sanitize_font_family( $font_family ) { if ( str_contains( $output, ',' ) ) { $items = explode( ',', $output ); foreach ( $items as $item ) { - $formatted_item = self::maybe_add_quotes( $item ); + $formatted_item = self::normalize_quoted_font_family_name( $item ); if ( ! empty( $formatted_item ) ) { $formatted_items[] = $formatted_item; } } return implode( ', ', $formatted_items ); } - return self::maybe_add_quotes( $output ); + return self::normalize_quoted_font_family_name( $output ); } /** From b88e32c6cfd83189e9d948da5e6469834873741d Mon Sep 17 00:00:00 2001 From: Sandeep Dahiya Date: Tue, 17 Jun 2025 14:24:27 +0530 Subject: [PATCH 5/6] "Reverting to previous minor fix" This reverts commit 9f2953761ad7ed85284a46acc13c266258799a0d. --- src/wp-includes/fonts/class-wp-font-face.php | 2 +- src/wp-includes/fonts/class-wp-font-utils.php | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index e62a76e853d0e..e7aa8b9d1e538 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -362,7 +362,7 @@ private function build_font_face_css( array $font_face ) { * Wrap font-family in quotes if it contains spaces * and is not already wrapped in quotes. */ - $font_face['font-family'] = WP_Font_Utils::normalize_quoted_font_family_name( $font_face['font-family'] ); + $font_face['font-family'] = WP_Font_Utils::maybe_add_quotes( $font_face['font-family'] ); foreach ( $font_face as $key => $value ) { // Compile the "src" parameter. diff --git a/src/wp-includes/fonts/class-wp-font-utils.php b/src/wp-includes/fonts/class-wp-font-utils.php index 93c842d9d976b..c783bebfe6bc9 100644 --- a/src/wp-includes/fonts/class-wp-font-utils.php +++ b/src/wp-includes/fonts/class-wp-font-utils.php @@ -29,15 +29,12 @@ class WP_Font_Utils { * @param string $item A font family name. * @return string The font family name with surrounding quotes, if necessary. */ - public static function normalize_quoted_font_family_name( $item ) { + public static function maybe_add_quotes( $item ) { // Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens). $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; $item = trim( $item ); if ( preg_match( $regex, $item ) ) { $item = trim( $item, "\"'" ); - $item = str_replace( [ "\r\n", "\r", "\n" ], '\\A', $item ); - $item = str_replace( '\\', '\\\\', $item ); - $item = str_replace( '"', '\\"', $item ); return '"' . $item . '"'; } return $item; @@ -70,14 +67,14 @@ public static function sanitize_font_family( $font_family ) { if ( str_contains( $output, ',' ) ) { $items = explode( ',', $output ); foreach ( $items as $item ) { - $formatted_item = self::normalize_quoted_font_family_name( $item ); + $formatted_item = self::maybe_add_quotes( $item ); if ( ! empty( $formatted_item ) ) { $formatted_items[] = $formatted_item; } } return implode( ', ', $formatted_items ); } - return self::normalize_quoted_font_family_name( $output ); + return self::maybe_add_quotes( $output ); } /** From 044e2618df4f6d591b9f51d4b5c7798f4b5ad2a9 Mon Sep 17 00:00:00 2001 From: Sandeep Dahiya Date: Tue, 17 Jun 2025 14:47:28 +0530 Subject: [PATCH 6/6] removed whitespace and renamed to normalize_css_font_family_name --- src/wp-includes/fonts/class-wp-font-face.php | 4 ++-- src/wp-includes/fonts/class-wp-font-utils.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index e7aa8b9d1e538..3dc4ab405e0dd 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -346,7 +346,7 @@ private function order_src( array $font_face ) { return $font_face; } - + /** * Builds the font-family's CSS. * @@ -362,7 +362,7 @@ private function build_font_face_css( array $font_face ) { * Wrap font-family in quotes if it contains spaces * and is not already wrapped in quotes. */ - $font_face['font-family'] = WP_Font_Utils::maybe_add_quotes( $font_face['font-family'] ); + $font_face['font-family'] = WP_Font_Utils::normalize_css_font_family_name( $font_face['font-family'] ); foreach ( $font_face as $key => $value ) { // Compile the "src" parameter. diff --git a/src/wp-includes/fonts/class-wp-font-utils.php b/src/wp-includes/fonts/class-wp-font-utils.php index c783bebfe6bc9..df75c8983bb2d 100644 --- a/src/wp-includes/fonts/class-wp-font-utils.php +++ b/src/wp-includes/fonts/class-wp-font-utils.php @@ -29,7 +29,7 @@ class WP_Font_Utils { * @param string $item A font family name. * @return string The font family name with surrounding quotes, if necessary. */ - public static function maybe_add_quotes( $item ) { + public static function normalize_css_font_family_name( $item ) { // Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens). $regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/'; $item = trim( $item ); @@ -67,14 +67,14 @@ public static function sanitize_font_family( $font_family ) { if ( str_contains( $output, ',' ) ) { $items = explode( ',', $output ); foreach ( $items as $item ) { - $formatted_item = self::maybe_add_quotes( $item ); + $formatted_item = self::normalize_css_font_family_name( $item ); if ( ! empty( $formatted_item ) ) { $formatted_items[] = $formatted_item; } } return implode( ', ', $formatted_items ); } - return self::maybe_add_quotes( $output ); + return self::normalize_css_font_family_name( $output ); } /**