diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 9b5114b7452bb2..3b3b270d115d4f 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. */ @@ -55,6 +60,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' ) ); } /** @@ -165,6 +173,7 @@ public function validate_font( $font ) { 'font-feature-settings', 'font-variation-settings', 'line-gap-override', + 'preload', 'size-adjust', 'src', 'unicode-range', @@ -291,4 +300,24 @@ public function generate_styles() { return $styles; } + + /** + * Preload webfonts to improve performance + * + * @return void + */ + public function preload_webfonts() { + foreach ( $this->get_fonts() as $font ) { + if ( ! empty( $font['preload'] ) && $font['preload'] ) { + foreach( $font['src'] as $src ) { + $src_as_array = explode( '.', $src ); + $file_type = end( $src_as_array ); + if ( ! in_array( $file_type, self::$allowed_types ) ) { + $file_type = ''; + } + echo ''; + } + } + } + } }