diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 077ec51057738f..f2afe770ab8b5a 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -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. @@ -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'] ) ) { diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php index 6ea38cbff536ac..b12359d432750d 100644 --- a/phpunit/blocks/render-block-core-post-date.php +++ b/phpunit/blocks/render-block-core-post-date.php @@ -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.' + ); } /**