author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
19 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
19 | 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 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 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
$classes = ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) ? 'has-link-color' : ''; |
19 | 29 |
|
30 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
31 |
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
32 |
// translators: %s: human-readable time difference. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
33 |
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( get_comment_date( 'U', $comment ) ) ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
34 |
} else { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
35 |
$formatted_date = get_comment_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $comment ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
36 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
37 |
$link = get_comment_link( $comment ); |
19 | 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. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
* @since 6.0.0 |
19 | 55 |
*/ |
56 |
function register_block_core_comment_date() { |
|
57 |
register_block_type_from_metadata( |
|
58 |
__DIR__ . '/comment-date', |
|
59 |
array( |
|
60 |
'render_callback' => 'render_block_core_comment_date', |
|
61 |
) |
|
62 |
); |
|
63 |
} |
|
64 |
add_action( 'init', 'register_block_core_comment_date' ); |