Skip to content
Open
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
20 changes: 20 additions & 0 deletions lib/compat/wordpress-7.1/block-bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Block Bindings API additions for WordPress 7.1.
*
* @since 7.1.0
* @package gutenberg
* @subpackage Block Bindings
*/

add_filter(
'block_bindings_supported_attributes',
function ( $attributes, $block_type ) {
if ( 'core/post-date' === $block_type && ! in_array( 'isLink', $attributes, true ) ) {
$attributes[] = 'isLink';
}
return $attributes;
},
10,
2
);
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-7.0/global-styles.php';

// WordPress 7.1 compat.
require __DIR__ . '/compat/wordpress-7.1/block-bindings.php';
require __DIR__ . '/compat/wordpress-7.1/view-config-api.php';
require __DIR__ . '/compat/wordpress-7.1/class-gutenberg-rest-view-config-controller-7-1.php';
require __DIR__ . '/compat/wordpress-7.1/rest-api.php';
Expand Down
55 changes: 44 additions & 11 deletions packages/block-editor/src/hooks/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ import InspectorControls from '../components/inspector-controls';
import BlockContext from '../components/block-context';
import { store as blockEditorStore } from '../store';

/*
* Per-block list of bindable attributes hidden from the Attributes panel.
* Binding these attributes to incompatible sources through the generic UI
* could break the block (e.g. binding the Date block's `datetime` attribute
* to a non-date value). They remain bindable programmatically and through
* dedicated UI, like the Date block's variations.
*/
const HIDDEN_BLOCK_BINDINGS_PANEL_ATTRIBUTES = {
'core/post-date': [ 'datetime' ],
};
Comment on lines +34 to +36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should try to find a best solution to this issue. Right now, even block bindings are "hardcoded" which is not ideal. So adding another "hardcode" for the attributes panel should not be the way to go.

Also, that panel will eventually by moved to a better position.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see. I looked a bit more into existing issues and found this: #72716. In this case, I believe the best way forward is to wait it out till that PR is merged.


