diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 04f6e87e6a5da1..c8405732bee27f 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -16,7 +16,15 @@ class WP_Webfonts { * @access private * @var array */ - private $webfonts = array(); + private $registered_webfonts = array(); + + /** + * An array of enqueued webfonts. + * + * @access private + * @var array + */ + private $enqueued_webfonts = array(); /** * An array of registered providers. @@ -56,12 +64,30 @@ public function init() { } /** - * Get the list of fonts. + * Get the list of registered fonts. + * + * @return array + */ + public function get_registered_webfonts() { + return $this->registered_webfonts; + } + + /** + * Get the list of enqueued fonts. * * @return array */ - public function get_fonts() { - return $this->webfonts; + public function get_enqueued_webfonts() { + return $this->enqueued_webfonts; + } + + /** + * Get the list of all fonts. + * + * @return array + */ + public function get_all_webfonts() { + return array_merge( $this->get_registered_webfonts(), $this->get_enqueued_webfonts() ); } /** @@ -83,14 +109,31 @@ public function register_font( $font ) { if ( $font ) { $slug = $this->get_font_slug( $font ); - if ( ! isset( $this->webfonts[ $slug ] ) ) { - $this->webfonts[ $slug ] = array(); + if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { + $this->registered_webfonts[ $slug ] = array(); } - $this->webfonts[ $slug ][] = $font; + $this->registered_webfonts[ $slug ][] = $font; } } + public function enqueue_font( $font_family ) { + $slug = $this->get_font_slug( $font_family ); + + if ( isset( $this->enqueued_webfonts[ $slug ] ) ) { + _doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is already enqueued.' ), $slug ), '6.0.0' ); + return false; + } + + if ( ! isset( $this->registered_webfonts[ $slug ] ) ) { + _doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is not registered.' ), $slug ), '6.0.0' ); + return false; + } + + $this->enqueued_webfonts[ $slug ] = $this->registered_webfonts[ $slug ]; + unset( $this->registered_webfonts[ $slug ] ); + } + /** * Get the font slug. * @@ -210,7 +253,7 @@ public function register_provider( $provider, $class ) { */ public function generate_and_enqueue_styles() { // Generate the styles. - $styles = $this->generate_styles(); + $styles = $this->generate_styles( $this->get_enqueued_webfonts() ); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -230,7 +273,7 @@ public function generate_and_enqueue_styles() { */ public function generate_and_enqueue_editor_styles() { // Generate the styles. - $styles = $this->generate_styles(); + $styles = $this->generate_styles( $this->get_all_webfonts() ); // Bail out if there are no styles to enqueue. if ( '' === $styles ) { @@ -247,14 +290,14 @@ public function generate_and_enqueue_editor_styles() { * * @return string $styles Generated styles. */ - public function generate_styles() { + public function generate_styles( $font_families ) { $styles = ''; $providers = $this->get_providers(); $webfonts = array(); // Grab only the font face declarations from $font_families. - foreach ( $this->get_fonts() as $font_family ) { + foreach ( $font_families as $font_family ) { foreach ( $font_family as $font_face ) { $webfonts[] = $font_face; } diff --git a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php index 9c444001e2cfe6..d974c9427f4706 100644 --- a/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php +++ b/lib/compat/wordpress-6.0/register-webfonts-from-theme-json.php @@ -62,7 +62,7 @@ function gutenberg_register_webfonts_from_theme_json() { * @return array The global styles with missing fonts data. */ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { - $font_families_registered = wp_webfonts()->get_fonts(); + $font_families_registered = wp_webfonts()->get_all_webfonts(); // Make sure the path to settings.typography.fontFamilies.theme exists // before adding missing fonts. diff --git a/lib/compat/wordpress-6.0/webfonts.php b/lib/compat/wordpress-6.0/webfonts.php index f3665766f06f36..24942fedf46eb3 100644 --- a/lib/compat/wordpress-6.0/webfonts.php +++ b/lib/compat/wordpress-6.0/webfonts.php @@ -97,6 +97,10 @@ function wp_register_webfont( array $webfont ) { wp_webfonts()->register_font( $webfont ); } +function wp_enqueue_webfont( $font_family ) { + wp_webfonts()->enqueue_font( $font_family ); +} + /** * Registers a custom font service provider. *