From b95148fd0a2fb7ef54e4d913771cb08d04bcc025 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> Date: Thu, 25 May 2023 23:05:39 +0200 Subject: [PATCH] [Fonts API] Refactor Theme JSON global functions into WP_Fonts_Resolver (#50811) Co-authored-by: hellofromtonya --- ...class-wp-theme-json-resolver-gutenberg.php | 2 +- .../fonts-api/class-wp-fonts-resolver.php | 289 ++++++++++++++++++ lib/experimental/fonts-api/class-wp-fonts.php | 11 +- lib/experimental/fonts-api/fonts-api.php | 9 + .../register-fonts-from-theme-json.php | 189 ------------ lib/load.php | 1 - .../addMissingFontsToThemeJson-test.php} | 16 +- .../registerFontsFromThemeJson-test.php} | 32 +- 8 files changed, 333 insertions(+), 216 deletions(-) delete mode 100644 lib/experimental/fonts-api/register-fonts-from-theme-json.php rename phpunit/fonts-api/{gutenbergAddRegisteredFontsToThemeJson-test.php => wpFontsResolver/addMissingFontsToThemeJson-test.php} (92%) rename phpunit/fonts-api/{gutenbergRegisterFontsFromThemeJson-test.php => wpFontsResolver/registerFontsFromThemeJson-test.php} (90%) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 2c61de7c5e24c2..33fd4defd51917 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -275,7 +275,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() } // BEGIN OF EXPERIMENTAL CODE. Not to backport to core. - static::$theme = gutenberg_add_registered_fonts_to_theme_json( static::$theme ); + static::$theme = WP_Fonts_Resolver::add_missing_fonts_to_theme_json( static::$theme ); // END OF EXPERIMENTAL CODE. } diff --git a/lib/experimental/fonts-api/class-wp-fonts-resolver.php b/lib/experimental/fonts-api/class-wp-fonts-resolver.php index d3d0ecba992b88..144f7b30acc153 100644 --- a/lib/experimental/fonts-api/class-wp-fonts-resolver.php +++ b/lib/experimental/fonts-api/class-wp-fonts-resolver.php @@ -106,4 +106,293 @@ private static function get_value_from_style( $style, $preset_type = 'font-famil $length = strpos( $style, $ending_pattern ) - $offset; return substr( $style, $offset, $length ); } + + /** + * Register fonts defined in theme.json. + * + * @since X.X.X + */ + public static function register_fonts_from_theme_json() { + + $settings = static::get_settings(); + // Bail out early if there are no settings for fonts. + if ( empty( $settings['typography'] ) || empty( $settings['typography']['fontFamilies'] ) ) { + return; + } + + list( $fonts, $handles ) = static::parse_font_families( $settings ); + + wp_register_fonts( $fonts ); + wp_enqueue_fonts( $handles ); + } + + /** + * Add missing fonts to the global styles. + * + * @since X.X.X + * + * @param WP_Theme_JSON_Gutenberg|WP_Theme_JSON $data The global styles. + * @return WP_Theme_JSON_Gutenberg|WP_Theme_JSON The global styles with missing fonts. + */ + public static function add_missing_fonts_to_theme_json( $data ) { + $font_families_registered = wp_fonts()->get_registered_font_families(); + + $raw_data = $data->get_raw_data(); + + $font_families_from_theme = ! empty( $raw_data['settings']['typography']['fontFamilies']['theme'] ) + ? $raw_data['settings']['typography']['fontFamilies']['theme'] + : array(); + + // Find missing fonts that are not in the theme's theme.json. + $to_add = array(); + if ( ! empty( $font_families_registered ) ) { + $to_add = array_diff( $font_families_registered, static::get_font_families( $font_families_from_theme ) ); + } + + // Bail out early if there are no missing fonts. + if ( empty( $to_add ) ) { + return $data; + } + + /* + * Make sure the path to settings.typography.fontFamilies.theme exists + * before adding missing fonts. + */ + if ( empty( $raw_data['settings'] ) ) { + $raw_data['settings'] = array(); + } + $raw_data['settings'] = static::set_tyopgraphy_settings_array_structure( $raw_data['settings'] ); + + foreach ( $to_add as $font_family_handle ) { + $raw_data['settings']['typography']['fontFamilies']['theme'][] = wp_fonts()->to_theme_json( $font_family_handle ); + } + + return new WP_Theme_JSON_Gutenberg( $raw_data ); + } + + /** + * Returns theme's settings and adds fonts defined in variations. + * + * @since X.X.X + * + * @return array An array containing theme's settings. + */ + private static function get_settings() { + // Get settings. + $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_settings(); + + if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + return $settings; + } + + // If in the editor, add fonts defined in variations. + $variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations(); + $set_theme_structure = true; + + foreach ( $variations as $variation ) { + + // Skip if settings.typography.fontFamilies are not defined in the variation. + if ( empty( $variation['settings']['typography']['fontFamilies'] ) ) { + continue; + } + + // One time, set any missing parts of the array structure. + if ( $set_theme_structure ) { + $set_theme_structure = false; + $settings = static::set_tyopgraphy_settings_array_structure( $settings ); + } + + // Merge the variation settings with the global settings. + $settings['typography']['fontFamilies']['theme'] = array_merge( + $settings['typography']['fontFamilies']['theme'], + $variation['settings']['typography']['fontFamilies']['theme'] + ); + + // Make sure there are no duplicates. + $settings['typography']['fontFamilies'] = array_unique( $settings['typography']['fontFamilies'] ); + } + + return $settings; + } + + /** + * Converts a list of font families into font handles and returns them as an array. + * + * @since X.X.X + * + * @param array $families_data An array of font families data. + * @return array An array containing font handles. + */ + private static function get_font_families( $families_data ) { + $families = array(); + foreach ( $families_data as $family ) { + $font_family = WP_Fonts_Utils::get_font_family_from_variation( $family ); + $handle = WP_Fonts_Utils::convert_font_family_into_handle( $font_family ); + if ( ! empty( $handle ) ) { + $families[ $handle ] = true; + } + } + + return ! empty( $families ) ? array_keys( $families ) : array(); + } + + /** + * Parse font families from theme.json. + * + * @since X.X.X + * + * @param array $settings Font settings to parse. + * @return array Returns an array that contains font data and corresponding handles. + */ + private static function parse_font_families( array $settings ) { + $handles = array(); + $fonts = array(); + + // Look for fontFamilies. + foreach ( $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']; + $font_family_slug = isset( $font_family['slug'] ) ? $font_family['slug'] : ''; + + foreach ( $font_family['fontFace'] as $font_face ) { + // Skip if the font was registered through the Fonts API. + if ( isset( $font_face['origin'] ) && WP_Fonts::REGISTERED_ORIGIN === $font_face['origin'] ) { + continue; + } + + // For each font "src", convert the "file:./" placeholder into a theme font file URI. + if ( ! empty( $font_face['src'] ) ) { + $font_face['src'] = static::to_theme_file_uri( (array) $font_face['src'] ); + } + + // Convert font-face properties into kebab-case. + $font_face = static::to_kebab_case( $font_face ); + + // Convert font-face properties into kebab-case. + $font_face = static::to_kebab_case( $font_face ); + + $font_family_handle = static::get_font_family_handle( $font_family_slug, $font_face ); + + // Skip if no font-family handle was found. + if ( null === $font_family_handle ) { + continue; + } + + $handles[] = $font_family_handle; + if ( ! array_key_exists( $font_family_handle, $fonts ) ) { + $fonts[ $font_family_handle ] = array(); + } + + $fonts[ $font_family_handle ][] = $font_face; + } + } + } + + return array( $fonts, $handles ); + } + + /** + * Sets the typography.fontFamilies.theme structure in the given array, if not already set. + * if not already set. + * + * @since X.X.X + * + * @param array $data The target array to process. + * @return array Data array with typography.fontFamilies.theme structure set. + */ + private static function set_tyopgraphy_settings_array_structure( array $data ) { + if ( empty( $data['typography'] ) ) { + $data['typography'] = array(); + } + if ( empty( $data['typography']['fontFamilies'] ) ) { + $data['typography']['fontFamilies'] = array(); + } + if ( empty( $data['typography']['fontFamilies']['theme'] ) ) { + $data['typography']['fontFamilies']['theme'] = array(); + } + + return $data; + } + + /** + * Converts each 'file:./' placeholder into a URI to the font file in the theme. + * + * The 'file:./' is specified in the theme's `theme.json` as a placeholder to be + * replaced with the URI to the font file's location in the theme. When a "src" + * beings with this placeholder, it is replaced, converting the src into a URI. + * + * @since X.X.X + * + * @param array $src An array of font file sources to process. + * @return array An array of font file src URI(s). + */ + private static function to_theme_file_uri( array $src ) { + $placeholder = 'file:./'; + + foreach ( $src as $src_key => $src_url ) { + // Skip if the src doesn't start with the placeholder, as there's nothing to replace. + if ( ! str_starts_with( $src_url, $placeholder ) ) { + continue; + } + + $src_file = str_replace( $placeholder, '', $src_url ); + $src[ $src_key ] = get_theme_file_uri( $src_file ); + } + + return $src; + } + + /** + * Converts all first dimension keys into kebab-case. + * + * @since X.X.X + * + * @param array $data The array to process. + */ + private static function to_kebab_case( array $data ) { + foreach ( $data as $key => $value ) { + $kebab_case = _wp_to_kebab_case( $key ); + $data[ $kebab_case ] = $value; + if ( $kebab_case !== $key ) { + unset( $data[ $key ] ); + } + } + + return $data; + } + + /** + * Gets the font-family handle if defined in the "slug" or font-face variation. + * + * @since X.X.X + * + * @param string $font_family_slug Font-family "slug". + * @param array $font_face Font-face (variation) to search. + * @return string|null Font-family handle on success, else null if not found. + */ + private static function get_font_family_handle( $font_family_slug, array $font_face ) { + $font_family_handle = WP_Fonts_Utils::get_font_family_from_variation( $font_face ); + + // Use the defined slug if no handle found. + if ( empty( $font_family_handle ) ) { + $font_family_handle = $font_family_slug; + } + + if ( ! empty( $font_family_handle ) ) { + $font_family_handle = WP_Fonts_Utils::convert_font_family_into_handle( $font_family_handle ); + } + + if ( empty( $font_family_handle ) ) { + _doing_it_wrong( __FUNCTION__, __( 'Font family not defined in the variation or "slug".', 'gutenberg' ), '6.1.0' ); + return null; + } + + return $font_family_handle; + } } diff --git a/lib/experimental/fonts-api/class-wp-fonts.php b/lib/experimental/fonts-api/class-wp-fonts.php index cbabd3d85d36d7..5ce85bab315094 100644 --- a/lib/experimental/fonts-api/class-wp-fonts.php +++ b/lib/experimental/fonts-api/class-wp-fonts.php @@ -18,6 +18,15 @@ */ class WP_Fonts extends WP_Dependencies { + /** + * Registered "origin", indicating the font is registered in the API. + * + * @since X.X.X + * + * @var string + */ + const REGISTERED_ORIGIN = 'gutenberg_wp_fonts_api'; + /** * An array of registered providers. * @@ -731,7 +740,7 @@ public function to_theme_json( $font_family_handle ) { } $variation_obj = $this->registered[ $variation_handle ]; - $variation_properties = array( 'origin' => 'gutenberg_wp_fonts_api' ); + $variation_properties = array( 'origin' => static::REGISTERED_ORIGIN ); foreach ( $variation_obj->extra['font-properties'] as $property_name => $property_value ) { $property_in_camelcase = lcfirst( str_replace( '-', '', ucwords( $property_name, '-' ) ) ); $variation_properties[ $property_in_camelcase ] = $property_value; diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 7075f20f76a217..348ae2dc875e26 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -241,3 +241,12 @@ function( $mime_types ) { return $mime_types; } ); + +/* + * To make sure blocks are registered before any Theme_JSON operations take place, a priority of 21 is used. + * + * Why 21? + * Blocks are registered via the "init" hook with a priority value of `20`, which is dynamically added + * during the build. See: tools/webpack/blocks.js. + */ +add_action( 'init', 'WP_Fonts_Resolver::register_fonts_from_theme_json', 21 ); diff --git a/lib/experimental/fonts-api/register-fonts-from-theme-json.php b/lib/experimental/fonts-api/register-fonts-from-theme-json.php deleted file mode 100644 index 5831217b3cd88f..00000000000000 --- a/lib/experimental/fonts-api/register-fonts-from-theme-json.php +++ /dev/null @@ -1,189 +0,0 @@ -get_settings(); - - // If in the editor, add webfonts defined in variations. - if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { - $variations = WP_Theme_JSON_Resolver_Gutenberg::get_style_variations(); - - foreach ( $variations as $variation ) { - - // Sanity check: Skip if fontFamilies are not defined in the variation. - if ( - empty( $variation['settings'] ) || - empty( $variation['settings']['typography'] ) || - empty( $variation['settings']['typography']['fontFamilies'] ) - ) { - continue; - } - - // Merge the variation settings with the global settings. - $settings['typography'] = empty( $settings['typography'] ) ? array() : $settings['typography']; - $settings['typography']['fontFamilies'] = empty( $settings['typography']['fontFamilies'] ) ? array() : $settings['typography']['fontFamilies']; - $settings['typography']['fontFamilies']['theme'] = empty( $settings['typography']['fontFamilies'] ) ? array() : $settings['typography']['fontFamilies']['theme']; - $settings['typography']['fontFamilies']['theme'] = array_merge( $settings['typography']['fontFamilies']['theme'], $variation['settings']['typography']['fontFamilies']['theme'] ); - - // Make sure there are no duplicates. - $settings['typography']['fontFamilies'] = array_unique( $settings['typography']['fontFamilies'] ); - } - } - - // Bail out early if there are no settings for webfonts. - if ( empty( $settings['typography'] ) || empty( $settings['typography']['fontFamilies'] ) ) { - return; - } - - $fonts = array(); - $handles = array(); - - // Look for fontFamilies. - foreach ( $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 ) { - // Skip if the font was registered through the Fonts API. - if ( isset( $font_face['origin'] ) && 'gutenberg_wp_fonts_api' === $font_face['origin'] ) { - continue; - } - - // 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 ( ! str_starts_with( $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 ] ); - } - } - - $font_family_handle = WP_Fonts_Utils::get_font_family_from_variation( $font_face ); - if ( empty( $font_family_handle ) && isset( $font_family['slug'] ) ) { - $font_family_handle = $font_family['slug']; - } - if ( ! empty( $font_family_handle ) ) { - $font_family_handle = WP_Fonts_Utils::convert_font_family_into_handle( $font_family_handle ); - } - if ( empty( $font_family_handle ) ) { - _doing_it_wrong( __FUNCTION__, __( 'Font family not defined in the variation or "slug".', 'gutenberg' ), '6.1.0' ); - } - - $handles[] = $font_family_handle; - if ( ! array_key_exists( $font_family_handle, $fonts ) ) { - $fonts[ $font_family_handle ] = array(); - } - - $fonts[ $font_family_handle ][] = $font_face; - } - } - } - - wp_register_fonts( $fonts ); - wp_enqueue_fonts( $handles ); - } -} - -if ( ! function_exists( 'gutenberg_add_registered_fonts_to_theme_json' ) ) { - /** - * Add missing fonts data to the global styles. - * - * @param array $data The global styles. - * @return array The global styles with missing fonts data. - */ - function gutenberg_add_registered_fonts_to_theme_json( $data ) { - $font_families_registered = wp_fonts()->get_registered_font_families(); - - $raw_data = $data->get_raw_data(); - - $font_families_from_theme = ! empty( $raw_data['settings']['typography']['fontFamilies']['theme'] ) - ? $raw_data['settings']['typography']['fontFamilies']['theme'] - : array(); - - /** - * Helper to get an array of the font-families. - * - * @param array $families_data The font-families data. - * @return array The font-families array. - */ - $get_families = static function ( $families_data ) { - $families = array(); - foreach ( $families_data as $family ) { - $font_family = WP_Fonts_Utils::get_font_family_from_variation( $family ); - $handle = WP_Fonts_Utils::convert_font_family_into_handle( $font_family ); - if ( ! empty( $handle ) ) { - $families[ $handle ] = true; - } - } - - return ! empty( $families ) ? array_keys( $families ) : array(); - }; - - // Find missing fonts that are not in the theme's theme.json. - $to_add = array(); - if ( ! empty( $font_families_registered ) ) { - $to_add = array_diff( $font_families_registered, $get_families( $font_families_from_theme ) ); - } - - // Bail out early if there are no missing fonts. - if ( empty( $to_add ) ) { - return $data; - } - - // Make sure the path to settings.typography.fontFamilies.theme exists - // before adding missing fonts. - if ( empty( $raw_data['settings'] ) ) { - $raw_data['settings'] = array(); - } - if ( empty( $raw_data['settings']['typography'] ) ) { - $raw_data['settings']['typography'] = array(); - } - if ( empty( $raw_data['settings']['typography']['fontFamilies'] ) ) { - $raw_data['settings']['typography']['fontFamilies'] = array(); - } - if ( empty( $raw_data['settings']['typography']['fontFamilies'] ) ) { - $raw_data['settings']['typography']['fontFamilies']['theme'] = array(); - } - - foreach ( $to_add as $font_family_handle ) { - $raw_data['settings']['typography']['fontFamilies']['theme'][] = wp_fonts()->to_theme_json( $font_family_handle ); - } - - return new WP_Theme_JSON_Gutenberg( $raw_data ); - } -} - -// `gutenberg_register_fonts_from_theme_json()` calls `WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()`, which instantiates `WP_Theme_JSON_Gutenberg()`; -// Gutenberg server-side blocks are registered via the init hook with a priority value of `20`. E.g., `add_action( 'init', 'register_block_core_image', 20 )`; -// This priority value is added dynamically during the build. See: tools/webpack/blocks.js. -// We want to make sure Gutenberg blocks are re-registered before any Theme_JSON operations take place -// so that we have access to updated merged data. -add_action( 'init', 'gutenberg_register_fonts_from_theme_json', 21 ); diff --git a/lib/load.php b/lib/load.php index f66fe7f474b961..801cd27527c96e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -114,7 +114,6 @@ function gutenberg_is_experiment_enabled( $name ) { // Fonts API files. require __DIR__ . '/experimental/fonts-api/class-wp-fonts-provider.php'; require __DIR__ . '/experimental/fonts-api/class-wp-fonts-utils.php'; - require __DIR__ . '/experimental/fonts-api/register-fonts-from-theme-json.php'; require __DIR__ . '/experimental/fonts-api/class-wp-fonts.php'; require __DIR__ . '/experimental/fonts-api/class-wp-fonts-provider-local.php'; require __DIR__ . '/experimental/fonts-api/class-wp-fonts-resolver.php'; diff --git a/phpunit/fonts-api/gutenbergAddRegisteredFontsToThemeJson-test.php b/phpunit/fonts-api/wpFontsResolver/addMissingFontsToThemeJson-test.php similarity index 92% rename from phpunit/fonts-api/gutenbergAddRegisteredFontsToThemeJson-test.php rename to phpunit/fonts-api/wpFontsResolver/addMissingFontsToThemeJson-test.php index 61ada8a403ea10..31bb4b1d409cc4 100644 --- a/phpunit/fonts-api/gutenbergAddRegisteredFontsToThemeJson-test.php +++ b/phpunit/fonts-api/wpFontsResolver/addMissingFontsToThemeJson-test.php @@ -1,18 +1,18 @@ assertInstanceOf( WP_Theme_JSON_Gutenberg::class, $actual, 'Instance of WP_Theme_JSON_Gutenberg should be returned' ); } @@ -60,7 +60,7 @@ public function test_should_bail_out_when_no_registered_fonts( $theme ) { switch_theme( $theme ); $data = new WP_Theme_JSON_Gutenberg( self::$theme_json_data[ $theme ] ); - $actual = gutenberg_add_registered_fonts_to_theme_json( $data ); + $actual = WP_Fonts_Resolver::add_missing_fonts_to_theme_json( $data ); $this->assertEmpty( wp_fonts()->get_registered_font_families(), 'No fonts should be registered in Fonts API' ); $this->assertSame( $data, $actual, 'Same instance of WP_Theme_JSON_Gutenberg should be returned' ); @@ -92,7 +92,7 @@ public function test_should_add_non_theme_json_fonts( $theme, $fonts, $expected wp_register_fonts( $fonts ); $data = new WP_Theme_JSON_Gutenberg( self::$theme_json_data[ $theme ] ); - $actual = gutenberg_add_registered_fonts_to_theme_json( $data ); + $actual = WP_Fonts_Resolver::add_missing_fonts_to_theme_json( $data ); $this->assertNotSame( $data, $actual, 'New instance of WP_Theme_JSON_Gutenberg should be returned' ); $actual_raw_data = $actual->get_raw_data(); @@ -104,7 +104,7 @@ public function test_should_add_non_theme_json_fonts( $theme, $fonts, $expected $this->assertContains( $expected, $actual_raw_data['settings']['typography']['fontFamilies']['theme'], - 'Fonts should be added after running gutenberg_add_registered_fonts_to_theme_json()' + 'Fonts should be added after running WP_Fonts_Resolver::add_missing_fonts_to_theme_json()' ); } diff --git a/phpunit/fonts-api/gutenbergRegisterFontsFromThemeJson-test.php b/phpunit/fonts-api/wpFontsResolver/registerFontsFromThemeJson-test.php similarity index 90% rename from phpunit/fonts-api/gutenbergRegisterFontsFromThemeJson-test.php rename to phpunit/fonts-api/wpFontsResolver/registerFontsFromThemeJson-test.php index d9c7fbaee5fff1..c3bedd9e63e9e6 100644 --- a/phpunit/fonts-api/gutenbergRegisterFontsFromThemeJson-test.php +++ b/phpunit/fonts-api/wpFontsResolver/registerFontsFromThemeJson-test.php @@ -1,18 +1,18 @@ array( @@ -33,7 +33,7 @@ public static function set_up_before_class() { public function test_should_bails_out_when_no_fonts_defined() { switch_theme( 'block-theme' ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); $wp_fonts = wp_fonts(); $this->assertEmpty( $wp_fonts->get_registered() ); @@ -43,7 +43,7 @@ public function test_should_bails_out_when_no_fonts_defined() { public function test_should_register_and_enqueue_style_variation_fonts() { switch_theme( static::FONTS_THEME ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); $wp_fonts = wp_fonts(); $this->assertContains( 'open-sans', $wp_fonts->get_registered_font_families(), 'Font families should be registered' ); @@ -57,7 +57,7 @@ public function test_should_register_and_enqueue_style_variation_fonts() { public function test_should_register_and_enqueue_all_defined_font_families() { switch_theme( static::FONTS_THEME ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); $wp_fonts = wp_fonts(); $expected = static::FONT_FAMILIES[ static::FONTS_THEME ]; @@ -74,7 +74,7 @@ public function test_should_register_and_enqueue_all_defined_font_families() { public function test_should_not_reregister_duplicate_fonts_from_style_variations() { switch_theme( static::FONTS_THEME ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); $wp_fonts = wp_fonts(); // Font families are not duplicated. @@ -117,7 +117,7 @@ public function test_should_not_reregister_duplicate_fonts_from_style_variations public function test_should_replace_src_file_placeholder( $handle, $expected ) { switch_theme( static::FONTS_THEME ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); $variation = wp_fonts()->registered[ $handle ]; $actual = array_pop( $variation->src ); @@ -185,7 +185,7 @@ public function data_should_replace_src_file_placeholder() { public function test_should_convert_font_face_properties_into_kebab_case() { switch_theme( static::FONTS_THEME ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); // Testing only one variation since this theme's fonts use the same properties. $variation = wp_fonts()->registered['dm-sans-400-normal']; @@ -202,12 +202,12 @@ public function test_should_convert_font_face_properties_into_kebab_case() { } /** - * Tests that gutenberg_register_fonts_from_theme_json() skips fonts that are already registered + * Tests that WP_Fonts_Resolver::register_fonts_from_theme_json() skips fonts that are already registered * in the Fonts API. How does it do that? Using the 'origin' property when checking each variation. * This property is added when WP_Theme_JSON_Resolver_Gutenberg::get_merged_data() runs. * * To simulate this scenario, a font is registered first, but not enqueued. Then after running, - * it checks if the gutenberg_register_fonts_from_theme_json() enqueued the font. If no, then + * it checks if the WP_Fonts_Resolver::register_fonts_from_theme_json() enqueued the font. If no, then * it was skipped as expected. */ public function test_should_skip_registered_fonts() { @@ -234,13 +234,13 @@ public function test_should_skip_registered_fonts() { ); // Pre-check to ensure no fonts are enqueued. - $this->assertEmpty( wp_fonts()->get_enqueued(), 'No fonts should be enqueued before running gutenberg_register_fonts_from_theme_json()' ); + $this->assertEmpty( wp_fonts()->get_enqueued(), 'No fonts should be enqueued before running WP_Fonts_Resolver::register_fonts_from_theme_json()' ); /* * When this function runs, it invokes WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(), * which will include the Lato fonts with a 'origin' property set in each variation. */ - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); $actual_enqueued_fonts = wp_fonts()->get_enqueued(); @@ -254,7 +254,7 @@ public function test_should_skip_when_font_face_not_defined() { /** * Callback that removes the 'fontFace' of the expected font family from the theme's theme.json data. - * This callback is invoked at the start of gutenberg_register_fonts_from_theme_json() before processing + * This callback is invoked at the start of WP_Fonts_Resolver::register_fonts_from_theme_json() before processing * within that function. How? It's in the call stack of WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(). * * @param WP_Theme_JSON_Data_Gutenberg| WP_Theme_JSON_Data $theme_json_data Instance of the Data object. @@ -288,7 +288,7 @@ public function test_should_skip_when_font_face_not_defined() { }; add_filter( 'wp_theme_json_data_theme', $remove_expected_font_family ); - gutenberg_register_fonts_from_theme_json(); + WP_Fonts_Resolver::register_fonts_from_theme_json(); remove_filter( 'wp_theme_json_data_theme', $remove_expected_font_family );