Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
876ec3c
Date block: Make date an explicit attribute
ockham Jul 1, 2025
29ff68d
Add Post Data source for Block Bindings
ockham Jul 2, 2025
484accc
Use new date attribute when rendering Date Block
ockham Jul 2, 2025
82d8dd2
Add Date block's date attribute to Block Bindings
ockham Jul 2, 2025
3b78d35
Rename source to core/post-data
ockham Jul 2, 2025
32ffff7
Prefix source keys with post_ to avoid confusion with attributes
ockham Jul 2, 2025
c3b4b0c
Client-side source registration
ockham Jul 2, 2025
c5f37e3
Fix field names
ockham Jul 3, 2025
0d87e81
Rename back to make source updates work
ockham Jul 3, 2025
796e3a6
Add block deprecation
ockham Jul 8, 2025
d2c8134
Update variation definition
ockham Jul 8, 2025
05a3f43
Add variation for publish date
ockham Jul 8, 2025
e3ce15d
Remove displayType attribute (from the editor)
ockham Jul 8, 2025
71615a5
Show date instead of placeholder in editor for bound date attribute
ockham Jul 8, 2025
d403a14
Remove logic to hide modified date if it's before publish date from b…
ockham Jul 8, 2025
f3fb91f
Add inline comment
ockham Jul 8, 2025
70c6267
Fix link insertion
ockham Jul 8, 2025
ea362db
Add back modified date specific class name
ockham Jul 15, 2025
476fe69
Retain metadata upon block migration
ockham Jul 15, 2025
05bf390
Retain modified-date class name during migration
ockham Jul 15, 2025
c48d577
Retain backwards compatibility
ockham Jul 15, 2025
eadc0a6
Comment wording
ockham Jul 15, 2025
b98c0a9
Prefix PHP functions with gutenberg_
ockham Jul 15, 2025
9d68452
Remove now-obsolete logic to add modified-date class name
ockham Jul 15, 2025
8350bca
Set date attribute to default value upon block insertion
ockham Jul 15, 2025
7af2a40
Change popover header
ockham Jul 15, 2025
99b426f
Document new title prop
ockham Jul 15, 2025
4344ab7
Update test fixtures to include modified-date class name
ockham Jul 15, 2025
25bd52c
Whitespace
ockham Jul 16, 2025
a806014
Use gmdate() instead of date()
ockham Jul 16, 2025
34e0ed6
Update e2e test snapshots
ockham Jul 16, 2025
9e01be4
Make comment wording more assertive
ockham Jul 17, 2025
3ecd1d5
Make sure last-modified date isn't shown if it's before publish date
ockham Jul 17, 2025
24d9a4b
Add basic unit test coverage for block PHP
ockham Jul 17, 2025
421cb42
Add unit test coverage for legacy version of the block
ockham Jul 17, 2025
63aeff5
Add coverage for modified date before publish date
ockham Jul 17, 2025
3d4ac7c
Use tabs instead of spaces for indentation
ockham Jul 17, 2025
c5fed1b
Coding Standards
ockham Jul 17, 2025
dfdff3f
Ensure that block works on current WP versions
ockham Jul 21, 2025
5edd4df
Allow arbitrary sources
ockham Jul 22, 2025
dca0fe4
Tweak empty return branch
ockham Jul 22, 2025
665795a
Add test coverage for explicitly set date attribute
ockham Jul 22, 2025
d5317f2
Typo in comment
ockham Jul 22, 2025
5053810
Rename date attribute to datetime
ockham Jul 22, 2025
88f44f6
Add backport changelog
ockham Jul 22, 2025
67864f8
Polish client-side post-data source code
ockham Jul 22, 2025
89311a8
Rephrase comment to be more accurate
ockham Jul 23, 2025
ac94d31
Add "role": "content" to datetime attribute
ockham Jul 23, 2025
57a47a5
Don't register post-data source if it already exists
ockham Jul 23, 2025
be1bd7c
Remove unnecessary postType context dependency
ockham Jul 23, 2025
f45745d
Use single line comment format
ockham Jul 23, 2025
1d6d7bc
Allow transforming to different variation from block inspector
ockham Jul 28, 2025
bf2ecd5
Extract WPBlockBindingsSource type, use in post-data source declaration
ockham Jul 28, 2025
78c58d8
Add types to other block bindings sources
ockham Jul 28, 2025
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
3 changes: 3 additions & 0 deletions backport-changelog/6.9/9299.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/9299

