author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
19 | 1 |
<?php |
2 |
/** |
|
3 |
* Server-side rendering of the `core/comment-template` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
9 |
* Function that recursively renders a list of nested comments. |
|
10 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 6.3.0 Changed render_block_context priority to `1`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
19 | 13 |
* @global int $comment_depth |
14 |
* |
|
15 |
* @param WP_Comment[] $comments The array of comments. |
|
16 |
* @param WP_Block $block Block instance. |
|
17 |
* @return string |
|
18 |
*/ |
|
19 |
function block_core_comment_template_render_comments( $comments, $block ) { |
|
20 |
global $comment_depth; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
$thread_comments = get_option( 'thread_comments' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
$thread_comments_depth = get_option( 'thread_comments_depth' ); |
19 | 23 |
|
24 |
if ( empty( $comment_depth ) ) { |
|
25 |
$comment_depth = 1; |
|
26 |
} |
|
27 |
||
28 |
$content = ''; |
|
29 |
foreach ( $comments as $comment ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
$comment_id = $comment->comment_ID; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
$filter_block_context = static function ( $context ) use ( $comment_id ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
$context['commentId'] = $comment_id; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
return $context; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
}; |
19 | 35 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
36 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
37 |
* We set commentId context through the `render_block_context` filter so |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
38 |
* that dynamically inserted blocks (at `render_block` filter stage) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
39 |
* will also receive that context. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
40 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
41 |
* Use an early priority to so that other 'render_block_context' filters |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
* have access to the values. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
43 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
44 |
add_filter( 'render_block_context', $filter_block_context, 1 ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
46 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
47 |
* We construct a new WP_Block instance from the parsed block so that |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
48 |
* it'll receive any changes made by the `render_block_data` filter. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
$block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
remove_filter( 'render_block_context', $filter_block_context, 1 ); |
19 | 53 |
|
54 |
$children = $comment->get_children(); |
|
55 |
||
56 |
/* |
|
57 |
* We need to create the CSS classes BEFORE recursing into the children. |
|
58 |
* This is because comment_class() uses globals like `$comment_alt` |
|
59 |
* and `$comment_thread_alt` which are order-sensitive. |
|
60 |
* |
|
61 |
* The `false` parameter at the end means that we do NOT want the function |
|
62 |
* to `echo` the output but to return a string. |
|
63 |
* See https://developer.wordpress.org/reference/functions/comment_class/#parameters. |
|
64 |
*/ |
|
65 |
$comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); |
|
66 |
||
67 |
// If the comment has children, recurse to create the HTML for the nested |
|
68 |
// comments. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
if ( ! empty( $children ) && ! empty( $thread_comments ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
if ( $comment_depth < $thread_comments_depth ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
++$comment_depth; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
$inner_content = block_core_comment_template_render_comments( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
$children, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
$block |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
$block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
--$comment_depth; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
$block_content .= block_core_comment_template_render_comments( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
$children, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
81 |
$block |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
} |
19 | 84 |
} |
85 |
||
86 |
$content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); |
|
87 |
} |
|
88 |
||
89 |
return $content; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Renders the `core/comment-template` block on the server. |
|
94 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
* @since 6.0.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
* |
19 | 97 |
* @param array $attributes Block attributes. |
98 |
* @param string $content Block default content. |
|
99 |
* @param WP_Block $block Block instance. |
|
100 |
* |
|
101 |
* @return string Returns the HTML representing the comments using the layout |
|
102 |
* defined by the block's inner blocks. |
|
103 |
*/ |
|
104 |
function render_block_core_comment_template( $attributes, $content, $block ) { |
|
105 |
// Bail out early if the post ID is not set for some reason. |
|
106 |
if ( empty( $block->context['postId'] ) ) { |
|
107 |
return ''; |
|
108 |
} |
|
109 |
||
110 |
if ( post_password_required( $block->context['postId'] ) ) { |
|
111 |
return; |
|
112 |
} |
|
113 |
||
114 |
$comment_query = new WP_Comment_Query( |
|
115 |
build_comment_query_vars_from_block( $block ) |
|
116 |
); |
|
117 |
||
118 |
// Get an array of comments for the current post. |
|
119 |
$comments = $comment_query->get_comments(); |
|
120 |
if ( count( $comments ) === 0 ) { |
|
121 |
return ''; |
|
122 |
} |
|
123 |
||
124 |
$comment_order = get_option( 'comment_order' ); |
|
125 |
||
126 |
if ( 'desc' === $comment_order ) { |
|
127 |
$comments = array_reverse( $comments ); |
|
128 |
} |
|
129 |
||
130 |
$wrapper_attributes = get_block_wrapper_attributes(); |
|
131 |
||
132 |
return sprintf( |
|
133 |
'<ol %1$s>%2$s</ol>', |
|
134 |
$wrapper_attributes, |
|
135 |
block_core_comment_template_render_comments( $comments, $block ) |
|
136 |
); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Registers the `core/comment-template` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
* @since 6.0.0 |
19 | 143 |
*/ |
144 |
function register_block_core_comment_template() { |
|
145 |
register_block_type_from_metadata( |
|
146 |
__DIR__ . '/comment-template', |
|
147 |
array( |
|
148 |
'render_callback' => 'render_block_core_comment_template', |
|
149 |
'skip_inner_blocks' => true, |
|
150 |
) |
|
151 |
); |
|
152 |
} |
|
153 |
add_action( 'init', 'register_block_core_comment_template' ); |