Repository navigation
fix: set up the edited post when rendering block previews over REST - #143
Merged
Merged
Conversation
The single render route read post_id while the editor sends postId, so setup_postdata() never ran. The batch route never set a post at all. Both now set up the edited post, the batch route only for posts the user can edit.
Contributor
|
Included in Blockstudio 7.7.6, now officially released. This PR is merged into main as part of the reviewed release, with full CI and distribution checks passing for the release commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Block templates rendered through the editor REST endpoints do not see the edited post consistently. On the edit screen (preloaded render)
get_the_ID(),get_the_title()andget_the_author()return the post. Through/gutenberg/block/render/allthey return nothing, and through/gutenberg/block/render/{name}(after an attribute change)get_the_author()and$idare empty.Cause:
gutenberg_block_render()readspost_id(includes/classes/rest.php:371), but the editor sendspostId(getPostParamsinsrc/blocks/components/block/index.tsx), sosetup_postdata()never runs. The global post was only set as a side effect of the permission callback.gutenberg_block_render_all()never set a post at all; it only copies each block's params into$_GET.Fix: the single route reads
postId(falling back topost_id), the same parameter its permission callback already checks. The batch route sets up each block's post, but only when the user can edit that post, because the route itself only checksedit_posts.Measured in Playground with a template printing
get_the_ID(),get_the_title()andget_the_author():6 / Probe post / admin, batch empty for all three, single6 / Probe post / (empty)7 / Probe post / adminTests:
tests/unit/rest/BlockRenderPostTest.phpcovers the single route, the batch route and a contributor rendering a post they cannot edit. The first two fail on main and pass with the fix;tests/unit/restpasses (22/22).post-meta.mdno longer saysget_the_ID()only works on the frontend, and the LLM context is regenerated.Related: #141 changes how
Block::render()picks the post id from block context. The two changes do not overlap; with both, editor REST renders get the same post from$_GET, the global post and the block context.