comments-title.php 2.7 KB

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