Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only display the modified post date if the post has been modified #46839

Merged
merged 7 commits into from
Apr 20, 2023
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 @@ -172,6 +173,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( 'Y-m-d h:i', $post_ID ) > get_the_date( 'Y-m-d h:i', $post_ID ) ) {
carolinan marked this conversation as resolved.
Show resolved Hide resolved
$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