3 * Comment API: Walker_Comment class
11 * Core walker class used to create an HTML list of comments.
17 class Walker_Comment extends Walker {
20 * What the class handles.
26 * @see Walker::$tree_type
28 public $tree_type = 'comment';
31 * Database fields to use.
37 * @see Walker::$db_fields
40 public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
43 * Starts the list before the elements are added.
48 * @see Walker::start_lvl()
49 * @global int $comment_depth
51 * @param string $output Passed by reference. Used to append additional content.
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.
55 public function start_lvl( &$output, $depth = 0, $args = array() ) {
56 $GLOBALS['comment_depth'] = $depth + 1;
58 switch ( $args['style'] ) {
62 $output .= '<ol class="children">' . "\n";
66 $output .= '<ul class="children">' . "\n";
72 * Ends the list of items after the elements are added.
77 * @see Walker::end_lvl()
78 * @global int $comment_depth
80 * @param string $output Passed by reference. Used to append additional content.
81 * @param int $depth Optional. Depth of the current comment. Default 0.
82 * @param array $args Optional. Will only append content if style argument value is 'ol' or 'ul'.
83 * Default empty array.
85 public function end_lvl( &$output, $depth = 0, $args = array() ) {
86 $GLOBALS['comment_depth'] = $depth + 1;
88 switch ( $args['style'] ) {
92 $output .= "</ol><!-- .children -->\n";
96 $output .= "</ul><!-- .children -->\n";
102 * Traverses elements to create list from elements.
104 * This function is designed to enhance Walker::display_element() to
105 * display children of higher nesting levels than selected inline on
106 * the highest depth level displayed. This prevents them being orphaned
107 * at the end of the comment list.
109 * Example: max_depth = 2, with 5 levels of nested content.
123 * @see Walker::display_element()
124 * @see wp_list_comments()
126 * @param WP_Comment $element Comment data object.
127 * @param array $children_elements List of elements to continue traversing. Passed by reference.
128 * @param int $max_depth Max depth to traverse.
129 * @param int $depth Depth of the current element.
130 * @param array $args An array of arguments.
131 * @param string $output Used to append additional content. Passed by reference.
133 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
137 $id_field = $this->db_fields['id'];
138 $id = $element->$id_field;
140 parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
143 * If at the max depth, and the current element still has children, loop over those
144 * and display them at this level. This is to prevent them being orphaned to the end
147 if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
148 foreach ( $children_elements[ $id ] as $child )
149 $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
151 unset( $children_elements[ $id ] );
157 * Starts the element output.
162 * @see Walker::start_el()
163 * @see wp_list_comments()
164 * @global int $comment_depth
165 * @global WP_Comment $comment
167 * @param string $output Used to append additional content. Passed by reference.
168 * @param WP_Comment $comment 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 $id Optional. ID of the current comment. Default 0 (unused).
173 public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
175 $GLOBALS['comment_depth'] = $depth;
176 $GLOBALS['comment'] = $comment;
178 if ( !empty( $args['callback'] ) ) {
180 call_user_func( $args['callback'], $comment, $args, $depth );
181 $output .= ob_get_clean();
185 if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
187 $this->ping( $comment, $depth, $args );
188 $output .= ob_get_clean();
189 } elseif ( 'html5' === $args['format'] ) {
191 $this->html5_comment( $comment, $depth, $args );
192 $output .= ob_get_clean();
195 $this->comment( $comment, $depth, $args );
196 $output .= ob_get_clean();
201 * Ends the element output, if needed.
206 * @see Walker::end_el()
207 * @see wp_list_comments()
209 * @param string $output Used to append additional content. Passed by reference.
210 * @param WP_Comment $comment The current comment object. Default current comment.
211 * @param int $depth Optional. Depth of the current comment. Default 0.
212 * @param array $args Optional. An array of arguments. Default empty array.
214 public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
215 if ( !empty( $args['end-callback'] ) ) {
217 call_user_func( $args['end-callback'], $comment, $args, $depth );
218 $output .= ob_get_clean();
221 if ( 'div' == $args['style'] )
222 $output .= "</div><!-- #comment-## -->\n";
224 $output .= "</li><!-- #comment-## -->\n";
228 * Outputs a pingback comment.
233 * @see wp_list_comments()
235 * @param WP_Comment $comment The comment object.
236 * @param int $depth Depth of the current comment.
237 * @param array $args An array of arguments.
239 protected function ping( $comment, $depth, $args ) {
240 $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
242 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
243 <div class="comment-body">
244 <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
250 * Outputs a single comment.
255 * @see wp_list_comments()
257 * @param WP_Comment $comment Comment to display.
258 * @param int $depth Depth of the current comment.
259 * @param array $args An array of arguments.
261 protected function comment( $comment, $depth, $args ) {
262 if ( 'div' == $args['style'] ) {
264 $add_below = 'comment';
267 $add_below = 'div-comment';
270 <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
271 <?php if ( 'div' != $args['style'] ) : ?>
272 <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
274 <div class="comment-author vcard">
275 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
276 <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link( $comment ) ); ?>
278 <?php if ( '0' == $comment->comment_approved ) : ?>
279 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
283 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
285 /* translators: 1: comment date, 2: comment time */
286 printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' );
290 <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
293 comment_reply_link( array_merge( $args, array(
294 'add_below' => $add_below,
296 'max_depth' => $args['max_depth'],
297 'before' => '<div class="reply">',
302 <?php if ( 'div' != $args['style'] ) : ?>
309 * Outputs a comment in the HTML5 format.
314 * @see wp_list_comments()
316 * @param WP_Comment $comment Comment to display.
317 * @param int $depth Depth of the current comment.
318 * @param array $args An array of arguments.
320 protected function html5_comment( $comment, $depth, $args ) {
321 $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
323 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
324 <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
325 <footer class="comment-meta">
326 <div class="comment-author vcard">
327 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
328 <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) ) ); ?>
329 </div><!-- .comment-author -->
331 <div class="comment-metadata">
332 <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
333 <time datetime="<?php comment_time( 'c' ); ?>">
335 /* translators: 1: comment date, 2: comment time */
336 printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
340 <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
341 </div><!-- .comment-metadata -->
343 <?php if ( '0' == $comment->comment_approved ) : ?>
344 <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
346 </footer><!-- .comment-meta -->
348 <div class="comment-content">
349 <?php comment_text(); ?>
350 </div><!-- .comment-content -->
353 comment_reply_link( array_merge( $args, array(
354 'add_below' => 'div-comment',
356 'max_depth' => $args['max_depth'],
357 'before' => '<div class="reply">',
361 </article><!-- .comment-body -->