Skip to content

Commit

Permalink
Enable overrides for attributes with property
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiomorales committed Oct 28, 2024
1 parent 86a3bc9 commit 3d7c879
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
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' );
if ( ! isset( $supported_block_attributes[ $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_attributes[ $parsed_block['blockName'] ] as $attribute_name ) {

Check warning on line 49 in lib/compat/wordpress-6.6/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Variable $supported_block_attributes is undefined.
// 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

0 comments on commit 3d7c879

Please sign in to comment.