diff --git a/inc/Engine/Optimization/CSSTrait.php b/inc/Engine/Optimization/CSSTrait.php index 321e70b8a0..c0499e6bf9 100644 --- a/inc/Engine/Optimization/CSSTrait.php +++ b/inc/Engine/Optimization/CSSTrait.php @@ -204,14 +204,16 @@ private function apply_font_display_swap( $css_file_content ) { $css_file_content = (string) $css_file_content; return preg_replace_callback( - '/(?:@font-face)\s*{(?[^}]+)}/', + '/(?:@font-face)\s*{(?[^}]+)}/i', function ( $matches ) { - if ( false !== strpos( $matches['value'], 'font-display' ) ) { - return $matches[0]; + if ( preg_match( '/font-display:\s*(?\w*);?/i', $matches['value'], $attribute ) ) { + return 'swap' === strtolower( $attribute['swap_value'] ) + ? $matches[0] + : str_replace( $attribute['swap_value'], 'swap', $matches[0] ); + } else { + $swap = "font-display:swap;{$matches['value']}"; } - $swap = "font-display:swap;{$matches['value']}"; - return str_replace( $matches['value'], $swap, $matches[0] ); }, $css_file_content diff --git a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php index ee79de6f0c..0483dcdb5e 100644 --- a/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php +++ b/inc/Engine/Optimization/GoogleFonts/AbstractGFOptimization.php @@ -46,11 +46,10 @@ protected function get_font_with_display( array $font ) { return $font[0]; } - $display = $this->get_font_display_value(); $parsed_font = wp_parse_args( $query ); $font_url = ! empty( $parsed_font['display'] ) - ? str_replace( "&display={$parsed_font['display']}", "&display={$display}", $font_url ) - : "{$font_url}&display={$display}"; + ? str_replace( "&display={$parsed_font['display']}", '&display=swap', $font_url ) + : "{$font_url}&display=swap"; return str_replace( $font['url'], esc_url( $font_url ), $font[0] ); } diff --git a/inc/Engine/Optimization/GoogleFonts/Combine.php b/inc/Engine/Optimization/GoogleFonts/Combine.php index 6dda5e82a5..3025f2dbf7 100644 --- a/inc/Engine/Optimization/GoogleFonts/Combine.php +++ b/inc/Engine/Optimization/GoogleFonts/Combine.php @@ -141,11 +141,9 @@ private function parse( array $matches ) { * @return string */ private function get_combine_tag() { - $display = $this->get_font_display_value(); - return sprintf( '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet - esc_url( "https://fonts.googleapis.com/css?family={$this->fonts}{$this->subsets}&display={$display}" ) + esc_url( "https://fonts.googleapis.com/css?family={$this->fonts}{$this->subsets}&display=swap" ) ); } } diff --git a/inc/Engine/Optimization/GoogleFonts/CombineV2.php b/inc/Engine/Optimization/GoogleFonts/CombineV2.php index a04e4e8ed3..be73f7a135 100644 --- a/inc/Engine/Optimization/GoogleFonts/CombineV2.php +++ b/inc/Engine/Optimization/GoogleFonts/CombineV2.php @@ -139,10 +139,9 @@ protected function parse( array $tag ) { * @return string */ protected function get_combine_tag( array $families ): string { - $display = $this->get_font_display_value(); return sprintf( '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet - esc_url( "https://fonts.googleapis.com/css2{$this->get_concatenated_families( $families )}&display={$display}" ) + esc_url( "https://fonts.googleapis.com/css2{$this->get_concatenated_families( $families )}&display=swap" ) ); } diff --git a/tests/Fixtures/inc/Engine/Optimization/CSSTraitApplyFontDisplaySwap.php b/tests/Fixtures/inc/Engine/Optimization/CSSTraitApplyFontDisplaySwap.php index 7d07065131..53561e170c 100644 --- a/tests/Fixtures/inc/Engine/Optimization/CSSTraitApplyFontDisplaySwap.php +++ b/tests/Fixtures/inc/Engine/Optimization/CSSTraitApplyFontDisplaySwap.php @@ -46,7 +46,7 @@ CSS ], - 'shouldNotChangeFontDisplayAttributeWhenAlreadySetInRule' => [ + 'shouldChangeFontDisplayAttributeWhenAlreadySetInRule' => [ 'css' => <<' . '' ], -]; \ No newline at end of file + 'shouldReplaceAnotherFontDisplayValueWithSwap' => [ + 'given' => + '' . + '' . + '' . + 'Sample Page' . + '' . + '' . + '' . + '' . + '' . + '' . + '' + , + 'expected' => + '' . + '' . + '' . + 'Sample Page' . + '' . + '' . + '' . + '' . + '' . + '' + ], +];