-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 Collections: update registration function signature and add caching #58363
Conversation
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❔ phpunit/tests/fonts/font-library/wpFontCollection/loadFromJson.php ❔ 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/class-wp-rest-font-collections-controller.php ❔ lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php ❔ lib/experimental/fonts/font-library/font-library.php ❔ phpunit/tests/fonts/font-library/wpFontCollection/__construct.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 |
Flaky tests detected in efce06e19986b9df5a4a6bb61d3aab785b34a69c. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7703293902
|
if ( is_wp_error( $data ) ) { | ||
return $data; | ||
} | ||
|
||
return true; | ||
return $data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be returning $data anyways. What's the purpose of the if() ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Will remove the conditional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I register a font collection with a wrong URL calling wp_register_font_collection like this:
wp_register_font_collection( 'https://example.com/test.json' );
- I get a notice which seems correct:
Notice: Function WP_Font_Collection::__construct was called incorrectly. Font collection "https://example.com/test.json" does not contain any font families. Please see Debugging in WordPress for more information. (This message was added in version 6.5.0.) in /var/www/html/wp1/wp-includes/functions.php on line 6031
- I get the collection with a collection with the url used as slug , which seems unexpected:
- The UI looks like this, the error message does not describe the error accordingly.
I think this is caused by the use of $slug_or_file
variable used to register a font collection.
public static function register_font_collection( $slug_or_file, $args = array() ) { | |
if ( file_exists( $slug_or_file ) || wp_http_validate_url( $slug_or_file ) ) { | |
$args = WP_Font_Collection::load_from_json( $slug_or_file ); | |
if ( is_wp_error( $args ) ) { | |
return $args; | |
} | |
$slug = $args['slug']; | |
} else { | |
$slug = $slug_or_file; | |
} | |
It seems like it's difficult to differentiate between a wrong url/filepath of a slug.
That seems strange. When I do |
Can the problem described here: #58363 (review) be solved by replacing this
with something like this? /**
* Register a new font collection.
*
* @since 6.5.0
*
* @param string $slug Font collection slug or path to a JSON file containing the font collection config.
* @param array $file_or_args Path to a JSON file containing the font collection config o an array with the font collection config.
* See {@see wp_register_font_collection()} for the supported fields.
* @return WP_Font_Collection|WP_Error A font collection is it was registered successfully and a WP_Error otherwise.
*/
public static function register_font_collection( $slug, $file_or_args = array() ) {
if ( is_array( $file_or_args ) ) {
$args = $file_or_args;
} else {
$args = WP_Font_Collection::load_from_json( $file_or_args );
}
$new_collection = new WP_Font_Collection( $slug, $args );
|
I do see a problem when mistyping a url--since it's not a valid url or file path, we have to assume it's a slug, which may not be the case. Seeing this, I think it's better if we use separate functions for php vs json registration, to make it clear what the intent is. I've updated the PR to include
|
Testing the new function, If I set a non existing file I get no notice about it:
I think a |
Yes, that's a good point--you may not be looking at the output of the function to see the WP_Error, so a notice would aid in debugging. Will add that. |
The name |
@matiasbenedetto What do you think would be better? |
Yes, it seems more descriptive of the actual functionality. |
I've updated the PR with the following:
|
I made a mistake and tried to register a collection like this.
I got this result: It can be a common mistake among extenders, so the slug should probably be tested to assess if it's a valid slug or not. |
…collection_from_json
d4bf565
to
d8f63b4
Compare
@matiasbenedetto That's a good point. In addition to addressing potential user error, I think the slug should be sanitized since it's used as an identifier in the REST API url. I've updated this in d8f63b4 to sanitize the slug and emit a notice if the given slug doesn't match the sanitized one. Let me know what you think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing well on my end. I verified that:
wp_register_font_collection_from_json
:
- Loads properly formatted JSON files as expected.
- Raise notices when the URL/file provided is existent or doesn't have the right content.
wp_register_font_collection
:
- Loads data as expected.
- Raises notices when the PHP data provided has missing required arguments like font_families.
What?
wp_register_font_collection
so that it takes a$slug
and optional$arg
array, but does not handle JSONwp_register_font_collection_from_json
function specifically for registering a collection from a JSON fileWP_Font_Collection::get_content
in favor of accessing all properties directly (e.g.$collection->font_families
)Why?
register_
functionsTesting Instructions
Example PHP font collection for testing:
In a plugin:
test-font-collection.json