This commit is contained in:
Hanson.xyz Dev
2026-01-04 17:50:08 -06:00
parent 7e45ce0756
commit acc8ac87a0
4131 changed files with 232562 additions and 250244 deletions
+43 -27
View File
@@ -9,6 +9,7 @@
* Renders the `core/post-date` block on the server.
*
* @since 5.8.0
* @since 6.9.0 Added `datetime` attribute and Block Bindings support.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
@@ -16,14 +17,49 @@
* @return string Returns the filtered post date for the current post wrapped inside "time" tags.
*/
function render_block_core_post_date( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
$classes = array();
if (
! isset( $attributes['datetime'] ) && ! (
isset( $attributes['metadata']['bindings']['datetime']['source'] ) &&
isset( $attributes['metadata']['bindings']['datetime']['args'] )
)
) {
/*
* This is the legacy version of the block that didn't have the `datetime` attribute.
* This branch needs to be kept for backward compatibility.
*/
$source = get_block_bindings_source( 'core/post-data' );
if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {
$source_args = array(
'field' => 'modified',
);
} else {
$source_args = array(
'field' => 'date',
);
}
$attributes['datetime'] = $source->get_value( $source_args, $block, 'datetime' );
}
if ( isset( $source_args['field'] ) && 'modified' === $source_args['field'] ) {
$classes[] = 'wp-block-post-date__modified-date';
}
if ( empty( $attributes['datetime'] ) ) {
// If the `datetime` attribute is set but empty, it could be because Block Bindings
// set it that way. This can happen e.g. if the block is bound to the
// post's last modified date, and the latter lies before the publish date.
// (See https://github.com/WordPress/gutenberg/pull/46839 where this logic was originally
// implemented.)
// In this case, we have to respect and return the empty value.
return '';
}
$post_ID = $block->context['postId'];
$unformatted_date = $attributes['datetime'];
$post_timestamp = strtotime( $unformatted_date );
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {
$post_timestamp = get_post_timestamp( $post_ID );
if ( $post_timestamp > time() ) {
// translators: %s: human-readable time difference.
$formatted_date = sprintf( __( '%s from now' ), human_time_diff( $post_timestamp ) );
@@ -32,10 +68,9 @@ function render_block_core_post_date( $attributes, $content, $block ) {
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( $post_timestamp ) );
}
} else {
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
$format = empty( $attributes['format'] ) ? get_option( 'date_format' ) : $attributes['format'];
$formatted_date = wp_date( $format, $post_timestamp );
}
$unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) );
$classes = array();
if ( isset( $attributes['textAlign'] ) ) {
$classes[] = 'has-text-align-' . $attributes['textAlign'];
@@ -44,29 +79,10 @@ function render_block_core_post_date( $attributes, $content, $block ) {
$classes[] = 'has-link-color';
}
/*
* 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'] ) {
if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) {
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {
// translators: %s: human-readable time difference.
$formatted_date = sprintf( __( '%s ago' ), 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 {
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 );
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && isset( $block->context['postId'] ) ) {
$formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $block->context['postId'] ), $formatted_date );
}
return sprintf(