6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Function that recursively renders a list of nested comments. |
9 * Function that recursively renders a list of nested comments. |
10 * |
10 * |
|
11 * @since 6.3.0 Changed render_block_context priority to `1`. |
|
12 * |
11 * @global int $comment_depth |
13 * @global int $comment_depth |
12 * |
14 * |
13 * @param WP_Comment[] $comments The array of comments. |
15 * @param WP_Comment[] $comments The array of comments. |
14 * @param WP_Block $block Block instance. |
16 * @param WP_Block $block Block instance. |
15 * @return string |
17 * @return string |
16 */ |
18 */ |
17 function block_core_comment_template_render_comments( $comments, $block ) { |
19 function block_core_comment_template_render_comments( $comments, $block ) { |
18 global $comment_depth; |
20 global $comment_depth; |
|
21 $thread_comments = get_option( 'thread_comments' ); |
|
22 $thread_comments_depth = get_option( 'thread_comments_depth' ); |
19 |
23 |
20 if ( empty( $comment_depth ) ) { |
24 if ( empty( $comment_depth ) ) { |
21 $comment_depth = 1; |
25 $comment_depth = 1; |
22 } |
26 } |
23 |
27 |
24 $content = ''; |
28 $content = ''; |
25 foreach ( $comments as $comment ) { |
29 foreach ( $comments as $comment ) { |
|
30 $comment_id = $comment->comment_ID; |
|
31 $filter_block_context = static function ( $context ) use ( $comment_id ) { |
|
32 $context['commentId'] = $comment_id; |
|
33 return $context; |
|
34 }; |
26 |
35 |
27 $block_content = ( new WP_Block( |
36 /* |
28 $block->parsed_block, |
37 * We set commentId context through the `render_block_context` filter so |
29 array( |
38 * that dynamically inserted blocks (at `render_block` filter stage) |
30 'commentId' => $comment->comment_ID, |
39 * will also receive that context. |
31 ) |
40 * |
32 ) )->render( array( 'dynamic' => false ) ); |
41 * Use an early priority to so that other 'render_block_context' filters |
|
42 * have access to the values. |
|
43 */ |
|
44 add_filter( 'render_block_context', $filter_block_context, 1 ); |
|
45 |
|
46 /* |
|
47 * We construct a new WP_Block instance from the parsed block so that |
|
48 * it'll receive any changes made by the `render_block_data` filter. |
|
49 */ |
|
50 $block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) ); |
|
51 |
|
52 remove_filter( 'render_block_context', $filter_block_context, 1 ); |
33 |
53 |
34 $children = $comment->get_children(); |
54 $children = $comment->get_children(); |
35 |
55 |
36 /* |
56 /* |
37 * We need to create the CSS classes BEFORE recursing into the children. |
57 * We need to create the CSS classes BEFORE recursing into the children. |
44 */ |
64 */ |
45 $comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); |
65 $comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); |
46 |
66 |
47 // If the comment has children, recurse to create the HTML for the nested |
67 // If the comment has children, recurse to create the HTML for the nested |
48 // comments. |
68 // comments. |
49 if ( ! empty( $children ) ) { |
69 if ( ! empty( $children ) && ! empty( $thread_comments ) ) { |
50 $comment_depth += 1; |
70 if ( $comment_depth < $thread_comments_depth ) { |
51 $inner_content = block_core_comment_template_render_comments( |
71 ++$comment_depth; |
52 $children, |
72 $inner_content = block_core_comment_template_render_comments( |
53 $block |
73 $children, |
54 ); |
74 $block |
55 $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); |
75 ); |
56 $comment_depth -= 1; |
76 $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); |
|
77 --$comment_depth; |
|
78 } else { |
|
79 $block_content .= block_core_comment_template_render_comments( |
|
80 $children, |
|
81 $block |
|
82 ); |
|
83 } |
57 } |
84 } |
58 |
85 |
59 $content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); |
86 $content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); |
60 } |
87 } |
61 |
88 |
62 return $content; |
89 return $content; |
63 |
|
64 } |
90 } |
65 |
91 |
66 /** |
92 /** |
67 * Renders the `core/comment-template` block on the server. |
93 * Renders the `core/comment-template` block on the server. |
|
94 * |
|
95 * @since 6.0.0 |
68 * |
96 * |
69 * @param array $attributes Block attributes. |
97 * @param array $attributes Block attributes. |
70 * @param string $content Block default content. |
98 * @param string $content Block default content. |
71 * @param WP_Block $block Block instance. |
99 * @param WP_Block $block Block instance. |
72 * |
100 * |