PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / comment-template.php

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

93 lines 2.4 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 * @param WP_Comment[] $comments The array of comments.
12 * @param WP_Block $block Block instance.
13 * @return string
14 */
15 function gutenberg_block_core_comment_template_render_comments( $comments, $block ) {
16 $content = '';
17 foreach ( $comments as $comment ) {
18
19 $block_content = ( new WP_Block(
20 $block->parsed_block,
21 array(
22 'commentId' => $comment->comment_ID,
23 )
24 ) )->render( array( 'dynamic' => false ) );
25
26 $children = $comment->get_children();
27
28 // If the comment has children, recurse to create the HTML for the nested
29 // comments.
30 if ( ! empty( $children ) ) {
31 $inner_content = gutenberg_block_core_comment_template_render_comments(
32 $children,
33 $block
34 );
35 $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content );
36 }
37
38 $content .= '<li>' . $block_content . '</li>';
39 }
40
41 return $content;
42
43 }
44
45 /**
46 * Renders the `core/comment-template` block on the server.
47 *
48 * @param array $attributes Block attributes.
49 * @param string $content Block default content.
50 * @param WP_Block $block Block instance.
51 *
52 * @return string Returns the HTML representing the comments using the layout
53 * defined by the block's inner blocks.
54 */
55 function gutenberg_render_block_core_comment_template( $attributes, $content, $block ) {
56 // Bail out early if the post ID is not set for some reason.
57 if ( empty( $block->context['postId'] ) ) {
58 return '';
59 }
60
61 $comment_query = new WP_Comment_Query(
62 build_comment_query_vars_from_block( $block )
63 );
64
65 // Get an array of comments for the current post.
66 $comments = $comment_query->get_comments();
67 if ( count( $comments ) === 0 ) {
68 return '';
69 }
70
71 $wrapper_attributes = get_block_wrapper_attributes();
72
73 return sprintf(
74 '<ol %1$s>%2$s</ol>',
75 $wrapper_attributes,
76 gutenberg_block_core_comment_template_render_comments( $comments, $block )
77 );
78 }
79
80 /**
81 * Registers the `core/comment-template` block on the server.
82 */
83 function gutenberg_register_block_core_comment_template() {
84 register_block_type_from_metadata(
85 __DIR__ . '/comment-template',
86 array(
87 'render_callback' => 'gutenberg_render_block_core_comment_template',
88 'skip_inner_blocks' => true,
89 )
90 );
91 }
92 add_action( 'init', 'gutenberg_register_block_core_comment_template', 20 );
93