Skip to content

Commit

Permalink
Closes #7189 Update self-host google fonts exclusion (#7194)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanna-meda authored Dec 19, 2024
2 parents eca50d3 + 2591bff commit a60bcda
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions inc/Engine/Media/Fonts/FontsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ protected function get_exclusions(): array {
}

/**
* Checks if a URL is excluded based on the provided exclusions.
* Checks if a font is excluded based on the provided exclusions.
*
* @param string $url The URL to check.
* @param string $subject The string to check.
* @param string[] $exclusions The list of exclusions.
*
* @return bool True if the URL is excluded, false otherwise.
*/
protected function is_excluded( string $url, array $exclusions ): bool {
protected function is_excluded( string $subject, array $exclusions ): bool {
// Bail out early if there are no exclusions.
if ( empty( $exclusions ) ) {
return false;
Expand All @@ -53,6 +53,6 @@ function ( $exclusion ) {
$exclusions_str = implode( '|', $escaped_exclusions );

// Check the URL against the combined regex pattern.
return (bool) preg_match( '#(' . $exclusions_str . ')#i', $url );
return (bool) preg_match( '#(' . $exclusions_str . ')#i', $subject );
}
}
4 changes: 2 additions & 2 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ private function rewrite_fonts( $html ): string {
$total_fonts = $total_v1 + $total_v2;

foreach ( $v1_fonts as $font ) {
if ( $this->is_excluded( $font['url'], $exclusions ) ) {
if ( $this->is_excluded( $font[0], $exclusions ) ) {
continue;
}
$html = $this->replace_font( $font, $html );
}

foreach ( $v2_fonts as $font ) {
if ( $this->is_excluded( $font['url'], $exclusions ) ) {
if ( $this->is_excluded( $font[0], $exclusions ) ) {
continue;
}
$html = $this->replace_font( $font, $html );
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Optimization/GoogleFonts/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function optimize( $html ): string {
$filtered_fonts = array_filter(
$fonts,
function ( $font ) use ( $exclusions ) {
return ! $this->is_excluded( $font['url'], $exclusions );
return ! $this->is_excluded( $font[0], $exclusions );
}
);

Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Optimization/GoogleFonts/CombineV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function optimize( $html ): string {
$filtered_tags = array_filter(
$font_tags,
function ( $tag ) use ( $exclusions ) {
return ! $this->is_excluded( $tag['url'], $exclusions );
return ! $this->is_excluded( $tag[0], $exclusions );
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<link href="https://fonts.googleapis.com/css?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-fonts/css/b/4/d/9ffca0114d3acb6ab0b068feaf933.css" data-wpr-hosted-gf-parameters="family=Lato:wght@400;700&display=swap"/>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<link rel='stylesheet' id='astra-google-fonts-css' href='https://fonts.googleapis.com/css?family=Noto+Sans%3A400%2C700%7CMontserrat%3A700&#038;display=fallback&#038;ver=4.7.0' media='all' />
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<link href="https://fonts.googleapis.com/css?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<link rel='stylesheet' id='astra-google-fonts-css' href='https://fonts.googleapis.com/css?family=Noto+Sans%3A400%2C700%7CMontserrat%3A700&#038;display=fallback&#038;ver=4.7.0' media='all' />
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@
],
'exclude_locally_host_fonts' => [
'family=Rob(.*)o',
'family(.*)Montserrat(.*)display=swap'
'family(.*)Montserrat(.*)display=swap',
'astra-google-(.*)-css',
]
],
'expected' => [
Expand Down

0 comments on commit a60bcda

Please sign in to comment.