* https://github.com/WordPress/gutenberg/pull/70585
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:** datetime, format, isLink, textAlign
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using datetime (rather than just date) as the block can still be bound to the post's publish date, which is really a timestamp that includes the time (and can be set from the block toolbar's "pencil" icon via a DateTimePicker).


## Excerpt

Expand Down
73 changes: 73 additions & 0 deletions lib/compat/wordpress-6.9/post-data-block-bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Post Data source for the block bindings.
*
* @since 6.9.0
* @package gutenberg
* @subpackage Block Bindings
*/

/**
* Gets value for Post Data source.
*
* @since 6.9.0
* @access private
*
* @param array $source_args Array containing source arguments used to look up the override value.
* Example: array( "key" => "foo" ).
* @param WP_Block $block_instance The block instance.
* @return mixed The value computed for the source.
*/
function gutenberg_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'] ) {
// Only return the modified date if it is later than the publishing date.
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 gutenberg_register_block_bindings_post_data_source() {
if ( get_block_bindings_source( 'core/post-data' ) ) {
// The source is already registered.
return;
}

register_block_bindings_source(
'core/post-data',
array(
'label' => _x( 'Post Data', 'block bindings source' ),
'get_value_callback' => 'gutenberg_block_bindings_post_data_get_value',
'uses_context' => array( 'postId' ),
)
);
}

add_action( 'init', 'gutenberg_register_block_bindings_post_data_source' );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const MyDateTimePicker = () => {
currentDate={ date }
onChange={ ( newDate ) => setDate( newDate ) }
onClose={ onClose }
title={ __( 'Select post date' ) }
/>
) }
/>
Expand All @@ -44,6 +45,14 @@ const MyDateTimePicker = () => {
`PublishDateTimePicker` supports all of the props that
[`DateTimePicker`](/packages/components/src/date-time#Props) supports, plus:

### title

The title displayed in the header of the popover that contains the `DateTimePicker`.

- Type: `String`
- Required: No
- Default: `Publish`

### onClose

Called when the user presses the close button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function PublishDateTimePicker(
showPopoverHeaderActions,
isCompact,
currentDate,
title,
...additionalProps
},
ref
Expand All @@ -33,7 +34,7 @@ export function PublishDateTimePicker(
return (
<div ref={ ref } className="block-editor-publish-date-time-picker">
<InspectorPopoverHeader
title={ __( 'Publish' ) }
title={ title || __( 'Publish' ) }
actions={
showPopoverHeaderActions
? [
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/utils/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': [ 'datetime' ],
};

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"description": "Display the publish date for an entry such as a post or page.",
"textdomain": "default",
"attributes": {
"datetime": {
"type": "string",
"role": "content"
},
"textAlign": {
"type": "string"
},
Expand All @@ -17,10 +21,6 @@
"type": "boolean",
"default": false,
"role": "content"
},
"displayType": {
"type": "string",
"default": "date"
}
},
"usesContext": [ "postId", "postType", "queryId" ],
Expand Down
105 changes: 104 additions & 1 deletion packages/block-library/src/post-date/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,111 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* Internal dependencies
*/
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( { className, displayType, metadata, ...otherAttributes } ) {
if ( displayType === 'date' || displayType === 'modified' ) {
if ( displayType === 'modified' ) {
className = clsx(
className,
'wp-block-post-date__modified-date'
);
}

return {
...otherAttributes,
className,
metadata: {
...metadata,
bindings: {
datetime: {
source: 'core/post-data',
args: { key: displayType },
},
},
},
};
}
},
isEligible( attributes ) {
// 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.datetime && ! attributes?.metadata?.bindings?.datetime
);
},
};

const v1 = {
attributes: {
textAlign: {
Expand Down Expand Up @@ -49,4 +152,4 @@ const v1 = {
*
* See block-deprecation.md
*/
export default [ v1 ];
export default [ v2, v1 ];
Loading
Loading