| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-comments-link` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/post-comments-link` 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 Returns the rendered link. |
| 15 |
*/ |
| 16 |
function gutenberg_render_block_core_post_comments_link( $attributes, $content, $block ) { |
| 17 |
if ( |
| 18 |
! isset( $block->context['postId'] ) || |
| 19 |
isset( $block->context['postId'] ) && |
| 20 |
! comments_open( $block->context['postId'] ) |
| 21 |
) { |
| 22 |
return ''; |
| 23 |
} |
| 24 |
|
| 25 |
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
| 26 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
| 27 |
$comments_number = (int) get_comments_number( $block->context['postId'] ); |
| 28 |
$comments_link = get_comments_link( $block->context['postId'] ); |
| 29 |
$post_title = get_the_title( $block->context['postId'] ); |
| 30 |
$comment_text = ''; |
| 31 |
|
| 32 |
if ( 0 === $comments_number ) { |
| 33 |
$comment_text = sprintf( |
| 34 |
/* translators: %s post title */ |
| 35 |
__( 'No comments<span class="screen-reader-text"> on %s</span>' ), |
| 36 |
$post_title |
| 37 |
); |
| 38 |
} else { |
| 39 |
$comment_text = sprintf( |
| 40 |
/* translators: 1: Number of comments, 2: post title */ |
| 41 |
_n( |
| 42 |
'%1$s comment<span class="screen-reader-text"> on %2$s</span>', |
| 43 |
'%1$s comments<span class="screen-reader-text"> on %2$s</span>', |
| 44 |
$comments_number |
| 45 |
), |
| 46 |
number_format_i18n( $comments_number ), |
| 47 |
$post_title |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
return "<div {$wrapper_attributes}><a href='{$comments_link}'>{$comment_text}</a></div>"; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Registers the `core/post-comments-link` block on the server. |
| 56 |
*/ |
| 57 |
function gutenberg_register_block_core_post_comments_link() { |
| 58 |
register_block_type_from_metadata( |
| 59 |
__DIR__ . '/post-comments-link', |
| 60 |
array( |
| 61 |
'render_callback' => 'gutenberg_render_block_core_post_comments_link', |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
add_action( 'init', 'gutenberg_register_block_core_post_comments_link', 20 ); |
| 66 |
|