const useToolsPanelDropdownMenuProps = () => {
const isMobile = useViewportMatch( 'medium', '<' );
return ! isMobile
Expand All @@ -39,7 +50,8 @@ const useToolsPanelDropdownMenuProps = () => {

export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
const blockContext = useContext( BlockContext );
const { removeAllBlockBindings } = useBlockBindingsUtils();
const { removeAllBlockBindings, updateBlockBindings } =
useBlockBindingsUtils();
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

const { bindableAttributes, hasCompatibleFields } = useSelect(
Expand Down Expand Up @@ -68,14 +80,25 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
[ blockName, blockContext ]
);

// Return early if there are no bindable attributes.
if ( ! bindableAttributes || bindableAttributes.length === 0 ) {
const hiddenAttributes =
HIDDEN_BLOCK_BINDINGS_PANEL_ATTRIBUTES[ blockName ];
const visibleAttributes = hiddenAttributes
? bindableAttributes?.filter(
( attribute ) => ! hiddenAttributes.includes( attribute )
)
: bindableAttributes;

// Return early if there are no bindable attributes to show.
if ( ! visibleAttributes || visibleAttributes.length === 0 ) {
return null;
}

const { bindings } = metadata || {};
const hasVisibleBindings = visibleAttributes.some(
( attribute ) => bindings?.[ attribute ] !== undefined
);

if ( bindings === undefined && ! hasCompatibleFields ) {
if ( ! hasVisibleBindings && ! hasCompatibleFields ) {
return null;
}

Expand All @@ -84,13 +107,25 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
<ToolsPanel
label={ __( 'Attributes' ) }
resetAll={ () => {
removeAllBlockBindings();
if ( hiddenAttributes?.length ) {
// Only remove the bindings shown in the panel, leaving the hidden ones untouched.
updateBlockBindings(
Object.fromEntries(
visibleAttributes.map( ( attribute ) => [
attribute,
undefined,
] )
)
);
} else {
removeAllBlockBindings();
}
} }
dropdownMenuProps={ dropdownMenuProps }
className="block-editor-bindings__panel"
>
<ItemGroup isBordered isSeparated>
{ bindableAttributes.map( ( attribute ) => (
{ visibleAttributes.map( ( attribute ) => (
<BlockBindingsAttributeControl
key={ attribute }
attribute={ attribute }
Expand Down Expand Up @@ -119,10 +154,8 @@ export default {
edit: BlockBindingsPanel,
attributeKeys: [ 'metadata' ],
hasSupport( name ) {
return ! [
'core/post-date',
'core/navigation-link',
'core/navigation-submenu',
].includes( name );
return ! [ 'core/navigation-link', 'core/navigation-submenu' ].includes(
name
);
},
};
114 changes: 114 additions & 0 deletions phpunit/block-bindings-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,120 @@ public function test_default_binding_for_pattern_overrides() {
);
}

/**
* Tests that the Post Date block's `isLink` attribute can be set through
* a block bindings source.
*
* @covers ::gutenberg_block_bindings_render_block
*/
public function test_post_date_is_link_binding() {
$post_id = self::factory()->post->create(
array(
'post_status' => 'publish',
)
);

register_block_bindings_source(
self::SOURCE_NAME,
array(
'label' => self::SOURCE_LABEL,
'get_value_callback' => function ( $source_args ) {
return $source_args['value'];
},
)
);

$block_content = <<<HTML
<!-- wp:post-date {"datetime":"2024-01-15T10:30:00","format":"Y-m-d","metadata":{"bindings":{"isLink":{"source":"test/source","args":{"value":true}}}}} /-->
HTML;

$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0], array( 'postId' => $post_id ) );
$result = $block->render();

$this->assertTrue(
$block->attributes['isLink'],
"The 'isLink' attribute should be updated with the value returned by the source."
);
$this->assertStringContainsString(
'<a href="' . esc_url( get_the_permalink( $post_id ) ) . '">',
$result,
'The block content should wrap the date in a link to the post when the bound value is true.'
);
$this->assertStringContainsString(
'<time datetime="2024-01-15T10:30:00">2024-01-15</time>',
$result,
'The block content should keep rendering the date from the `datetime` attribute.'
);

$block_content = <<<HTML
<!-- wp:post-date {"datetime":"2024-01-15T10:30:00","format":"Y-m-d","isLink":true,"metadata":{"bindings":{"isLink":{"source":"test/source","args":{"value":false}}}}} /-->
HTML;

$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block( $parsed_blocks[0], array( 'postId' => $post_id ) );
$result = $block->render();

$this->assertFalse(
$block->attributes['isLink'],
"The 'isLink' attribute fallback value should be overridden by the value returned by the source."
);
$this->assertStringNotContainsString(
'<a href=',
$result,
'The block content should not wrap the date in a link when the bound value is false.'
);
}

/**
* Tests that the Post Date block's `isLink` attribute supports the default
* binding for pattern overrides.
*
* @covers ::gutenberg_block_bindings_render_block
*/
public function test_default_binding_for_pattern_overrides_post_date_is_link() {
$post_id = self::factory()->post->create(
array(
'post_status' => 'publish',
)
);

$block_content = <<<HTML
<!-- wp:post-date {"datetime":"2024-01-15T10:30:00","format":"Y-m-d","metadata":{"bindings":{"__default":{"source":"core/pattern-overrides"}},"name":"Test date"}} /-->
HTML;

$parsed_blocks = parse_blocks( $block_content );
$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => $post_id,
'pattern/overrides' => array(
'Test date' => array(
'isLink' => true,
),
),
)
);

$result = $block->render();

$this->assertStringContainsString(
'<a href="' . esc_url( get_the_permalink( $post_id ) ) . '">',
$result,
'The block content should wrap the date in a link when the pattern override sets `isLink` to true.'
);
$this->assertStringContainsString(
'<time datetime="2024-01-15T10:30:00">2024-01-15</time>',
$result,
'The block content should keep rendering the date from the `datetime` attribute.'
);
$this->assertSame(
array( 'source' => 'core/pattern-overrides' ),
$block->attributes['metadata']['bindings']['isLink'],
'The __default binding should be expanded to include the `isLink` binding in the block metadata.'
);
}

/**
* Tests that filter `block_bindings_source_value` is applied.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,26 @@ test.describe( 'Registered sources', () => {
await expect( contentAttribute ).toBeVisible();
} );

test( 'should be possible to connect the post date isLink attribute, but not datetime', async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
name: 'core/post-date',
} );
await page.getByLabel( 'Attributes options' ).click();
const isLinkAttribute = page.getByRole( 'menuitemcheckbox', {
name: 'Show isLink',
} );
await expect( isLinkAttribute ).toBeVisible();
// The `datetime` attribute is managed through the block variations,
// so it should be hidden from the Attributes panel.
const datetimeAttribute = page.getByRole( 'menuitemcheckbox', {
name: 'Show datetime',
} );
await expect( datetimeAttribute ).toBeHidden();
} );

test( 'should be possible to connect the button supported attributes', async ( {
editor,
page,
Expand Down
Loading