-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Navigation Link: use get_block_type_variations to register variations #58389
Navigation Link: use get_block_type_variations to register variations #58389
Conversation
I tested this by applying the code directly from this diff to WordPress/wordpress-develop#5967. Now all the PHPUnit tests pass without timeout which makes me think this is the culprit of the issue there. Let's look to bring this in. |
Regarding backwards-compatibility:
register_block_type_from_metadata(
__DIR__ . '/navigation-link',
array(
'render_callback' => 'render_block_core_navigation_link',
'variations' => build_navigation_link_block_variations(),
)
); BUT that would mean, all the performance benefits of the
|
After trying some things out, I came to the following conclusions
If anybody else has some ideas, that would be great 😅 |
I don't know much about the problem but I can answer some of the questions above.
Ideally we deprecate and remove since they were not in Core but if we think that it's very unlikely that these things are used by third-party code, I believe it's fine to just remove them.
I think that's fine personally. What would it take to support previous core versions? (6.4 and 6.3), if it's possible to do so somehow, we can potentially add this code to |
@youknowriad thank you for your input! I just pushed a commit which
I've also updated the PR description with all this new information. This should also include the code style changes from #58385 (so i closed that one). |
b129d29
to
35585bd
Compare
@getdave should we try bringing this in for the next release, or do you think the workaround with disabling the filters in unit tests is enough for now? |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@gaambo I'm going to review this as a priority tomorrow and try and catch up here. |
Looking now |
I can confirm these changes will not need backporting to WP 6.5 so long as this PR lands in time for the Gutenberg Plugin 17.7.0 RC release on Friday 9th Feb 2024. |
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.
Let's bring this in. A few nits.
Thank you for all the work on this 🙇
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.
Did not test but the code updates LGTM apart from some nits.
35585bd
to
a142a17
Compare
Thanks for the feedback. I applied it and rebased :) |
I restarted all the tests. We've had some trouble in |
@youknowriad Please could I get a confidence check here that:
I don't think any of the tests are faling due to changes in this PR. It @gaambo can sync his fork and then rebase this PR the tests should pass but that might not be possible prior to the 17.7.0 RC today. If not then I'd like to force merge here. |
0471b9c
to
ab2afaf
Compare
Just rebased the PR, so let's see if tests go through now :) |
Aside the performance tests that are still failing on trunk, everything should be passing. |
* @since 6.5.0 | ||
* @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. | ||
* | ||
* TODO: After two WP versions (6.7), we can remove this. |
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 this is here only to ensure compatibility with previous versions in the Gutenberg plugin, can we define these functions outside the "block-library" package or no?
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.
It's there for two reasons:
- The functions have been in Gutenberg since November (but no core version)
- They provide backwards-compatibility when the Gutenberg plugin is used with WP versions < 6.5
Do you mean, I could move them into the lib/compat/wordpress-6.5
directory?
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.
Do you mean, I could move them into the lib/compat/wordpress-6.5 directory?
Yes, since these functions are "Gutenberg specific" and shouldn't "run" in Core. I was wondering if there's a way to keep the "navigation" block code "core only" and add the compat code from the compat folder. The reason is that the navigation block code will be copied as is into Core.
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.
@youknowriad I understand.
I did that in dd6857c
I disabled the phpcs rules for these functions, since they were never in core and therefore imho shouldn't need a guarded clause. But if that thought was wrong, let me know.
Tests are passing locally. Only two other tests related to WP_Style_Engine_CSS_Rule_Gutenberg::set_at_rule
are failing locally - but let's see what the GitHub tests say.
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.
Maybe I'm missing something the functions are still in the navigation-link/index.php file?
ab2afaf
to
dd6857c
Compare
PHPUnit failure:
It's likely because the code already existed in We could add a guard clause for safety? 🤷 @gaambo Not sure if you already did this but you might find it easier to:
That way we should bring in any fixes from |
Oh, because the code was already once synced into core in the previous packages update. Yeah, of course - I'll just add the guard clause. |
@gaambo not really, we should not guard but just use prefixes functions |
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.
So I didn't test but the diff looks good to me.
🎉 🎉 🎉 |
Huzzah! |
if ( 'core/navigation-link' !== $args['name'] || ! empty( $args['variation_callback'] ) ) { | ||
return $args; | ||
} | ||
$args['variation_callback'] = 'build_navigation_link_block_variations'; |
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 compat code doesn't work because the build_navigation_link_block_variations
function does not exist.
The consequence is that in WordPress 6.4 the Navigation Link variations for post types (Post Link, Page Link) are not registered, we have only "Custom Link" available in the editor.
I was seeing strange Playwright e2e failures in my PR #59064 and I tracked them down to this. The e2e tests for the core/navigation
block are failing because they expect "Page Link" components, but they see only "Custom Link" ones. See for example this code:
gutenberg/test/e2e/specs/editor/blocks/navigation-list-view.spec.js
Lines 103 to 112 in 237865f
await expect( | |
listView | |
.getByRole( 'gridcell', { | |
name: 'Page Link', | |
} ) | |
.filter( { | |
hasText: 'Block 1 of 2, Level 1', // proxy for filtering by description. | |
} ) | |
.getByText( 'Top Level Item 1' ) | |
).toBeVisible(); |
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.
Oh gosh, this PR (or my work on it) seems to be cursed 🙈
You are right, the functions name should be gutenberg_block_core_navigation_link_build_variations
- since that's the prefixed name after building.
If I change it to that and use WP 6.4 locally, I get "Page Link" and "Post Link" variations correctly.
I wonder why no other tests caught that, but since the function is just passed as a callback it's not called directly and therefore doesn't trigger an error - probably that's why.
I've created a PR to fix that here: #59126
This is an exploration to follow up to #54801, but using the new
get_block_type_variations
filter to register those variations.This has performance benefits (see #56952) and also should fix the phpunit timeouts happening after merging the original PR and merging that into core.
Why?
How?
Instead of using the
registered_post_type
andregistered_taxonomy
hooks (which lead to timeouts an can be a performance issue on sites with many CPTs/taxonomies), this uses the newget_block_type_variations
filter.Since Gutenberg has to support the last two core versions and the old functions have been in Gutenberg since November, those were kept in and marked deprecated (I've still added a
@since
and a@deprecated
tag).The unit tests use the old deprecated methods on core versions <6.5 so unit tests work on older and new versions.
This way:
get_block_type_variations
Alternatives tried
Using the
variation_callback
instead of the filter was tried, and would register CPTs/taxonomies registered after init#10. But unregistering them would not unregister those variations, since the callback is only called once per request.Testing Instructions
Automated testing
Run
npm run test:unit:php:base -- --filter Block_Navigation_Link_Variations_Test
Manual Testing
Post Type
Important: Set the item_link label or the block variation is called Post Link.
Important: change add_action priority to something higher than 10 (eg 11) - when using something lower it already works in trunk.
2. Go into site-editor and add a navigation block
3. try to search for a block named like the custom taxonomy (eg "Product Link").
4. Block is available
Taxonomy
Important: Set the item_link label or the block variation is called Post Link.
Important: change add_action priority to something higher than 10 (eg 11) - when using something lower it already works in trunk.
3. Go into site-editor and add a navigation block
4. try to search for a block named like the custom taxonomy (eg "Product Link").
5. Block is available
Testing unregistering
Post Type
Important: Set the item_link label or the block variation is called Post Link.
Important: change add_action priority to something higher than 10 (eg 11) - when using something lower it already works in trunk.
2. Unregister the custom post type after the navigation link block is registered and the variation is created:
Taxonomy
Important: Set the item_link label or the block variation is called Post Link.
Important: change add_action priority to something higher than 10 (eg 11) - when using something lower it already works in trunk.
2. Unregister the custom taxonomy after the navigation link block is registered and the variation is created:
Co-authored-by: gaambo gaambo@git.wordpress.org
Co-authored-by: getdave get_dave@git.wordpress.org
Co-authored-by: youknowriad youknowriad@git.wordpress.org
Co-authored-by: Andrei Draganescu 107534+draganescu@users.noreply.github.com