Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: Enable synced pattern overrides for attributes with role: content property #66518

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/compat/wordpress-6.6/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,27 @@ function gutenberg_replace_pattern_override_default_binding( $parsed_block ) {
// Build an binding array of all supported attributes.
// Note that this also omits the `__default` attribute from the
// resulting array.
foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) {
// Retain any non-pattern override bindings that might be present.
$updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
? $bindings[ $attribute_name ]
: array( 'source' => 'core/pattern-overrides' );
Comment on lines -34 to -38
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code fires an error for the custom block type. I'm unsure of the best way to handle this, so I just copied the code from core for now to avoid the error while testing.

if ( ! isset( $supported_block_attrs[ $parsed_block['blockName'] ] ) ) {
// get block type
$block_registry = WP_Block_Type_Registry::get_instance();
$block_type = $block_registry->get_registered( $parsed_block['blockName'] );
$attribute_definitions = $block_type->attributes;

foreach ( $attribute_definitions as $attribute_name => $attribute_definition ) {
// Include the attribute in the updated bindings if the role is set to 'content'.
if ( isset( $attribute_definition['role'] ) && 'content' === $attribute_definition['role'] ) {
$updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
? $bindings[ $attribute_name ]
: array( 'source' => 'core/pattern-overrides' );
}
}
} else {
foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) {
// Retain any non-pattern override bindings that might be present.
$updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
? $bindings[ $attribute_name ]
: array( 'source' => 'core/pattern-overrides' );
}
}
$parsed_block['attrs']['metadata']['bindings'] = $updated_bindings;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/editor/src/hooks/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useBlockEditingMode } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { getBlockBindingsSource } from '@wordpress/blocks';
import { getBlockBindingsSource, getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -36,8 +36,12 @@ const {
*/
const withPatternOverrideControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const blockType = getBlockType( props.name );
const isSupportedBlock =
!! PARTIAL_SYNCING_SUPPORTED_BLOCKS[ props.name ];
!! PARTIAL_SYNCING_SUPPORTED_BLOCKS[ props.name ] ||
Object.values( blockType.attributes ).some(
( attribute ) => attribute.role === 'content'
);

return (
<>
Expand Down
15 changes: 13 additions & 2 deletions packages/patterns/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';

/**
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';

/**
* Determines whether a block is overridable.
*
Expand All @@ -11,10 +16,16 @@ import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';
* @return {boolean} `true` if a block is overridable, `false` otherwise.
*/
export function isOverridableBlock( block ) {
return (
const blockType = getBlockType( block.name );
const isSupportedBlock =
Object.keys( PARTIAL_SYNCING_SUPPORTED_BLOCKS ).includes(
block.name
) &&
) ||
Object.values( blockType.attributes ).some(
( attribute ) => attribute.role === 'content'
);
return (
isSupportedBlock &&
!! block.attributes.metadata?.name &&
!! block.attributes.metadata?.bindings &&
Object.values( block.attributes.metadata.bindings ).some(
Expand Down
Loading