[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-walker-comment.php (source)

   1  <?php
   2  /**
   3   * Comment API: Walker_Comment class
   4   *
   5   * @package WordPress
   6   * @subpackage Comments
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core walker class used to create an HTML list of comments.
  12   *
  13   * @since 2.7.0
  14   *
  15   * @see Walker
  16   */
  17  class Walker_Comment extends Walker {
  18  
  19      /**
  20       * What the class handles.
  21       *
  22       * @since 2.7.0
  23       * @var string
  24       *
  25       * @see Walker::$tree_type
  26       */
  27      public $tree_type = 'comment';
  28  
  29      /**
  30       * Database fields to use.
  31       *
  32       * @since 2.7.0
  33       * @var string[]
  34       *
  35       * @see Walker::$db_fields
  36       * @todo Decouple this
  37       */
  38      public $db_fields = array(
  39          'parent' => 'comment_parent',
  40          'id'     => 'comment_ID',
  41      );
  42  
  43      /**
  44       * Starts the list before the elements are added.
  45       *
  46       * @since 2.7.0
  47       *
  48       * @see Walker::start_lvl()
  49       * @global int $comment_depth
  50       *
  51       * @param string $output Used to append additional content (passed by reference).
  52       * @param int    $depth  Optional. Depth of the current comment. Default 0.
  53       * @param array  $args   Optional. Uses 'style' argument for type of HTML list. Default empty array.
  54       */
  55  	public function start_lvl( &$output, $depth = 0, $args = array() ) {
  56          $GLOBALS['comment_depth'] = $depth + 1;
  57  
  58          switch ( $args['style'] ) {
  59              case 'div':
  60                  break;
  61              case 'ol':
  62                  $output .= '<ol class="children">' . "\n";
  63                  break;
  64              case 'ul':
  65              default:
  66                  $output .= '<ul class="children">' . "\n";
  67                  break;
  68          }
  69      }
  70  
  71      /**
  72       * Ends the list of items after the elements are added.
  73       *
  74       * @since 2.7.0
  75       *
  76       * @see Walker::end_lvl()
  77       * @global int $comment_depth
  78       *
  79       * @param string $output Used to append additional content (passed by reference).
  80       * @param int    $depth  Optional. Depth of the current comment. Default 0.
  81       * @param array  $args   Optional. Will only append content if style argument value is 'ol' or 'ul'.
  82       *                       Default empty array.
  83       */
  84  	public function end_lvl( &$output, $depth = 0, $args = array() ) {
  85          $GLOBALS['comment_depth'] = $depth + 1;
  86  
  87          switch ( $args['style'] ) {
  88              case 'div':
  89                  break;
  90              case 'ol':
  91                  $output .= "</ol><!-- .children -->\n";
  92                  break;
  93              case 'ul':
  94              default:
  95                  $output .= "</ul><!-- .children -->\n";
  96                  break;
  97          }
  98      }
  99  
 100      /**
 101       * Traverses elements to create list from elements.
 102       *
 103       * This function is designed to enhance Walker::display_element() to
 104       * display children of higher nesting levels than selected inline on
 105       * the highest depth level displayed. This prevents them being orphaned
 106       * at the end of the comment list.
 107       *
 108       * Example: max_depth = 2, with 5 levels of nested content.
 109       *     1
 110       *      1.1
 111       *        1.1.1
 112       *        1.1.1.1
 113       *        1.1.1.1.1
 114       *        1.1.2
 115       *        1.1.2.1
 116       *     2
 117       *      2.2
 118       *
 119       * @since 2.7.0
 120       *
 121       * @see Walker::display_element()
 122       * @see wp_list_comments()
 123       *
 124       * @param WP_Comment $element           Comment data object.
 125       * @param array      $children_elements List of elements to continue traversing. Passed by reference.
 126       * @param int        $max_depth         Max depth to traverse.
 127       * @param int        $depth             Depth of the current element.
 128       * @param array      $args              An array of arguments.
 129       * @param string     $output            Used to append additional content. Passed by reference.
 130       */
 131  	public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
 132          if ( ! $element ) {
 133              return;
 134          }
 135  
 136          $id_field = $this->db_fields['id'];
 137          $id       = $element->$id_field;
 138  
 139          parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
 140  
 141          /*
 142           * If at the max depth, and the current element still has children, loop over those
 143           * and display them at this level. This is to prevent them being orphaned to the end
 144           * of the list.
 145           */
 146          if ( $max_depth <= $depth + 1 && isset( $children_elements[ $id ] ) ) {
 147              foreach ( $children_elements[ $id ] as $child ) {
 148                  $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
 149              }
 150  
 151              unset( $children_elements[ $id ] );
 152          }
 153  
 154      }
 155  
 156      /**
 157       * Starts the element output.
 158       *
 159       * @since 2.7.0
 160       * @since 5.9.0 Renamed `$comment` to `$data_object` and `$id` to `$current_object_id`
 161       *              to match parent class for PHP 8 named parameter support.
 162       *
 163       * @see Walker::start_el()
 164       * @see wp_list_comments()
 165       * @global int        $comment_depth
 166       * @global WP_Comment $comment       Global comment object.
 167       *
 168       * @param string     $output            Used to append additional content. Passed by reference.
 169       * @param WP_Comment $data_object       Comment data object.
 170       * @param int        $depth             Optional. Depth of the current comment in reference to parents. Default 0.
 171       * @param array      $args              Optional. An array of arguments. Default empty array.
 172       * @param int        $current_object_id Optional. ID of the current comment. Default 0.
 173       */
 174  	public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
 175          // Restores the more descriptive, specific name for use within this method.
 176          $comment = $data_object;
 177  
 178          $depth++;
 179          $GLOBALS['comment_depth'] = $depth;
 180          $GLOBALS['comment']       = $comment;
 181  
 182          if ( ! empty( $args['callback'] ) ) {
 183              ob_start();
 184              call_user_func( $args['callback'], $comment, $args, $depth );
 185              $output .= ob_get_clean();
 186              return;
 187          }
 188  
 189          if ( 'comment' === $comment->comment_type ) {
 190              add_filter( 'comment_text', array( $this, 'filter_comment_text' ), 40, 2 );
 191          }
 192  
 193          if ( ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) && $args['short_ping'] ) {
 194              ob_start();
 195              $this->ping( $comment, $depth, $args );
 196              $output .= ob_get_clean();
 197          } elseif ( 'html5' === $args['format'] ) {
 198              ob_start();
 199              $this->html5_comment( $comment, $depth, $args );
 200              $output .= ob_get_clean();
 201          } else {
 202              ob_start();
 203              $this->comment( $comment, $depth, $args );
 204              $output .= ob_get_clean();
 205          }
 206  
 207          if ( 'comment' === $comment->comment_type ) {
 208              remove_filter( 'comment_text', array( $this, 'filter_comment_text' ), 40 );
 209          }
 210      }
 211  
 212      /**
 213       * Ends the element output, if needed.
 214       *
 215       * @since 2.7.0
 216       * @since 5.9.0 Renamed `$comment` to `$data_object` to match parent class for PHP 8 named parameter support.
 217       *
 218       * @see Walker::end_el()
 219       * @see wp_list_comments()
 220       *
 221       * @param string     $output      Used to append additional content. Passed by reference.
 222       * @param WP_Comment $data_object Comment data object.
 223       * @param int        $depth       Optional. Depth of the current comment. Default 0.
 224       * @param array      $args        Optional. An array of arguments. Default empty array.
 225       */
 226  	public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
 227          if ( ! empty( $args['end-callback'] ) ) {
 228              ob_start();
 229              call_user_func(
 230                  $args['end-callback'],
 231                  $data_object, // The current comment object.
 232                  $args,
 233                  $depth
 234              );
 235              $output .= ob_get_clean();
 236              return;
 237          }
 238          if ( 'div' === $args['style'] ) {
 239              $output .= "</div><!-- #comment-## -->\n";
 240          } else {
 241              $output .= "</li><!-- #comment-## -->\n";
 242          }
 243      }
 244  
 245      /**
 246       * Outputs a pingback comment.
 247       *
 248       * @since 3.6.0
 249       *
 250       * @see wp_list_comments()
 251       *
 252       * @param WP_Comment $comment The comment object.
 253       * @param int        $depth   Depth of the current comment.
 254       * @param array      $args    An array of arguments.
 255       */
 256  	protected function ping( $comment, $depth, $args ) {
 257          $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
 258          ?>
 259          <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
 260              <div class="comment-body">
 261                  <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
 262              </div>
 263          <?php
 264      }
 265  
 266      /**
 267       * Filters the comment text.
 268       *
 269       * Removes links from the pending comment's text if the commenter did not consent
 270       * to the comment cookies.
 271       *
 272       * @since 5.4.2
 273       *
 274       * @param string          $comment_text Text of the current comment.
 275       * @param WP_Comment|null $comment      The comment object. Null if not found.
 276       * @return string Filtered text of the current comment.
 277       */
 278  	public function filter_comment_text( $comment_text, $comment ) {
 279          $commenter          = wp_get_current_commenter();
 280          $show_pending_links = ! empty( $commenter['comment_author'] );
 281  
 282          if ( $comment && '0' == $comment->comment_approved && ! $show_pending_links ) {
 283              $comment_text = wp_kses( $comment_text, array() );
 284          }
 285  
 286          return $comment_text;
 287      }
 288  
 289      /**
 290       * Outputs a single comment.
 291       *
 292       * @since 3.6.0
 293       *
 294       * @see wp_list_comments()
 295       *
 296       * @param WP_Comment $comment Comment to display.
 297       * @param int        $depth   Depth of the current comment.
 298       * @param array      $args    An array of arguments.
 299       */
 300  	protected function comment( $comment, $depth, $args ) {
 301          if ( 'div' === $args['style'] ) {
 302              $tag       = 'div';
 303              $add_below = 'comment';
 304          } else {
 305              $tag       = 'li';
 306              $add_below = 'div-comment';
 307          }
 308  
 309          $commenter          = wp_get_current_commenter();
 310          $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
 311  
 312          if ( $commenter['comment_author_email'] ) {
 313              $moderation_note = __( 'Your comment is awaiting moderation.' );
 314          } else {
 315              $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
 316          }
 317          ?>
 318          <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
 319          <?php if ( 'div' !== $args['style'] ) : ?>
 320          <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
 321          <?php endif; ?>
 322          <div class="comment-author vcard">
 323              <?php
 324              if ( 0 != $args['avatar_size'] ) {
 325                  echo get_avatar( $comment, $args['avatar_size'] );
 326              }
 327              ?>
 328              <?php
 329              $comment_author = get_comment_author_link( $comment );
 330  
 331              if ( '0' == $comment->comment_approved && ! $show_pending_links ) {
 332                  $comment_author = get_comment_author( $comment );
 333              }
 334  
 335              printf(
 336                  /* translators: %s: Comment author link. */
 337                  __( '%s <span class="says">says:</span>' ),
 338                  sprintf( '<cite class="fn">%s</cite>', $comment_author )
 339              );
 340              ?>
 341          </div>
 342          <?php if ( '0' == $comment->comment_approved ) : ?>
 343          <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
 344          <br />
 345          <?php endif; ?>
 346  
 347          <div class="comment-meta commentmetadata">
 348              <?php
 349              printf(
 350                  '<a href="%s">%s</a>',
 351                  esc_url( get_comment_link( $comment, $args ) ),
 352                  sprintf(
 353                      /* translators: 1: Comment date, 2: Comment time. */
 354                      __( '%1$s at %2$s' ),
 355                      get_comment_date( '', $comment ),
 356                      get_comment_time()
 357                  )
 358              );
 359  
 360              edit_comment_link( __( '(Edit)' ), ' &nbsp;&nbsp;', '' );
 361              ?>
 362          </div>
 363  
 364          <?php
 365          comment_text(
 366              $comment,
 367              array_merge(
 368                  $args,
 369                  array(
 370                      'add_below' => $add_below,
 371                      'depth'     => $depth,
 372                      'max_depth' => $args['max_depth'],
 373                  )
 374              )
 375          );
 376          ?>
 377  
 378          <?php
 379          comment_reply_link(
 380              array_merge(
 381                  $args,
 382                  array(
 383                      'add_below' => $add_below,
 384                      'depth'     => $depth,
 385                      'max_depth' => $args['max_depth'],
 386                      'before'    => '<div class="reply">',
 387                      'after'     => '</div>',
 388                  )
 389              )
 390          );
 391          ?>
 392  
 393          <?php if ( 'div' !== $args['style'] ) : ?>
 394          </div>
 395          <?php endif; ?>
 396          <?php
 397      }
 398  
 399      /**
 400       * Outputs a comment in the HTML5 format.
 401       *
 402       * @since 3.6.0
 403       *
 404       * @see wp_list_comments()
 405       *
 406       * @param WP_Comment $comment Comment to display.
 407       * @param int        $depth   Depth of the current comment.
 408       * @param array      $args    An array of arguments.
 409       */
 410  	protected function html5_comment( $comment, $depth, $args ) {
 411          $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
 412  
 413          $commenter          = wp_get_current_commenter();
 414          $show_pending_links = ! empty( $commenter['comment_author'] );
 415  
 416          if ( $commenter['comment_author_email'] ) {
 417              $moderation_note = __( 'Your comment is awaiting moderation.' );
 418          } else {
 419              $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
 420          }
 421          ?>
 422          <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
 423              <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
 424                  <footer class="comment-meta">
 425                      <div class="comment-author vcard">
 426                          <?php
 427                          if ( 0 != $args['avatar_size'] ) {
 428                              echo get_avatar( $comment, $args['avatar_size'] );
 429                          }
 430                          ?>
 431                          <?php
 432                          $comment_author = get_comment_author_link( $comment );
 433  
 434                          if ( '0' == $comment->comment_approved && ! $show_pending_links ) {
 435                              $comment_author = get_comment_author( $comment );
 436                          }
 437  
 438                          printf(
 439                              /* translators: %s: Comment author link. */
 440                              __( '%s <span class="says">says:</span>' ),
 441                              sprintf( '<b class="fn">%s</b>', $comment_author )
 442                          );
 443                          ?>
 444                      </div><!-- .comment-author -->
 445  
 446                      <div class="comment-metadata">
 447                          <?php
 448                          printf(
 449                              '<a href="%s"><time datetime="%s">%s</time></a>',
 450                              esc_url( get_comment_link( $comment, $args ) ),
 451                              get_comment_time( 'c' ),
 452                              sprintf(
 453                                  /* translators: 1: Comment date, 2: Comment time. */
 454                                  __( '%1$s at %2$s' ),
 455                                  get_comment_date( '', $comment ),
 456                                  get_comment_time()
 457                              )
 458                          );
 459  
 460                          edit_comment_link( __( 'Edit' ), ' <span class="edit-link">', '</span>' );
 461                          ?>
 462                      </div><!-- .comment-metadata -->
 463  
 464                      <?php if ( '0' == $comment->comment_approved ) : ?>
 465                      <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
 466                      <?php endif; ?>
 467                  </footer><!-- .comment-meta -->
 468  
 469                  <div class="comment-content">
 470                      <?php comment_text(); ?>
 471                  </div><!-- .comment-content -->
 472  
 473                  <?php
 474                  if ( '1' == $comment->comment_approved || $show_pending_links ) {
 475                      comment_reply_link(
 476                          array_merge(
 477                              $args,
 478                              array(
 479                                  'add_below' => 'div-comment',
 480                                  'depth'     => $depth,
 481                                  'max_depth' => $args['max_depth'],
 482                                  'before'    => '<div class="reply">',
 483                                  'after'     => '</div>',
 484                              )
 485                          )
 486                      );
 487                  }
 488                  ?>
 489              </article><!-- .comment-body -->
 490          <?php
 491      }
 492  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1