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 |
* |
|
11 |
* @global int $comment_depth |
|
12 |
* |
|
13 |
* @param WP_Comment[] $comments The array of comments. |
|
14 |
* @param WP_Block $block Block instance. |
|
15 |
* @return string |
|
16 |
*/ |
|
17 |
function block_core_comment_template_render_comments( $comments, $block ) { |
|
18 |
global $comment_depth; |
|
19 |
|
|
20 |
if ( empty( $comment_depth ) ) { |
|
21 |
$comment_depth = 1; |
|
22 |
} |
|
23 |
|
|
24 |
$content = ''; |
|
25 |
foreach ( $comments as $comment ) { |
|
26 |
|
|
27 |
$block_content = ( new WP_Block( |
|
28 |
$block->parsed_block, |
|
29 |
array( |
|
30 |
'commentId' => $comment->comment_ID, |
|
31 |
) |
|
32 |
) )->render( array( 'dynamic' => false ) ); |
|
33 |
|
|
34 |
$children = $comment->get_children(); |
|
35 |
|
|
36 |
/* |
|
37 |
* We need to create the CSS classes BEFORE recursing into the children. |
|
38 |
* This is because comment_class() uses globals like `$comment_alt` |
|
39 |
* and `$comment_thread_alt` which are order-sensitive. |
|
40 |
* |
|
41 |
* The `false` parameter at the end means that we do NOT want the function |
|
42 |
* to `echo` the output but to return a string. |
|
43 |
* See https://developer.wordpress.org/reference/functions/comment_class/#parameters. |
|
44 |
*/ |
|
45 |
$comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); |
|
46 |
|
|
47 |
// If the comment has children, recurse to create the HTML for the nested |
|
48 |
// comments. |
|
49 |
if ( ! empty( $children ) ) { |
|
50 |
$comment_depth += 1; |
|
51 |
$inner_content = block_core_comment_template_render_comments( |
|
52 |
$children, |
|
53 |
$block |
|
54 |
); |
|
55 |
$block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); |
|
56 |
$comment_depth -= 1; |
|
57 |
} |
|
58 |
|
|
59 |
$content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); |
|
60 |
} |
|
61 |
|
|
62 |
return $content; |
|
63 |
|
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Renders the `core/comment-template` block on the server. |
|
68 |
* |
|
69 |
* @param array $attributes Block attributes. |
|
70 |
* @param string $content Block default content. |
|
71 |
* @param WP_Block $block Block instance. |
|
72 |
* |
|
73 |
* @return string Returns the HTML representing the comments using the layout |
|
74 |
* defined by the block's inner blocks. |
|
75 |
*/ |
|
76 |
function render_block_core_comment_template( $attributes, $content, $block ) { |
|
77 |
// Bail out early if the post ID is not set for some reason. |
|
78 |
if ( empty( $block->context['postId'] ) ) { |
|
79 |
return ''; |
|
80 |
} |
|
81 |
|
|
82 |
if ( post_password_required( $block->context['postId'] ) ) { |
|
83 |
return; |
|
84 |
} |
|
85 |
|
|
86 |
$comment_query = new WP_Comment_Query( |
|
87 |
build_comment_query_vars_from_block( $block ) |
|
88 |
); |
|
89 |
|
|
90 |
// Get an array of comments for the current post. |
|
91 |
$comments = $comment_query->get_comments(); |
|
92 |
if ( count( $comments ) === 0 ) { |
|
93 |
return ''; |
|
94 |
} |
|
95 |
|
|
96 |
$comment_order = get_option( 'comment_order' ); |
|
97 |
|
|
98 |
if ( 'desc' === $comment_order ) { |
|
99 |
$comments = array_reverse( $comments ); |
|
100 |
} |
|
101 |
|
|
102 |
$wrapper_attributes = get_block_wrapper_attributes(); |
|
103 |
|
|
104 |
return sprintf( |
|
105 |
'<ol %1$s>%2$s</ol>', |
|
106 |
$wrapper_attributes, |
|
107 |
block_core_comment_template_render_comments( $comments, $block ) |
|
108 |
); |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* Registers the `core/comment-template` block on the server. |
|
113 |
*/ |
|
114 |
function register_block_core_comment_template() { |
|
115 |
register_block_type_from_metadata( |
|
116 |
__DIR__ . '/comment-template', |
|
117 |
array( |
|
118 |
'render_callback' => 'render_block_core_comment_template', |
|
119 |
'skip_inner_blocks' => true, |
|
120 |
) |
|
121 |
); |
|
122 |
} |
|
123 |
add_action( 'init', 'register_block_core_comment_template' ); |