| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/comment-date` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/comment-date` block on the server. |
| 10 |
* |
| 11 |
* @since 6.0.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 Return the post comment's date. |
| 17 |
*/ |
| 18 |
function gutenberg_render_block_core_comment_date( $attributes, $content, $block ) { |
| 19 |
if ( ! isset( $block->context['commentId'] ) ) { |
| 20 |
return ''; |
| 21 |
} |
| 22 |
|
| 23 |
$comment = get_comment( $block->context['commentId'] ); |
| 24 |
if ( empty( $comment ) ) { |
| 25 |
return ''; |
| 26 |
} |
| 27 |
|
| 28 |
$classes = ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) ? 'has-link-color' : ''; |
| 29 |
|
| 30 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
| 31 |
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { |
| 32 |
// translators: %s: human-readable time difference. |
| 33 |
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( get_comment_date( 'U', $comment ) ) ); |
| 34 |
} else { |
| 35 |
$formatted_date = get_comment_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $comment ); |
| 36 |
} |
| 37 |
$link = get_comment_link( $comment ); |
| 38 |
|
| 39 |
if ( ! empty( $attributes['isLink'] ) ) { |
| 40 |
$formatted_date = sprintf( '<a href="%1s">%2s</a>', esc_url( $link ), $formatted_date ); |
| 41 |
} |
| 42 |
|
| 43 |
return sprintf( |
| 44 |
'<div %1$s><time datetime="%2$s">%3$s</time></div>', |
| 45 |
$wrapper_attributes, |
| 46 |
esc_attr( get_comment_date( 'c', $comment ) ), |
| 47 |
$formatted_date |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Registers the `core/comment-date` block on the server. |
| 53 |
* |
| 54 |
* @since 6.0.0 |
| 55 |
*/ |
| 56 |
function gutenberg_register_block_core_comment_date() { |
| 57 |
register_block_type_from_metadata( |
| 58 |
__DIR__ . '/comment-date', |
| 59 |
array( |
| 60 |
'render_callback' => 'gutenberg_render_block_core_comment_date', |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
add_action( 'init', 'gutenberg_register_block_core_comment_date', 20 ); |
| 65 |
|