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

Font face resolver: print font faces from font families defined in all theme.json origins #59376

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ private static function parse_settings( array $settings ) {
continue;
}

// Prepare the fonts array structure for this font-family.
if ( ! array_key_exists( $font_family_name, $fonts ) ) {
$fonts[ $font_family_name ] = array();
}

$fonts[ $font_family_name ] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );
$fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should move to 6.5 folder now I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that because of this particular change or because of something else?

If you mean because of this particular change, this is my rationale:

I understand that the wordpress-6.4 folder holds the code needed to make the code work with the latest Gutenberg version running on WordPress < 6.4, right? That's still true, so I'm unsure why this should be moved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the wordpress-6.4 folder holds the code needed to make the code work with the latest Gutenberg version running on WordPress < 6.4, right?

no, wordpress-6.4 is the code that was backported during 6.4, so to make Gutenberg work with 6.3 and lower.

Copy link
Contributor Author

@matiasbenedetto matiasbenedetto Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wordpress-6.4 is the code that was backported during 6.4, so to make Gutenberg work with 6.3 and lower.

I think we said the same but with different words :)


Given this PR is only a bugfix, and these classes were merged in 6.4, and only one bugfix will be different in 6.5, should we still move all files to the WordPress-6.5 folder?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, because the 6.4 folder will get removed once the minimum supported version for Gutenberg is going to be 6.4 but that particular fix will still be needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we don't figure out a way to override the 6.4 use-case properly, then there's no match point in "fixing" this bug in Gutenberg. Maybe we should just close this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

figure out a way to override the 6.4 use-case properly

The use 6.4 use-case would be printing all the font faces when running wp 6.4 + the latest GB plugin, but I don't understand the quote. Could you explain or add more context, please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your question :P

I'm saying that if we can't override the class properly on Gutenberg, why change it at all, the 6.4 code should match the code that was merged in 6.4 and we can't move it to 6.5 and update it because it will have no effect anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, thanks for explaining. In that case, the bug will be still there when running 6.4 + Gutenberg. Is that ok? If that's OK we can close the PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok for me I think.

}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/compat/wordpress-6.4/fonts/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
* @since 6.4.0
*
* @param array[][] $fonts {
* Optional. The font-families and their font variations. Default empty array.
* Optional. The font-families and their font faces. Default empty array.
*
* @type string $font-family => array[] $variations {
* Optional. An associated array of font variations for this font-family.
* Each variation has the following structure.
* @type array {
* An indexed or associative (keyed by font-family) array of font variations for this font-family.
* Each font face has the following structure.
*
* @type array $font_variation {
* @type array {
* @type string $font-family The font-family property.
* @type string|string[] $src The URL(s) to each resource containing the font data.
* @type string $font_style Optional. The font-style property. Default 'normal'.
* @type string $font-style Optional. The font-style property. Default 'normal'.
* @type string $font-weight Optional. The font-weight property. Default '400'.
* @type string $font-display Optional. The font-display property. Default 'fallback'.
* @type string $ascent-override Optional. The ascent-override property.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/**
* WP_Font_Face_Resolver class.
*
* @package WordPress
* @subpackage Fonts
* @since 6.4.0
*
* @core-merge: this file is located in `wp-includes/fonts/`.
*/

if ( ! class_exists( 'WP_Font_Face_Resolver' ) ) {

/**
* The Font Face Resolver abstracts the processing of different data sources
* (such as theme.json) for processing within the Font Face.
*
* This class is for internal core usage and is not supposed to be used by
* extenders (plugins and/or themes).
*
* @access private
*/
class WP_Font_Face_Resolver {

/**
* Gets fonts defined in theme.json.
*
* @since 6.4.0
*
* @return array Returns the font-families, each with their font-face variations.
*/
public static function get_fonts_from_theme_json() {
$settings = gutenberg_get_global_settings();

// Bail out early if there are no font settings.
if ( empty( $settings['typography']['fontFamilies'] ) ) {
return array();
}

return static::parse_settings( $settings );
}

/**
* Parse theme.json settings to extract font definitions with variations grouped by font-family.
*
* @since 6.4.0
*
* @param array $settings Font settings to parse.
* @return array Returns an array of fonts, grouped by font-family.
*/
private static function parse_settings( array $settings ) {
$fonts = array();

foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
foreach ( $font_families as $definition ) {

// Skip if "fontFace" is not defined, meaning there are no variations.
if ( empty( $definition['fontFace'] ) ) {
continue;
}

// Skip if "fontFamily" is not defined.
if ( empty( $definition['fontFamily'] ) ) {
continue;
}

$font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] );

// Skip if no font family is defined.
if ( empty( $font_family_name ) ) {
continue;
}

$fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );
}
}

return $fonts;
}

/**
* Parse font-family name from comma-separated lists.
*
* If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ),
* parse and return the fist font from the list.
*
* @since 6.4.0
*
* @param string $font_family Font family `fontFamily' to parse.
* @return string Font-family name.
*/
private static function maybe_parse_name_from_comma_separated_list( $font_family ) {
if ( str_contains( $font_family, ',' ) ) {
$font_family = explode( ',', $font_family )[0];
}

return trim( $font_family, "\"'" );
}

/**
* Converts font-face properties from theme.json format.
*
* @since 6.4.0
*
* @param array $font_face_definition The font-face definitions to convert.
* @param string $font_family_property The value to store in the font-face font-family property.
* @return array Converted font-face properties.
*/
private static function convert_font_face_properties( array $font_face_definition, $font_family_property ) {
$converted_font_faces = array();

foreach ( $font_face_definition as $font_face ) {
// Add the font-family property to the font-face.
$font_face['font-family'] = $font_family_property;

// Converts the "file:./" src 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 camelCase properties into kebab-case.
$font_face = static::to_kebab_case( $font_face );

$converted_font_faces[] = $font_face;
}

return $converted_font_faces;
}

/**
* 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 6.4.0
*
* @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 6.4.0
*
* @param array $data The array to process.
* @return array Data with first dimension keys converted into kebab-case.
*/
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;
}
}
}
4 changes: 2 additions & 2 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ function gutenberg_is_experiment_enabled( $name ) {

// Load the Font Face and Font Face Resolver, if not already loaded by WordPress Core.
if ( ! class_exists( 'WP_Font_Face' ) ) {
require __DIR__ . '/compat/wordpress-6.4/fonts/font-face/class-wp-font-face.php';
require __DIR__ . '/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php';
require __DIR__ . '/compat/wordpress-6.5/fonts/font-face/class-wp-font-face.php';
require __DIR__ . '/compat/wordpress-6.5/fonts/font-face/class-wp-font-face-resolver.php';
}

/*
Expand Down
Loading