1<?php
2/**
3 * Server-side rendering of the `core/comment-content` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/comment-content` block on the server.
10 *
11 * @since 6.0.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param WP_Block $block Block instance.
16 * @return string Return the post comment's content.
17 */
18function render_block_core_comment_content( $attributes, $content, $block ) {
19 if ( ! isset( $block->context['commentId'] ) ) {
20 return '';
21 }
22
23 $comment = get_comment( $block->context['commentId'] );
24 $commenter = wp_get_current_commenter();
25 $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
26 if ( empty( $comment ) ) {
27 return '';
28 }
29
30 $args = array();
31 $comment_text = get_comment_text( $comment, $args );
32 if ( ! $comment_text ) {
33 return '';
34 }
35
36 /** This filter is documented in wp-includes/comment-template.php */
37 $comment_text = apply_filters( 'comment_text', $comment_text, $comment, $args );
38
39 $moderation_note = '';
40 if ( '0' === $comment->comment_approved ) {
41 $commenter = wp_get_current_commenter();
42
43 if ( $commenter['comment_author_email'] ) {
44 $moderation_note = __( 'Your comment is awaiting moderation.' );
45 } else {
46 $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
47 }
48 $moderation_note = '<p><em class="comment-awaiting-moderation">' . $moderation_note . '</em></p>';
49 if ( ! $show_pending_links ) {
50 $comment_text = wp_kses( $comment_text, array() );
51 }
52 }
53
54 $classes = array();
55 if ( isset( $attributes['textAlign'] ) ) {
56 $classes[] = 'has-text-align-' . $attributes['textAlign'];
57 }
58 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
59 $classes[] = 'has-link-color';
60 }
61
62 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
63
64 return sprintf(
65 '<div %1$s>%2$s%3$s</div>',
66 $wrapper_attributes,
67 $moderation_note,
68 $comment_text
69 );
70}
71
72/**
73 * Registers the `core/comment-content` block on the server.
74 *
75 * @since 6.0.0
76 */
77function register_block_core_comment_content() {
78 register_block_type_from_metadata(
79 __DIR__ . '/comment-content',
80 array(
81 'render_callback' => 'render_block_core_comment_content',
82 )
83 );
84}
85add_action( 'init', 'register_block_core_comment_content' );
86