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
3 changes: 3 additions & 0 deletions backport-changelog/7.2/15517.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/15517

* https://github.com/WordPress/gutenberg/pull/86367
55 changes: 55 additions & 0 deletions lib/compat/wordpress-7.2/view-config-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,61 @@ function gutenberg_register_author_layout_7_2( $post_type ) {
}
add_action( 'registered_post_type', 'gutenberg_register_author_layout_7_2' );

/**
* Summarizes the `date` panel of the post form with the `publish_date` field,
* which reads "Immediately" while the post has no date of its own, as the post
* summary did before it moved to the form.
*
* Only the form changes: lists keep showing the `date` field, which can't say
* "Immediately" since a floating date is still a date there. The patch merges
* into the existing `date` member by id, leaving the rest of the form untouched.
*
* @param Gutenberg_View_Config_Data $data The view configuration container for the entity.
* @return Gutenberg_View_Config_Data The updated view configuration container.
*/
function _gutenberg_update_date_summary_in_view_config( $data ) {
return $data->merge(
array(
'form' => array(
'fields' => array(
array(
'id' => 'date',
'layout' => array(
'type' => 'panel',
'summary' => 'publish_date',
),
),
),
),
),
1
);
}

/**
* Layers the `date` summary on top of the view configuration of every post
* type that uses the default form, including custom post types registered at
* any point.
*
* A member that is absent would be appended instead of merged, so post types
* whose base definition provides its own form are skipped.
*
* @param string $post_type The post type being registered.
*/
function gutenberg_register_date_summary_7_2( $post_type ) {
if ( in_array( $post_type, GUTENBERG_VIEW_CONFIG_POST_TYPES_WITH_OWN_FORM, true ) ) {
return;
}

add_filter(
gutenberg_get_entity_view_config_hook_name( 'postType', $post_type ),
'_gutenberg_update_date_summary_in_view_config',
6,
1
);
}
add_action( 'registered_post_type', 'gutenberg_register_date_summary_7_2' );

/**
* Registers the entity view configuration filters that layer on top of the base
* definitions, at a priority between those (5) and third-party callbacks (10),
Expand Down
10 changes: 6 additions & 4 deletions packages/core-fields/src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* - `password`, for every post type but the design ones.
* - `ping_status`, for the post types supporting `trackbacks`.
* - `post-content-info`, for the post types supporting `editor`.
* - `publish_date`, for every post type but the design ones.
* - `scheduled_date`, for every post type but the design ones.
* - `slug`, for the viewable post types but the design ones.
* - `status`, for every post type but the design ones.
Expand Down Expand Up @@ -74,6 +75,7 @@ function wp_register_core_post_type_supports_fields() {
'password' => true,
'ping_status' => post_type_supports( $post_type, 'trackbacks' ),
'post-content-info' => post_type_supports( $post_type, 'editor' ),
'publish_date' => true,
'scheduled_date' => true,
'slug' => is_post_type_viewable( $post_type ),
'status' => true,
Expand Down Expand Up @@ -110,15 +112,15 @@ function wp_register_core_field_collections() {
wp_register_entity_field_collection( __DIR__ . '/page' );

// wp_template: unregister unwanted default fields, and register its own.
wp_unregister_entity_fields( 'postType', 'wp_template', array( 'author', 'date', 'excerpt', 'password', 'post-content-info', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
wp_unregister_entity_fields( 'postType', 'wp_template', array( 'author', 'date', 'excerpt', 'password', 'post-content-info', 'publish_date', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
wp_register_entity_field_collection( __DIR__ . '/wp_template' );

// wp_template_part: unregister unwanted default fields, and register its own.
wp_unregister_entity_fields( 'postType', 'wp_template_part', array( 'author', 'date', 'excerpt', 'password', 'post-content-info', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
wp_unregister_entity_fields( 'postType', 'wp_template_part', array( 'author', 'date', 'excerpt', 'password', 'post-content-info', 'publish_date', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
wp_register_entity_field_collection( __DIR__ . '/wp_template_part' );

// wp_block: unregister unwanted default fields, and register its own.
wp_unregister_entity_fields( 'postType', 'wp_block', array( 'date', 'excerpt', 'password', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
wp_unregister_entity_fields( 'postType', 'wp_block', array( 'date', 'excerpt', 'password', 'publish_date', 'scheduled_date', 'slug', 'status', 'template', 'title' ) );
wp_register_entity_field_collection( __DIR__ . '/wp_block' );
// Patterns are sorted client-side, where the author's ID isn't a useful
// order, like the template authors.
Expand All @@ -141,7 +143,7 @@ function wp_register_core_field_collections() {
wp_register_entity_field_collection( __DIR__ . '/attachment' );

// wp_navigation: unregister unwanted default fields.
wp_unregister_entity_fields( 'postType', 'wp_navigation', array( 'date', 'password', 'post-content-info', 'scheduled_date', 'slug', 'status', 'template' ) );
wp_unregister_entity_fields( 'postType', 'wp_navigation', array( 'date', 'password', 'post-content-info', 'publish_date', 'scheduled_date', 'slug', 'status', 'template' ) );

// Register fields for root/site (entity/kind).
wp_register_entity_field_collection( __DIR__ . '/root_site' );
Expand Down
2 changes: 2 additions & 0 deletions packages/core-fields/src/post_type_supports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { fieldExtensions as parent } from './parent/field';
import { fieldExtensions as password } from './password/field';
import { fieldExtensions as pingStatus } from './ping_status/field';
import { fieldExtensions as postContentInfo } from './post_content_info/field';
import { fieldExtensions as publishDate } from './publish_date/field';
import { fieldExtensions as scheduledDate } from './scheduled_date/field';
import { fieldExtensions as slug } from './slug/field';
import { fieldExtensions as status } from './status/field';
Expand All @@ -42,6 +43,7 @@ const fields: FieldsScriptParts = {
password,
ping_status: pingStatus,
'post-content-info': postContentInfo,
publish_date: publishDate,
scheduled_date: scheduledDate,
slug,
status,
Expand Down
21 changes: 21 additions & 0 deletions packages/core-fields/src/post_type_supports/publish_date/field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* The publish date of a post, as a form summarizes it, for every post type
* but the design ones. It reads the date of the post, so its JavaScript parts
* (`getValue` and `render`) are in `field.tsx`, next to this file.
*
* It is only meant as the summary of the `date` field in a form panel, so
* lists can't show, sort, or filter by it: they keep the `date` field, whose
* value is never "Immediately".
*
* @package WordPress
*/

