Skip to content

Commit

Permalink
Add support for relative dates to Comment Date block
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jun 12, 2024
1 parent c8391ed commit 2dcb4fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
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

0 comments on commit 2dcb4fd

Please sign in to comment.