[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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       * Starts the element output.
 157       *
 158       * @since 2.7.0
 159       * @since 5.9.0 Renamed `$comment` to `$data_object` and `$id` to `$current_object_id`
 160       *              to match parent class for PHP 8 named parameter support.
 161       *
 162       * @see Walker::start_el()
 163       * @see wp_list_comments()
 164       * @global int        $comment_depth
 165       * @global WP_Comment $comment       Global comment object.
 166       *
 167       * @param string     $output            Used to append additional content. Passed by reference.
 168       * @param WP_Comment $data_object       Comment data object.
 169       * @param int        $depth             Optional. Depth of the current comment in reference to parents. Default 0.
 170       * @param array      $args              Optional. An array of arguments. Default empty array.
 171       * @param int        $current_object_id Optional. ID of the current comment. Default 0.
 172       */
 173  	public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
 174          // Restores the more descriptive, specific name for use within this method.
 175          $comment = $data_object;
 176  
 177          ++$depth;
 178          $GLOBALS['comment_depth'] = $depth;
 179          $GLOBALS['comment']       = $comment;
 180  
 181          if ( ! empty( $args['callback'] ) ) {
 182              ob_start();
 183              call_user_func( $args['callback'], $comment, $args, $depth );
 184              $output .= ob_get_clean();
 185              return;
 186          }
 187  
 188          if ( 'comment' === $comment->comment_type ) {
 189              add_filter( 'comment_text', array( $this, 'filter_comment_text' ), 40, 2 );
 190          }
 191  
 192          if ( ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) && $args['short_ping'] ) {
 193              ob_start();
 194              $this->ping( $comment, $depth, $args );
 195              $output .= ob_get_clean();
 196          } elseif ( 'html5' === $args['format'] ) {
 197              ob_start();
 198              $this->html5_comment( $comment, $depth, $args );
 199              $output .= ob_get_clean();
 200          } else {
 201              ob_start();
 202              $this->comment( $comment, $depth, $args );
 203              $output .= ob_get_clean();
 204          }
 205  
 206          if ( 'comment' === $comment->comment_type ) {
 207              remove_filter( 'comment_text', array( $this, 'filter_comment_text' ), 40 );
 208          }
 209      }
 210  
 211      /**
 212       * Ends the element output, if needed.
 213       *
 214       * @since 2.7.0
 215       * @since 5.9.0 Renamed `$comment` to `$data_object` to match parent class for PHP 8 named parameter support.
 216       *
 217       * @see Walker::end_el()
 218       * @see wp_list_comments()
 219       *
 220       * @param string     $output      Used to append additional content. Passed by reference.
 221       * @param WP_Comment $data_object Comment data object.
 222       * @param int        $depth       Optional. Depth of the current comment. Default 0.
 223       * @param array      $args        Optional. An array of arguments. Default empty array.
 224       */
 225  	public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
 226          if ( ! empty( $args['end-callback'] ) ) {
 227              ob_start();
 228              call_user_func(
 229                  $args['end-callback'],
 230                  $data_object, // The current comment object.
 231                  $args,
 232                  $depth
 233              );
 234              $output .= ob_get_clean();
 235              return;
 236          }
 237          if ( 'div' === $args['style'] ) {
 238              $output .= "</div><!-- #comment-## -->\n";
 239          } else {
 240              $output .= "</li><!-- #comment-## -->\n";
 241          }
 242      }
 243  
 244      /**
 245       * Outputs a pingback comment.
 246       *
 247       * @since 3.6.0
 248       *
 249       * @see wp_list_comments()
 250       *
 251       * @param WP_Comment $comment The comment object.
 252       * @param int        $depth   Depth of the current comment.
 253       * @param array      $args    An array of arguments.
 254       */
 255  	protected function ping( $comment, $depth, $args ) {
 256          $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
 257          ?>
 258          <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
 259              <div class="comment-body">
 260                  <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
 261              </div>
 262          <?php
 263      }
 264  
 265      /**
 266       * Filters the comment text.
 267       *
 268       * Removes links from the pending comment's text if the commenter did not consent
 269       * to the comment cookies.
 270       *
 271       * @since 5.4.2
 272       *
 273       * @param string          $comment_text Text of the current comment.
 274       * @param WP_Comment|null $comment      The comment object. Null if not found.
 275       * @return string Filtered text of the current comment.
 276       */
 277  	public function filter_comment_text( $comment_text, $comment ) {
 278          $commenter          = wp_get_current_commenter();
 279          $show_pending_links = ! empty( $commenter['comment_author'] );
 280  
 281          if ( $comment && '0' === $comment->comment_approved && ! $show_pending_links ) {
 282              $comment_text = wp_kses( $comment_text, array() );
 283          }
 284  
 285          return $comment_text;
 286      }
 287  
 288      /**
 289       * Outputs a single comment.
 290       *
 291       * @since 3.6.0
 292       *
 293       * @see wp_list_comments()
 294       *
 295       * @param WP_Comment $comment Comment to display.
 296       * @param int        $depth   Depth of the current comment.
 297       * @param array      $args    An array of arguments.
 298       */
 299  	protected function comment( $comment, $depth, $args ) {
 300          if ( 'div' === $args['style'] ) {
 301              $tag       = 'div';
 302              $add_below = 'comment';
 303          } else {
 304              $tag       = 'li';
 305              $add_below = 'div-comment';
 306          }
 307  
 308          $commenter          = wp_get_current_commenter();
 309          $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
 310  
 311          if ( $commenter['comment_author_email'] ) {
 312              $moderation_note = __( 'Your comment is awaiting moderation.' );
 313          } else {
 314              $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
 315          }
 316          ?>
 317          <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
 318          <?php if ( 'div' !== $args['style'] ) : ?>
 319          <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
 320          <?php endif; ?>
 321          <div class="comment-author vcard">
 322              <?php
 323              if ( 0 !== $args['avatar_size'] ) {
 324                  echo get_avatar( $comment, $args['avatar_size'] );
 325              }
 326              ?>
 327              <?php
 328              $comment_author = get_comment_author_link( $comment );
 329  
 330              if ( '0' === $comment->comment_approved && ! $show_pending_links ) {
 331                  $comment_author = get_comment_author( $comment );
 332              }
 333  
 334              printf(
 335                  /* translators: %s: Comment author link. */
 336                  __( '%s <span class="says">says:</span>' ),
 337                  sprintf( '<cite class="fn">%s</cite>', $comment_author )
 338              );
 339              ?>
 340          </div>
 341          <?php if ( '0' === $comment->comment_approved ) : ?>
 342          <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
 343          <br />
 344          <?php endif; ?>
 345  
 346          <div class="comment-meta commentmetadata">
 347              <?php
 348              printf(
 349                  '<a href="%s">%s</a>',
 350                  esc_url( get_comment_link( $comment, $args ) ),
 351                  sprintf(
 352                      /* translators: 1: Comment date, 2: Comment time. */
 353                      __( '%1$s at %2$s' ),
 354                      get_comment_date( '', $comment ),
 355                      get_comment_time()
 356                  )
 357              );
 358  
 359              edit_comment_link( __( '(Edit)' ), ' &nbsp;&nbsp;', '' );
 360              ?>
 361          </div>
 362  
 363          <?php
 364          comment_text(
 365              $comment,
 366              array_merge(
 367                  $args,
 368                  array(
 369                      'add_below' => $add_below,
 370                      'depth'     => $depth,
 371                      'max_depth' => $args['max_depth'],
 372                  )
 373              )
 374          );
 375          ?>
 376  
 377          <?php
 378          comment_reply_link(
 379              array_merge(
 380                  $args,
 381                  array(
 382                      'add_below' => $add_below,
 383                      'depth'     => $depth,
 384                      'max_depth' => $args['max_depth'],
 385                      'before'    => '<div class="reply">',
 386                      'after'     => '</div>',
 387                  )
 388              )
 389          );
 390          ?>
 391  
 392          <?php if ( 'div' !== $args['style'] ) : ?>
 393          </div>
 394          <?php endif; ?>
 395          <?php
 396      }
 397  
 398      /**
 399       * Outputs a comment in the HTML5 format.
 400       *
 401       * @since 3.6.0
 402       *
 403       * @see wp_list_comments()
 404       *
 405       * @param WP_Comment $comment Comment to display.
 406       * @param int        $depth   Depth of the current comment.
 407       * @param array      $args    An array of arguments.
 408       */
 409  	protected function html5_comment( $comment, $depth, $args ) {
 410          $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
 411  
 412          $commenter          = wp_get_current_commenter();
 413          $show_pending_links = ! empty( $commenter['comment_author'] );
 414  
 415          if ( $commenter['comment_author_email'] ) {
 416              $moderation_note = __( 'Your comment is awaiting moderation.' );
 417          } else {
 418              $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
 419          }
 420          ?>
 421          <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
 422              <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
 423                  <footer class="comment-meta">
 424                      <div class="comment-author vcard">
 425                          <?php
 426                          if ( 0 !== $args['avatar_size'] ) {
 427                              echo get_avatar( $comment, $args['avatar_size'] );
 428                          }
 429                          ?>
 430                          <?php
 431                          $comment_author = get_comment_author_link( $comment );
 432  
 433                          if ( '0' === $comment->comment_approved && ! $show_pending_links ) {
 434                              $comment_author = get_comment_author( $comment );
 435                          }
 436  
 437                          printf(
 438                              /* translators: %s: Comment author link. */
 439                              __( '%s <span class="says">says:</span>' ),
 440                              sprintf( '<b class="fn">%s</b>', $comment_author )
 441                          );
 442                          ?>
 443                      </div><!-- .comment-author -->
 444  
 445                      <div class="comment-metadata">
 446                          <?php
 447                          printf(
 448                              '<a href="%s"><time datetime="%s">%s</time></a>',
 449                              esc_url( get_comment_link( $comment, $args ) ),
 450                              get_comment_time( 'c' ),
 451                              sprintf(
 452                                  /* translators: 1: Comment date, 2: Comment time. */
 453                                  __( '%1$s at %2$s' ),
 454                                  get_comment_date( '', $comment ),
 455                                  get_comment_time()
 456                              )
 457                          );
 458  
 459                          edit_comment_link( __( 'Edit' ), ' <span class="edit-link">', '</span>' );
 460                          ?>
 461                      </div><!-- .comment-metadata -->
 462  
 463                      <?php if ( '0' === $comment->comment_approved ) : ?>
 464                      <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
 465                      <?php endif; ?>
 466                  </footer><!-- .comment-meta -->
 467  
 468                  <div class="comment-content">
 469                      <?php comment_text(); ?>
 470                  </div><!-- .comment-content -->
 471  
 472                  <?php
 473                  if ( '1' === $comment->comment_approved || $show_pending_links ) {
 474                      comment_reply_link(
 475                          array_merge(
 476                              $args,
 477                              array(
 478                                  'add_below' => 'div-comment',
 479                                  'depth'     => $depth,
 480                                  'max_depth' => $args['max_depth'],
 481                                  'before'    => '<div class="reply">',
 482                                  'after'     => '</div>',
 483                              )
 484                          )
 485                      );
 486                  }
 487                  ?>
 488              </article><!-- .comment-body -->
 489          <?php
 490      }
 491  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref