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


Generated : Thu Sep 24 08:20:02 2020 Cross-referenced by PHPXref