diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 9b39213fd14226..76999b6ac34366 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -245,9 +245,6 @@ public static function get_theme_data( $deprecated = array(), $options = array() } else { $theme_json_data = array(); } - // BEGIN OF EXPERIMENTAL CODE. Not to backport to core. - $theme_json_data = gutenberg_add_registered_fonts_to_theme_json( $theme_json_data ); - // END OF EXPERIMENTAL CODE. /** * Filters the data provided by the theme for global styles and settings. @@ -266,10 +263,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() if ( '' !== $parent_theme_json_file ) { $parent_theme_json_data = static::read_json_file( $parent_theme_json_file ); $parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) ); - // BEGIN OF EXPERIMENTAL CODE. Not to backport to core. - $parent_theme_json_data = gutenberg_add_registered_fonts_to_theme_json( $parent_theme_json_data ); - // END OF EXPERIMENTAL CODE. - $parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data ); + $parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data ); /* * Merge the child theme.json into the parent theme.json. @@ -279,6 +273,11 @@ public static function get_theme_data( $deprecated = array(), $options = array() static::$theme = $parent_theme; } } + + // BEGIN OF EXPERIMENTAL CODE. Not to backport to core. + static::$theme = gutenberg_add_registered_fonts_to_theme_json( static::$theme ); + // END OF EXPERIMENTAL CODE. + } if ( ! $options['with_supports'] ) { diff --git a/lib/experimental/fonts-api/register-fonts-from-theme-json.php b/lib/experimental/fonts-api/register-fonts-from-theme-json.php index cf943111963fdd..1038af2956a69b 100644 --- a/lib/experimental/fonts-api/register-fonts-from-theme-json.php +++ b/lib/experimental/fonts-api/register-fonts-from-theme-json.php @@ -121,8 +121,11 @@ function gutenberg_register_fonts_from_theme_json() { */ function gutenberg_add_registered_fonts_to_theme_json( $data ) { $font_families_registered = wp_fonts()->get_registered_font_families(); - $font_families_from_theme = ! empty( $data['settings']['typography']['fontFamilies'] ) - ? $data['settings']['typography']['fontFamilies'] + + $raw_data = $data->get_raw_data(); + + $font_families_from_theme = ! empty( $raw_data['settings']['typography']['fontFamilies']['theme'] ) + ? $raw_data['settings']['typography']['fontFamilies']['theme'] : array(); /** @@ -157,21 +160,24 @@ function gutenberg_add_registered_fonts_to_theme_json( $data ) { // Make sure the path to settings.typography.fontFamilies.theme exists // before adding missing fonts. - if ( empty( $data['settings'] ) ) { - $data['settings'] = array(); + if ( empty( $raw_data['settings'] ) ) { + $raw_data['settings'] = array(); + } + if ( empty( $raw_data['settings']['typography'] ) ) { + $raw_data['settings']['typography'] = array(); } - if ( empty( $data['settings']['typography'] ) ) { - $data['settings']['typography'] = array(); + if ( empty( $raw_data['settings']['typography']['fontFamilies'] ) ) { + $raw_data['settings']['typography']['fontFamilies'] = array(); } - if ( empty( $data['settings']['typography']['fontFamilies'] ) ) { - $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 ) { - $data['settings']['typography']['fontFamilies'][] = wp_fonts()->to_theme_json( $font_family_handle ); + $raw_data['settings']['typography']['fontFamilies']['theme'][] = wp_fonts()->to_theme_json( $font_family_handle ); } - return $data; + return new WP_Theme_JSON_Gutenberg( $raw_data ); } }