[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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 ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
 185              ob_start();
 186              $this->ping( $comment, $depth, $args );
 187              $output .= ob_get_clean();
 188          } elseif ( 'html5' === $args['format'] ) {
 189              ob_start();
 190              $this->html5_comment( $comment, $depth, $args );
 191              $output .= ob_get_clean();
 192          } else {
 193              ob_start();
 194              $this->comment( $comment, $depth, $args );
 195              $output .= ob_get_clean();
 196          }
 197      }
 198  
 199      /**
 200       * Ends the element output, if needed.
 201       *
 202       * @since 2.7.0
 203       *
 204       * @see Walker::end_el()
 205       * @see wp_list_comments()
 206       *
 207       * @param string     $output  Used to append additional content. Passed by reference.
 208       * @param WP_Comment $comment The current comment object. Default current comment.
 209       * @param int        $depth   Optional. Depth of the current comment. Default 0.
 210       * @param array      $args    Optional. An array of arguments. Default empty array.
 211       */
 212  	public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
 213          if ( ! empty( $args['end-callback'] ) ) {
 214              ob_start();
 215              call_user_func( $args['end-callback'], $comment, $args, $depth );
 216              $output .= ob_get_clean();
 217              return;
 218          }
 219          if ( 'div' == $args['style'] ) {
 220              $output .= "</div><!-- #comment-## -->\n";
 221          } else {
 222              $output .= "</li><!-- #comment-## -->\n";
 223          }
 224      }
 225  
 226      /**
 227       * Outputs a pingback comment.
 228       *
 229       * @since 3.6.0
 230       *
 231       * @see wp_list_comments()
 232       *
 233       * @param WP_Comment $comment The comment object.
 234       * @param int        $depth   Depth of the current comment.
 235       * @param array      $args    An array of arguments.
 236       */
 237  	protected function ping( $comment, $depth, $args ) {
 238          $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
 239          ?>
 240          <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
 241              <div class="comment-body">
 242                  <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
 243              </div>
 244          <?php
 245      }
 246  
 247      /**
 248       * Outputs a single comment.
 249       *
 250       * @since 3.6.0
 251       *
 252       * @see wp_list_comments()
 253       *
 254       * @param WP_Comment $comment Comment to display.
 255       * @param int        $depth   Depth of the current comment.
 256       * @param array      $args    An array of arguments.
 257       */
 258  	protected function comment( $comment, $depth, $args ) {
 259          if ( 'div' == $args['style'] ) {
 260              $tag       = 'div';
 261              $add_below = 'comment';
 262          } else {
 263              $tag       = 'li';
 264              $add_below = 'div-comment';
 265          }
 266  
 267          $commenter = wp_get_current_commenter();
 268          if ( $commenter['comment_author_email'] ) {
 269              $moderation_note = __( 'Your comment is awaiting moderation.' );
 270          } else {
 271              $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' );
 272          }
 273  
 274          ?>
 275          <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
 276          <?php if ( 'div' != $args['style'] ) : ?>
 277          <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
 278          <?php endif; ?>
 279          <div class="comment-author vcard">
 280              <?php
 281              if ( 0 != $args['avatar_size'] ) {
 282                  echo get_avatar( $comment, $args['avatar_size'] );}
 283              ?>
 284              <?php
 285                  printf(
 286                      /* translators: %s: Comment author link. */
 287                      __( '%s <span class="says">says:</span>' ),
 288                      sprintf( '<cite class="fn">%s</cite>', get_comment_author_link( $comment ) )
 289                  );
 290              ?>
 291          </div>
 292          <?php if ( '0' == $comment->comment_approved ) : ?>
 293          <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
 294          <br />
 295          <?php endif; ?>
 296  
 297          <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
 298              <?php
 299                  /* translators: 1: Comment date, 2: Comment time. */
 300                  printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
 301              ?>
 302                  </a>
 303                  <?php
 304                  edit_comment_link( __( '(Edit)' ), '&nbsp;&nbsp;', '' );
 305                  ?>
 306          </div>
 307  
 308          <?php
 309          comment_text(
 310              $comment,
 311              array_merge(
 312                  $args,
 313                  array(
 314                      'add_below' => $add_below,
 315                      'depth'     => $depth,
 316                      'max_depth' => $args['max_depth'],
 317                  )
 318              )
 319          );
 320          ?>
 321  
 322          <?php
 323          comment_reply_link(
 324              array_merge(
 325                  $args,
 326                  array(
 327                      'add_below' => $add_below,
 328                      'depth'     => $depth,
 329                      'max_depth' => $args['max_depth'],
 330                      'before'    => '<div class="reply">',
 331                      'after'     => '</div>',
 332                  )
 333              )
 334          );
 335          ?>
 336  
 337          <?php if ( 'div' != $args['style'] ) : ?>
 338          </div>
 339          <?php endif; ?>
 340          <?php
 341      }
 342  
 343      /**
 344       * Outputs a comment in the HTML5 format.
 345       *
 346       * @since 3.6.0
 347       *
 348       * @see wp_list_comments()
 349       *
 350       * @param WP_Comment $comment Comment to display.
 351       * @param int        $depth   Depth of the current comment.
 352       * @param array      $args    An array of arguments.
 353       */
 354  	protected function html5_comment( $comment, $depth, $args ) {
 355          $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
 356  
 357          $commenter = wp_get_current_commenter();
 358          if ( $commenter['comment_author_email'] ) {
 359              $moderation_note = __( 'Your comment is awaiting moderation.' );
 360          } else {
 361              $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' );
 362          }
 363  
 364          ?>
 365          <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
 366              <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
 367                  <footer class="comment-meta">
 368                      <div class="comment-author vcard">
 369                          <?php
 370                          if ( 0 != $args['avatar_size'] ) {
 371                              echo get_avatar( $comment, $args['avatar_size'] );
 372                          }
 373                          ?>
 374                          <?php
 375                              printf(
 376                                  /* translators: %s: Comment author link. */
 377                                  __( '%s <span class="says">says:</span>' ),
 378                                  sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) )
 379                              );
 380                          ?>
 381                      </div><!-- .comment-author -->
 382  
 383                      <div class="comment-metadata">
 384                          <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
 385                              <time datetime="<?php comment_time( 'c' ); ?>">
 386                                  <?php
 387                                      /* translators: 1: Comment date, 2: Comment time. */
 388                                      printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
 389                                  ?>
 390                              </time>
 391                          </a>
 392                          <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
 393                      </div><!-- .comment-metadata -->
 394  
 395                      <?php if ( '0' == $comment->comment_approved ) : ?>
 396                      <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
 397                      <?php endif; ?>
 398                  </footer><!-- .comment-meta -->
 399  
 400                  <div class="comment-content">
 401                      <?php comment_text(); ?>
 402                  </div><!-- .comment-content -->
 403  
 404                  <?php
 405                  comment_reply_link(
 406                      array_merge(
 407                          $args,
 408                          array(
 409                              'add_below' => 'div-comment',
 410                              'depth'     => $depth,
 411                              'max_depth' => $args['max_depth'],
 412                              'before'    => '<div class="reply">',
 413                              'after'     => '</div>',
 414                          )
 415                      )
 416                  );
 417                  ?>
 418              </article><!-- .comment-body -->
 419          <?php
 420      }
 421  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7