Skip to content
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
60 changes: 28 additions & 32 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Renders the `core/post-date` block on the server.
*
* @since 5.8.0
* @since 6.9.0 Added `datetime` attribute and Block Bindings support.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
Expand All @@ -18,44 +19,39 @@
function render_block_core_post_date( $attributes, $content, $block ) {
$classes = array();

if ( ! isset( $attributes['datetime'] ) ) {
if (
isset( $attributes['metadata']['bindings']['datetime']['source'] ) &&
isset( $attributes['metadata']['bindings']['datetime']['args'] )
) {
/*
* This can mean two things:
*
* 1. We're dealing with the legacy version of the block that didn't have the `datetime` attribute.
* 2. The `datetime` attribute is bound to a Block Bindings source, but we're on a version of WordPress
* that doesn't support binding that attribute to a Block Bindings source.
*
* In both cases, we set the `datetime` attribute to its correct value by applying Block Bindings manually.
* We might be running on a version of WordPress that doesn't support binding the block's `datetime` attribute
* to a Block Bindings source. In this case, we need to manually set the `datetime` attribute to its correct value.
* This branch can be removed once the minimum required WordPress version is 6.9 or newer.
*/
if (
isset( $attributes['metadata']['bindings']['datetime']['source'] ) &&
isset( $attributes['metadata']['bindings']['datetime']['args'] )
) {
// We're using a version of WordPress that doesn't support binding the block's `datetime` attribute to a Block Bindings source.
// This branch can be removed once the minimum required WordPress version supports the `core/post-data` source.
$source = get_block_bindings_source( $attributes['metadata']['bindings']['datetime']['source'] );
$source_args = $attributes['metadata']['bindings']['datetime']['args'];
$source = get_block_bindings_source( $attributes['metadata']['bindings']['datetime']['source'] );
$source_args = $attributes['metadata']['bindings']['datetime']['args'];

$attributes['datetime'] = $source->get_value( $source_args, $block, 'datetime' );
} elseif ( ! isset( $attributes['datetime'] ) ) {
/*
* This is the legacy version of the block that didn't have the `datetime` attribute.
* This branch needs to be kept for backward compatibility.
*/
$source = get_block_bindings_source( 'core/post-data' );
if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {
$source_args = array(
'key' => 'modified',
);
} else {
// This is the legacy version of the block that didn't have the `datetime` attribute.
// This branch needs to be kept for backward compatibility.
$source = get_block_bindings_source( 'core/post-data' );
if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {
$source_args = array(
'key' => 'modified',
);
} else {
$source_args = array(
'key' => 'date',
);
}
$source_args = array(
'key' => 'date',
);
}

$attributes['datetime'] = $source->get_value( $source_args, $block, 'datetime' );
}

if ( isset( $source_args['key'] ) && 'modified' === $source_args['key'] ) {
$classes[] = 'wp-block-post-date__modified-date';
}
if ( isset( $source_args['key'] ) && 'modified' === $source_args['key'] ) {
$classes[] = 'wp-block-post-date__modified-date';
}

if ( empty( $attributes['datetime'] ) ) {
Expand Down
16 changes: 15 additions & 1 deletion phpunit/blocks/render-block-core-post-date.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,21 @@ public function test_render_with_date_attribute_binding( $field, $expected_date_
);

$output = $block->render();
$this->assertStringContainsString( $expected_date, $output );
$this->assertStringContainsString(
$expected_date,
$output,
'The datetime attribute was not set correctly from the Block Bindings source.'
);

// Now verify that a fallback value is overridden by Block Bindings.
$block->attributes['datetime'] = '2025-01-01 00:00:00';

$output = $block->render();
$this->assertStringContainsString(
$expected_date,
$output,
'The datetime attribute fallback value was not overridden by the Block Bindings source.'
);
}

/**
Expand Down
Loading