PluginProbe
Gutenberg / 13.5.1
Gutenberg v13.5.1
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 13.5.1, at build/block-library/blocks/comment-template.php

124 lines 3.3 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
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 = gutenberg_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 gutenberg_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 gutenberg_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 gutenberg_register_block_core_comment_template() {
115 register_block_type_from_metadata(
116 __DIR__ . '/comment-template',
117 array(
118 'render_callback' => 'gutenberg_render_block_core_comment_template',
119 'skip_inner_blocks' => true,
120 )
121 );
122 }
123 add_action( 'init', 'gutenberg_register_block_core_comment_template', 20 );
124