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: Add Templates Controller filter to avoid triggering wp_update_post #6225

Conversation

tjcafferkey
Copy link

@tjcafferkey tjcafferkey commented Mar 5, 2024

This change is inspired by this PR WordPress/gutenberg#59561 since they are suffering from the same problem.

By creating a rest_pre_insert_{$this->post_type} filter in theWP_REST_Templates_Controller rather than using the rest_insert_{$this->post_type} action. This avoids calling wp_update_post twice, which was the original reason for the issue, as it removed the backslash from the already-encoded entity. I suspect wp_update_post was also creating additional revisions which was mentioned in the linked Trac ticket.

By creating this filter it also creates consistency with the WP_REST_Posts_Controller which has the same filter.

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


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

github-actions bot commented Mar 5, 2024

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 tjcafferkey marked this pull request as ready for review March 5, 2024 14:25
Copy link

github-actions bot commented Mar 5, 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, gziolo, swissspidy, karolmanijak.

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

@tjcafferkey tjcafferkey changed the title Add filter to templates REST API controller to avoid using wp_update_… Block Hooks API: Add Templates Controller filter to avoid triggering wp_update_post Mar 5, 2024
@ockham ockham force-pushed the update/templates-controller-prepare-database-item-filter branch from 376ebdf to 0e65613 Compare March 5, 2024 15:16
@ockham ockham requested a review from gziolo March 5, 2024 15:30
@ockham
Copy link
Contributor

ockham commented Mar 5, 2024

This should be ready for review 😊

@gziolo
Copy link
Member

gziolo commented Mar 6, 2024

By creating this filter it also creates consistency with the WP_REST_Posts_Controller which has the same filter.

That makes sense to me. Avoiding two database operations should be one of the goals of this fix. See my #6225 (comment), where I'm unsure if it will work as intended for the logic that injects blocks.

Copy link
Member

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

Is there test coverage? Can we add tests.

…troller.php

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Copy link

@kmanijak kmanijak left a comment

Choose a reason for hiding this comment

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

I tested the PR against the original issue and it works just fine! Thanks @tjcafferkey for picking it up 🚀

@ockham
Copy link
Contributor

ockham commented Mar 6, 2024

Is there test coverage? Can we add tests.

I'll add some 👍

@ockham ockham force-pushed the update/templates-controller-prepare-database-item-filter branch from fb29d99 to 9bd9fcb Compare March 6, 2024 12:04
@ockham
Copy link
Contributor

ockham commented Mar 6, 2024

@swissspidy I've added some tests, both for the filter in isolation, and when used inside the controller. LMK if this looks good to you, or if you'd like anything else covered 🙂

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍🏻

@ockham
Copy link
Contributor

ockham commented Mar 7, 2024

Committed to Core in https://core.trac.wordpress.org/changeset/57790.

@ockham
Copy link
Contributor

ockham commented Mar 11, 2024

Backported to the 6.5 branch in https://core.trac.wordpress.org/changeset/57802.

pento pushed a commit that referenced this pull request Oct 15, 2024
Bootstrap block bindings sources earlier in the process through an inline script to ensure they are available when developers want to extend them in the client. Following the same pattern other APIs like registering block types are doing.

Props santosguillamot, cbravobernal, gziolo.
Fixes #6225.



git-svn-id: https://develop.svn.wordpress.org/trunk@59238 602fd350-edb4-49c9-b593-d223f7449a82
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.

5 participants