From 5cdf55fda7dfc97c6d5394a1ff3562b906516255 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 20 Oct 2024 23:25:16 +0000 Subject: [PATCH] Editor: Load all style variation fonts within the editors. Loads the font family files from style variations defined within a theme for user in the site and post editors. This is to ensure the fonts are shown while editing without the need for a reload after switching styles. Props ironprogrammer, mmaattiiaass. Fixes #62231. Built from https://develop.svn.wordpress.org/trunk@59260 git-svn-id: http://core.svn.wordpress.org/trunk@58652 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/admin-filters.php | 1 + wp-includes/block-editor.php | 1 + wp-includes/fonts.php | 16 ++++++++++ .../fonts/class-wp-font-face-resolver.php | 32 +++++++++++++++++++ wp-includes/version.php | 2 +- 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/wp-admin/includes/admin-filters.php b/wp-admin/includes/admin-filters.php index b5adb946cf..587fbc25e9 100644 --- a/wp-admin/includes/admin-filters.php +++ b/wp-admin/includes/admin-filters.php @@ -172,3 +172,4 @@ add_filter( 'list_pages', '_wp_privacy_settings_filter_draft_page_titles', 10, 2 // Font management. add_action( 'admin_print_styles', 'wp_print_font_faces', 50 ); +add_action( 'admin_print_styles', 'wp_print_font_faces_from_style_variations', 50 ); diff --git a/wp-includes/block-editor.php b/wp-includes/block-editor.php index be8a8f901f..bdca0d2c08 100644 --- a/wp-includes/block-editor.php +++ b/wp-includes/block-editor.php @@ -366,6 +366,7 @@ function _wp_get_iframed_editor_assets() { ob_start(); wp_print_styles(); wp_print_font_faces(); + wp_print_font_faces_from_style_variations(); $styles = ob_get_clean(); if ( $has_emoji_styles ) { diff --git a/wp-includes/fonts.php b/wp-includes/fonts.php index 4e286b71dc..17db039608 100644 --- a/wp-includes/fonts.php +++ b/wp-includes/fonts.php @@ -54,6 +54,22 @@ function wp_print_font_faces( $fonts = array() ) { $wp_font_face->generate_and_print( $fonts ); } +/** + * Generates and prints font-face styles defined the the theme style variations. + * + * @since 6.7.0 + * + */ +function wp_print_font_faces_from_style_variations() { + $fonts = WP_Font_Face_Resolver::get_fonts_from_style_variations(); + + if ( empty( $fonts ) ) { + return; + } + + wp_print_font_faces( $fonts ); +} + /** * Registers a new font collection in the font library. * diff --git a/wp-includes/fonts/class-wp-font-face-resolver.php b/wp-includes/fonts/class-wp-font-face-resolver.php index 12245af556..153008b53a 100644 --- a/wp-includes/fonts/class-wp-font-face-resolver.php +++ b/wp-includes/fonts/class-wp-font-face-resolver.php @@ -36,6 +36,38 @@ class WP_Font_Face_Resolver { return static::parse_settings( $settings ); } + /** + * Gets fonts defined in style variations. + * + * @since 6.7.0 + * + * @return array Returns an array of font-families. + */ + public static function get_fonts_from_style_variations() { + $variations = WP_Theme_JSON_Resolver::get_style_variations(); + $fonts = array(); + + if ( empty( $variations ) ) { + return $fonts; + } + + foreach ( $variations as $variation ) { + if ( ! empty( $variation['settings']['typography']['fontFamilies']['theme'] ) ) { + $fonts = array_merge( $fonts, $variation['settings']['typography']['fontFamilies']['theme'] ); + } + } + + $settings = array( + 'typography' => array( + 'fontFamilies' => array( + 'theme' => $fonts, + ), + ), + ); + + return static::parse_settings( $settings ); + } + /** * Parse theme.json settings to extract font definitions with variations grouped by font-family. * diff --git a/wp-includes/version.php b/wp-includes/version.php index f12770c8a4..76e97c6b81 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-beta3-59259'; +$wp_version = '6.7-beta3-59260'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.