1<?php
2/**
3 * Server-side rendering of the `core/comment-author-name` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/comment-author-name` 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 author.
17 */
18function render_block_core_comment_author_name( $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 $classes = array();
31 if ( isset( $attributes['textAlign'] ) ) {
32 $classes[] = 'has-text-align-' . $attributes['textAlign'];
33 }
34 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
35 $classes[] = 'has-link-color';
36 }
37
38 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
39 $comment_author = get_comment_author( $comment );
40 $link = get_comment_author_url( $comment );
41
42 if ( ! empty( $link ) && ! empty( $attributes['isLink'] ) && ! empty( $attributes['linkTarget'] ) ) {
43 $comment_author = sprintf( '<a rel="external nofollow ugc" href="%1s" target="%2s" >%3s</a>', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $comment_author );
44 }
45 if ( '0' === $comment->comment_approved && ! $show_pending_links ) {
46 $comment_author = wp_kses( $comment_author, array() );
47 }
48
49 return sprintf(
50 '<div %1$s>%2$s</div>',
51 $wrapper_attributes,
52 $comment_author
53 );
54}
55
56/**
57 * Registers the `core/comment-author-name` block on the server.
58 *
59 * @since 6.0.0
60 */
61function register_block_core_comment_author_name() {
62 register_block_type_from_metadata(
63 __DIR__ . '/comment-author-name',
64 array(
65 'render_callback' => 'render_block_core_comment_author_name',
66 )
67 );
68}
69add_action( 'init', 'register_block_core_comment_author_name' );
70