PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / comment-template.php

comment-template.php in Gutenberg 14.7.0, at build/block-library/blocks/comment-template.php

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