Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Fonts: Prevent the default fonts from overridden by the theme fonts #37050

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Google Fonts: Avoid theme fonts overriding the default fonts so fonts with a specific font weight that are not supported by the provided theme can be rendered correctly
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function ( $font_slug ) use ( $font_slug_aliases ) {
foreach ( $fonts as $font_faces ) {
$font_family = $font_faces[0]['font-family'] ?? '';
if ( in_array( $this->format_font( $font_family ), $fonts_in_use, true ) ) {
$fonts_to_print[ $font_family ] = $font_faces;
$fonts_to_print[] = $font_faces;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,6 @@ function ( $font_family ) {
return $available_google_fonts_map;
}

/**
* Gets the font families of the active theme
*
* @return object[] The font families of the active theme.
*/
function jetpack_get_theme_fonts_map() {
if ( ! class_exists( 'WP_Theme_JSON_Resolver' ) ) {
return array();
}

$theme_json = WP_Theme_JSON_Resolver::get_theme_data();
$raw_data = $theme_json->get_data();
if ( empty( $raw_data['settings']['typography']['fontFamilies'] ) ) {
return array();
}

$theme_fonts_map = array();
foreach ( $raw_data['settings']['typography']['fontFamilies'] as $font_family ) {
$font_family_name = $font_family['name'] ?? Jetpack_Google_Font_Face::get_font_family_name( $font_family );
if ( $font_family_name ) {
$theme_fonts_map[ $font_family_name ] = true;
}
}

return $theme_fonts_map;
}

/**
* Register google fonts to the theme json data
*
Expand All @@ -123,18 +96,11 @@ function jetpack_register_google_fonts_to_theme_json( $theme_json ) {
}

$available_google_fonts_map = jetpack_get_available_google_fonts_map( $google_fonts_data );
$theme_fonts_map = jetpack_get_theme_fonts_map();
$google_fonts_families = array_values(
array_filter(
$google_fonts_data['fontFamilies'],
function ( $google_fonts_family ) use ( $available_google_fonts_map, $theme_fonts_map ) {
function ( $google_fonts_family ) use ( $available_google_fonts_map ) {
$name = $google_fonts_family['name'];

// Filter out the fonts that are provided by the active theme.
if ( isset( $theme_fonts_map[ $name ] ) && $theme_fonts_map[ $name ] ) {
return false;
}

return $available_google_fonts_map[ $name ] ?? false;
}
)
Expand Down
Loading