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: Simplify block bindings object #58337

Merged
merged 7 commits into from
Jan 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
};
wp_block_bindings_register_source(
'pattern_attributes',
'core/pattern-attributes',
Copy link
Member

Choose a reason for hiding this comment

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

@talldan and @kevin940726, are you settled on the final name for the block binding source for Pattern Overrides? Tomorrow is the last day to change it 😄

Copy link
Member

Choose a reason for hiding this comment

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

I'd vote for core/pattern-overrides but I wouldn't mind any other choices as long as it's consistent 😄.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree I would try to keep it as consistent as possible. What is the name of the feature? Synced pattern overrides?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm merging this pull request, but let's decide the name and we can change that in another pull request

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also vote for core/pattern-overrides

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've created this pull request to update that: link.

array(
'label' => __( 'Pattern Attributes' ),
'apply' => $pattern_source_callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
if ( function_exists( 'wp_block_bindings_register_source' ) ) {
$post_meta_source_callback = function ( $source_attrs ) {
if ( ! isset( $source_attrs['key'] ) ) {
return null;
}

// Use the postId attribute if available
if ( isset( $source_attrs['postId'] ) ) {
$post_id = $source_attrs['postId'];
Expand All @@ -14,10 +18,10 @@
$post_id = get_the_ID();
}

return get_post_meta( $post_id, $source_attrs['value'], true );
return get_post_meta( $post_id, $source_attrs['key'], true );
};
wp_block_bindings_register_source(
'post_meta',
'core/post-meta',
array(
'label' => __( 'Post Meta' ),
'apply' => $post_meta_source_callback,
Expand Down
20 changes: 8 additions & 12 deletions lib/compat/wordpress-6.5/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,12 @@ function gutenberg_process_block_bindings( $block_content, $block, $block_instan
*
* "bindings": {
* "title": {
* "source": {
* "name": "post_meta",
* "attributes": { "value": "text_custom_field" }
* }
* "source": "core/post-meta",
* "args": { "key": "text_custom_field" }
* },
* "url": {
* "source": {
* "name": "post_meta",
* "attributes": { "value": "text_custom_field" }
* }
* "source": "core/post-meta",
* "args": { "key": "url_custom_field" }
* }
* }
*/
Expand All @@ -99,16 +95,16 @@ function gutenberg_process_block_bindings( $block_content, $block, $block_instan
continue;
}
// If no source is provided, or that source is not registered, process next attribute.
if ( ! isset( $binding_source['source'] ) || ! isset( $binding_source['source']['name'] ) || ! isset( $block_bindings_sources[ $binding_source['source']['name'] ] ) ) {
if ( ! isset( $binding_source['source'] ) || ! is_string( $binding_source['source'] ) || ! isset( $block_bindings_sources[ $binding_source['source'] ] ) ) {
continue;
}

$source_callback = $block_bindings_sources[ $binding_source['source']['name'] ]['apply'];
$source_callback = $block_bindings_sources[ $binding_source['source'] ]['apply'];
Copy link
Member

Choose a reason for hiding this comment

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

I think that we might need to introduce WP_Block_Binding_Source class so we don't have to do so many checks in the code. Here, we should check whether apply is defined with asset since it might not be as we don't don any validation at all.

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 could make sense 🤔 I've added that as a follow-up task in the tracking issue.

// Get the value based on the source.
if ( ! isset( $binding_source['source']['attributes'] ) ) {
if ( ! isset( $binding_source['args'] ) ) {
$source_args = array();
} else {
$source_args = $binding_source['source']['attributes'];
$source_args = $binding_source['args'];
}
$source_value = $source_callback( $source_args, $block_instance, $binding_attribute );
// If the value is null, process next attribute.
Expand Down
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ export function RichTextWrapper(
if (
blockTypeAttributes?.[ attribute ]?.source ===
'rich-text' &&
getBlockBindingsSource( args.source.name )
?.lockAttributesEditing
getBlockBindingsSource( args.source )?.lockAttributesEditing
) {
shouldDisableEditing = true;
break;
Expand Down
7 changes: 2 additions & 5 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ const createEditFunctionWithBindingsAttribute = () =>
Object.entries( updatedAttributes.metadata.bindings ).forEach(
( [ attributeName, settings ] ) => {
const source = getBlockBindingsSource(
settings.source.name
settings.source
);

if ( source ) {
// Second argument (`updateMetaValue`) will be used to update the value in the future.
const {
placeholder,
useValue: [ metaValue = null ] = [],
} = source.useSource(
props,
settings.source.attributes
);
} = source.useSource( props, settings.args );

if ( placeholder && ! metaValue ) {
// If the attribute is `src` or `href`, a placeholder can't be used because it is not a valid url.
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ function isPartiallySynced( block ) {
) &&
!! block.attributes.metadata?.bindings &&
Object.values( block.attributes.metadata.bindings ).some(
( binding ) => binding.source.name === 'pattern_attributes'
( binding ) => binding.source === 'core/pattern-attributes'
)
);
}
function getPartiallySyncedAttributes( block ) {
return Object.entries( block.attributes.metadata.bindings )
.filter(
( [ , binding ] ) => binding.source.name === 'pattern_attributes'
( [ , binding ] ) => binding.source === 'core/pattern-attributes'
)
.map( ( [ attributeKey ] ) => attributeKey );
}
Expand Down
5 changes: 2 additions & 3 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ function ButtonEdit( props ) {
return {
lockUrlControls:
!! metadata?.bindings?.url &&
getBlockBindingsSource(
metadata?.bindings?.url?.source?.name
)?.lockAttributesEditing === true,
getBlockBindingsSource( metadata?.bindings?.url?.source )
?.lockAttributesEditing === true,
};
},
[ isSelected ]
Expand Down
5 changes: 2 additions & 3 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,8 @@ export function ImageEdit( {
return {
lockUrlControls:
!! metadata?.bindings?.url &&
getBlockBindingsSource(
metadata?.bindings?.url?.source?.name
)?.lockAttributesEditing === true,
getBlockBindingsSource( metadata?.bindings?.url?.source )
?.lockAttributesEditing === true,
};
},
[ isSelected ]
Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ export default function Image( {
return {
lockUrlControls:
!! urlBinding &&
getBlockBindingsSource( urlBinding?.source?.name )
getBlockBindingsSource( urlBinding?.source )
?.lockAttributesEditing === true,
lockAltControls:
!! altBinding &&
getBlockBindingsSource( altBinding?.source?.name )
getBlockBindingsSource( altBinding?.source )
?.lockAttributesEditing === true,
lockTitleControls:
!! titleBinding &&
getBlockBindingsSource( titleBinding?.source?.name )
getBlockBindingsSource( titleBinding?.source )
?.lockAttributesEditing === true,
};
},
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { __ } from '@wordpress/i18n';
import { store as editorStore } from '../store';

export default {
name: 'post_meta',
name: 'core/post-meta',
label: __( 'Post Meta' ),
useSource( props, sourceAttributes ) {
const { getCurrentPostType } = useSelect( editorStore );
const { context } = props;
const { value: metaKey } = sourceAttributes;
const { key: metaKey } = sourceAttributes;
const postType = context.postType
? context.postType
: getCurrentPostType();
Expand Down
14 changes: 6 additions & 8 deletions packages/patterns/src/components/partial-syncing-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];
const attributeSources = Object.keys( syncedAttributes ).map(
( attributeName ) =>
attributes.metadata?.bindings?.[ attributeName ]?.source?.name
attributes.metadata?.bindings?.[ attributeName ]?.source
);
const isConnectedToOtherSources = attributeSources.every(
( source ) => source && source !== 'pattern_attributes'
( source ) => source && source !== 'core/pattern-attributes'
);

// Render nothing if all supported attributes are connected to other sources.
Expand All @@ -38,8 +38,8 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
if ( ! isChecked ) {
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if (
updatedBindings[ attributeName ]?.source?.name ===
'pattern_attributes'
updatedBindings[ attributeName ]?.source ===
'core/pattern-attributes'
) {
delete updatedBindings[ attributeName ];
}
Expand All @@ -59,9 +59,7 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if ( ! updatedBindings[ attributeName ] ) {
updatedBindings[ attributeName ] = {
source: {
name: 'pattern_attributes',
},
source: 'core/pattern-attributes',
};
}
}
Expand Down Expand Up @@ -96,7 +94,7 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
__nextHasNoMarginBottom
label={ __( 'Allow instance overrides' ) }
checked={ attributeSources.some(
( source ) => source === 'pattern_attributes'
( source ) => source === 'core/pattern-attributes'
) }
onChange={ ( isChecked ) => {
updateBindings( isChecked );
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test.describe( 'Pattern Overrides', () => {
id: expect.any( String ),
bindings: {
content: {
source: { name: 'pattern_attributes' },
source: 'core/pattern-attributes',
},
},
},
Expand Down Expand Up @@ -222,7 +222,7 @@ test.describe( 'Pattern Overrides', () => {
const paragraphId = 'paragraph-id';
const { id } = await requestUtils.createBlock( {
title: 'Pattern',
content: `<!-- wp:paragraph {"metadata":{"id":"${ paragraphId }","bindings":{"content":{"source":{"name":"pattern_attributes"}}}}} -->
content: `<!-- wp:paragraph {"metadata":{"id":"${ paragraphId }","bindings":{"content":{"source":"core/pattern-attributes"}}}} -->
<p>Editable</p>
<!-- /wp:paragraph -->`,
status: 'publish',
Expand Down Expand Up @@ -270,7 +270,7 @@ test.describe( 'Pattern Overrides', () => {
const { id } = await requestUtils.createBlock( {
title: 'Pattern with overrides',
content: `<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"metadata":{"id":"${ buttonId }","bindings":{"text":{"source":{"name":"pattern_attributes"}},"url":{"source":{"name":"pattern_attributes"}},"linkTarget":{"source":{"name":"pattern_attributes"}}}}} -->
<div class="wp-block-buttons"><!-- wp:button {"metadata":{"id":"${ buttonId }","bindings":{"text":{"source":"core/pattern-attributes"},"url":{"source":"core/pattern-attributes"},"linkTarget":{"source":"core/pattern-attributes"}}}} -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="http://wp.org" target="_blank" rel="noreferrer noopener">wp.org</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`,
Expand Down
Loading