Skip to content

Commit

Permalink
Make the PHP implementation of the replacement of default attributes …
Browse files Browse the repository at this point in the history
…more generic (not implemented in the pattern block)
  • Loading branch information
talldan committed May 31, 2024
1 parent 29a65a4 commit 869d53d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
43 changes: 43 additions & 0 deletions lib/compat/wordpress-6.6/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Temporary compatibility shims for block APIs present in Gutenberg.
*
* @package gutenberg
*/

/**
* Replace the `__default` block bindings attribute with the full list of supported
* attribute names for pattern overrides.
*
* @param array $parsed_block The full block, including name and attributes.
*
* @return string The parsed block with default binding replace.
*/
function gutenberg_replace_pattern_override_default_binding( $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' ),
);

$bindings = $parsed_block['attrs']['metadata']['bindings'] ?? array();
if (
isset( $bindings[ '__default' ][ 'source' ] ) &&

Check failure on line 26 in lib/compat/wordpress-6.6/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 26 in lib/compat/wordpress-6.6/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
$bindings[ '__default' ][ 'source' ] === 'core/pattern-overrides'

Check failure on line 27 in lib/compat/wordpress-6.6/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 27 in lib/compat/wordpress-6.6/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 27 in lib/compat/wordpress-6.6/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
) {
// Build an binding array of all supported attributes.
foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) {
$bindings[ $attribute_name ] = array( 'source' => 'core/pattern-overrides' );
}
// Merge this into the parsed block's bindings & avoid overwriting existing bindings.
$parsed_block['attrs']['metadata']['bindings'] = array_merge(
$bindings,
$parsed_block['attrs']['metadata']['bindings']
);
}

return $parsed_block;
}

add_filter( 'render_block_data', 'gutenberg_replace_pattern_override_default_binding', 10, 1 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// WordPress 6.6 compat.
require __DIR__ . '/compat/wordpress-6.6/admin-bar.php';
require __DIR__ . '/compat/wordpress-6.6/blocks.php';
require __DIR__ . '/compat/wordpress-6.6/compat.php';
require __DIR__ . '/compat/wordpress-6.6/resolve-patterns.php';
require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php';
Expand Down
36 changes: 0 additions & 36 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,49 +85,13 @@ function render_block_core_block( $attributes ) {
return $context;
};
add_filter( 'render_block_context', $filter_block_context, 1 );

// Add bindings for the default pattern overrides source.
$apply_default_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,
! isset( $parsed_block['attrs']['metadata']['name'] ) ||
// or doesn't have the default binding.
empty( $parsed_block['attrs']['metadata']['bindings']['__default'] ) ||
// or the default binding isn't the pattern overrides source.
'core/pattern-overrides' !== $parsed_block['attrs']['metadata']['bindings']['__default']['source']
) {
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'] = array_merge(
$bindings,
$parsed_block['attrs']['metadata']['bindings']
);

return $parsed_block;
};
add_filter( 'render_block_data', $apply_default_pattern_overrides_bindings );
}

$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', $apply_default_pattern_overrides_bindings );
}

return $content;
Expand Down

0 comments on commit 869d53d

Please sign in to comment.