Skip to content

Block Bindings: Support binding to innerBlocks (generic mechanism) - #79379

Closed
cbravobernal wants to merge 1 commit into
WordPress:trunkfrom
cbravobernal:61225-innerblocks-bindings
Closed

cbravobernal wants to merge 1 commit into
WordPress:trunkfrom
cbravobernal:61225-innerblocks-bindings

Conversation

@cbravobernal

@cbravobernal cbravobernal commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Warning

🚧 Do not review yet — work in progress. This needs more input, more testing, and more knowledge-gathering before it is ready. I will mark it ready and request review when it is.

What?

Extends the Block Bindings mechanism so a registered binding source can supply and receive a block's inner blocks (its child block tree), not just scalar attributes.

Part of #61225. This PR is the generic, source-agnostic mechanism only — the first real consumer (core/pattern-overrides) and the user-facing documentation are intentionally not included here (see "Scope" below).

A block declares the binding with a reserved innerBlocks key inside metadata.bindings, on the container that owns the inner blocks:

<!-- wp:group {"metadata":{"bindings":{"innerBlocks":{"source":"my/source"}}}} -->
<div class="wp-block-group"></div>
<!-- /wp:group -->

The value contract is a single serialized block-markup string, parsed at each runtime boundary (parse() in JS, parse_blocks() in PHP):

Source returns Result
undefined (JS) / null (PHP) Absence → fall back to the block's own serialized children
'' Intentionally-empty area (no fallback)
a serialized string Parsed into the controlled inner-block tree

