From 876ec3c57ab29130c82ffd53957cf9beb1776d2e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 1 Jul 2025 17:08:27 +0200 Subject: [PATCH 01/54] Date block: Make date an explicit attribute --- docs/reference-guides/core-blocks.md | 2 +- .../block-library/src/post-date/block.json | 3 + packages/block-library/src/post-date/edit.js | 92 +++++++++---------- 3 files changed, 46 insertions(+), 51 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 259e40e859aaf3..2f5135c901f6ea 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -624,7 +624,7 @@ Display the publish date for an entry such as a post or page. ([Source](https:// - **Name:** core/post-date - **Category:** theme - **Supports:** color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** displayType, format, isLink, textAlign +- **Attributes:** date, displayType, format, isLink, textAlign ## Excerpt diff --git a/packages/block-library/src/post-date/block.json b/packages/block-library/src/post-date/block.json index dadc0d2f489fee..932ccb725adcc2 100644 --- a/packages/block-library/src/post-date/block.json +++ b/packages/block-library/src/post-date/block.json @@ -7,6 +7,9 @@ "description": "Display the publish date for an entry such as a post or page.", "textdomain": "default", "attributes": { + "date": { + "type": "string" + }, "textAlign": { "type": "string" }, diff --git a/packages/block-library/src/post-date/edit.js b/packages/block-library/src/post-date/edit.js index 0599e860904652..4524f016fc2fd1 100644 --- a/packages/block-library/src/post-date/edit.js +++ b/packages/block-library/src/post-date/edit.js @@ -40,8 +40,8 @@ import { useSelect } from '@wordpress/data'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; export default function PostDateEdit( { - attributes: { textAlign, format, isLink, displayType }, - context: { postId, postType: postTypeSlug, queryId }, + attributes: { date, textAlign, format, isLink, displayType }, + context: { postType: postTypeSlug, queryId }, setAttributes, } ) { const blockProps = useBlockProps( { @@ -73,12 +73,6 @@ export default function PostDateEdit( { 'site', 'time_format' ); - const [ date, setDate ] = useEntityProp( - 'postType', - postTypeSlug, - displayType, - postId - ); const postType = useSelect( ( select ) => @@ -121,49 +115,46 @@ export default function PostDateEdit( { setAttributes( { textAlign: nextAlign } ); } } /> - { date && - displayType === 'date' && - ! isDescendentOfQueryLoop && ( - - ( - + ( + + setAttributes( { date: newDate } ) + } + is12Hour={ is12HourFormat( + siteTimeFormat + ) } + onClose={ onClose } + dateOrder={ + /* translators: Order of day, month, and year. Available formats are 'dmy', 'mdy', and 'ymd'. */ + _x( 'dmy', 'date order' ) + } + /> + ) } + renderToggle={ ( { isOpen, onToggle } ) => { + const openOnArrowDown = ( event ) => { + if ( ! isOpen && event.keyCode === DOWN ) { + event.preventDefault(); + onToggle(); + } + }; + return ( + - ) } - renderToggle={ ( { isOpen, onToggle } ) => { - const openOnArrowDown = ( event ) => { - if ( - ! isOpen && - event.keyCode === DOWN - ) { - event.preventDefault(); - onToggle(); - } - }; - return ( - - ); - } } - /> - - ) } + ); + } } + /> + + ) } @@ -171,6 +162,7 @@ export default function PostDateEdit( { label={ __( 'Settings' ) } resetAll={ () => { setAttributes( { + date: undefined, format: undefined, isLink: false, displayType: 'date', From 29ff68d7d409325ebdb7371e2d3020f810e554dc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 2 Jul 2025 15:10:43 +0200 Subject: [PATCH 02/54] Add Post Data source for Block Bindings --- .../post-data-block-bindings.php | 67 +++++++++++++++++++ lib/load.php | 1 + 2 files changed, 68 insertions(+) create mode 100644 lib/compat/wordpress-6.9/post-data-block-bindings.php diff --git a/lib/compat/wordpress-6.9/post-data-block-bindings.php b/lib/compat/wordpress-6.9/post-data-block-bindings.php new file mode 100644 index 00000000000000..fbfbeb57c58345 --- /dev/null +++ b/lib/compat/wordpress-6.9/post-data-block-bindings.php @@ -0,0 +1,67 @@ + "foo" ). + * @param WP_Block $block_instance The block instance. + * @return mixed The value computed for the source. + */ +function _block_bindings_post_data_get_value( array $source_args, $block_instance ) { + if ( empty( $source_args['key'] ) ) { + return null; + } + + if ( empty( $block_instance->context['postId'] ) ) { + return null; + } + $post_id = $block_instance->context['postId']; + + // If a post isn't public, we need to prevent unauthorized users from accessing the post data. + $post = get_post( $post_id ); + if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) { + return null; + } + + if ( 'date' === $source_args['key'] ) { + return esc_attr( get_the_date( 'c', $post_id ) ); + } + + if ( 'modified' === $source_args['key'] ) { + if ( get_the_modified_date( 'Ymdhi', $post_id ) > get_the_date( 'Ymdhi', $post_id ) ) { + return esc_attr( get_the_modified_date( 'c', $post_id ) ); + } else { + return ''; + } + } +} + +/** + * Registers Post Data source in the block bindings registry. + * + * @since 6.9.0 + * @access private + */ +function _register_block_bindings_post_data_source() { + register_block_bindings_source( + 'core/post-date', + array( + 'label' => _x( 'Post Data', 'block bindings source' ), + 'get_value_callback' => '_block_bindings_post_data_get_value', + 'uses_context' => array( 'postId', 'postType' ), + ) + ); +} + +add_action( 'init', '_register_block_bindings_post_data_source' ); diff --git a/lib/load.php b/lib/load.php index fe896202319b24..535cef389cc15e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -39,6 +39,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.8/rest-api.php'; // WordPress 6.9 compat. + require __DIR__ . '/compat/wordpress-6.9/post-data-block-bindings.php'; require __DIR__ . '/compat/wordpress-6.9/rest-api.php'; require __DIR__ . '/compat/wordpress-6.9/class-gutenberg-hierarchical-sort.php'; From 484acccbbdfaa8de3f5e6c72c5a14a1873474074 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 2 Jul 2025 15:12:31 +0200 Subject: [PATCH 03/54] Use new date attribute when rendering Date Block --- packages/block-library/src/post-date/index.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 79aa81e7f9c11a..30f4193031c1cd 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -16,14 +16,14 @@ * @return string Returns the filtered post date for the current post wrapped inside "time" tags. */ function render_block_core_post_date( $attributes, $content, $block ) { - if ( ! isset( $block->context['postId'] ) ) { + if ( ! isset( $attributes['date'] ) ) { return ''; } - $post_ID = $block->context['postId']; + $unformatted_date = $attributes['date']; + $post_timestamp = strtotime( $unformatted_date ); if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { - $post_timestamp = get_post_timestamp( $post_ID ); if ( $post_timestamp > time() ) { // translators: %s: human-readable time difference. $formatted_date = sprintf( __( '%s from now' ), human_time_diff( $post_timestamp ) ); @@ -32,10 +32,10 @@ function render_block_core_post_date( $attributes, $content, $block ) { $formatted_date = sprintf( __( '%s ago' ), human_time_diff( $post_timestamp ) ); } } else { - $formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); + $formatted_date = date( empty( $attributes['format'] ) ? get_option( 'date_format' ) : $attributes['format'], $post_timestamp ); } - $unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) ); - $classes = array(); + + $classes = array(); if ( isset( $attributes['textAlign'] ) ) { $classes[] = 'has-text-align-' . $attributes['textAlign']; From 82d8dd2feca662282740f35a03f91eb8729666da Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 2 Jul 2025 15:13:17 +0200 Subject: [PATCH 04/54] Add Date block's date attribute to Block Bindings --- packages/block-editor/src/utils/block-bindings.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-editor/src/utils/block-bindings.js b/packages/block-editor/src/utils/block-bindings.js index 9a4c6acf9a9032..797d773b5c1183 100644 --- a/packages/block-editor/src/utils/block-bindings.js +++ b/packages/block-editor/src/utils/block-bindings.js @@ -16,6 +16,7 @@ const BLOCK_BINDINGS_ALLOWED_BLOCKS = { 'core/heading': [ 'content' ], 'core/image': [ 'id', 'url', 'title', 'alt' ], 'core/button': [ 'url', 'text', 'linkTarget', 'rel' ], + 'core/post-date': [ 'date' ], }; /** From 3b78d35296577a59b36358165f28be4d45eb469f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 2 Jul 2025 15:47:23 +0200 Subject: [PATCH 05/54] Rename source to core/post-data --- lib/compat/wordpress-6.9/post-data-block-bindings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.9/post-data-block-bindings.php b/lib/compat/wordpress-6.9/post-data-block-bindings.php index fbfbeb57c58345..f88079c841e270 100644 --- a/lib/compat/wordpress-6.9/post-data-block-bindings.php +++ b/lib/compat/wordpress-6.9/post-data-block-bindings.php @@ -55,7 +55,7 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc */ function _register_block_bindings_post_data_source() { register_block_bindings_source( - 'core/post-date', + 'core/post-data', array( 'label' => _x( 'Post Data', 'block bindings source' ), 'get_value_callback' => '_block_bindings_post_data_get_value', From 32ffff718f63226b98d8c1bbac9b95af90e787d2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 2 Jul 2025 15:47:58 +0200 Subject: [PATCH 06/54] Prefix source keys with post_ to avoid confusion with attributes --- lib/compat/wordpress-6.9/post-data-block-bindings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.9/post-data-block-bindings.php b/lib/compat/wordpress-6.9/post-data-block-bindings.php index f88079c841e270..78baf71972cbc7 100644 --- a/lib/compat/wordpress-6.9/post-data-block-bindings.php +++ b/lib/compat/wordpress-6.9/post-data-block-bindings.php @@ -34,11 +34,11 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc return null; } - if ( 'date' === $source_args['key'] ) { + if ( 'post_date' === $source_args['key'] ) { return esc_attr( get_the_date( 'c', $post_id ) ); } - if ( 'modified' === $source_args['key'] ) { + if ( 'post_modified' === $source_args['key'] ) { if ( get_the_modified_date( 'Ymdhi', $post_id ) > get_the_date( 'Ymdhi', $post_id ) ) { return esc_attr( get_the_modified_date( 'c', $post_id ) ); } else { From c3b4b0c817f2aa8854605c56a3d52c38a4944280 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 2 Jul 2025 19:05:39 +0200 Subject: [PATCH 07/54] Client-side source registration --- packages/editor/src/bindings/api.js | 2 + packages/editor/src/bindings/post-data.js | 135 ++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 packages/editor/src/bindings/post-data.js diff --git a/packages/editor/src/bindings/api.js b/packages/editor/src/bindings/api.js index 2d32d76abbc3bc..2016aad7cd9d0d 100644 --- a/packages/editor/src/bindings/api.js +++ b/packages/editor/src/bindings/api.js @@ -7,6 +7,7 @@ import { registerBlockBindingsSource } from '@wordpress/blocks'; * Internal dependencies */ import patternOverrides from './pattern-overrides'; +import postData from './post-data'; import postMeta from './post-meta'; /** @@ -21,5 +22,6 @@ import postMeta from './post-meta'; */ export function registerCoreBlockBindingsSources() { registerBlockBindingsSource( patternOverrides ); + registerBlockBindingsSource( postData ); registerBlockBindingsSource( postMeta ); } diff --git a/packages/editor/src/bindings/post-data.js b/packages/editor/src/bindings/post-data.js new file mode 100644 index 00000000000000..e04082f3d1213e --- /dev/null +++ b/packages/editor/src/bindings/post-data.js @@ -0,0 +1,135 @@ +/** + * WordPress dependencies + */ +import { store as coreDataStore } from '@wordpress/core-data'; + +/** + * Internal dependencies + */ +import { store as editorStore } from '../store'; + +/** + * Gets a list of post data fields with their values and labels + * to be consumed in the needed callbacks. + * If the value is not available based on context, like in templates, + * it falls back to the default value, label, or key. + * + * @param {Object} select The select function from the data store. + * @param {Object} context The context provided. + * @return {Object} List of post data fields with their value and label. + * + * @example + * ```js + * { + * field_1_key: { + * label: 'Field 1 Label', + * value: 'Field 1 Value', + * }, + * field_2_key: { + * label: 'Field 2 Label', + * value: 'Field 2 Value', + * }, + * ... + * } + * ``` + */ +function getPostDataFields( select, context ) { + const { getEditedEntityRecord } = select( coreDataStore ); + + let entityDataValues, dataFields; + // Try to get the current entity data values. + if ( context?.postType && context?.postId ) { + entityDataValues = getEditedEntityRecord( + 'postType', + context?.postType, + context?.postId + ); + dataFields = { + date: { + label: 'Post Date', + value: entityDataValues?.date, + type: 'string', + }, + modified: { + label: 'Post Modified Date', + value: entityDataValues?.modified, + type: 'string', + }, + }; + } + + if ( ! Object.keys( dataFields || {} ).length ) { + return null; + } + + return dataFields; +} + +export default { + name: 'core/post-data', + getValues( { select, context, bindings } ) { + const dataFields = getPostDataFields( select, context ); + + const newValues = {}; + for ( const [ attributeName, source ] of Object.entries( bindings ) ) { + // Use the value, the field label, or the field key. + const fieldKey = source.args.key; + const { value: fieldValue, label: fieldLabel } = + dataFields?.[ fieldKey ] || {}; + newValues[ attributeName ] = fieldValue ?? fieldLabel ?? fieldKey; + } + return newValues; + }, + setValues( { dispatch, context, bindings } ) { + const newData = {}; + Object.values( bindings ).forEach( ( { args, newValue } ) => { + newData[ args.key ] = newValue; + } ); + + dispatch( coreDataStore ).editEntityRecord( + 'postType', + context?.postType, + context?.postId, + newData + ); + }, + canUserEditValue( { select, context, args } ) { + // Lock editing in query loop. + if ( context?.query || context?.queryId ) { + return false; + } + + // Lock editing when `postType` is not defined. + if ( ! context?.postType ) { + return false; + } + + const fieldValue = getPostDataFields( select, context )?.[ args.key ] + ?.value; + // Empty string or `false` could be a valid value, so we need to check if the field value is undefined. + if ( fieldValue === undefined ) { + return false; + } + // Check that custom fields metabox is not enabled. + const areCustomFieldsEnabled = + select( editorStore ).getEditorSettings().enableCustomFields; + if ( areCustomFieldsEnabled ) { + return false; + } + + // Check that the user has the capability to edit post meta. + const canUserEdit = select( coreDataStore ).canUser( 'update', { + kind: 'postType', + name: context?.postType, + id: context?.postId, + } ); + if ( ! canUserEdit ) { + return false; + } + + return true; + }, + getFieldsList( { select, context } ) { + return getPostDataFields( select, context ); + }, +}; From c5f37e366cb5ade1e9a024801055161a2af5b5af Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 3 Jul 2025 12:13:42 +0200 Subject: [PATCH 08/54] Fix field names --- packages/editor/src/bindings/post-data.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/bindings/post-data.js b/packages/editor/src/bindings/post-data.js index e04082f3d1213e..99f3dff6588fc3 100644 --- a/packages/editor/src/bindings/post-data.js +++ b/packages/editor/src/bindings/post-data.js @@ -45,12 +45,12 @@ function getPostDataFields( select, context ) { context?.postId ); dataFields = { - date: { + post_date: { label: 'Post Date', value: entityDataValues?.date, type: 'string', }, - modified: { + post_modified: { label: 'Post Modified Date', value: entityDataValues?.modified, type: 'string', From 0d87e81e04883df25a7f1dc55adb5081daba6413 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 3 Jul 2025 12:53:43 +0200 Subject: [PATCH 09/54] Rename back to make source updates work --- lib/compat/wordpress-6.9/post-data-block-bindings.php | 4 ++-- packages/editor/src/bindings/post-data.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.9/post-data-block-bindings.php b/lib/compat/wordpress-6.9/post-data-block-bindings.php index 78baf71972cbc7..f88079c841e270 100644 --- a/lib/compat/wordpress-6.9/post-data-block-bindings.php +++ b/lib/compat/wordpress-6.9/post-data-block-bindings.php @@ -34,11 +34,11 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc return null; } - if ( 'post_date' === $source_args['key'] ) { + if ( 'date' === $source_args['key'] ) { return esc_attr( get_the_date( 'c', $post_id ) ); } - if ( 'post_modified' === $source_args['key'] ) { + if ( 'modified' === $source_args['key'] ) { if ( get_the_modified_date( 'Ymdhi', $post_id ) > get_the_date( 'Ymdhi', $post_id ) ) { return esc_attr( get_the_modified_date( 'c', $post_id ) ); } else { diff --git a/packages/editor/src/bindings/post-data.js b/packages/editor/src/bindings/post-data.js index 99f3dff6588fc3..e04082f3d1213e 100644 --- a/packages/editor/src/bindings/post-data.js +++ b/packages/editor/src/bindings/post-data.js @@ -45,12 +45,12 @@ function getPostDataFields( select, context ) { context?.postId ); dataFields = { - post_date: { + date: { label: 'Post Date', value: entityDataValues?.date, type: 'string', }, - post_modified: { + modified: { label: 'Post Modified Date', value: entityDataValues?.modified, type: 'string', From 796e3a61eca480380bb9db4a970acafc31e1c803 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 8 Jul 2025 16:41:13 +0200 Subject: [PATCH 10/54] Add block deprecation --- .../block-library/src/post-date/deprecated.js | 89 ++++++++++++++++++- .../fixtures/blocks/core__post-date.json | 11 ++- .../blocks/core__post-date.serialized.html | 2 +- .../core__post-date__deprecated-v2.html | 1 + .../core__post-date__deprecated-v2.json | 20 +++++ ...core__post-date__deprecated-v2.parsed.json | 11 +++ ...__post-date__deprecated-v2.serialized.html | 1 + .../blocks/core__query__deprecated-3.json | 11 ++- .../core__query__deprecated-3.serialized.html | 2 +- 9 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 test/integration/fixtures/blocks/core__post-date__deprecated-v2.html create mode 100644 test/integration/fixtures/blocks/core__post-date__deprecated-v2.json create mode 100644 test/integration/fixtures/blocks/core__post-date__deprecated-v2.parsed.json create mode 100644 test/integration/fixtures/blocks/core__post-date__deprecated-v2.serialized.html diff --git a/packages/block-library/src/post-date/deprecated.js b/packages/block-library/src/post-date/deprecated.js index d217f38bf30ed8..1d65c4ed4f1395 100644 --- a/packages/block-library/src/post-date/deprecated.js +++ b/packages/block-library/src/post-date/deprecated.js @@ -3,6 +3,93 @@ */ import migrateFontFamily from '../utils/migrate-font-family'; +const v2 = { + attributes: { + textAlign: { + type: 'string', + }, + format: { + type: 'string', + }, + isLink: { + type: 'boolean', + default: false, + role: 'content', + }, + displayType: { + type: 'string', + default: 'date', + }, + }, + supports: { + html: false, + color: { + gradients: true, + link: true, + __experimentalDefaultControls: { + background: true, + text: true, + link: true, + }, + }, + spacing: { + margin: true, + padding: true, + }, + typography: { + fontSize: true, + lineHeight: true, + __experimentalFontFamily: true, + __experimentalFontWeight: true, + __experimentalFontStyle: true, + __experimentalTextTransform: true, + __experimentalTextDecoration: true, + __experimentalLetterSpacing: true, + __experimentalDefaultControls: { + fontSize: true, + }, + }, + interactivity: { + clientNavigation: true, + }, + __experimentalBorder: { + radius: true, + color: true, + width: true, + style: true, + __experimentalDefaultControls: { + radius: true, + color: true, + width: true, + style: true, + }, + }, + }, + save() { + return null; + }, + migrate( { displayType, ...otherAttributes } ) { + if ( displayType === 'date' || displayType === 'modified' ) { + return { + ...otherAttributes, + metadata: { + bindings: { + date: { + source: 'core/post-data', + args: { key: displayType }, + }, + }, + }, + }; + } + }, + isEligible( attributes ) { + // If there's neither an explicit `date` attribute nor a block binding for that attriubte, + // then we're dealing with an old version of the block. + return ! attributes.date && ! attributes?.metadata?.bindings?.date; + }, +}; + const v1 = { attributes: { textAlign: { @@ -49,4 +136,4 @@ const v1 = { * * See block-deprecation.md */ -export default [ v1 ]; +export default [ v2, v1 ]; diff --git a/test/integration/fixtures/blocks/core__post-date.json b/test/integration/fixtures/blocks/core__post-date.json index ec8dc784b3403b..56c155f05edcda 100644 --- a/test/integration/fixtures/blocks/core__post-date.json +++ b/test/integration/fixtures/blocks/core__post-date.json @@ -4,7 +4,16 @@ "isValid": true, "attributes": { "isLink": false, - "displayType": "date" + "metadata": { + "bindings": { + "date": { + "source": "core/post-data", + "args": { + "key": "date" + } + } + } + } }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__post-date.serialized.html b/test/integration/fixtures/blocks/core__post-date.serialized.html index 56357e03f49902..3e8f5829074617 100644 --- a/test/integration/fixtures/blocks/core__post-date.serialized.html +++ b/test/integration/fixtures/blocks/core__post-date.serialized.html @@ -1 +1 @@ - + diff --git a/test/integration/fixtures/blocks/core__post-date__deprecated-v2.html b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.html new file mode 100644 index 00000000000000..fe543b26ed7d96 --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/integration/fixtures/blocks/core__post-date__deprecated-v2.json b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.json new file mode 100644 index 00000000000000..2d32074b2246ed --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.json @@ -0,0 +1,20 @@ +[ + { + "name": "core/post-date", + "isValid": true, + "attributes": { + "isLink": false, + "metadata": { + "bindings": { + "date": { + "source": "core/post-data", + "args": { + "key": "modified" + } + } + } + } + }, + "innerBlocks": [] + } +] diff --git a/test/integration/fixtures/blocks/core__post-date__deprecated-v2.parsed.json b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.parsed.json new file mode 100644 index 00000000000000..5ff0a9ea999ef1 --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/post-date", + "attrs": { + "displayType": "modified" + }, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + } +] diff --git a/test/integration/fixtures/blocks/core__post-date__deprecated-v2.serialized.html b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.serialized.html new file mode 100644 index 00000000000000..9fcc375f89b21e --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-date__deprecated-v2.serialized.html @@ -0,0 +1 @@ + diff --git a/test/integration/fixtures/blocks/core__query__deprecated-3.json b/test/integration/fixtures/blocks/core__query__deprecated-3.json index d41ce2c5c826b7..125ad5341966df 100644 --- a/test/integration/fixtures/blocks/core__query__deprecated-3.json +++ b/test/integration/fixtures/blocks/core__query__deprecated-3.json @@ -68,7 +68,16 @@ "isValid": true, "attributes": { "isLink": false, - "displayType": "date" + "metadata": { + "bindings": { + "date": { + "source": "core/post-data", + "args": { + "key": "date" + } + } + } + } }, "innerBlocks": [] }, diff --git a/test/integration/fixtures/blocks/core__query__deprecated-3.serialized.html b/test/integration/fixtures/blocks/core__query__deprecated-3.serialized.html index 86c87dde71c3bd..b7a76d74e74442 100644 --- a/test/integration/fixtures/blocks/core__query__deprecated-3.serialized.html +++ b/test/integration/fixtures/blocks/core__query__deprecated-3.serialized.html @@ -3,7 +3,7 @@ \ No newline at end of file From 9e01be4226b21dd8ab085c2df5c2b2026da766e7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 15:57:31 +0200 Subject: [PATCH 32/54] Make comment wording more assertive --- packages/block-library/src/post-date/index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 75de6debcea2bb..32efebc18cd524 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -19,10 +19,9 @@ function render_block_core_post_date( $attributes, $content, $block ) { $classes = array(); if ( ! isset( $attributes['date'] ) ) { - // This must be the legacy version of the block. + // This is the legacy version of the block. // In this case, we manually apply block bindings for the // `core/post-data` source to set the `date` attribute. - $source = get_block_bindings_source( 'core/post-data' ); if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { From 3ecd1d5c49632ffdac6df826cf0245e032bacbbc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 16:02:09 +0200 Subject: [PATCH 33/54] Make sure last-modified date isn't shown if it's before publish date --- packages/block-library/src/post-date/index.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 32efebc18cd524..5ae4babe643f8a 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -35,6 +35,13 @@ function render_block_core_post_date( $attributes, $content, $block ) { ); } $attributes['date'] = $source->get_value( $source_args, $block, 'date' ); + } elseif ( empty( $attributes['date'] ) ) { + // If the `date` attribute is set but empty, it's because the block is bound to the + // post's last modified date, and the latter lies before the publish date. + // In this case, we return an empty string. + // See https://github.com/WordPress/gutenberg/pull/46839 where this logic was originally + // implemented. + return ''; } $unformatted_date = $attributes['date']; From 24d9a4bb81f8c7eddeeb46415b1a0ef1d7f52361 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 19:06:42 +0200 Subject: [PATCH 34/54] Add basic unit test coverage for block PHP --- .../blocks/render-block-core-post-date.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 phpunit/blocks/render-block-core-post-date.php diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php new file mode 100644 index 00000000000000..568e269513fe18 --- /dev/null +++ b/phpunit/blocks/render-block-core-post-date.php @@ -0,0 +1,69 @@ +post->create( array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_name' => 'tabby', + 'post_title' => 'Tabby cats', + 'post_content' => 'Tabby cat content', + 'post_date' => '2025-07-05 00:00:00', + ) ); + } + + public function set_up() { + parent::set_up(); + + $this->update_post_modified( self::$post_id, '2025-07-10 00:00:00' ); + } + + function data_render_with_date_attribute_binding() { + return array( + 'Publish date' => array( 'date', 'get_the_date' ), + 'Modified date' => array( 'modified', 'get_the_modified_date' ), + ); + } + + /** + * @dataProvider data_render_with_date_attribute_binding + */ + public function test_render_with_date_attribute_binding( $field, $expected_date_function ) { + $expected_date = call_user_func( $expected_date_function, 'c', self::$post_id ); + + $attributes = array( + 'metadata' => array( + 'bindings' => array( + 'date' => array( + 'source' => 'core/post-data', + 'args' => array( 'key' => $field ), + ), + ), + ), + ); + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id + ) + ); + + $output = $block->render(); + $this->assertStringContainsString( $expected_date, $output ); + } +} \ No newline at end of file From 421cb422d2da76fa0446b1faa956640dc537bbfc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 19:12:01 +0200 Subject: [PATCH 35/54] Add unit test coverage for legacy version of the block --- .../blocks/render-block-core-post-date.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php index 568e269513fe18..daf28e03e9aef1 100644 --- a/phpunit/blocks/render-block-core-post-date.php +++ b/phpunit/blocks/render-block-core-post-date.php @@ -66,4 +66,30 @@ public function test_render_with_date_attribute_binding( $field, $expected_date_ $output = $block->render(); $this->assertStringContainsString( $expected_date, $output ); } + + /** + * @dataProvider data_render_with_date_attribute_binding + */ + public function test_render_legacy_block( $field, $expected_date_function ) { + $expected_date = call_user_func( $expected_date_function, 'c', self::$post_id ); + + $attributes = array(); + + if ( 'modified' === $field ) { + $attributes['displayType'] = 'modified'; + } + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id + ) + ); + + $output = $block->render(); + $this->assertStringContainsString( $expected_date, $output ); + } } \ No newline at end of file From 63aeff5188e4e50a77ae296a7b5b1ecdbd067039 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 19:14:13 +0200 Subject: [PATCH 36/54] Add coverage for modified date before publish date --- .../blocks/render-block-core-post-date.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php index daf28e03e9aef1..b9ac0890f47405 100644 --- a/phpunit/blocks/render-block-core-post-date.php +++ b/phpunit/blocks/render-block-core-post-date.php @@ -92,4 +92,32 @@ public function test_render_legacy_block( $field, $expected_date_function ) { $output = $block->render(); $this->assertStringContainsString( $expected_date, $output ); } + + public function test_render_modified_date_before_publish_date() { + $this->update_post_modified( self::$post_id, '2025-07-01 00:00:00' ); + + $attributes = array( + 'metadata' => array( + 'bindings' => array( + 'date' => array( + 'source' => 'core/post-data', + 'args' => array( 'key' => 'modified' ), + ), + ), + ), + ); + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id + ) + ); + + $output = $block->render(); + $this->assertSame( '', $output ); + } } \ No newline at end of file From 3d4ac7cafd03a11d5fe2c5b316aa6910a7ee269c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 23:08:08 +0200 Subject: [PATCH 37/54] Use tabs instead of spaces for indentation --- .../blocks/render-block-core-post-date.php | 220 +++++++++--------- 1 file changed, 110 insertions(+), 110 deletions(-) diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php index b9ac0890f47405..43629d11871f3d 100644 --- a/phpunit/blocks/render-block-core-post-date.php +++ b/phpunit/blocks/render-block-core-post-date.php @@ -10,114 +10,114 @@ */ class Test_Render_Block_Core_Post_Date extends WP_UnitTestCase { - protected static $post_id; - - public static function wpSetUpBeforeClass( $factory ) { - self::$post_id = $factory->post->create( array( - 'post_type' => 'post', - 'post_status' => 'publish', - 'post_name' => 'tabby', - 'post_title' => 'Tabby cats', - 'post_content' => 'Tabby cat content', - 'post_date' => '2025-07-05 00:00:00', - ) ); - } - - public function set_up() { - parent::set_up(); - - $this->update_post_modified( self::$post_id, '2025-07-10 00:00:00' ); - } - - function data_render_with_date_attribute_binding() { - return array( - 'Publish date' => array( 'date', 'get_the_date' ), - 'Modified date' => array( 'modified', 'get_the_modified_date' ), - ); - } - - /** - * @dataProvider data_render_with_date_attribute_binding - */ - public function test_render_with_date_attribute_binding( $field, $expected_date_function ) { - $expected_date = call_user_func( $expected_date_function, 'c', self::$post_id ); - - $attributes = array( - 'metadata' => array( - 'bindings' => array( - 'date' => array( - 'source' => 'core/post-data', - 'args' => array( 'key' => $field ), - ), - ), - ), - ); - - $block = new WP_Block( - array( - 'blockName' => 'core/post-date', - 'attrs' => $attributes, - ), - array( - 'postId' => self::$post_id - ) - ); - - $output = $block->render(); - $this->assertStringContainsString( $expected_date, $output ); - } - - /** - * @dataProvider data_render_with_date_attribute_binding - */ - public function test_render_legacy_block( $field, $expected_date_function ) { - $expected_date = call_user_func( $expected_date_function, 'c', self::$post_id ); - - $attributes = array(); - - if ( 'modified' === $field ) { - $attributes['displayType'] = 'modified'; - } - - $block = new WP_Block( - array( - 'blockName' => 'core/post-date', - 'attrs' => $attributes, - ), - array( - 'postId' => self::$post_id - ) - ); - - $output = $block->render(); - $this->assertStringContainsString( $expected_date, $output ); - } - - public function test_render_modified_date_before_publish_date() { - $this->update_post_modified( self::$post_id, '2025-07-01 00:00:00' ); - - $attributes = array( - 'metadata' => array( - 'bindings' => array( - 'date' => array( - 'source' => 'core/post-data', - 'args' => array( 'key' => 'modified' ), - ), - ), - ), - ); - - $block = new WP_Block( - array( - 'blockName' => 'core/post-date', - 'attrs' => $attributes, - ), - array( - 'postId' => self::$post_id - ) - ); - - $output = $block->render(); - $this->assertSame( '', $output ); - } + protected static $post_id; + + public static function wpSetUpBeforeClass( $factory ) { + self::$post_id = $factory->post->create( array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_name' => 'tabby', + 'post_title' => 'Tabby cats', + 'post_content' => 'Tabby cat content', + 'post_date' => '2025-07-05 00:00:00', + ) ); + } + + public function set_up() { + parent::set_up(); + + $this->update_post_modified( self::$post_id, '2025-07-10 00:00:00' ); + } + + function data_render_with_date_attribute_binding() { + return array( + 'Publish date' => array( 'date', 'get_the_date' ), + 'Modified date' => array( 'modified', 'get_the_modified_date' ), + ); + } + + /** + * @dataProvider data_render_with_date_attribute_binding + */ + public function test_render_with_date_attribute_binding( $field, $expected_date_function ) { + $expected_date = call_user_func( $expected_date_function, 'c', self::$post_id ); + + $attributes = array( + 'metadata' => array( + 'bindings' => array( + 'date' => array( + 'source' => 'core/post-data', + 'args' => array( 'key' => $field ), + ), + ), + ), + ); + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id + ) + ); + + $output = $block->render(); + $this->assertStringContainsString( $expected_date, $output ); + } + + /** + * @dataProvider data_render_with_date_attribute_binding + */ + public function test_render_legacy_block( $field, $expected_date_function ) { + $expected_date = call_user_func( $expected_date_function, 'c', self::$post_id ); + + $attributes = array(); + + if ( 'modified' === $field ) { + $attributes['displayType'] = 'modified'; + } + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id + ) + ); + + $output = $block->render(); + $this->assertStringContainsString( $expected_date, $output ); + } + + public function test_render_modified_date_before_publish_date() { + $this->update_post_modified( self::$post_id, '2025-07-01 00:00:00' ); + + $attributes = array( + 'metadata' => array( + 'bindings' => array( + 'date' => array( + 'source' => 'core/post-data', + 'args' => array( 'key' => 'modified' ), + ), + ), + ), + ); + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id + ) + ); + + $output = $block->render(); + $this->assertSame( '', $output ); + } } \ No newline at end of file From c5fed1b8d8f6e4e8d7fca0dea2c0fec7abde9c36 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 17 Jul 2025 23:28:59 +0200 Subject: [PATCH 38/54] Coding Standards --- .../blocks/render-block-core-post-date.php | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php index 43629d11871f3d..12890466f3de50 100644 --- a/phpunit/blocks/render-block-core-post-date.php +++ b/phpunit/blocks/render-block-core-post-date.php @@ -13,14 +13,16 @@ class Test_Render_Block_Core_Post_Date extends WP_UnitTestCase { protected static $post_id; public static function wpSetUpBeforeClass( $factory ) { - self::$post_id = $factory->post->create( array( - 'post_type' => 'post', - 'post_status' => 'publish', - 'post_name' => 'tabby', - 'post_title' => 'Tabby cats', - 'post_content' => 'Tabby cat content', - 'post_date' => '2025-07-05 00:00:00', - ) ); + self::$post_id = $factory->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_name' => 'tabby', + 'post_title' => 'Tabby cats', + 'post_content' => 'Tabby cat content', + 'post_date' => '2025-07-05 00:00:00', + ) + ); } public function set_up() { @@ -29,7 +31,7 @@ public function set_up() { $this->update_post_modified( self::$post_id, '2025-07-10 00:00:00' ); } - function data_render_with_date_attribute_binding() { + public function data_render_with_date_attribute_binding() { return array( 'Publish date' => array( 'date', 'get_the_date' ), 'Modified date' => array( 'modified', 'get_the_modified_date' ), @@ -59,7 +61,7 @@ public function test_render_with_date_attribute_binding( $field, $expected_date_ 'attrs' => $attributes, ), array( - 'postId' => self::$post_id + 'postId' => self::$post_id, ) ); @@ -85,7 +87,7 @@ public function test_render_legacy_block( $field, $expected_date_function ) { 'attrs' => $attributes, ), array( - 'postId' => self::$post_id + 'postId' => self::$post_id, ) ); @@ -113,11 +115,11 @@ public function test_render_modified_date_before_publish_date() { 'attrs' => $attributes, ), array( - 'postId' => self::$post_id + 'postId' => self::$post_id, ) ); $output = $block->render(); $this->assertSame( '', $output ); } -} \ No newline at end of file +} From dfdff3ff151da86ecf23853e09c802a7c877458c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 21 Jul 2025 21:51:46 +0200 Subject: [PATCH 39/54] Ensure that block works on current WP versions --- .../block-library/src/post-date/index.php | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 5ae4babe643f8a..5c3a34f1243727 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -19,23 +19,46 @@ function render_block_core_post_date( $attributes, $content, $block ) { $classes = array(); if ( ! isset( $attributes['date'] ) ) { - // This is the legacy version of the block. - // In this case, we manually apply block bindings for the - // `core/post-data` source to set the `date` attribute. - $source = get_block_bindings_source( 'core/post-data' ); - - if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { - $source_args = array( - 'key' => 'modified', - ); - $classes[] = 'wp-block-post-date__modified-date'; + /* + * This can mean two things: + * + * 1. We're dealing with the legacy version of the block that didn't have the `date` attribute. + * 2. The `date` attribute is bound to a Block Bindings source, but we're on a version of WordPress + * that doesn't support the `core/post-data` source. + * + * In both cases, we set the `date` attribute to its correct value by applying Block Bindings manually. + */ + if ( + isset( $attributes['metadata']['bindings']['date']['source'] ) && + 'core/post-data' === $attributes['metadata']['bindings']['date']['source'] && + isset( $attributes['metadata']['bindings']['date']['args'] ) + ) { + // We're using a version of WordPress that doesn't support the `core/post-data` source for block bindings. + // This branch can be removed once the minimum required WordPress version supports the `core/post-data` source. + $source_args = $attributes['metadata']['bindings']['date']['args']; } else { - $source_args = array( - 'key' => 'date', - ); + // This is the legacy version of the block that didn't have the `date` attribute. + // This branch needs to be kept for backward compatibility. + if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { + $source_args = array( + 'key' => 'modified', + ); + } else { + $source_args = array( + 'key' => 'date', + ); + } } + + $source = get_block_bindings_source( 'core/post-data' ); $attributes['date'] = $source->get_value( $source_args, $block, 'date' ); - } elseif ( empty( $attributes['date'] ) ) { + + if ( isset( $source_args['key'] ) && 'modified' === $source_args['key'] ) { + $classes[] = 'wp-block-post-date__modified-date'; + } + } + + if ( empty( $attributes['date'] ) ) { // If the `date` attribute is set but empty, it's because the block is bound to the // post's last modified date, and the latter lies before the publish date. // In this case, we return an empty string. From 5edd4df42fe973dd90e3e3cff3bd00339239d429 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 22 Jul 2025 10:55:25 +0200 Subject: [PATCH 40/54] Allow arbitrary sources --- packages/block-library/src/post-date/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 5c3a34f1243727..ab11126506ff09 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -30,15 +30,16 @@ function render_block_core_post_date( $attributes, $content, $block ) { */ if ( isset( $attributes['metadata']['bindings']['date']['source'] ) && - 'core/post-data' === $attributes['metadata']['bindings']['date']['source'] && isset( $attributes['metadata']['bindings']['date']['args'] ) ) { // We're using a version of WordPress that doesn't support the `core/post-data` source for block bindings. // 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']['date']['source'] ); $source_args = $attributes['metadata']['bindings']['date']['args']; } else { // This is the legacy version of the block that didn't have the `date` 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', @@ -50,7 +51,6 @@ function render_block_core_post_date( $attributes, $content, $block ) { } } - $source = get_block_bindings_source( 'core/post-data' ); $attributes['date'] = $source->get_value( $source_args, $block, 'date' ); if ( isset( $source_args['key'] ) && 'modified' === $source_args['key'] ) { From dca0fe4c224f6c15f405438ad9198067ee379865 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 22 Jul 2025 10:57:39 +0200 Subject: [PATCH 41/54] Tweak empty return branch --- packages/block-library/src/post-date/index.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index ab11126506ff09..7127fb44ac61fb 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -59,12 +59,13 @@ function render_block_core_post_date( $attributes, $content, $block ) { } if ( empty( $attributes['date'] ) ) { - // If the `date` attribute is set but empty, it's because the block is bound to the + // If the `date` attribute is set but empty, it could be because Block Bindings + // set it that way. This can happen e.g. if the block is bound to the // post's last modified date, and the latter lies before the publish date. - // In this case, we return an empty string. - // See https://github.com/WordPress/gutenberg/pull/46839 where this logic was originally - // implemented. - return ''; + // (See https://github.com/WordPress/gutenberg/pull/46839 where this logic was originally + // implemented.) + // In this case, we have to respect and return the empty value. + return $attributes['date']; } $unformatted_date = $attributes['date']; From 665795aebf1371d0751799698f702ec66993931a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 22 Jul 2025 11:17:18 +0200 Subject: [PATCH 42/54] Add test coverage for explicitly set date attribute --- .../blocks/render-block-core-post-date.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/phpunit/blocks/render-block-core-post-date.php b/phpunit/blocks/render-block-core-post-date.php index 12890466f3de50..bf19f9102291fe 100644 --- a/phpunit/blocks/render-block-core-post-date.php +++ b/phpunit/blocks/render-block-core-post-date.php @@ -31,6 +31,27 @@ public function set_up() { $this->update_post_modified( self::$post_id, '2025-07-10 00:00:00' ); } + public function test_render_with_explicit_date_attribute() { + $expected_date = '2025-03-02 00:00:00'; + + $attributes = array( + 'date' => $expected_date, + ); + + $block = new WP_Block( + array( + 'blockName' => 'core/post-date', + 'attrs' => $attributes, + ), + array( + 'postId' => self::$post_id, + ) + ); + + $output = $block->render(); + $this->assertStringContainsString( $expected_date, $output ); + } + public function data_render_with_date_attribute_binding() { return array( 'Publish date' => array( 'date', 'get_the_date' ), From d5317f21504b5d0914e8b1e7e79184593760a971 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 22 Jul 2025 14:33:12 +0200 Subject: [PATCH 43/54] Typo in comment --- packages/block-library/src/post-date/deprecated.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-date/deprecated.js b/packages/block-library/src/post-date/deprecated.js index 32255a918d6d44..27ff334182a6ec 100644 --- a/packages/block-library/src/post-date/deprecated.js +++ b/packages/block-library/src/post-date/deprecated.js @@ -98,7 +98,7 @@ const v2 = { } }, isEligible( attributes ) { - // If there's neither an explicit `date` attribute nor a block binding for that attriubte, + // If there's neither an explicit `date` attribute nor a block binding for that attribute, // then we're dealing with an old version of the block. return ! attributes.date && ! attributes?.metadata?.bindings?.date; }, From 505381024f9f9dc146c3ec53fa7fe3a82ca31a99 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 22 Jul 2025 14:53:05 +0200 Subject: [PATCH 44/54] Rename date attribute to datetime --- docs/reference-guides/core-blocks.md | 2 +- .../block-editor/src/utils/block-bindings.js | 2 +- .../block-library/src/post-date/block.json | 2 +- .../block-library/src/post-date/deprecated.js | 8 +++-- packages/block-library/src/post-date/edit.js | 32 ++++++++++--------- .../block-library/src/post-date/index.php | 30 ++++++++--------- .../block-library/src/post-date/variations.js | 13 ++++---- .../blocks/render-block-core-post-date.php | 6 ++-- ...ping-from-the-global-inserter-1-webkit.txt | 2 +- .../fixtures/blocks/core__post-date.json | 2 +- .../blocks/core__post-date.serialized.html | 2 +- .../core__post-date__deprecated-v2.json | 2 +- ...__post-date__deprecated-v2.serialized.html | 2 +- .../blocks/core__query__deprecated-3.json | 2 +- .../core__query__deprecated-3.serialized.html | 2 +- 15 files changed, 57 insertions(+), 52 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index a28392015c0e64..630bcdd574e9a0 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -624,7 +624,7 @@ Display the publish date for an entry such as a post or page. ([Source](https:// - **Name:** core/post-date - **Category:** theme - **Supports:** color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** date, format, isLink, textAlign +- **Attributes:** datetime, format, isLink, textAlign ## Excerpt diff --git a/packages/block-editor/src/utils/block-bindings.js b/packages/block-editor/src/utils/block-bindings.js index 797d773b5c1183..e67ee74648da8c 100644 --- a/packages/block-editor/src/utils/block-bindings.js +++ b/packages/block-editor/src/utils/block-bindings.js @@ -16,7 +16,7 @@ const BLOCK_BINDINGS_ALLOWED_BLOCKS = { 'core/heading': [ 'content' ], 'core/image': [ 'id', 'url', 'title', 'alt' ], 'core/button': [ 'url', 'text', 'linkTarget', 'rel' ], - 'core/post-date': [ 'date' ], + 'core/post-date': [ 'datetime' ], }; /** diff --git a/packages/block-library/src/post-date/block.json b/packages/block-library/src/post-date/block.json index d9acbe8175c225..734af86ebb7f59 100644 --- a/packages/block-library/src/post-date/block.json +++ b/packages/block-library/src/post-date/block.json @@ -7,7 +7,7 @@ "description": "Display the publish date for an entry such as a post or page.", "textdomain": "default", "attributes": { - "date": { + "datetime": { "type": "string" }, "textAlign": { diff --git a/packages/block-library/src/post-date/deprecated.js b/packages/block-library/src/post-date/deprecated.js index 27ff334182a6ec..a590015d883095 100644 --- a/packages/block-library/src/post-date/deprecated.js +++ b/packages/block-library/src/post-date/deprecated.js @@ -88,7 +88,7 @@ const v2 = { metadata: { ...metadata, bindings: { - date: { + datetime: { source: 'core/post-data', args: { key: displayType }, }, @@ -98,9 +98,11 @@ const v2 = { } }, isEligible( attributes ) { - // If there's neither an explicit `date` attribute nor a block binding for that attribute, + // If there's neither an explicit `datetime` attribute nor a block binding for that attribute, // then we're dealing with an old version of the block. - return ! attributes.date && ! attributes?.metadata?.bindings?.date; + return ( + ! attributes.datetime && ! attributes?.metadata?.bindings?.datetime + ); }, }; diff --git a/packages/block-library/src/post-date/edit.js b/packages/block-library/src/post-date/edit.js index bbe3b494cd4701..2c4aed7cbbf6c3 100644 --- a/packages/block-library/src/post-date/edit.js +++ b/packages/block-library/src/post-date/edit.js @@ -41,13 +41,13 @@ import { useSelect, useDispatch } from '@wordpress/data'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; export default function PostDateEdit( { - attributes: { date, textAlign, format, isLink, metadata }, + attributes: { datetime, textAlign, format, isLink, metadata }, context: { postType: postTypeSlug, queryId }, setAttributes, } ) { const displayType = - metadata?.bindings?.date?.source === 'core/post-data' && - metadata?.bindings?.date?.args?.key; + metadata?.bindings?.datetime?.source === 'core/post-data' && + metadata?.bindings?.datetime?.args?.key; const blockProps = useBlockProps( { className: clsx( { @@ -68,15 +68,15 @@ export default function PostDateEdit( { const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); - // We need to set the date to a default value upon first loading + // We need to set the datetime to a default value upon first loading // to discern the block from its legacy version (which would default // to the containing post's publish date). useEffect( () => { - if ( date === undefined ) { + if ( datetime === undefined ) { __unstableMarkNextChangeAsNotPersistent(); - setAttributes( { date: new Date() } ); + setAttributes( { datetime: new Date() } ); } - }, [ date ] ); + }, [ datetime ] ); const isDescendentOfQueryLoop = Number.isFinite( queryId ); const dateSettings = getDateSettings(); @@ -100,14 +100,14 @@ export default function PostDateEdit( { ); let postDate = ( -