[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/comment-reply-link` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/comment-reply-link` block on the server.
  10   *
  11   * @param array    $attributes Block attributes.
  12   * @param string   $content    Block default content.
  13   * @param WP_Block $block      Block instance.
  14   * @return string Return the post comment's reply link.
  15   */
  16  function render_block_core_comment_reply_link( $attributes, $content, $block ) {
  17      if ( ! isset( $block->context['commentId'] ) ) {
  18          return '';
  19      }
  20  
  21      $thread_comments = get_option( 'thread_comments' );
  22      if ( ! $thread_comments ) {
  23          return '';
  24      }
  25  
  26      $comment = get_comment( $block->context['commentId'] );
  27      if ( empty( $comment ) ) {
  28          return '';
  29      }
  30  
  31      $depth     = 1;
  32      $max_depth = get_option( 'thread_comments_depth' );
  33      $parent_id = $comment->comment_parent;
  34  
  35      // Compute comment's depth iterating over its ancestors.
  36      while ( ! empty( $parent_id ) ) {
  37          ++$depth;
  38          $parent_id = get_comment( $parent_id )->comment_parent;
  39      }
  40  
  41      $comment_reply_link = get_comment_reply_link(
  42          array(
  43              'depth'     => $depth,
  44              'max_depth' => $max_depth,
  45          ),
  46          $comment
  47      );
  48  
  49      // Render nothing if the generated reply link is empty.
  50      if ( empty( $comment_reply_link ) ) {
  51          return;
  52      }
  53  
  54      $classes = array();
  55      if ( isset( $attributes['textAlign'] ) ) {
  56          $classes[] = 'has-text-align-' . $attributes['textAlign'];
  57      }
  58      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
  59          $classes[] = 'has-link-color';
  60      }
  61  
  62      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
  63  
  64      return sprintf(
  65          '<div %1$s>%2$s</div>',
  66          $wrapper_attributes,
  67          $comment_reply_link
  68      );
  69  }
  70  
  71  /**
  72   * Registers the `core/comment-reply-link` block on the server.
  73   */
  74  function register_block_core_comment_reply_link() {
  75      register_block_type_from_metadata(
  76          __DIR__ . '/comment-reply-link',
  77          array(
  78              'render_callback' => 'render_block_core_comment_reply_link',
  79          )
  80      );
  81  }
  82  
  83  add_action( 'init', 'register_block_core_comment_reply_link' );


Generated : Tue Apr 23 08:20:01 2024 Cross-referenced by PHPXref