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

Register post terms variations on taxonomy registration #56080

Conversation

gaambo
Copy link
Contributor

@gaambo gaambo commented Nov 13, 2023

What?

This solves the issue from #52569 where custom taxonomies registered after init priority 10 are not taken into account for the post-terms blocks variations.

Why?

See #52569 - server side function runs too early to make sure it gets all custom taxonomies/post types by plugins, themes etc..

How?

Similar to #54801 (which does the same thing for the navigation link block)
Variations for all taxonomies registered until register_block_core_post_terms (at init priority 10) are registered directly in here. For all taxonomies registered after that, actions are added to registered_taxonomy, which will register variations during taxonomy registration.

We can't use the second approach for all taxonomies, because some of them (eg builtin ones) are registered before the post terms block is registered, therefore variations can't yet be added.

I'm directly adding the variation on the WP_Block_Type object from WP_Block_Type_Registry, because there's no API for registering variations in PHP (yet).

Testing Instructions

Manual testing

Taxonomy

  1. Register a custom taxonomy (example code from developer.wordpress.org)
add_action('init', function() {
	 $args   = array(
		 'labels'            => array(
		     'name'         => __( 'Courses' ),
	         ),
	 );
	 register_taxonomy( 'course', [ 'post' ], $args );
});

Important: Set the name/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- or post-editor (for the post type post)
4. try to search for a block named like the custom taxonomy (eg "Courses").
5. Block is available

Automated testing

Run npm run test:unit:php:base -- --filter Block_Post_Terms_Variations_Test

Testing Instructions for Keyboard

Screenshots or screencast

Copy link

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core.
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@gaambo
Copy link
Contributor Author

gaambo commented Nov 13, 2023

@draganescu Could you please have a look at this as well? It's pretty much the same as #54801, but for the post-terms block.

@gaambo gaambo force-pushed the fix/52569-post-terms-taxonomies-variations branch from 38c3bc1 to e61364f Compare November 14, 2023 08:09
Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

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

This looks good to me, I found myself wondering if unregistering before the block registration needs anything but it does not since once unregistered the tyeps won't exist for the block registration to build variations on them.

👏🏻

@draganescu
Copy link
Contributor

Depending on what you get on the previous core ticket (if we need to port this in any way) this PR is subject to the same conclusions.

@gaambo
Copy link
Contributor Author

gaambo commented Jan 29, 2024

@desrosj since we've got #54801 and #56100 merged (for the navigation-link block), is there any chance this could get merged as well?
I probably have to update this PR as well, to work with the changes about the WP_Block_Type::variations property made in 2c247e2 and #56952 as well.

@gaambo gaambo force-pushed the fix/52569-post-terms-taxonomies-variations branch from 2a1a262 to eba6100 Compare January 29, 2024 09:46
@gaambo
Copy link
Contributor Author

gaambo commented Jan 29, 2024

Updated the PR to be similar to the changes in #56952 and work with the new private variations property of WP_Block_Type and variation_callback. I also renamed the functions so the post-terms block PHP is very similar to the navigation-link ones.

Also FYI: According to this comment in 56100 no backport is required for phpunit tests from blocks. Since this PR is the same but for the post-terms block, it's my understanding no backporting is required either.

@gaambo gaambo force-pushed the fix/52569-post-terms-taxonomies-variations branch from eba6100 to a448131 Compare January 29, 2024 14:11
@gaambo gaambo closed this Feb 1, 2024
@draganescu
Copy link
Contributor

@gaambo what happened with this one? I see you closed it but what's the replacement?

@gaambo
Copy link
Contributor Author

gaambo commented Feb 1, 2024

@draganescu
I've closed it, because the original implementation (via registered_post_type and registered_taxonomy) was also used in #54801 for the navigation link block and that lead to timeouts during PHPUnit tests (see #54801 (comment)).
For the navigation link block, there's currently a workaround by unregistering this hooks when running PHPUnit tests (see WordPress/wordpress-develop#5922 (comment)) and I've made a follow-up PR which uses the new get_block_type_variations hook instead: #58389

I want to see which way it goes for the navigation link block and when that's settled I will do the same for the post-terms block, since it's pretty similar and quick to do then.

@gaambo
Copy link
Contributor Author

gaambo commented Feb 26, 2024

FYI: this was already fixed in 2c247e2 via the new variation_callback argument when registering the block.

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

Successfully merging this pull request may close these issues.

2 participants