From 9a21788a0c374890d91c913e1e5b4a420e3abbbd Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 11 Mar 2022 15:56:07 +0000 Subject: [PATCH 1/6] Webfonts: Preload fonts in the head to stop FOUC --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 9b5114b7452bb..24018253c4ba0 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -55,6 +55,9 @@ public function init() { // Enqueue webfonts in the block editor. add_action( 'admin_init', array( $this, 'generate_and_enqueue_editor_styles' ) ); + + // Preload the font files. + add_action( 'wp_head', array( $this, 'preload_webfonts' ) ); } /** @@ -291,4 +294,18 @@ public function generate_styles() { return $styles; } + + /** + * Preload webfonts to improve performance + * + * @return void + */ + function preload_webfonts() { + foreach ( $this->get_fonts() as $font ) { + foreach( $font['src'] as $src ) { + $srcAsArray = explode( '.', $src ); + echo ''; + } + } + } } From 16ac0d665b916fbf8176f163c172d03d88f41338 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 11 Mar 2022 18:44:05 +0000 Subject: [PATCH 2/6] prevent security issues by sanitizing input from theme.json --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 24018253c4ba0..03dd39b79955b 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -301,10 +301,16 @@ public function generate_styles() { * @return void */ function preload_webfonts() { + $allowed_types = array( 'woff', 'woff2', 'ttf', 'eot', 'otf' ); foreach ( $this->get_fonts() as $font ) { + var_dump( $font ); foreach( $font['src'] as $src ) { $srcAsArray = explode( '.', $src ); - echo ''; + $file_type = end( $srcAsArray ); + if ( ! in_array( $file_type, $allowed_types ) ) { + $file_type = ''; + } + echo ''; } } } From 529103a55121f45fc16a0302c9f41478dbf7b435 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 11 Mar 2022 18:49:23 +0000 Subject: [PATCH 3/6] prevent security issues by sanitizing input from theme.json --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 03dd39b79955b..ecb0c847914d8 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -35,6 +35,11 @@ class WP_Webfonts { */ private $stylesheet_handle = ''; + /** + * Allowed file types + */ + private static $allowed_types = array( 'woff', 'woff2', 'ttf', 'eot', 'otf' ); + /** * Init. */ @@ -300,17 +305,15 @@ public function generate_styles() { * * @return void */ - function preload_webfonts() { - $allowed_types = array( 'woff', 'woff2', 'ttf', 'eot', 'otf' ); + public function preload_webfonts() { foreach ( $this->get_fonts() as $font ) { - var_dump( $font ); foreach( $font['src'] as $src ) { $srcAsArray = explode( '.', $src ); $file_type = end( $srcAsArray ); - if ( ! in_array( $file_type, $allowed_types ) ) { + if ( ! in_array( $file_type, self::$allowed_types ) ) { $file_type = ''; } - echo ''; + echo ''; } } } From 9f5fb53ab959a9147f9a14dc1a610be38702c5b6 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 11 Mar 2022 18:56:13 +0000 Subject: [PATCH 4/6] allow themes to specify which fonts to preload --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index ecb0c847914d8..b59c98e877ad6 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -173,6 +173,7 @@ public function validate_font( $font ) { 'font-feature-settings', 'font-variation-settings', 'line-gap-override', + 'preload', 'size-adjust', 'src', 'unicode-range', @@ -307,13 +308,15 @@ public function generate_styles() { */ public function preload_webfonts() { foreach ( $this->get_fonts() as $font ) { - foreach( $font['src'] as $src ) { - $srcAsArray = explode( '.', $src ); - $file_type = end( $srcAsArray ); - if ( ! in_array( $file_type, self::$allowed_types ) ) { - $file_type = ''; + if ( $font['preload'] ) { + foreach( $font['src'] as $src ) { + $srcAsArray = explode( '.', $src ); + $file_type = end( $srcAsArray ); + if ( ! in_array( $file_type, self::$allowed_types ) ) { + $file_type = ''; + } + echo ''; } - echo ''; } } } From 4aab001d31edef2353d4fd964fe9ec6079e6a5fa Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 15 Mar 2022 12:20:48 +0000 Subject: [PATCH 5/6] Update lib/compat/wordpress-6.0/class-wp-webfonts.php Co-authored-by: Ari Stathopoulos --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index b59c98e877ad6..95a3394ac92f9 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -310,8 +310,8 @@ public function preload_webfonts() { foreach ( $this->get_fonts() as $font ) { if ( $font['preload'] ) { foreach( $font['src'] as $src ) { - $srcAsArray = explode( '.', $src ); - $file_type = end( $srcAsArray ); + $src_as_array = explode( '.', $src ); + $file_type = end( $src_as_array ); if ( ! in_array( $file_type, self::$allowed_types ) ) { $file_type = ''; } From 8dcf2b01e13769b56f022c38a847db10697f503e Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 15 Mar 2022 12:20:54 +0000 Subject: [PATCH 6/6] Update lib/compat/wordpress-6.0/class-wp-webfonts.php Co-authored-by: Ari Stathopoulos --- lib/compat/wordpress-6.0/class-wp-webfonts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 95a3394ac92f9..3b3b270d115d4 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -308,7 +308,7 @@ public function generate_styles() { */ public function preload_webfonts() { foreach ( $this->get_fonts() as $font ) { - if ( $font['preload'] ) { + if ( ! empty( $font['preload'] ) && $font['preload'] ) { foreach( $font['src'] as $src ) { $src_as_array = explode( '.', $src ); $file_type = end( $src_as_array );