Skip to content

Read block attributes written at the top level - #91

Merged
dnnsjsk merged 2 commits into
inline0:mainfrom
elliottmangham:fix/top-level-block-attributes
Sep 12, 2026
Merged

dnnsjsk merged 2 commits into
inline0:mainfrom
elliottmangham:fix/top-level-block-attributes

Conversation

@elliottmangham

@elliottmangham elliottmangham commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

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

$attr is keyed by the ids the block declares, so the intersect is what keeps this from becoming a free-for-all: anchor, className, lock, metadata, style and 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

Payload Before After
{"message":"TOP"} false (default) "TOP"
{"blockstudio":{"attributes":{"message":"NESTED"}}} "NESTED" "NESTED"
both "NESTED" "NESTED"
{"className":"c","anchor":"a","notAField":"n"} ignored ignored

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 —

$raw_blockstudio_attributes =
	isset( $attributes['blockstudio']['attributes'] ) && is_array( … )
		? $attributes['blockstudio']['attributes']
		: array_filter( $attributes, fn( $key ) => ! in_array( $key, array( 'blockstudio', '_BLOCKSTUDIO_CONTEXT' ), true ), ARRAY_FILTER_USE_KEY );

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.php covers the four rows above against the blockstudio/native fixture, which dumps $a as JSON.

Removing array_intersect_key from transform() fails only test_top_level_attribute_reaches_the_template and leaves the other three passing — so the backward-compatibility cases are guarding, not restating the fix.

Full suite in wp-env, before and after:

Tests Assertions Failures
upstream/main untouched 1774 4685 7
this branch 1778 4695 7

The 7 are identical by name and pre-existing on your main in this environment (AssetsTest::test_compile_scss_supports_bootstrap_prelude, four BlockTagsPrefixesTest prefix 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 checked IslandsTest specifically, since it is the closest to this code path.

One note on running them: npm run test:unit hardcodes --env-cwd=…/plugins/blockstudio7, so it fails for anyone whose checkout is named blockstudio. 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, wrote visual_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.

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.
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.

2 participants