Repository navigation
fix: read the postId block context when rendering a block - #141
Merged
Merged
Conversation
Block::render() took the post from $_GET['postId'], get_the_ID() and get_queried_object_id() only. A block rendered outside a main query, for example through do_blocks() in a REST request or WP-CLI, got $block['postId'] = 0 even though WordPress passed the post as postId block context, and a block rendered through WP_Block with an explicit postId context and no global post got an empty $post_id. $post_id now uses the postId block context before get_the_ID(), and $block['postId'] falls back to $post_id when there is no queried object. An explicit $_GET['postId'] still wins, the editor path is unchanged, and inside a Query Loop $block['postId'] stays the queried post as documented.
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.
A Blockstudio block rendered outside a main query gets
$block['postId'] = 0, although WordPress hands it the post aspostIdblock context. A block rendered throughWP_Blockwith an explicitpostIdcontext and no global post gets an empty$post_id.Measured with a probe block that prints
$post_id,$block['postId'],$block['postType']and$block['context']['postId'], on unchangedmain:$post_id$block['postId']$block['postType']context.postIddo_blocks()with a global$post(38), no queried object, as in a REST request or WP-CLI( new WP_Block( $parsed, [ 'postId' => 39, 'postType' => 'post' ] ) )->render(), no global post$_GET['postId'] = 43, global post 44In the first row
postIdandpostTypedisagree:get_post_type( 0 )falls back to the global post. Tools that render outside a request currently have to set$_GET['postId']to get a usable$block['postId'], which is whatBlocks(preload) andCanvas_Page_Documentdo internally.Cause
Block::render()(includes/classes/block.php, start of the method):$object_idbecomes$block['postId']. The block type declaresusesContextpostIdandpostType(Build, where theWP_Block_Typeis created), so$wp_block->context['postId']is filled byrender_block()from the global post and bycore/post-templatefor each item, butrender()never reads it. Without a queried object,get_queried_object_id()is 0.Fix
$post_id:$_GET['postId'], then$wp_block->context['postId']when$wp_blockis aWP_Blockwith that context, thenget_the_ID().$block['postId']:$_GET['postId'], then the editor path as before, thenget_queried_object_id(), and only when that is 0,$post_id.$block['postId']deliberately does not switch to the loop item inside a Query Loop:docs/docs/blocks/context.mddocuments it as the current post there, next to$block['context']['postId']for the loop item, and templates may rely on that. With a queried object nothing changes. Without one (REST, WP-CLI, and frontend views without a queried object such as the posts index or search),$block['postId']was 0 and is now the rendered post, which is also the post$block['postType']already reported.One sentence added to
docs/docs/blocks/context.md, LLM context rebuilt withnpm run build:llm.Tests
tests/unit/block/BlockPostContextTest.phpregisters a probe block from a temporary directory (same pattern asBlockRepeaterMinRowsTest):test_render_outside_the_main_query_uses_the_global_post: fails onmain($block['postId']is'0', expected the post ID), passes with the fix.test_block_context_post_reaches_the_template_without_a_global_post: fails onmain($post_idis''), passes with the fix.test_query_loop_items_keep_the_queried_post_in_block_post_idandtest_request_post_id_wins_over_block_context: pass before and after, they pin the documented Query Loop behaviour and the$_GET['postId']priority.Run in WordPress Playground (WordPress 7.1.3, plugin and test theme mounted as in
tests/wp-env), one PHPUnit run per test class: the new class is 2 failures onmainandOK (4 tests, 22 assertions)with the fix. The otherblockclasses andrender,block-tags,islands,canvas,wpmlandrestgive identical results onmainand on this branch (the failures there are the same tests onmain, environment related;GithubUpdaterTestneeds process isolation and cannot run in Playground).composer cs, PHPStan onincludes/classes/block.phpandnpm run checkare clean.This touches the lines right below the render mode check that #140 moves into a helper; whichever lands second needs a trivial rebase, happy to do it.