[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> comments-title.php (source)

   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      $post_title          = $show_post_title ? sprintf( '"%1$s"', get_the_title() ) : null;
  26      $comments_count      = number_format_i18n( get_comments_number() );
  27      $tag_name            = 'h2';
  28      if ( isset( $attributes['level'] ) ) {
  29          $tag_name = 'h' . $attributes['level'];
  30      }
  31  
  32      if ( '0' === $comments_count ) {
  33          return;
  34      }
  35  
  36      $single_default_comment_label = $show_post_title ? __( 'Response to' ) : __( 'Response' );
  37      if ( $show_comments_count ) {
  38          $single_default_comment_label = $show_post_title ? __( 'One response to' ) : __( 'One response' );
  39      }
  40      $single_comment_label = ! empty( $attributes['singleCommentLabel'] ) ? $attributes['singleCommentLabel'] : $single_default_comment_label;
  41  
  42      $multiple_default_comment_label = $show_post_title ? __( 'Responses to' ) : __( 'Responses' );
  43      if ( $show_comments_count ) {
  44          $multiple_default_comment_label = $show_post_title ? __( 'responses to' ) : __( 'responses' );
  45      }
  46  
  47      $multiple_comment_label = ! empty( $attributes['multipleCommentsLabel'] ) ? $attributes['multipleCommentsLabel'] : $multiple_default_comment_label;
  48  
  49      $comments_title = '%1$s %2$s %3$s';
  50  
  51      $comments_title = sprintf(
  52          $comments_title,
  53          // If there is only one comment, only display the label.
  54          '1' !== $comments_count && $show_comments_count ? $comments_count : null,
  55          '1' === $comments_count ? $single_comment_label : $multiple_comment_label,
  56          $post_title
  57      );
  58  
  59      return sprintf(
  60          '<%1$s id="comments" %2$s>%3$s</%1$s>',
  61          $tag_name,
  62          $wrapper_attributes,
  63          $comments_title
  64      );
  65  }
  66  
  67  /**
  68   * Registers the `core/comments-title` block on the server.
  69   */
  70  function register_block_core_comments_title() {
  71      register_block_type_from_metadata(
  72          __DIR__ . '/comments-title',
  73          array(
  74              'render_callback' => 'render_block_core_comments_title',
  75          )
  76      );
  77  }
  78  
  79  add_action( 'init', 'register_block_core_comments_title' );


Generated: Sat Jun 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1