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 */
17function 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 /* translators: %s: Post title. */
29 $post_title = sprintf( __( '“%s”' ), get_the_title() );
30 $tag_name = 'h2';
31 if ( isset( $attributes['level'] ) ) {
32 $tag_name = 'h' . $attributes['level'];
33 }
34
35 if ( '0' === $comments_count ) {
36 return;
37 }
38
39 if ( $show_comments_count ) {
40 if ( $show_post_title ) {
41 if ( '1' === $comments_count ) {
42 /* translators: %s: Post title. */
43 $comments_title = sprintf( __( 'One response to %s' ), $post_title );
44 } else {
45 $comments_title = sprintf(
46 /* translators: 1: Number of comments, 2: Post title. */
47 _n(
48 '%1$s response to %2$s',
49 '%1$s responses to %2$s',
50 $comments_count
51 ),
52 number_format_i18n( $comments_count ),
53 $post_title
54 );
55 }
56 } elseif ( '1' === $comments_count ) {
57 $comments_title = __( 'One response' );
58 } else {
59 $comments_title = sprintf(
60 /* translators: %s: Number of comments. */
61 _n( '%s response', '%s responses', $comments_count ),
62 number_format_i18n( $comments_count )
63 );
64 }
65 } elseif ( $show_post_title ) {
66 if ( '1' === $comments_count ) {
67 /* translators: %s: Post title. */
68 $comments_title = sprintf( __( 'Response to %s' ), $post_title );
69 } else {
70 /* translators: %s: Post title. */
71 $comments_title = sprintf( __( 'Responses to %s' ), $post_title );
72 }
73 } elseif ( '1' === $comments_count ) {
74 $comments_title = __( 'Response' );
75 } else {
76 $comments_title = __( 'Responses' );
77 }
78
79 return sprintf(
80 '<%1$s id="comments" %2$s>%3$s</%1$s>',
81 $tag_name,
82 $wrapper_attributes,
83 $comments_title
84 );
85}
86
87/**
88 * Registers the `core/comments-title` block on the server.
89 *
90 * @since 6.0.0
91 */
92function register_block_core_comments_title() {
93 register_block_type_from_metadata(
94 __DIR__ . '/comments-title',
95 array(
96 'render_callback' => 'render_block_core_comments_title',
97 )
98 );
99}
100
101add_action( 'init', 'register_block_core_comments_title' );
102