Skip to content

Commit

Permalink
Add bindings attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Mar 25, 2024
1 parent e9858fe commit 7fba5e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 54 deletions.
52 changes: 0 additions & 52 deletions lib/compat/wordpress-6.6/blocks.php

This file was deleted.

1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.5/script-loader.php';

// WordPress 6.6 compat.
require __DIR__ . '/compat/wordpress-6.6/blocks.php';
require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php';

// Experimental features.
Expand Down
32 changes: 31 additions & 1 deletion packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,50 @@ function render_block_core_block( $attributes ) {
* filter so that it is available when a pattern's inner blocks are
* rendering via do_blocks given it only receives the inner content.
*/
$has_pattern_overrides = isset( $attributes['content'] );
$has_pattern_overrides = isset( $attributes['content'] ) && null !== get_block_bindings_source( 'core/pattern-overrides' );
if ( $has_pattern_overrides ) {
$filter_block_context = static function ( $context ) use ( $attributes ) {
$context['pattern/overrides'] = $attributes['content'];
return $context;
};
add_filter( 'render_block_context', $filter_block_context, 1 );

$filter_pattern_overrides_bindings = static function ( $parsed_block ) {
$supported_block_attrs = array(
'core/paragraph' => array( 'content' ),
'core/heading' => array( 'content' ),
'core/image' => array( 'id', 'url', 'title', 'alt' ),
'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ),
);

if (
// Return early if the block isn't one of the supported block types,
! isset( $supported_block_attrs[ $parsed_block['blockName'] ] ) ||
// or doesn't have a name,
empty( $parsed_block['attrs']['metadata']['name'] ) ||
// or already has bindings.
! empty( $parsed_block['attrs']['metadata']['bindings'] )
) {
return $parsed_block;
}

$bindings = array();
foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) {
$bindings[ $attribute_name ] = array( 'source' => 'core/pattern-overrides' );
}
$parsed_block['attrs']['metadata']['bindings'] = $bindings;

return $parsed_block;
};
add_filter( 'render_block_data', $filter_pattern_overrides_bindings, 10, 1 );
}

$content = do_blocks( $content );
unset( $seen_refs[ $attributes['ref'] ] );

if ( $has_pattern_overrides ) {
remove_filter( 'render_block_context', $filter_block_context, 1 );
remove_filter( 'render_block_data', $filter_pattern_overrides_bindings, 10 );
}

return $content;
Expand Down

0 comments on commit 7fba5e9

Please sign in to comment.