Repository navigation
Read block attributes written at the top level - #91
Merged
dnnsjsk merged 2 commits intoSep 12, 2026
Merged
Conversation
Blockstudio registers each of a block's attributes individually on the WP_Block_Type, so a REST, WP-CLI or migration client discovering a block from the registry writes them at the top level of the block's attributes. transform() merged its defaults with blockstudio.attributes alone, so those values were stored, parsed back byte-identically, and then dropped at render: the block showed its default with no error and no warning. render() already accepts the top-level shape when building the Islands marker, so the two paths disagreed about what a valid payload looks like. Top-level values are merged between the defaults and blockstudio.attributes, scoped with array_intersect_key against the ids the block declares — so WordPress' own attributes (anchor, className, lock, metadata, style) and Blockstudio's reserved keys can never enter, and the merged key set is unchanged. A value nested under blockstudio.attributes still wins, so every block the editor has saved renders exactly as before. Fixes inline0#90
Four cases against the blockstudio/native fixture, which dumps $a as JSON: a top-level attribute reaches the template, a nested value still beats a top-level one of the same name, undeclared top-level keys (className, anchor, and a name no block declares) are not adopted, and nested-only attributes are untouched. Removing array_intersect_key from transform() fails the first and leaves the other three passing, so the backward-compatibility cases are guarding rather than restating the fix.
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.
Implements option 1 from #90.
The change
$attributes = array_merge( $attr, + array_intersect_key( $attributes, $attr ), $attributes['blockstudio']['attributes'] ?? array() );Top-level values slot between the defaults and
blockstudio.attributes, so they beat a default but never beat an explicitly nested value.Why
array_intersect_key$attris keyed by the ids the block declares, so the intersect is what keeps this from becoming a free-for-all:anchor,className,lock,metadata,styleand Blockstudio's own reserved keys are not declared attributes, so they cannot enter. The merged key set is identical before and after — this can only change the value of a key that was already going to be there.Behaviour
{"message":"TOP"}false(default)"TOP"{"blockstudio":{"attributes":{"message":"NESTED"}}}"NESTED""NESTED""NESTED""NESTED"{"className":"c","anchor":"a","notAField":"n"}Only the first row changes, and only from a value that was being discarded.
Backward compatibility
Every block the editor has ever saved carries the nested shape, so the new branch only fires for attributes that are currently being thrown away. I could not construct a case where an existing site's render changes.
There is also a precedent in the same function's caller:
render()already falls back to reading top-level attributes when building the Islands marker —So the two paths disagreed about what a valid payload looks like: the marker honoured the top-level shape, the render did not.
Testing
tests/unit/block/BlockTopLevelAttributesTest.phpcovers the four rows above against theblockstudio/nativefixture, which dumps$aas JSON.Removing
array_intersect_keyfromtransform()fails onlytest_top_level_attribute_reaches_the_templateand leaves the other three passing — so the backward-compatibility cases are guarding, not restating the fix.Full suite in
wp-env, before and after:upstream/mainuntouchedThe 7 are identical by name and pre-existing on your
mainin this environment (AssetsTest::test_compile_scss_supports_bootstrap_prelude, fourBlockTagsPrefixesTestprefix cases,IslandsTest::test_non_island_blocks_render_unchanged,SiteTemplatesTest::test_compiles_php_and_twig_sources_to_block_markup) — nothing here adds or fixes one. I checkedIslandsTestspecifically, since it is the closest to this code path.One note on running them:
npm run test:unithardcodes--env-cwd=…/plugins/blockstudio7, so it fails for anyone whose checkout is namedblockstudio. Happy to send that as a separate one-liner if useful.How this was found
Writing an MCP server that drafts content into WordPress. It read
okd/hero's attributes from the registry, wrotevisual_type: "media"— a valid option — and got a hero still rendering its WebGL default.parse_blocks()returned the attribute exactly as sent, the editor opened the block with no invalid-content notice, and nothing errored. Only comparing rendered output against the payload showed it, which is a slow way to find out.