19
|
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 |
* @param array $attributes Block attributes. |
|
12 |
* @param string $content Block default content. |
|
13 |
* @param WP_Block $block Block instance. |
|
14 |
* @return string Return the post comment's author. |
|
15 |
*/ |
|
16 |
function render_block_core_comment_author_name( $attributes, $content, $block ) { |
|
17 |
if ( ! isset( $block->context['commentId'] ) ) { |
|
18 |
return ''; |
|
19 |
} |
|
20 |
|
|
21 |
$comment = get_comment( $block->context['commentId'] ); |
|
22 |
$commenter = wp_get_current_commenter(); |
|
23 |
$show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author']; |
|
24 |
if ( empty( $comment ) ) { |
|
25 |
return ''; |
|
26 |
} |
|
27 |
|
|
28 |
$classes = ''; |
|
29 |
if ( isset( $attributes['textAlign'] ) ) { |
|
30 |
$classes .= 'has-text-align-' . $attributes['textAlign']; |
|
31 |
} |
|
32 |
|
|
33 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
|
34 |
$comment_author = get_comment_author( $comment ); |
|
35 |
$link = get_comment_author_url( $comment ); |
|
36 |
|
|
37 |
if ( ! empty( $attributes['isLink'] ) && ! empty( $attributes['linkTarget'] ) ) { |
|
38 |
$comment_author = sprintf( '<a rel="external nofollow ugc" href="%1s" target="%2s" >%3s</a>', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $comment_author ); |
|
39 |
} |
|
40 |
if ( '0' === $comment->comment_approved && ! $show_pending_links ) { |
|
41 |
$comment_author = wp_kses( $comment_author, array() ); |
|
42 |
} |
|
43 |
|
|
44 |
return sprintf( |
|
45 |
'<div %1$s>%2$s</div>', |
|
46 |
$wrapper_attributes, |
|
47 |
$comment_author |
|
48 |
); |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Registers the `core/comment-author-name` block on the server. |
|
53 |
*/ |
|
54 |
function register_block_core_comment_author_name() { |
|
55 |
register_block_type_from_metadata( |
|
56 |
__DIR__ . '/comment-author-name', |
|
57 |
array( |
|
58 |
'render_callback' => 'render_block_core_comment_author_name', |
|
59 |
) |
|
60 |
); |
|
61 |
} |
|
62 |
add_action( 'init', 'register_block_core_comment_author_name' ); |