No new source callbacks are introduced — the reserved key flows through the existing getValues / setValues / canUserEditValue (JS, with canUserEditValue receiving the binding's args) and get_value_callback (PHP, read-only) API.

Why?

Block Bindings could only bind scalar attributes. Many use cases need a source to own a block's child tree. This adds that capability generically, decoupled from any particular consumer.

How?

  • Generic injection seam in useInnerBlocksProps (useBoundInnerBlocksProps): makes any locally-controlled container controlled when it declares the binding, reusing the existing controlled-inner-blocks engine (useBlockSync).
  • Undo/redo + write-back: edits write back through the source as one coherent undo level, and undo/redo of those edits works — the resolver skips a transient write that merely echoes the source's current value, and a bounded parse cache replays previously seen values with the same block identities, so stepping through history does not remount the subtree or drop selection.
  • Read-only enforcement: a source whose canUserEditValue returns false locks the area structurally (templateLock: 'all', no appender) and disables the editing mode of the bound subtree (the child wrappers render inert). Content in a read-only area cannot be edited at all — an edit that rendered but was dropped by the suppressed write-back would be silent data loss.
  • Release restores content: removing the binding (detach), or a source flipping to absence, re-seeds the container with the last controlled tree instead of leaving it emptied by the controlled-sync teardown — releasing a binding keeps what the user was looking at.
  • Recursion guards in both runtimes: a source supplying a block bound to itself (directly or through a cycle of sources) resolves as absence instead of recursing — a bound-ancestry context plus a hard depth cap in the editor; on the server, a render stack keyed by (source, args) pushed on render_block_data and released on render_block, plus _doing_it_wrong.
  • Server-side substitution on render_block_data (WP 7.1 compat): replaces a bound block's inner blocks and rebuilds its innerContent template around the host's own static chunks, so the source tree renders in place with nested bindings resolved recursively. Top-level context mirrors core's render_block() setup, including the render_block_context filter.
  • core/list-item reconciliation: when a list item declares both a content and an innerBlocks binding, the shim's existing containment check prevents a double render on fixed cores; on WP 6.9/7.0 — where applying the content binding wipes the whole <li> inner HTML — the shim restores the substituted children exactly as it restores a block's own.
  • An example/test binding source (in the e2e plugins) exercises read / write / read-only / absence without depending on any consumer.

Scope — what is deliberately NOT here

  • core/pattern-overrides consumer (editable freeform area in a synced pattern) and its synced-pattern editing carve-outs. It depends on a per-instance override storage model still under design: today it would serialize the edited subtree as a markup string into the placed core/block's content attribute, which inflates (~2–4× via attribute escaping), duplicates per instance, and re-serializes the whole subtree per commit. That storage question is being worked separately before the consumer ships.
  • User-facing documentation — added at release time, not here.
  • Known perf follow-up: transient input (onInput) currently serializes the full bound subtree per change to support echo suppression; on very large areas this could add typing latency. Tracked as a follow-up, entangled with the transient-write contract.

Testing Instructions

  1. Register a binding source returning serialized markup for the reserved innerBlocks key (or use the e2e example source in packages/e2e-tests/plugins/block-bindings/).
  2. Add a container declaring metadata.bindings.innerBlocks. Confirm the source-supplied children render in the editor and on the frontend, edits write back, undo/redo of those edits preserves selection, a read-only source locks the area (no appender, children inert — typing does nothing), removing the binding keeps the current children, and absence falls back to the block's own children.

Automated coverage:

  • JS unit (use-bound-inner-blocks): value contract, write-back/echo/undo guards, canUserEditValue args, read-only subtree editing-mode enforcement, recursion-guard ancestry (including same-source-different-args composition), release-restores-content.
  • PHP unit (phpunit/block-bindings-innerblocks-test.php, block-bindings-list-item-test.php): substitution, absence, empty area, static-chunk preservation, nested/recursive resolution, context inheritance (including via render_block_context), recursion-guard termination and sequential-area release, list-item reconciliation including the WP 6.9/7.0 wiped-content restoration.
  • e2e (block-bindings/inner-blocks.spec.js): editor+frontend read path, write-back, read-only (structure locked, children not selectable, typing produces no change), detach keeps content, absence fallback.

🤖 Generated with Claude Code

@github-actions github-actions Bot added [Package] Blocks /packages/blocks [Package] Editor /packages/editor [Package] Block editor /packages/block-editor labels Jun 21, 2026
@cbravobernal
cbravobernal force-pushed the 61225-innerblocks-bindings branch from e77097a to 9f3f18c Compare June 22, 2026 11:12
@github-actions github-actions Bot removed the [Package] Editor /packages/editor label Jun 22, 2026
@cbravobernal cbravobernal changed the title Block Bindings: Support binding to innerBlocks Block Bindings: Support binding to innerBlocks (generic mechanism) Jun 22, 2026
@cbravobernal
cbravobernal force-pushed the 61225-innerblocks-bindings branch from 9f3f18c to 2ac2035 Compare June 22, 2026 11:28
@cbravobernal
cbravobernal force-pushed the 61225-innerblocks-bindings branch 2 times, most recently from 001bff9 to 2529d3e Compare July 3, 2026 11:02
Extend the Block Bindings mechanism so a registered binding source can
supply and receive a block's inner blocks (its child block tree), not just
scalar attributes. Refs WordPress#61225.

A block declares the binding with a reserved `innerBlocks` key inside
`metadata.bindings` on the container that owns the inner blocks. The value
contract is a single serialized block-markup string, parsed at each runtime
boundary (`parse()` in JS, `parse_blocks()` in PHP): `undefined`/`null`
falls back to the block's own serialized children, `''` is an
intentionally-empty area, and a string is parsed into the controlled tree.
No new source callbacks are introduced — the reserved key flows through the
existing `getValues`/`setValues`/`canUserEditValue` (JS) and
`get_value_callback` (PHP, read-only) API.

This PR is the generic, source-agnostic mechanism only:

- A generic injection seam in `useInnerBlocksProps`
  (`useBoundInnerBlocksProps`) that makes any locally-controlled container
  controlled when it declares the binding, reusing the
  controlled-inner-blocks engine (`useBlockSync`). Edits write back as one
  coherent undo level, and undo/redo of those edits is supported: the
  resolver skips a transient write that merely echoes the source's current
  value, and a bounded parse cache replays previously seen values with the
  same block identities so history steps do not remount the subtree.
- Read-only enforcement: a source whose `canUserEditValue` (called with the
  binding's `args`) returns `false` locks the area structurally
  (`templateLock: 'all'`, no appender) and disables the editing mode of the
  bound subtree, rendering the child wrappers inert so content cannot be
  edited and silently dropped.
- Release restores content: removing the binding, or a source flipping to
  absence, re-seeds the container with the last controlled tree instead of
  leaving it emptied by the controlled-sync teardown.
- Recursion guards in both runtimes: a source supplying a block bound to
  itself (directly or through a cycle) resolves as absence — a bound
  ancestry context plus depth cap in the editor, a render stack keyed by
  (source, args) pushed on `render_block_data` and released on
  `render_block` on the server.
- Server-side substitution on `render_block_data` (WordPress 7.1 compat)
  that replaces a bound block's inner blocks and rebuilds its `innerContent`
  template around the host's own static chunks, so the source tree renders
  in place with nested bindings resolved recursively. Top-level context
  mirrors core's `render_block()` setup, including the
  `render_block_context` filter.
- `core/list-item` reconciliation: when a list item declares both a
  `content` and an `innerBlocks` binding, the shim's existing containment
  check prevents a double render on fixed cores, and on WP 6.9/7.0 — where
  applying the content binding wipes the `<li>` inner HTML — the shim
  restores the substituted children exactly as it restores a block's own.

An example/test binding source (in the e2e plugins) exercises the
read/write/read-only/absence paths without depending on any consumer.

The first real consumer (`core/pattern-overrides`, an editable freeform
area inside a synced pattern) and the user-facing documentation are
intentionally NOT included here; they depend on a per-instance override
storage model still under design and will follow separately.

Covered by JS unit tests, PHP unit tests, and editor/frontend e2e tests
(read, write-back, read-only locking and typing, detach, absence).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cbravobernal
cbravobernal force-pushed the 61225-innerblocks-bindings branch from 2529d3e to 9e6c5e7 Compare July 3, 2026 15:09
@cbravobernal

Copy link
Copy Markdown
Contributor Author

Superseded by #79852 — same commit, moved from the fork to a branch on this repository (add/61225-innerblocks-bindings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor [Package] Blocks /packages/blocks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant