[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> comment-template.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/comment-template` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Function that recursively renders a list of nested comments.
  10   *
  11   * @since 6.3.0 Changed render_block_context priority to `1`.
  12   *
  13   * @global int $comment_depth
  14   *
  15   * @param WP_Comment[] $comments        The array of comments.
  16   * @param WP_Block     $block           Block instance.
  17   * @return string
  18   */
  19  function block_core_comment_template_render_comments( $comments, $block ) {
  20      global $comment_depth;
  21      $thread_comments       = get_option( 'thread_comments' );
  22      $thread_comments_depth = get_option( 'thread_comments_depth' );
  23  
  24      if ( empty( $comment_depth ) ) {
  25          $comment_depth = 1;
  26      }
  27  
  28      $content = '';
  29      foreach ( $comments as $comment ) {
  30          $comment_id           = $comment->comment_ID;
  31          $filter_block_context = static function ( $context ) use ( $comment_id ) {
  32              $context['commentId'] = $comment_id;
  33              return $context;
  34          };
  35  
  36          /*
  37           * We set commentId context through the `render_block_context` filter so
  38           * that dynamically inserted blocks (at `render_block` filter stage)
  39           * will also receive that context.
  40           *
  41           * Use an early priority to so that other 'render_block_context' filters
  42           * have access to the values.
  43           */
  44          add_filter( 'render_block_context', $filter_block_context, 1 );
  45  
  46          /*
  47           * We construct a new WP_Block instance from the parsed block so that
  48           * it'll receive any changes made by the `render_block_data` filter.
  49           */
  50          $block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) );
  51  
  52          remove_filter( 'render_block_context', $filter_block_context, 1 );
  53  
  54          $children = $comment->get_children();
  55  
  56          /*
  57           * We need to create the CSS classes BEFORE recursing into the children.
  58           * This is because comment_class() uses globals like `$comment_alt`
  59           * and `$comment_thread_alt` which are order-sensitive.
  60           *
  61           * The `false` parameter at the end means that we do NOT want the function
  62           * to `echo` the output but to return a string.
  63           * See https://developer.wordpress.org/reference/functions/comment_class/#parameters.
  64           */
  65          $comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false );
  66  
  67          // If the comment has children, recurse to create the HTML for the nested
  68          // comments.
  69          if ( ! empty( $children ) && ! empty( $thread_comments ) ) {
  70              if ( $comment_depth < $thread_comments_depth ) {
  71                  ++$comment_depth;
  72                  $inner_content  = block_core_comment_template_render_comments(
  73                      $children,
  74                      $block
  75                  );
  76                  $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content );
  77                  --$comment_depth;
  78              } else {
  79                  $block_content .= block_core_comment_template_render_comments(
  80                      $children,
  81                      $block
  82                  );
  83              }
  84          }
  85  
  86          $content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content );
  87      }
  88  
  89      return $content;
  90  }
  91  
  92  /**
  93   * Renders the `core/comment-template` block on the server.
  94   *
  95   * @param array    $attributes Block attributes.
  96   * @param string   $content    Block default content.
  97   * @param WP_Block $block      Block instance.
  98   *
  99   * @return string Returns the HTML representing the comments using the layout
 100   * defined by the block's inner blocks.
 101   */
 102  function render_block_core_comment_template( $attributes, $content, $block ) {
 103      // Bail out early if the post ID is not set for some reason.
 104      if ( empty( $block->context['postId'] ) ) {
 105          return '';
 106      }
 107  
 108      if ( post_password_required( $block->context['postId'] ) ) {
 109          return;
 110      }
 111  
 112      $comment_query = new WP_Comment_Query(
 113          build_comment_query_vars_from_block( $block )
 114      );
 115  
 116      // Get an array of comments for the current post.
 117      $comments = $comment_query->get_comments();
 118      if ( count( $comments ) === 0 ) {
 119          return '';
 120      }
 121  
 122      $comment_order = get_option( 'comment_order' );
 123  
 124      if ( 'desc' === $comment_order ) {
 125          $comments = array_reverse( $comments );
 126      }
 127  
 128      $wrapper_attributes = get_block_wrapper_attributes();
 129  
 130      return sprintf(
 131          '<ol %1$s>%2$s</ol>',
 132          $wrapper_attributes,
 133          block_core_comment_template_render_comments( $comments, $block )
 134      );
 135  }
 136  
 137  /**
 138   * Registers the `core/comment-template` block on the server.
 139   */
 140  function register_block_core_comment_template() {
 141      register_block_type_from_metadata(
 142          __DIR__ . '/comment-template',
 143          array(
 144              'render_callback'   => 'render_block_core_comment_template',
 145              'skip_inner_blocks' => true,
 146          )
 147      );
 148  }
 149  add_action( 'init', 'register_block_core_comment_template' );


Generated : Wed Apr 17 08:20:01 2024 Cross-referenced by PHPXref