From faa4899055e3d4a0ca6b551497aa6348c65db645 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Thu, 20 Apr 2023 10:25:07 +0200 Subject: [PATCH] Only display the modified post date if the post has been modified (#46839) * Only display the modified post date if the post has been modified * Add wp-block-post-date__modified-date to the $classes array * Update index.php * Update packages/block-library/src/post-date/index.php Co-authored-by: Ari Stathopoulos --------- Co-authored-by: Ari Stathopoulos --- packages/block-library/src/post-date/edit.js | 4 +++ .../block-library/src/post-date/index.php | 25 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/post-date/edit.js b/packages/block-library/src/post-date/edit.js index d6b8cb2b15ae57..f8cd268358d49f 100644 --- a/packages/block-library/src/post-date/edit.js +++ b/packages/block-library/src/post-date/edit.js @@ -37,6 +37,7 @@ export default function PostDateEdit( { const blockProps = useBlockProps( { className: classnames( { [ `has-text-align-${ textAlign }` ]: textAlign, + [ `wp-block-post-date__modified-date` ]: displayType === 'modified', } ), } ); @@ -175,6 +176,9 @@ export default function PostDateEdit( { } ) } checked={ displayType === 'modified' } + help={ __( + 'Only shows if the post has been modified' + ) } /> diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 5c20ee30c2b398..b59db80b242cd1 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -18,25 +18,34 @@ function render_block_core_post_date( $attributes, $content, $block ) { return ''; } - $post_ID = $block->context['postId']; + $post_ID = $block->context['postId']; + $formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); + $unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) ); + $classes = array(); - $classes = array(); if ( isset( $attributes['textAlign'] ) ) { $classes[] = 'has-text-align-' . $attributes['textAlign']; } if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { $classes[] = 'has-link-color'; } - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); + /* + * If the "Display last modified date" setting is enabled, + * only display the modified date if it is later than the publishing date. + */ if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { - $formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); - $unformatted_date = esc_attr( get_the_modified_date( 'c', $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 ) ); + 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 ); + $unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) ); + $classes[] = 'wp-block-post-date__modified-date'; + } else { + return ''; + } } + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); + if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { $formatted_date = sprintf( '%2s', get_the_permalink( $post_ID ), $formatted_date ); }