Skip to content

Commit 13c84d3

Browse files
authored
fix: optimize google font loading (#3567)
Co-authored-by: [email protected] <>
1 parent 12c3447 commit 13c84d3

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/fonts.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ public static function gather_theme_fonts() {
4545
$theme_fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
4646

4747
foreach ( $theme_fonts as $key => $value ) {
48-
self::$theme_fonts[] = $value[ 0 ][ 'font-family' ];
48+
self::$theme_fonts[ $value[ 0 ][ 'font-family' ] ] = true;
4949
}
5050

5151
return self::$theme_fonts;
5252
}
5353

5454
public static function is_theme_font( $font_name ) {
55-
return in_array( strtolower( $font_name ), self::gather_theme_fonts() );
55+
return isset( self::gather_theme_fonts()[ strtolower( $font_name ) ] );
5656
}
5757

5858
public function register_rest_fields() {
@@ -72,7 +72,7 @@ public function register_rest_fields() {
7272
* @return array
7373
*/
7474
public static function get_theme_fonts() {
75-
$response = self::gather_theme_fonts();
75+
$response = array_keys( self::gather_theme_fonts() );
7676

7777
return new WP_REST_Response( $response, 200 );
7878
}
@@ -87,6 +87,7 @@ public static function get_theme_fonts() {
8787
class Stackable_Google_Fonts {
8888

8989
public static $google_fonts = [];
90+
public static $done_registering = [];
9091

9192
function __construct() {
9293
if ( is_frontend() ) {
@@ -116,7 +117,7 @@ public function gather_google_fonts( $block_content, $block ) {
116117
}
117118

118119
public static function enqueue_frontend_block_fonts() {
119-
self::enqueue_google_fonts( array_unique( self::$google_fonts ) );
120+
self::enqueue_google_fonts( array_keys( self::$google_fonts ) );
120121
}
121122

122123
public static function is_web_font( $font_name ) {
@@ -131,18 +132,25 @@ public function is_stackable_block( $block_name ) {
131132
}
132133

133134
public static function register_font( $font_name ) {
135+
if ( isset( self::$done_registering[ $font_name ] ) ) {
136+
return;
137+
}
138+
134139
if ( ! self::is_web_font( $font_name ) ) {
140+
self::$done_registering[ $font_name ] = true;
135141
return;
136142
}
137143

138144
if ( Stackable_Theme_Fonts::is_theme_font( $font_name ) ) {
145+
self::$done_registering[ $font_name ] = true;
139146
return;
140147
}
141148

142-
if ( ! in_array( $font_name, self::$google_fonts ) ) {
149+
if ( ! isset( self::$google_fonts[ $font_name ] ) ) {
143150
// Allow themes to disable enqueuing fonts, helpful for custom fonts.
144151
if ( apply_filters( 'stackable_enqueue_font', true, $font_name ) ) {
145-
self::$google_fonts[] = $font_name;
152+
self::$google_fonts[ $font_name ] = true;
153+
self::$done_registering[ $font_name ] = true;
146154

147155
// Enqueue the fonts in the footer.
148156
add_filter( 'wp_footer', array( 'Stackable_Google_Fonts', 'enqueue_frontend_block_fonts' ) );

0 commit comments

Comments
 (0)