Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fonts API] Registered variation for font in theme not added in gutenberg_add_registered_fonts_to_theme_json() #50048

Closed
hellofromtonya opened this issue Apr 24, 2023 · 1 comment
Assignees
Labels
[Type] Bug An existing feature does not function as intended

Comments

@hellofromtonya
Copy link
Contributor

hellofromtonya commented Apr 24, 2023

Part of the Fonts API Roadmap #41479.

Description

Scenario:
A variation is programmatically registered when the font-family is defined in theme, but the variation is not defined in the theme.

When this scenario happens, the variation is not added by gutenberg_add_registered_fonts_to_theme_json(). It should be.

This is because the diff comparisons are at the font-family level. Only those font-families that differ from the theme's font-families get added.

It is conceivable that a plugin could register the same font family but with one or more different variations than the theme.

Step-by-step reproduction instructions

  1. Use the TT3 theme.
  2. Add the following code as a new file in wp-content/mu-plugins.
<?php

add_action( 'init', static function() {
	if ( ! function_exists( 'wp_register_fonts' ) ) {
		return;
	}

	$fonts = array(
		'DM Sans' => array(
			'dm-sans-500-normal' => array(
				'font-family' => 'DM Sans',
				'font-style'  => 'normal',
				'font-weight' => '500',
				'src'         => 'https://example.com/tests/assets/fonts/dm-sans/DMSans-Medium.woff2',
			),
			'dm-sans-500-italic' => array(
				'font-family' => 'DM Sans',
				'font-style'  => 'italic',
				'font-weight' => '500',
				'src'         => 'https://example.com/tests/assets/fonts/dm-sans/DMSans-Medium.woff2',
			),
		),
	);
	wp_register_fonts( $fonts );

	$file         = get_stylesheet_directory() . '/theme.json';
	$decoded_file = wp_json_file_decode( $file, array( 'associative' => true ) );;
	$data         = new WP_Theme_JSON_Gutenberg( $decoded_file );
	$raw_data     = gutenberg_add_registered_fonts_to_theme_json( $data )->get_raw_data();

	echo "<pre>";
	var_dump( $raw_data['settings']['typography']['fontFamilies']['theme'] );
	echo "</pre>";
	exit;
}, 100 );
  1. Refresh your test site.

Notice the data does not include the DM Sans Medium font variation data. It does include DM Sans 400 and 700, but not the 500 variations.

Screenshots, screen recording, code snippet

Screen Shot 2023-04-24 at 4 16 48 PM

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@hellofromtonya
Copy link
Contributor Author

Update:

This issue is no longer valid for the Fonts API once the Font Library is merged.

Why?

As shared in Ongoing Roadmap update #41479 (comment), the Fonts API will no longer inject / add fonts into theme.json. Rather, it will become read-only of the theme.json merged data.

The WP_Fonts_Resolver::add_missing_fonts_to_theme_json() will be removed (see #51716).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

2 participants