| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-date` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/post-date` block on the server. |
| 10 |
* |
| 11 |
* @since 5.8.0 |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* @param string $content Block default content. |
| 15 |
* @param WP_Block $block Block instance. |
| 16 |
* @return string Returns the filtered post date for the current post wrapped inside "time" tags. |
| 17 |
*/ |
| 18 |
function gutenberg_render_block_core_post_date( $attributes, $content, $block ) { |
| 19 |
if ( ! isset( $block->context['postId'] ) ) { |
| 20 |
return ''; |
| 21 |
} |
| 22 |
|
| 23 |
$post_ID = $block->context['postId']; |
| 24 |
|
| 25 |
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { |
| 26 |
$post_timestamp = get_post_timestamp( $post_ID ); |
| 27 |
if ( $post_timestamp > time() ) { |
| 28 |
// translators: %s: human-readable time difference. |
| 29 |
$formatted_date = sprintf( __( '%s from now', 'gutenberg' ), human_time_diff( $post_timestamp ) ); |
| 30 |
} else { |
| 31 |
// translators: %s: human-readable time difference. |
| 32 |
$formatted_date = sprintf( __( '%s ago', 'gutenberg' ), human_time_diff( $post_timestamp ) ); |
| 33 |
} |
| 34 |
} else { |
| 35 |
$formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); |
| 36 |
} |
| 37 |
$unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) ); |
| 38 |
$classes = array(); |
| 39 |
|
| 40 |
if ( isset( $attributes['textAlign'] ) ) { |
| 41 |
$classes[] = 'has-text-align-' . $attributes['textAlign']; |
| 42 |
} |
| 43 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
| 44 |
$classes[] = 'has-link-color'; |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* If the "Display last modified date" setting is enabled, |
| 49 |
* only display the modified date if it is later than the publishing date. |
| 50 |
*/ |
| 51 |
if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { |
| 52 |
if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) { |
| 53 |
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { |
| 54 |
// translators: %s: human-readable time difference. |
| 55 |
$formatted_date = sprintf( __( '%s ago', 'gutenberg' ), human_time_diff( get_post_timestamp( $post_ID, 'modified' ) ) ); |
| 56 |
} else { |
| 57 |
$formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); |
| 58 |
} |
| 59 |
$unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) ); |
| 60 |
$classes[] = 'wp-block-post-date__modified-date'; |
| 61 |
} else { |
| 62 |
return ''; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
| 67 |
|
| 68 |
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
| 69 |
$formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date ); |
| 70 |
} |
| 71 |
|
| 72 |
return sprintf( |
| 73 |
'<div %1$s><time datetime="%2$s">%3$s</time></div>', |
| 74 |
$wrapper_attributes, |
| 75 |
$unformatted_date, |
| 76 |
$formatted_date |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Registers the `core/post-date` block on the server. |
| 82 |
* |
| 83 |
* @since 5.8.0 |
| 84 |
*/ |
| 85 |
function gutenberg_register_block_core_post_date() { |
| 86 |
register_block_type_from_metadata( |
| 87 |
__DIR__ . '/post-date', |
| 88 |
array( |
| 89 |
'render_callback' => 'gutenberg_render_block_core_post_date', |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
add_action( 'init', 'gutenberg_register_block_core_post_date', 20 ); |
| 94 |
|