From 8c816ac33d1d3d074e45439ad4e9aa51716f8df5 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Thu, 10 Mar 2022 16:03:11 -0300 Subject: [PATCH] Expose bulk webfont enqueueing method --- lib/compat/wordpress-6.0/webfonts.php | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/compat/wordpress-6.0/webfonts.php index bce877aa3d35a8..55bab657f9d4c3 100644 --- a/lib/compat/wordpress-6.0/webfonts.php +++ b/lib/compat/wordpress-6.0/webfonts.php @@ -104,6 +104,60 @@ function wp_register_webfont( string $id, array $webfont ) { wp_webfonts()->register_font( $id, $webfont ); } +/** + * Enqueues a collection of webfonts. + * + * Example of how to enqueue Source Serif Pro font with font-weight range of 200-900 + * and font-style of normal and italic: + * + * + * wp_enqueue_webfonts( + * array( + * 'some-already-registered-font', // This requires the font to be registered beforehand. + * array( + * 'id' => 'source-serif-200-900-normal', + * 'provider' => 'local', + * 'font_family' => 'Source Serif Pro', + * 'font_weight' => '200 900', + * 'font_style' => 'normal', + * 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), + * ), + * array( + * 'id' => 'source-serif-200-900-italic', + * 'provider' => 'local', + * 'font_family' => 'Source Serif Pro', + * 'font_weight' => '200 900', + * 'font_style' => 'italic', + * 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ), + * ), + * ) + * ); + * + * + * Webfonts should be enqueued in the `after_setup_theme` hook. + * + * @since 6.0.0 + * + * @param array $webfonts Webfonts to be enqueued. + * This contains an array of webfonts to be enqueued. + * Each webfont is an array. + * See {@see WP_Webfonts_Registry::register()} for a list of + * supported arguments for each webfont. + */ +function wp_enqueue_webfonts( array $webfonts = array() ) { + foreach ( $webfonts as $webfont ) { + if ( is_string( $webfont ) ) { + wp_enqueue_webfont( $webfont ); + continue; + } + + $id = $webfont['id']; + unset( $webfont['id'] ); + + wp_enqueue_webfont( $id, $webfont ); + } +} + /** * Enqueue a single webfont. *