| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/comments-title` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/comments-title` block on the server. |
| 10 |
* |
| 11 |
* @since 6.0.0 |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* |
| 15 |
* @return string Return the post comments title. |
| 16 |
*/ |
| 17 |
function gutenberg_render_block_core_comments_title( $attributes ) { |
| 18 |
|
| 19 |
if ( post_password_required() ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
| 24 |
$show_post_title = ! empty( $attributes['showPostTitle'] ) && $attributes['showPostTitle']; |
| 25 |
$show_comments_count = ! empty( $attributes['showCommentsCount'] ) && $attributes['showCommentsCount']; |
| 26 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
| 27 |
$comments_count = get_comments_number(); |
| 28 |
$post_title = get_the_title(); |
| 29 |
$tag_name = 'h2'; |
| 30 |
if ( isset( $attributes['level'] ) ) { |
| 31 |
$tag_name = 'h' . $attributes['level']; |
| 32 |
} |
| 33 |
|
| 34 |
if ( '0' === $comments_count ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $show_comments_count ) { |
| 39 |
if ( $show_post_title ) { |
| 40 |
if ( '1' === $comments_count ) { |
| 41 |
/* translators: %s: Post title. */ |
| 42 |
$comments_title = sprintf( __( 'One response to “%s”' ), $post_title ); |
| 43 |
} else { |
| 44 |
$comments_title = sprintf( |
| 45 |
/* translators: 1: Number of comments, 2: Post title. */ |
| 46 |
_n( |
| 47 |
'%1$s response to “%2$s”', |
| 48 |
'%1$s responses to “%2$s”', |
| 49 |
$comments_count |
| 50 |
), |
| 51 |
number_format_i18n( $comments_count ), |
| 52 |
$post_title |
| 53 |
); |
| 54 |
} |
| 55 |
} elseif ( '1' === $comments_count ) { |
| 56 |
$comments_title = __( 'One response' ); |
| 57 |
} else { |
| 58 |
$comments_title = sprintf( |
| 59 |
/* translators: %s: Number of comments. */ |
| 60 |
_n( '%s response', '%s responses', $comments_count ), |
| 61 |
number_format_i18n( $comments_count ) |
| 62 |
); |
| 63 |
} |
| 64 |
} elseif ( $show_post_title ) { |
| 65 |
if ( '1' === $comments_count ) { |
| 66 |
/* translators: %s: Post title. */ |
| 67 |
$comments_title = sprintf( __( 'Response to “%s”' ), $post_title ); |
| 68 |
} else { |
| 69 |
/* translators: %s: Post title. */ |
| 70 |
$comments_title = sprintf( __( 'Responses to “%s”' ), $post_title ); |
| 71 |
} |
| 72 |
} elseif ( '1' === $comments_count ) { |
| 73 |
$comments_title = __( 'Response' ); |
| 74 |
} else { |
| 75 |
$comments_title = __( 'Responses' ); |
| 76 |
} |
| 77 |
|
| 78 |
return sprintf( |
| 79 |
'<%1$s id="comments" %2$s>%3$s</%1$s>', |
| 80 |
$tag_name, |
| 81 |
$wrapper_attributes, |
| 82 |
$comments_title |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Registers the `core/comments-title` block on the server. |
| 88 |
* |
| 89 |
* @since 6.0.0 |
| 90 |
*/ |
| 91 |
function gutenberg_register_block_core_comments_title() { |
| 92 |
register_block_type_from_metadata( |
| 93 |
__DIR__ . '/comments-title', |
| 94 |
array( |
| 95 |
'render_callback' => 'gutenberg_render_block_core_comments_title', |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
add_action( 'init', 'gutenberg_register_block_core_comments_title', 20 ); |
| 101 |
|