@@ -273,6 +273,7 @@ public function __get( $name ) {
273273 * @since 6.5.0
274274 * @since 6.6.0 Handle the `__default` attribute for pattern overrides.
275275 * @since 6.7.0 Return any updated bindings metadata in the computed attributes.
276+ * @since 7.3.0 Replace the inner blocks of the block for an `innerBlocks` binding.
276277 *
277278 * @return array The computed block attributes for the provided block bindings.
278279 */
@@ -310,6 +311,10 @@ private function process_block_bindings() {
310311 * resulting array.
311312 */
312313 foreach ( $ supported_block_attributes as $ attribute_name ) {
314+ // Inner blocks are only bound explicitly.
315+ if ( 'innerBlocks ' === $ attribute_name && ! isset ( $ bindings ['innerBlocks ' ] ) ) {
316+ continue ;
317+ }
313318 // Retain any non-pattern override bindings that might be present.
314319 $ updated_bindings [ $ attribute_name ] = $ bindings [ $ attribute_name ] ?? array ( 'source ' => 'core/pattern-overrides ' );
315320 }
@@ -351,6 +356,12 @@ private function process_block_bindings() {
351356 $ source_args = ! empty ( $ block_binding ['args ' ] ) && is_array ( $ block_binding ['args ' ] ) ? $ block_binding ['args ' ] : array ();
352357 $ source_value = $ block_binding_source ->get_value ( $ source_args , $ this , $ attribute_name );
353358
359+ // Inner blocks are blocks, not an attribute.
360+ if ( 'innerBlocks ' === $ attribute_name ) {
361+ $ this ->replace_inner_blocks ( $ source_value );
362+ continue ;
363+ }
364+
354365 // If the value is not null, process the HTML based on the block and the attribute.
355366 if ( ! is_null ( $ source_value ) ) {
356367 $ computed_attributes [ $ attribute_name ] = $ source_value ;
@@ -360,6 +371,148 @@ private function process_block_bindings() {
360371 return $ computed_attributes ;
361372 }
362373
374+ /**
375+ * Replaces the inner blocks of the block with the ones a block bindings
376+ * source supplies, before they render.
377+ *
378+ * The value is serialized block markup. An empty string leaves the block
379+ * without inner blocks. Any other value, like the plain text of a field,
380+ * leaves the block with its own inner blocks.
381+ *
382+ * @since 7.3.0
383+ *
384+ * @global WP_Embed $wp_embed WordPress Embed object.
385+ *
386+ * @param mixed $value The value of the source.
387+ */
388+ private function replace_inner_blocks ( $ value ) {
389+ global $ wp_embed ;
390+
391+ if ( '' !== $ value && ! ( is_string ( $ value ) && '<!-- wp: ' === substr ( $ value , strspn ( $ value , " \t\n\f\r" ), 8 ) ) ) {
392+ return ;
393+ }
394+
395+ /*
396+ * The markup skips the filters that sanitize post content when it's saved,
397+ * and the user who supplied it may not be allowed unfiltered HTML. Like on
398+ * save, this also sanitizes the attributes of the blocks, through the
399+ * `pre_kses` filter.
400+ */
401+ $ markup = wp_kses_post ( $ value );
402+ if ( $ wp_embed instanceof WP_Embed ) {
403+ $ markup = $ wp_embed ->autoembed ( $ markup );
404+ }
405+ $ blocks = self ::prepare_bound_inner_blocks ( parse_blocks ( $ markup ) );
406+
407+ $ inner_content = self ::get_inner_content_with_slots ( $ this ->parsed_block ['innerContent ' ] ?? null , count ( $ blocks ) );
408+ if ( null === $ inner_content ) {
409+ return ;
410+ }
411+
412+ $ this ->parsed_block ['innerBlocks ' ] = $ blocks ;
413+ $ this ->parsed_block ['innerContent ' ] = $ inner_content ;
414+ $ this ->parsed_block ['innerHTML ' ] = implode ( '' , array_filter ( $ inner_content , 'is_string ' ) );
415+ $ this ->refresh_parsed_block_dependents ();
416+ // The refresh keeps the previous inner blocks when there are none.
417+ if ( empty ( $ blocks ) ) {
418+ $ this ->inner_blocks = array ();
419+ }
420+ }
421+
422+ /**
423+ * Prepares the blocks a block bindings source supplies, and their inner
424+ * blocks, to render.
425+ *
426+ * Removes their `innerBlocks` bindings: none of them gets its inner blocks
427+ * from a source, so that two sources whose values bind each other's can't
428+ * render forever.
429+ *
430+ * Also removes their custom CSS, which only users who can edit CSS may add:
431+ * whoever supplied the value is unknown.
432+ *
433+ * @since 7.3.0
434+ *
435+ * @param array[] $blocks The parsed blocks.
436+ * @return array[] The parsed blocks.
437+ */
438+ private static function prepare_bound_inner_blocks ( array $ blocks ): array {
439+ foreach ( $ blocks as &$ block ) {
440+ if ( isset ( $ block ['attrs ' ]['style ' ] ) ) {
441+ $ block ['attrs ' ]['style ' ] = wp_strip_custom_css_from_style_array ( $ block ['attrs ' ]['style ' ] );
442+ if ( empty ( $ block ['attrs ' ]['style ' ] ) ) {
443+ unset( $ block ['attrs ' ]['style ' ] );
444+ }
445+ }
446+
447+ if ( isset ( $ block ['attrs ' ]['metadata ' ]['bindings ' ]['innerBlocks ' ] ) ) {
448+ unset( $ block ['attrs ' ]['metadata ' ]['bindings ' ]['innerBlocks ' ] );
449+ // Drop what's left empty, as the editor does.
450+ if ( empty ( $ block ['attrs ' ]['metadata ' ]['bindings ' ] ) ) {
451+ unset( $ block ['attrs ' ]['metadata ' ]['bindings ' ] );
452+ }
453+ if ( empty ( $ block ['attrs ' ]['metadata ' ] ) ) {
454+ unset( $ block ['attrs ' ]['metadata ' ] );
455+ }
456+ }
457+ $ block ['innerBlocks ' ] = self ::prepare_bound_inner_blocks ( $ block ['innerBlocks ' ] );
458+ }
459+ return $ blocks ;
460+ }
461+
462+ /**
463+ * Returns the inner content of a block with slots for the given number of
464+ * inner blocks, where its own inner blocks were.
465+ *
466+ * @since 7.3.0
467+ *
468+ * @param array|null $inner_content The inner content of the block.
469+ * @param int $count The number of inner blocks.
470+ * @return array|null The inner content, or null if there's no element to put
471+ * the inner blocks in.
472+ */
473+ private static function get_inner_content_with_slots ( $ inner_content , int $ count ): ?array {
474+ if ( ! is_array ( $ inner_content ) ) {
475+ return null ;
476+ }
477+
478+ // Keeps the HTML around the block's own inner blocks as it is, and drops
479+ // the HTML between them.
480+ $ slots = array_keys ( $ inner_content , null , true );
481+ if ( $ slots ) {
482+ return array_merge (
483+ array_slice ( $ inner_content , 0 , $ slots [0 ] ),
484+ array_fill ( 0 , $ count , null ),
485+ array_slice ( $ inner_content , end ( $ slots ) + 1 )
486+ );
487+ }
488+
489+ /*
490+ * Without any, the block wraps them in one element, so they go before its
491+ * closer. Serializes the HTML token by token, as
492+ * `WP_HTML_Processor::serialize()` does, and starts the second part there.
493+ */
494+ $ processor = WP_HTML_Processor ::create_fragment ( implode ( '' , $ inner_content ) );
495+ if ( null === $ processor ) {
496+ return null ;
497+ }
498+ $ parts = array ( '' , '' );
499+ $ part = 0 ;
500+ $ closer_depth = null ;
501+ while ( $ processor ->next_token () ) {
502+ if ( null === $ closer_depth && '#tag ' === $ processor ->get_token_type () && ! $ processor ->is_tag_closer () ) {
503+ // A closer is visited at the depth of the element that contains it.
504+ $ closer_depth = $ processor ->get_current_depth () - 1 ;
505+ } elseif ( 0 === $ part && $ processor ->is_tag_closer () && $ processor ->get_current_depth () === $ closer_depth ) {
506+ $ part = 1 ;
507+ }
508+ $ parts [ $ part ] .= $ processor ->serialize_token ();
509+ }
510+ if ( 1 !== $ part || null !== $ processor ->get_last_error () || $ processor ->paused_at_incomplete_token () ) {
511+ return null ;
512+ }
513+ return array_merge ( array ( $ parts [0 ] ), array_fill ( 0 , $ count , null ), array ( $ parts [1 ] ) );
514+ }
515+
363516 /**
364517 * Depending on the block attribute name, replace its value in the HTML based on the value provided.
365518 *
0 commit comments