Skip to content

Commit

Permalink
Only display the modified post date if the post has been modified (#4…
Browse files Browse the repository at this point in the history
…6839)

* 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 <aristath@gmail.com>

---------

Co-authored-by: Ari Stathopoulos <aristath@gmail.com>
  • Loading branch information
carolinan and aristath authored Apr 20, 2023
1 parent f20e276 commit faa4899
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
} ),
} );

Expand Down Expand Up @@ -175,6 +176,9 @@ export default function PostDateEdit( {
} )
}
checked={ displayType === 'modified' }
help={ __(
'Only shows if the post has been modified'
) }
/>
</PanelBody>
</InspectorControls>
Expand Down
25 changes: 17 additions & 8 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
}
Expand Down

0 comments on commit faa4899

Please sign in to comment.