| 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 |
* @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 content. |
| 15 |
*/ |
| 16 |
function gutenberg_render_block_core_comment_content( $attributes, $content, $block ) { |
| 17 |
if ( ! isset( $block->context['commentId'] ) ) { |
| 18 |
return ''; |
| 19 |
} |
| 20 |
|
| 21 |
$comment = get_comment( $block->context['commentId'] ); |
| 22 |
if ( empty( $comment ) ) { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
|
| 26 |
$comment_text = get_comment_text( $comment ); |
| 27 |
if ( ! $comment_text ) { |
| 28 |
return ''; |
| 29 |
} |
| 30 |
|
| 31 |
$classes = ''; |
| 32 |
if ( isset( $attributes['textAlign'] ) ) { |
| 33 |
$classes .= 'has-text-align-' . $attributes['textAlign']; |
| 34 |
} |
| 35 |
|
| 36 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
| 37 |
|
| 38 |
return sprintf( |
| 39 |
'<div %1$s>%2$s</div>', |
| 40 |
$wrapper_attributes, |
| 41 |
$comment_text |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Registers the `core/comment-content` block on the server. |
| 47 |
*/ |
| 48 |
function gutenberg_register_block_core_comment_content() { |
| 49 |
register_block_type_from_metadata( |
| 50 |
__DIR__ . '/comment-content', |
| 51 |
array( |
| 52 |
'render_callback' => 'gutenberg_render_block_core_comment_content', |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
add_action( 'init', 'gutenberg_register_block_core_comment_content', 20 ); |
| 57 |
|