Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,8 @@ Display a custom date. ([Source](https://github.com/WordPress/gutenberg/tree/tru

- **Name:** core/post-date
- **Category:** theme
- **Supports:** anchor, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** datetime, format, isLink, textAlign
- **Supports:** anchor, color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight, textAlign), ~~html~~
- **Attributes:** datetime, format, isLink

## Excerpt

Expand Down
4 changes: 1 addition & 3 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"type": "string",
"role": "content"
},
"textAlign": {
"type": "string"
},
"format": {
"type": "string"
},
Expand Down Expand Up @@ -46,6 +43,7 @@
"typography": {
"fontSize": true,
"lineHeight": true,
"textAlign": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
Expand Down
92 changes: 82 additions & 10 deletions packages/block-library/src/post-date/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,85 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* Internal dependencies
*/
import migrateFontFamily from '../utils/migrate-font-family';
import migrateTextAlign from '../utils/migrate-text-align';

const v4 = {
attributes: {
datetime: {
type: 'string',
role: 'content',
},
textAlign: {
type: 'string',
},
format: {
type: 'string',
},
isLink: {
type: 'boolean',
default: false,
role: 'content',
},
},
supports: {
anchor: true,
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: migrateTextAlign,
isEligible( attributes ) {
return (
!! attributes.textAlign ||
!! attributes.className?.match(
/\bhas-text-align-(left|center|right)\b/
)
);
},
};

const v3 = {
attributes: {
Expand Down Expand Up @@ -178,10 +251,9 @@ const v2 = {
migrate( { className, displayType, metadata, ...otherAttributes } ) {
if ( displayType === 'date' || displayType === 'modified' ) {
if ( displayType === 'modified' ) {
className = clsx(
className,
'wp-block-post-date__modified-date'
);
className = [ className, 'wp-block-post-date__modified-date' ]
.filter( Boolean )
.join( ' ' );
Comment thread
t-hamano marked this conversation as resolved.
Resolved
}

return {
Expand Down Expand Up @@ -254,4 +326,4 @@ const v1 = {
*
* See block-deprecation.md
*/
export default [ v3, v2, v1 ];
export default [ v4, v3, v2, v1 ];
36 changes: 11 additions & 25 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
Expand All @@ -14,7 +9,6 @@ import {
getSettings as getDateSettings,
} from '@wordpress/date';
import {
AlignmentControl,
BlockControls,
InspectorControls,
store as blockEditorStore,
Expand All @@ -41,19 +35,18 @@ import { store as blocksStore } from '@wordpress/blocks';
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import useDeprecatedTextAlign from '../utils/deprecated-text-align-attributes';

export default function PostDateEdit( {
attributes,
context: { postType: postTypeSlug, queryId },
setAttributes,
name,
} ) {
const { datetime, textAlign, format, isLink } = attributes;
const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
export default function PostDateEdit( props ) {
const {
attributes,
context: { postType: postTypeSlug, queryId },
setAttributes,
name,
} = props;
useDeprecatedTextAlign( props );
const { datetime, format, isLink } = attributes;
const blockProps = useBlockProps();
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

// Use internal state instead of a ref to make sure that the component
Expand Down Expand Up @@ -129,13 +122,6 @@ export default function PostDateEdit( {
{ ( blockEditingMode === 'default' ||

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.

{ ( blockEditingMode === 'default' || ! isDescendentOfQueryLoop ) &&
	activeBlockVariationName !== 'post-date-modified' &&
	( ! isDescendentOfQueryLoop || ! activeBlockVariationName ) && (
		<BlockControls group="block">
			<ToolbarGroup>
			</ToolbarGroup>
		</BlockControls>
	) }

All of the conditional checks here should be able to be consolidated.

! isDescendentOfQueryLoop ) && (
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>

{ activeBlockVariationName !== 'post-date-modified' &&
( ! isDescendentOfQueryLoop ||
! activeBlockVariationName ) && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:post-date {"textAlign":"left"} /-->

<!-- wp:post-date {"textAlign":"center"} /-->

<!-- wp:post-date {"textAlign":"right"} /-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[
{
"name": "core/post-date",
"isValid": true,
"attributes": {
"textAlign": "left",
"isLink": false,
"metadata": {
"bindings": {
"datetime": {
"source": "core/post-data",
"args": {
"field": "date"
}
}
}
}
},
"innerBlocks": []
},
{
"name": "core/post-date",
"isValid": true,
"attributes": {
"textAlign": "center",
"isLink": false,
"metadata": {
"bindings": {
"datetime": {
"source": "core/post-data",
"args": {
"field": "date"
}
}
}
}
},
"innerBlocks": []
},
{
"name": "core/post-date",
"isValid": true,
"attributes": {
"textAlign": "right",
"isLink": false,
"metadata": {
"bindings": {
"datetime": {
"source": "core/post-data",
"args": {
"field": "date"
}
}
}
}
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"blockName": "core/post-date",
"attrs": {
"textAlign": "left"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n\n",
"innerContent": [ "\n\n" ]
},
{
"blockName": "core/post-date",
"attrs": {
"textAlign": "center"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n\n",
"innerContent": [ "\n\n" ]
},
{
"blockName": "core/post-date",
"attrs": {
"textAlign": "right"
},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:post-date {"metadata":{"bindings":{"datetime":{"source":"core/post-data","args":{"field":"date"}}}}} /-->

<!-- wp:post-date {"metadata":{"bindings":{"datetime":{"source":"core/post-data","args":{"field":"date"}}}}} /-->

<!-- wp:post-date {"metadata":{"bindings":{"datetime":{"source":"core/post-data","args":{"field":"date"}}}}} /-->

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.

This doesn't seem to be the expected result. The text alignment seems to be discarded.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The v2 deprecation migration was unintentionally overwriting v4's correctly migrated textAlign attribute back to the top level, because both were triggering sequentially on the original block attributes.

I fixed this by updating v2's isEligible check to explicitly exclude blocks that have textAlign, allowing v4 to handle them correctly. The fixtures have been regenerated with the proper output!

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.

I fixed this by updating v2's isEligible check to explicitly exclude blocks that have textAlign

The v2 deprecation is a necessary step to migrate block attributes to Block Bindings. Adding exclusion rules will cause unintended problems. Please try the following:

  1. Open the code editor and insert the following HTML:
<!-- wp:post-date {"displayType":"modified"} /-->
<!-- wp:post-date {"displayType":"modified", "textAlign":"right"} /-->
  1. Back to the Visual editor, and back to the Code editor again.
  2. Unintended migration is occurring in a block that has textAlign.

✅

<!-- wp:post-date {"metadata":{"bindings":{"datetime":{"source":"core/post-data","args":{"field":"modified"}}}},"className":"wp-block-post-date__modified-date"} /-->

❌

<!-- wp:post-date {"datetime":"2026-03-16T10:14:11.214Z","style":{"typography":{"textAlign":"right"}}} /-->

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.

The solution is probably to add migrate: migrateTextAlign to v1, v2, and v3 as well. See #74383 (comment)

Loading