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

Block Hooks API: Harmonize ignoredHookedBlocks metadata injection logic #6550

Conversation

tjcafferkey
Copy link

@tjcafferkey tjcafferkey commented May 14, 2024

This PR aims to haromize the injection of the ignoredHookedBlocks data. We currently have two points of processing for this with the first being for wp_template & wp_template_part and the second being for wp_navigation which is stored separately in the wp_postmeta table. For us to combine these into a single approach this PR has a conditional statement to handle wp_navigation items separately.

I've also created (and tested) a new function to build template objects (WP_Template) from database objects (stdClass) which is where we intercept this data when the controller prepares it for the database.

Trac ticket: https://core.trac.wordpress.org/ticket/60759


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@tjcafferkey
Copy link
Author

tjcafferkey commented May 15, 2024

@ockham interested to know your thoughts on this approach. I can see an argument against the new function (_build_block_template_object_from_database_object) but without it the function inject_ignored_hooked_blocks_metadata_attributes becomes somewhat unwieldy and potentially hard to navigation.

I would prefer to write more unit tests but will wait on your feedback on the general approach before doing so.

@tjcafferkey tjcafferkey marked this pull request as ready for review May 15, 2024 10:01
Copy link

github-actions bot commented May 15, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props tomjcafferkey, bernhard-reiter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines +760 to +761
// Note: When can we remove this conditional?
if ( ! has_filter( 'rest_pre_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta' ) && ! has_filter( 'rest_pre_insert_wp_navigation', 'gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove it when the block_core_navigation_update_ignore_hooked_blocks_meta filter is no longer attached to the rest_pre_insert_wp_navigation hook. Since this is done by GB code, we'll have to wait until it's removed there, and blocks code is sync'd to Core.

OTOH, we'll have to retain the code in GB for at least two WP releases, as it's required to support the current WordPress version and the one before. So if this PR makes it into WP 6.6, GB still needs to support 6.5, so we'll have to retain the code there.

I'd suggest doing it the other way round: Add the filter unconditionally here, and instead add (yet another 😬) has_filter check in GB. (We will need to wait for GB to release a version that includes that check before we can land the Core PR in order to prevent Core trunk + GB stable from breaking.)

@ockham
Copy link
Contributor

ockham commented May 21, 2024

Hmm, looks like inject_ignored_hooked_blocks_metadata_attributes was previously really template-specific. I'm second-guessing now if it makes sense to use it as a shared filter for both rest_pre_insert_wp_template (and _part) on the one hand, and for rest_pre_insert_wp_navigation on the other, when we have to accommodate vastly different logic for those cases under the roof of that function after all 😕

I'm starting to think that we should after all start with a distinct filter function for rest_pre_insert_wp_navigation (basically copying over block_core_navigation_update_ignore_hooked_blocks_meta), and then work backwards from there 🤔

@ockham
Copy link
Contributor

ockham commented May 21, 2024

I'm starting to think that we should after all start with a distinct filter function for rest_pre_insert_wp_navigation (basically copying over block_core_navigation_update_ignore_hooked_blocks_meta), and then work backwards from there 🤔

We could probably extract a helper function for the applying the Block Hooks algorithm itself (i.e. running get_hooked_blocks, creating the visitors, and running traverse_and_serialized_blocks) that could hopefully be used by both inject_ignored_hooked_blocks_metadata_attributes and block_core_navigation_update_ignore_hooked_blocks_meta (or whatever we'll rename the latter to).

@tjcafferkey
Copy link
Author

We could probably extract a helper function for the applying the Block Hooks algorithm itself (i.e. running get_hooked_blocks, creating the visitors, and running traverse_and_serialized_blocks) that could hopefully be used by both inject_ignored_hooked_blocks_metadata_attributes and block_core_navigation_update_ignore_hooked_blocks_meta (or whatever we'll rename the latter to).

I think it makes sense to keep the two concerns (injecting the data into template markup vs updating it in wp_postmeta) separate.

I've experimented with this approach here #6604

Keen to know how you feel it compares to this one based on your comments above.

@ockham
Copy link
Contributor

ockham commented May 23, 2024

I've experimented with this approach here #6604

Keen to know how you feel it compares to this one based on your comments above.

Loving it! That one reads a lot more naturally IMO 😄

@ockham
Copy link
Contributor

ockham commented May 27, 2024

Should we close this one in favor of #6604? 😊

@tjcafferkey
Copy link
Author

Should we close this one in favor of #6604? 😊

Closing 😄

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