home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / class-walker-comment.php < prev    next >
Encoding:
PHP Script  |  2017-10-02  |  10.9 KB  |  365 lines

  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 ('parent' => 'comment_parent', 'id' => 'comment_ID');
  39.  
  40.     /**
  41.      * Starts the list before the elements are added.
  42.      *
  43.      * @since 2.7.0
  44.      *
  45.      * @see Walker::start_lvl()
  46.      * @global int $comment_depth
  47.      *
  48.      * @param string $output Used to append additional content (passed by reference).
  49.      * @param int    $depth  Optional. Depth of the current comment. Default 0.
  50.      * @param array  $args   Optional. Uses 'style' argument for type of HTML list. Default empty array.
  51.      */
  52.     public function start_lvl( &$output, $depth = 0, $args = array() ) {
  53.         $GLOBALS['comment_depth'] = $depth + 1;
  54.  
  55.         switch ( $args['style'] ) {
  56.             case 'div':
  57.                 break;
  58.             case 'ol':
  59.                 $output .= '<ol class="children">' . "\n";
  60.                 break;
  61.             case 'ul':
  62.             default:
  63.                 $output .= '<ul class="children">' . "\n";
  64.                 break;
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Ends the list of items after the elements are added.
  70.      *
  71.      * @since 2.7.0
  72.      *
  73.      * @see Walker::end_lvl()
  74.      * @global int $comment_depth
  75.      *
  76.      * @param string $output Used to append additional content (passed by reference).
  77.      * @param int    $depth  Optional. Depth of the current comment. Default 0.
  78.      * @param array  $args   Optional. Will only append content if style argument value is 'ol' or 'ul'.
  79.      *                       Default empty array.
  80.      */
  81.     public function end_lvl( &$output, $depth = 0, $args = array() ) {
  82.         $GLOBALS['comment_depth'] = $depth + 1;
  83.  
  84.         switch ( $args['style'] ) {
  85.             case 'div':
  86.                 break;
  87.             case 'ol':
  88.                 $output .= "</ol><!-- .children -->\n";
  89.                 break;
  90.             case 'ul':
  91.             default:
  92.                 $output .= "</ul><!-- .children -->\n";
  93.                 break;
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Traverses elements to create list from elements.
  99.      *
  100.      * This function is designed to enhance Walker::display_element() to
  101.      * display children of higher nesting levels than selected inline on
  102.      * the highest depth level displayed. This prevents them being orphaned
  103.      * at the end of the comment list.
  104.      *
  105.      * Example: max_depth = 2, with 5 levels of nested content.
  106.      *     1
  107.      *      1.1
  108.      *        1.1.1
  109.      *        1.1.1.1
  110.      *        1.1.1.1.1
  111.      *        1.1.2
  112.      *        1.1.2.1
  113.      *     2
  114.      *      2.2
  115.      *
  116.      * @since 2.7.0
  117.      *
  118.      * @see Walker::display_element()
  119.      * @see wp_list_comments()
  120.      *
  121.      * @param WP_Comment $element           Comment data object.
  122.      * @param array      $children_elements List of elements to continue traversing. Passed by reference.
  123.      * @param int        $max_depth         Max depth to traverse.
  124.      * @param int        $depth             Depth of the current element.
  125.      * @param array      $args              An array of arguments.
  126.      * @param string     $output            Used to append additional content. Passed by reference.
  127.      */
  128.     public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  129.         if ( !$element )
  130.             return;
  131.  
  132.         $id_field = $this->db_fields['id'];
  133.         $id = $element->$id_field;
  134.  
  135.         parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  136.  
  137.         /*
  138.          * If at the max depth, and the current element still has children, loop over those
  139.          * and display them at this level. This is to prevent them being orphaned to the end
  140.          * of the list.
  141.          */
  142.         if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
  143.             foreach ( $children_elements[ $id ] as $child )
  144.                 $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
  145.  
  146.             unset( $children_elements[ $id ] );
  147.         }
  148.  
  149.     }
  150.  
  151.     /**
  152.      * Starts the element output.
  153.      *
  154.      * @since 2.7.0
  155.      *
  156.      * @see Walker::start_el()
  157.      * @see wp_list_comments()
  158.      * @global int        $comment_depth
  159.      * @global WP_Comment $comment
  160.      *
  161.      * @param string     $output  Used to append additional content. Passed by reference.
  162.      * @param WP_Comment $comment Comment data object.
  163.      * @param int        $depth   Optional. Depth of the current comment in reference to parents. Default 0.
  164.      * @param array      $args    Optional. An array of arguments. Default empty array.
  165.      * @param int        $id      Optional. ID of the current comment. Default 0 (unused).
  166.      */
  167.     public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
  168.         $depth++;
  169.         $GLOBALS['comment_depth'] = $depth;
  170.         $GLOBALS['comment'] = $comment;
  171.  
  172.         if ( !empty( $args['callback'] ) ) {
  173.             ob_start();
  174.             call_user_func( $args['callback'], $comment, $args, $depth );
  175.             $output .= ob_get_clean();
  176.             return;
  177.         }
  178.  
  179.         if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
  180.             ob_start();
  181.             $this->ping( $comment, $depth, $args );
  182.             $output .= ob_get_clean();
  183.         } elseif ( 'html5' === $args['format'] ) {
  184.             ob_start();
  185.             $this->html5_comment( $comment, $depth, $args );
  186.             $output .= ob_get_clean();
  187.         } else {
  188.             ob_start();
  189.             $this->comment( $comment, $depth, $args );
  190.             $output .= ob_get_clean();
  191.         }
  192.     }
  193.  
  194.     /**
  195.      * Ends the element output, if needed.
  196.      *
  197.      * @since 2.7.0
  198.      *
  199.      * @see Walker::end_el()
  200.      * @see wp_list_comments()
  201.      *
  202.      * @param string     $output  Used to append additional content. Passed by reference.
  203.      * @param WP_Comment $comment The current comment object. Default current comment.
  204.      * @param int        $depth   Optional. Depth of the current comment. Default 0.
  205.      * @param array      $args    Optional. An array of arguments. Default empty array.
  206.      */
  207.     public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
  208.         if ( !empty( $args['end-callback'] ) ) {
  209.             ob_start();
  210.             call_user_func( $args['end-callback'], $comment, $args, $depth );
  211.             $output .= ob_get_clean();
  212.             return;
  213.         }
  214.         if ( 'div' == $args['style'] )
  215.             $output .= "</div><!-- #comment-## -->\n";
  216.         else
  217.             $output .= "</li><!-- #comment-## -->\n";
  218.     }
  219.  
  220.     /**
  221.      * Outputs a pingback comment.
  222.      *
  223.      * @since 3.6.0
  224.      *
  225.      * @see wp_list_comments()
  226.      *
  227.      * @param WP_Comment $comment The comment object.
  228.      * @param int        $depth   Depth of the current comment.
  229.      * @param array      $args    An array of arguments.
  230.      */
  231.     protected function ping( $comment, $depth, $args ) {
  232.         $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
  233. ?>
  234.         <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
  235.             <div class="comment-body">
  236.                 <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  237.             </div>
  238. <?php
  239.     }
  240.  
  241.     /**
  242.      * Outputs a single comment.
  243.      *
  244.      * @since 3.6.0
  245.      *
  246.      * @see wp_list_comments()
  247.      *
  248.      * @param WP_Comment $comment Comment to display.
  249.      * @param int        $depth   Depth of the current comment.
  250.      * @param array      $args    An array of arguments.
  251.      */
  252.     protected function comment( $comment, $depth, $args ) {
  253.         if ( 'div' == $args['style'] ) {
  254.             $tag = 'div';
  255.             $add_below = 'comment';
  256.         } else {
  257.             $tag = 'li';
  258.             $add_below = 'div-comment';
  259.         }
  260. ?>
  261.         <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
  262.         <?php if ( 'div' != $args['style'] ) : ?>
  263.         <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  264.         <?php endif; ?>
  265.         <div class="comment-author vcard">
  266.             <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  267.             <?php
  268.                 /* translators: %s: comment author link */
  269.                 printf( __( '%s <span class="says">says:</span>' ),
  270.                     sprintf( '<cite class="fn">%s</cite>', get_comment_author_link( $comment ) )
  271.                 );
  272.             ?>
  273.         </div>
  274.         <?php if ( '0' == $comment->comment_approved ) : ?>
  275.         <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
  276.         <br />
  277.         <?php endif; ?>
  278.  
  279.         <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
  280.             <?php
  281.                 /* translators: 1: comment date, 2: comment time */
  282.                 printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '  ', '' );
  283.             ?>
  284.         </div>
  285.  
  286.         <?php comment_text( $comment, array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  287.  
  288.         <?php
  289.         comment_reply_link( array_merge( $args, array(
  290.             'add_below' => $add_below,
  291.             'depth'     => $depth,
  292.             'max_depth' => $args['max_depth'],
  293.             'before'    => '<div class="reply">',
  294.             'after'     => '</div>'
  295.         ) ) );
  296.         ?>
  297.  
  298.         <?php if ( 'div' != $args['style'] ) : ?>
  299.         </div>
  300.         <?php endif; ?>
  301. <?php
  302.     }
  303.  
  304.     /**
  305.      * Outputs a comment in the HTML5 format.
  306.      *
  307.      * @since 3.6.0
  308.      *
  309.      * @see wp_list_comments()
  310.      *
  311.      * @param WP_Comment $comment Comment to display.
  312.      * @param int        $depth   Depth of the current comment.
  313.      * @param array      $args    An array of arguments.
  314.      */
  315.     protected function html5_comment( $comment, $depth, $args ) {
  316.         $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
  317. ?>
  318.         <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
  319.             <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  320.                 <footer class="comment-meta">
  321.                     <div class="comment-author vcard">
  322.                         <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  323.                         <?php
  324.                             /* translators: %s: comment author link */
  325.                             printf( __( '%s <span class="says">says:</span>' ),
  326.                                 sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) )
  327.                             );
  328.                         ?>
  329.                     </div><!-- .comment-author -->
  330.  
  331.                     <div class="comment-metadata">
  332.                         <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
  333.                             <time datetime="<?php comment_time( 'c' ); ?>">
  334.                                 <?php
  335.                                     /* translators: 1: comment date, 2: comment time */
  336.                                     printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
  337.                                 ?>
  338.                             </time>
  339.                         </a>
  340.                         <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  341.                     </div><!-- .comment-metadata -->
  342.  
  343.                     <?php if ( '0' == $comment->comment_approved ) : ?>
  344.                     <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
  345.                     <?php endif; ?>
  346.                 </footer><!-- .comment-meta -->
  347.  
  348.                 <div class="comment-content">
  349.                     <?php comment_text(); ?>
  350.                 </div><!-- .comment-content -->
  351.  
  352.                 <?php
  353.                 comment_reply_link( array_merge( $args, array(
  354.                     'add_below' => 'div-comment',
  355.                     'depth'     => $depth,
  356.                     'max_depth' => $args['max_depth'],
  357.                     'before'    => '<div class="reply">',
  358.                     'after'     => '</div>'
  359.                 ) ) );
  360.                 ?>
  361.             </article><!-- .comment-body -->
  362. <?php
  363.     }
  364. }
  365.