diff --git a/lib/compat/wordpress-6.0/global-styles.php b/lib/compat/wordpress-6.0/global-styles.php index 3895311ca21d69..d80099b7e0067e 100644 --- a/lib/compat/wordpress-6.0/global-styles.php +++ b/lib/compat/wordpress-6.0/global-styles.php @@ -5,6 +5,61 @@ * @package gutenberg */ +/** + * Register webfonts defined in theme.json. + */ +function gutenberg_register_webfonts_from_theme_json() { + // Get settings from theme.json. + $theme_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings(); + + // Bail out early if there are no settings for webfonts. + if ( empty( $theme_settings['typography'] ) || empty( $theme_settings['typography']['fontFamilies'] ) ) { + return; + } + + $webfonts = array(); + + // Look for fontFamilies. + foreach ( $theme_settings['typography']['fontFamilies'] as $font_families ) { + foreach ( $font_families as $font_family ) { + + // Skip if fontFace is not defined. + if ( empty( $font_family['fontFace'] ) ) { + continue; + } + + $font_family['fontFace'] = (array) $font_family['fontFace']; + + foreach ( $font_family['fontFace'] as $font_face ) { + // Check if webfonts have a "src" param, and if they do account for the use of "file:./". + if ( ! empty( $font_face['src'] ) ) { + $font_face['src'] = (array) $font_face['src']; + + foreach ( $font_face['src'] as $src_key => $url ) { + // Tweak the URL to be relative to the theme root. + if ( 0 !== strpos( $url, 'file:./' ) ) { + continue; + } + $font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) ); + } + } + + // Convert keys to kebab-case. + foreach ( $font_face as $property => $value ) { + $kebab_case = _wp_to_kebab_case( $property ); + $font_face[ $kebab_case ] = $value; + if ( $kebab_case !== $property ) { + unset( $font_face[ $property ] ); + } + } + + $webfonts[] = $font_face; + } + } + } + wp_register_webfonts( $webfonts ); +} + /** * Add missing fonts data to the global styles. * @@ -76,3 +131,5 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { return $data; } + +add_action( 'wp_loaded', 'gutenberg_register_webfonts_from_theme_json' ); diff --git a/lib/global-styles.php b/lib/global-styles.php index 0fdc086b56cf73..2a2e1925ee1e11 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -288,61 +288,6 @@ function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $ return ! ! preg_match( '/^var\(--wp-[a-zA-Z0-9\-]+\)$/', trim( $parts[1] ) ); } -/** - * Register webfonts defined in theme.json. - */ -function gutenberg_register_webfonts_from_theme_json() { - // Get settings from theme.json. - $theme_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings(); - - // Bail out early if there are no settings for webfonts. - if ( empty( $theme_settings['typography'] ) || empty( $theme_settings['typography']['fontFamilies'] ) ) { - return; - } - - $webfonts = array(); - - // Look for fontFamilies. - foreach ( $theme_settings['typography']['fontFamilies'] as $font_families ) { - foreach ( $font_families as $font_family ) { - - // Skip if fontFace is not defined. - if ( empty( $font_family['fontFace'] ) ) { - continue; - } - - $font_family['fontFace'] = (array) $font_family['fontFace']; - - foreach ( $font_family['fontFace'] as $font_face ) { - // Check if webfonts have a "src" param, and if they do account for the use of "file:./". - if ( ! empty( $font_face['src'] ) ) { - $font_face['src'] = (array) $font_face['src']; - - foreach ( $font_face['src'] as $src_key => $url ) { - // Tweak the URL to be relative to the theme root. - if ( 0 !== strpos( $url, 'file:./' ) ) { - continue; - } - $font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) ); - } - } - - // Convert keys to kebab-case. - foreach ( $font_face as $property => $value ) { - $kebab_case = _wp_to_kebab_case( $property ); - $font_face[ $kebab_case ] = $value; - if ( $kebab_case !== $property ) { - unset( $font_face[ $property ] ); - } - } - - $webfonts[] = $font_face; - } - } - } - wp_register_webfonts( $webfonts ); -} - // The else clause can be removed when plugin support requires WordPress 5.8.0+. if ( function_exists( 'get_block_editor_settings' ) ) { add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); @@ -352,7 +297,6 @@ function gutenberg_register_webfonts_from_theme_json() { add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' ); add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); -add_action( 'wp_loaded', 'gutenberg_register_webfonts_from_theme_json' ); // kses actions&filters. add_action( 'init', 'gutenberg_global_styles_kses_init' );