Skip to content

Commit

Permalink
Post Date: Adds relative date format (WordPress#62298)
Browse files Browse the repository at this point in the history
* Adds "Time Ago" option to SelectControl of DateFormatPicker

* Update post-date block to have "Time ago" format

* Use 'human-diff' as format for relative dates

* Add support for relative dates to Comment Date block

* Always use a somewhat recent example date

---------

Co-authored-by: amitraj2203 <amitraj2203@git.wordpress.org>
Co-authored-by: noisysocks <noisysocks@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
  • Loading branch information
4 people authored and patil-vipul committed Jun 17, 2024
1 parent edb3d97 commit 2a0f9e8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
38 changes: 25 additions & 13 deletions packages/block-editor/src/components/date-format-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { _x, __ } from '@wordpress/i18n';
import { dateI18n } from '@wordpress/date';
import { dateI18n, humanTimeDiff } from '@wordpress/date';
import { useState, createInterpolateElement } from '@wordpress/element';
import {
TextControl,
Expand All @@ -13,11 +13,15 @@ import {
__experimentalVStack as VStack,
} from '@wordpress/components';

// So that we can illustrate the different formats in the dropdown properly,
// show a date that has a day greater than 12 and a month with more than three
// letters. Here we're using 2022-01-25 which is when WordPress 5.9 was
// released.
const EXAMPLE_DATE = new Date( 2022, 0, 25 );
// So that we illustrate the different formats in the dropdown properly, show a date that is
// somwhat recent, has a day greater than 12, and a month with more than three letters.
const exampleDate = new Date();
exampleDate.setDate( 20 );
exampleDate.setMonth( exampleDate.getMonth() - 3 );
if ( exampleDate.getMonth() === 4 ) {
// May has three letters, so use March.
exampleDate.setMonth( 3 );
}

/**
* The `DateFormatPicker` component renders controls that let the user choose a
Expand Down Expand Up @@ -54,7 +58,7 @@ export default function DateFormatPicker( {
label={ __( 'Default format' ) }
help={ `${ __( 'Example:' ) } ${ dateI18n(
defaultFormat,
EXAMPLE_DATE
exampleDate
) }` }
checked={ ! format }
onChange={ ( checked ) =>
Expand Down Expand Up @@ -95,13 +99,19 @@ function NonDefaultControls( { format, onChange } ) {
] ),
];

const suggestedOptions = suggestedFormats.map(
( suggestedFormat, index ) => ( {
const suggestedOptions = [
...suggestedFormats.map( ( suggestedFormat, index ) => ( {
key: `suggested-${ index }`,
name: dateI18n( suggestedFormat, EXAMPLE_DATE ),
name: dateI18n( suggestedFormat, exampleDate ),
format: suggestedFormat,
} )
);
} ) ),
{
key: 'human-diff',
name: humanTimeDiff( exampleDate ),
format: 'human-diff',
},
];

const customOption = {
key: 'custom',
name: __( 'Custom' ),
Expand All @@ -111,7 +121,9 @@ function NonDefaultControls( { format, onChange } ) {
};

const [ isCustom, setIsCustom ] = useState(
() => !! format && ! suggestedFormats.includes( format )
() =>
!! format &&
! suggestedOptions.some( ( option ) => option.format === format )
);

return (
Expand Down
10 changes: 8 additions & 2 deletions packages/block-library/src/comment-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { dateI18n, getSettings as getDateSettings } from '@wordpress/date';
import {
dateI18n,
humanTimeDiff,
getSettings as getDateSettings,
} from '@wordpress/date';
import {
InspectorControls,
useBlockProps,
Expand Down Expand Up @@ -64,7 +68,9 @@ export default function Edit( {
let commentDate =
date instanceof Date ? (
<time dateTime={ dateI18n( 'c', date ) }>
{ dateI18n( format || siteFormat, date ) }
{ format === 'human-diff'
? humanTimeDiff( date )
: dateI18n( format || siteFormat, date ) }
</time>
) : (
<time>{ date }</time>
Expand Down
12 changes: 7 additions & 5 deletions packages/block-library/src/comment-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ function render_block_core_comment_date( $attributes, $content, $block ) {
$classes = ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) ? 'has-link-color' : '';

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
$formatted_date = get_comment_date(
isset( $attributes['format'] ) ? $attributes['format'] : '',
$comment
);
$link = get_comment_link( $comment );
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {
// translators: %s: human-readable time difference.
$formatted_date = sprintf( __( '%s ago', 'gutenberg' ), human_time_diff( get_comment_date( 'U', $comment ) ) );
} else {
$formatted_date = get_comment_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $comment );
}
$link = get_comment_link( $comment );

if ( ! empty( $attributes['isLink'] ) ) {
$formatted_date = sprintf( '<a href="%1s">%2s</a>', esc_url( $link ), $formatted_date );
Expand Down
10 changes: 8 additions & 2 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import clsx from 'clsx';
*/
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useMemo, useState } from '@wordpress/element';
import { dateI18n, getSettings as getDateSettings } from '@wordpress/date';
import {
dateI18n,
humanTimeDiff,
getSettings as getDateSettings,
} from '@wordpress/date';
import {
AlignmentControl,
BlockControls,
Expand Down Expand Up @@ -82,7 +86,9 @@ export default function PostDateEdit( {

let postDate = date ? (
<time dateTime={ dateI18n( 'c', date ) } ref={ setPopoverAnchor }>
{ dateI18n( format || siteFormat, date ) }
{ format === 'human-diff'
? humanTimeDiff( date )
: dateI18n( format || siteFormat, date ) }
</time>
) : (
dateLabel
Expand Down
17 changes: 14 additions & 3 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ function render_block_core_post_date( $attributes, $content, $block ) {
return '';
}

$post_ID = $block->context['postId'];
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
$post_ID = $block->context['postId'];

if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {
// translators: %s: human-readable time difference.
$formatted_date = sprintf( __( '%s ago', 'gutenberg' ), human_time_diff( get_post_timestamp( $post_ID ) ) );
} else {
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
}
$unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) );
$classes = array();

Expand All @@ -38,7 +44,12 @@ function render_block_core_post_date( $attributes, $content, $block ) {
*/
if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {
if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) {
$formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
if ( 'human-diff' === $attributes['format'] ) {
// translators: %s: human-readable time difference.
$formatted_date = sprintf( __( '%s ago', 'gutenberg' ), human_time_diff( get_post_timestamp( $post_ID, 'modified' ) ) );
} else {
$formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
}
$unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) );
$classes[] = 'wp-block-post-date__modified-date';
} else {
Expand Down

0 comments on commit 2a0f9e8

Please sign in to comment.