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