Browse Source

Blockbase: Improve blockbase_fonts_url handling (#4489)

This change adresses multiple issues. 
It:
1. prevents the possibility of an "empty" call to fonts.googleapis.com by returning empty instead of `https://fonts.googleapis.com/css2?&display=swap`, see issue #4458
2. prevents a child theme custom Google font to ALWAYS be loaded even if it's is not selected in the user settings
3. prevents duplicate URL parameters when both user body and header fonts are set to the same value with `array_unique`
Rolf Allard van Hagen 3 years ago
parent
commit
474ffbeb3c
1 changed files with 14 additions and 12 deletions
  1. 14 12
      blockbase/functions.php

+ 14 - 12
blockbase/functions.php

@@ -83,26 +83,28 @@ function blockbase_fonts_url() {
 	}
 	}
 
 
 	$font_families = [];
 	$font_families = [];
-	if ( ! empty( $theme_data['typography']['fontFamilies']['theme'] ) ) {
-		foreach( $theme_data['typography']['fontFamilies']['theme'] as $font ) {
-			if ( ! empty( $font['google'] ) ) {
-				$font_families[] = $font['google'];
-			}
-		}
-	}
-
 	if ( ! empty( $theme_data['typography']['fontFamilies']['user'] ) ) {
 	if ( ! empty( $theme_data['typography']['fontFamilies']['user'] ) ) {
 		foreach( $theme_data['typography']['fontFamilies']['user'] as $font ) {
 		foreach( $theme_data['typography']['fontFamilies']['user'] as $font ) {
 			if ( ! empty( $font['google'] ) ) {
 			if ( ! empty( $font['google'] ) ) {
 				$font_families[] = $font['google'];
 				$font_families[] = $font['google'];
 			}
 			}
 		}
 		}
+	} else {
+		if ( ! empty( $theme_data['typography']['fontFamilies']['theme'] ) ) {
+			foreach( $theme_data['typography']['fontFamilies']['theme'] as $font ) {
+				if ( ! empty( $font['google'] ) ) {
+					$font_families[] = $font['google'];
+				}
+			}
+		}
+	}
+	
+	if ( empty( $font_families ) ) {
+		return '';
 	}
 	}
 
 
-	$font_families[] = 'display=swap';
-
-	// Make a single request for the theme fonts.
-	return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', $font_families ) );
+	// Make a single request for the theme or user fonts.
+	return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', array_unique( $font_families ) ) . '&display=swap' );
 }
 }
 
 
 /**
 /**