return array(
'type' => 'datetime',
'label' => __( 'Publish date' ),
'readOnly' => true,
'enableHiding' => false,
'enableSorting' => false,
'filterBy' => false,
);
12 changes: 12 additions & 0 deletions packages/core-fields/src/post_type_supports/publish_date/field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { FieldsScriptParts } from '../../types';
import type { PostWithDate } from '../date/types';
import PublishDateView from './view';

/**
* The JavaScript parts of the publish date field; its data is in
* `field.php`.
*/
export const fieldExtensions: FieldsScriptParts< PostWithDate >[ string ] = {
getValue: ( { item } ) => item.date,
render: PublishDateView,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { __, _x, isRTL, sprintf } from '@wordpress/i18n';
import { dateI18n, getDate, getSettings } from '@wordpress/date';

/*
* The publish date wording of the post summary, as the editor's
* `getPostScheduleLabel()` and `getFullPostScheduleLabel()` have it. This
* script module can't import the editor, so the strings keep their source and
* context to share its translations.
*/
Comment on lines +4 to +9

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.

This file matches packages/editor/src/components/post-schedule/label.js (how labels are computed in the old inspector) line by line.


/**
* The full publish date with the site's timezone, e.g. "November 5, 2026
* 9:24 am UTC+0".
*
* @param dateAttribute The post date.
* @return The full label.
*/
export function getFullPublishDateLabel( dateAttribute: string | null ) {
const date = getDate( dateAttribute );

const timezoneAbbreviation = getTimezoneAbbreviation();
const formattedDate = dateI18n(
// translators: Use a non-breaking space between 'g:i' and 'a' if appropriate.
_x( 'F j, Y g:i\xa0a', 'post schedule full date format' ),
date
);
return isRTL()
? `${ timezoneAbbreviation } ${ formattedDate }`
: `${ formattedDate } ${ timezoneAbbreviation }`;
}

/**
* The publish date in words, the first that applies:
*
* 1. "Immediately", for a reset or floating date;
* 2. the full date with the site's timezone, when the user's timezone differs
* from the site's, as "today" or "tomorrow" would be ambiguous;
* 3. "Today at 9:24 am";
* 4. "Tomorrow at 9:24 am";
* 5. "November 12 9:24 am", within the current year;
* 6. "November 12, 2027 9:24 am", otherwise.
*
* @param dateAttribute The post date.
* @param options Options.
* @param options.isFloating Whether the date floats, see isDateFloating().
* @param options.now The current date, for tests.
* @return The label.
*/
export function getPublishDateLabel(
dateAttribute: string | null | undefined,
{ isFloating = false, now = new Date() } = {}
) {
if ( ! dateAttribute || isFloating ) {
return __( 'Immediately' );
}

if ( ! isTimezoneSameAsSiteTimezone( now ) ) {
return getFullPublishDateLabel( dateAttribute );
}

const date = getDate( dateAttribute );

if ( isSameDay( date, now ) ) {
return sprintf(
// translators: %s: Time of day the post is scheduled for.
__( 'Today at %s' ),
// translators: If using a space between 'g:i' and 'a', use a non-breaking space.
dateI18n( _x( 'g:i\xa0a', 'post schedule time format' ), date )
);
}

const tomorrow = new Date( now );
tomorrow.setDate( tomorrow.getDate() + 1 );

if ( isSameDay( date, tomorrow ) ) {
return sprintf(
// translators: %s: Time of day the post is scheduled for.
__( 'Tomorrow at %s' ),
// translators: If using a space between 'g:i' and 'a', use a non-breaking space.
dateI18n( _x( 'g:i\xa0a', 'post schedule time format' ), date )
);
}

if ( date.getFullYear() === now.getFullYear() ) {
return dateI18n(
// translators: If using a space between 'g:i' and 'a', use a non-breaking space.
_x( 'F j g:i\xa0a', 'post schedule date format without year' ),
date
);
}

return dateI18n(
// translators: Use a non-breaking space between 'g:i' and 'a' if appropriate.
_x( 'F j, Y g:i\xa0a', 'post schedule full date format' ),
date
);
}

function getTimezoneAbbreviation() {
const { timezone } = getSettings();

if ( timezone.abbr && isNaN( Number( timezone.abbr ) ) ) {
return timezone.abbr;
}

const symbol = Number( timezone.offset ) < 0 ? '' : '+';
return `UTC${ symbol }${ timezone.offsetFormatted }`;
}

function isTimezoneSameAsSiteTimezone( date: Date ) {
const { timezone } = getSettings();

const siteOffset = Number( timezone.offset );
const dateOffset = -1 * ( date.getTimezoneOffset() / 60 );
return siteOffset === dateOffset;
}

function isSameDay( left: Date, right: Date ) {
return (
left.getDate() === right.getDate() &&
left.getMonth() === right.getMonth() &&
left.getFullYear() === right.getFullYear()
);
}
53 changes: 53 additions & 0 deletions packages/core-fields/src/post_type_supports/publish_date/view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import type { PostWithDate } from '../date/types';
import { isDateFloating } from '../date/is-date-floating';
import { getPublishDateLabel } from './get-publish-date-label';

interface PostWithDateAndId extends PostWithDate {
id?: number;
type?: string;
}

/*
* The publish date of a post in words, as the post summary has it: see
* getPublishDateLabel().
*/
export default function PublishDateView( {
item,
}: {
item: PostWithDateAndId;
} ) {
// The saved status, not the edited one: a status picked in the same form
// doesn't change whether the date floats until the post is saved.
const savedStatus = useSelect(
( select ) => {
if ( ! item.type || ! item.id ) {
return undefined;
}
const saved: { status?: string } | undefined = select(
coreStore
).getEntityRecord( 'postType', item.type, item.id );
return saved?.status;
},
[ item.type, item.id ]
);

// A bulk edit passes only the edits made so far, so a date it hasn't
// touched is absent: each post keeps its own, and none reads
// "Immediately". A reset date is `null`, not absent.
if ( item.date === undefined ) {
return null;
}

return (
<>
{ getPublishDateLabel( item.date, {
isFloating: isDateFloating( {
...item,
status: savedStatus ?? item.status,
} ),
} ) }
</>
);
}
Loading
Loading