Skip to content

Commit e79d560

Browse files
authored
Google Fonts: Update the implementation of the print_font_faces (2nd try) (#35986)
* Google Fonts: Update the implementation of the print_font_faces (#35890) * Google Fonts: Update the implementation of the print_font_faces * changelog * Fix incorrect font family check
1 parent 311af16 commit e79d560

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: other
3+
4+
Google Fonts: Update the implement of print_font_faces as the value of WP_Font_Face_Resolver::get_fonts_from_theme_json may be an indexed array

projects/plugins/jetpack/modules/google-fonts/current/class-jetpack-google-font-face.php

+24-14
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,22 @@ public function current_screen() {
5454
* Print fonts that are used in global styles or block-level settings.
5555
*/
5656
public function print_font_faces() {
57-
$fonts = $this->get_fonts();
58-
$fonts_to_print = array();
57+
$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
58+
$font_slug_aliases = $this->get_font_slug_aliases();
59+
$fonts_to_print = array();
5960

6061
$this->collect_global_styles_fonts();
61-
$this->fonts_in_use = array_values( array_unique( $this->fonts_in_use, SORT_STRING ) );
62-
foreach ( $fonts as $font_family => $font_faces ) {
63-
if ( in_array( $this->format_font( $font_family ), $this->fonts_in_use, true ) ) {
62+
$fonts_in_use = array_values( array_unique( $this->fonts_in_use, SORT_STRING ) );
63+
$fonts_in_use = array_map(
64+
function ( $font_slug ) use ( $font_slug_aliases ) {
65+
return $font_slug_aliases[ $font_slug ] ?? $font_slug;
66+
},
67+
$this->fonts_in_use
68+
);
69+
70+
foreach ( $fonts as $font_faces ) {
71+
$font_family = $font_faces[0]['font-family'] ?? '';
72+
if ( in_array( $this->format_font( $font_family ), $fonts_in_use, true ) ) {
6473
$fonts_to_print[ $font_family ] = $font_faces;
6574
}
6675
}
@@ -141,26 +150,27 @@ public function format_font( $font_slug ) {
141150
}
142151

143152
/**
144-
* Get all font definitions from theme json.
153+
* Get the font slug aliases that maps the font slug to the font family if they are different.
154+
*
155+
* The font definition may define an alias slug name, so we have to add the map from the slug name to the font family.
156+
* See https://github.com/WordPress/twentytwentyfour/blob/df92472089ede6fae5924c124a93c843b84e8cbd/theme.json#L215.
145157
*/
146-
public function get_fonts() {
147-
$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
158+
public function get_font_slug_aliases() {
159+
$font_slug_aliases = array();
148160

149-
// The font definition might define an alias slug name, so we have to add the map from the slug name to font faces.
150-
// See https://github.com/WordPress/twentytwentyfour/blob/df92472089ede6fae5924c124a93c843b84e8cbd/theme.json#L215.
151161
$theme_json = WP_Theme_JSON_Resolver::get_theme_data();
152162
$raw_data = $theme_json->get_data();
153163
if ( ! empty( $raw_data['settings']['typography']['fontFamilies'] ) ) {
154164
foreach ( $raw_data['settings']['typography']['fontFamilies'] as $font ) {
155-
$font_family_name = $this->get_font_family_name( $font );
165+
$font_family_name = $this->format_font( $this->get_font_family_name( $font ) );
156166
$font_slug = $font['slug'] ?? '';
157-
if ( $font_slug && ! array_key_exists( $font_slug, $fonts ) && array_key_exists( $font_family_name, $fonts ) ) {
158-
$fonts[ $font_slug ] = $fonts[ $font_family_name ];
167+
if ( $font_slug && $font_slug !== $font_family_name && ! array_key_exists( $font_slug, $font_slug_aliases ) ) {
168+
$font_slug_aliases[ $font_slug ] = $font_family_name;
159169
}
160170
}
161171
}
162172

163-
return $fonts;
173+
return $font_slug_aliases;
164174
}
165175

166176
/**

0 commit comments

Comments
 (0)