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 Library: Update function signature of wp register font collection #58269

Conversation

matiasbenedetto
Copy link
Contributor

What?

  • Updates function signature of wp_register_font_collection from wp_register_font_collection( $config ) to wp_register_font_collection( $slug, $config ).
  • Updates the WP_Font_Collection constructor and settings validation function.
  • Updates and improves unit tests.

Why?

To make the function more likely to other core functions.
Requested here: #57884 (comment)

How?

Change the parameters received by the functions.

Testing Instructions

  • Use the font library and check that font collections continue working as expected.
  • Run unit tests

Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/fonts/font-library/class-wp-font-collection.php
❔ lib/experimental/fonts/font-library/class-wp-font-library.php
❔ lib/experimental/fonts/font-library/font-library.php
❔ phpunit/tests/fonts/font-library/wpFontCollection/__construct.php
❔ phpunit/tests/fonts/font-library/wpFontCollection/getConfig.php
❔ phpunit/tests/fonts/font-library/wpFontCollection/getContent.php
❔ phpunit/tests/fonts/font-library/wpFontCollection/isConfigValid.php
❔ phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php
❔ phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php
❔ phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php
❔ phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php
❔ phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php

Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

This tests well for me, the font library and font collections are mostly working as expected 👍

I noticed one issue but I'm not sure if I've tested in a weird way. I'm using the Modern Fonts Stacks plugin to test, and I made the slug an empty string, in the wp_register_font_collection call, like this: wp_register_font_collection ( '', $modern_fonts_config );

This seems to break the Modern Fonts Stacks collection but also changes the default fonts collection to the default modal tab name, "Install Fonts":

image

I'm not sure what's expected here, but the empty slug leaking into the default collection config doesn't seem right.

Comment on lines 142 to 155
if ( isset( $config['src'] ) && ! is_string( $config['src'] ) ) {
_doing_it_wrong( __METHOD__, __( 'Font Collection config "src" option is required as a non-empty string.', 'gutenberg' ), '6.5.0' );
return false;
}

if ( isset( $config['font_families'] ) && ( empty( $config['font_families'] ) || ! is_array( $config['font_families'] ) ) ) {
_doing_it_wrong( __METHOD__, __( 'Font Collection config "font_families" option is required as a non-empty array.', 'gutenberg' ), '6.5.0' );
return false;
}

if ( isset( $config['categories'] ) && ( empty( $config['categories'] ) || ! is_array( $config['categories'] ) ) ) {
_doing_it_wrong( __METHOD__, __( 'Font Collection config "categories" option is required as a non-empty array.', 'gutenberg' ), '6.5.0' );
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

Should these three _doing_it_wrong calls use sprintf placeholders, similar to this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Added that here: b826cea

@matiasbenedetto
Copy link
Contributor Author

This seems to break the Modern Fonts Stacks collection but also changes the default fonts collection to the default modal tab name, "Install Fonts".

Yep, that's expected. That was a request from some time ago about displaying a different name for the default font collection if there is more than one collection registered.

@creativecoder
Copy link
Contributor

I've been looking over existing registration functions and related classes in Core and using an $args = array() as the last param is standard, but it seems to always be optional.

I'm thinking we should have all of the required font collection config be explicit arguments, and allow $args to be empty. For example:

// These would default to empty strings or array if not provided:
$args = array(
    'name'        => __( 'My Collection' ),
    'description' => __( 'My collection of fonts', 'text-domain' ),
    'categories   => array(
        array(
            'name' => __( 'Sans Serif', 'text-domain' ),
            'slug' => 'sans-serif'
         ),
    ),
);
$src = 'https://s.w.org/images/fonts/17.6/collections/google-fonts-with-preview.json';
wp_register_font_collection( 'my-collection', $src, $args );

Now $src can be a file or an array of font families, so this needs to be addressed. Perhaps two separate functions would be clearer

  • wp_register_font_collection accepts a php array of font families
  • wp_register_font_collection_from_file accepts a string that's a path to a local or remote file

This is similar to how register_block_type and register_block_type_from_metadata work for blocks, which is another entity that can be registered from php or a json file.

What do you think?

@creativecoder
Copy link
Contributor

@matiasbenedetto @mikachan @pbking and I discussed this a bit on a hangout today. Using a signature like this seems to be most consistent with other registration functions

wp_register_font_collection( $slug, $file_or_array, $args );
  • $slug is the font collection slug
  • $file_or_array is either a path to a local/remote json file, or an array with font_families and categories
  • $args is optional, and currently takes name and description strings

@creativecoder
Copy link
Contributor

After working with font collections more, having the font collection properties split between a json file and PHP config seems quite strange.

I think any performance issues with fetching remote collections should be dealt with separately and not leak into the structure of WP_Font_Collection. (e.g. not using ::get_content() to access some properties that are remote and direct getters for other properties.)

I did an exploration here that addresses this, and adds caching to handle performance considerations: #58363

@matiasbenedetto
Copy link
Contributor Author

closing in favor of #58363

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants