3 * Comment API: Walker_Comment class
11 * HTML comment list class.
16 class Walker_Comment extends Walker {
18 * What the class handles.
20 * @see Walker::$tree_type
25 public $tree_type = 'comment';
30 * @see Walker::$db_fields
35 public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
38 * Start the list before the elements are added.
40 * @see Walker::start_lvl()
44 * @global int $comment_depth
46 * @param string $output Passed by reference. Used to append additional content.
47 * @param int $depth Depth of comment.
48 * @param array $args Uses 'style' argument for type of HTML list.
50 public function start_lvl( &$output, $depth = 0, $args = array() ) {
51 $GLOBALS['comment_depth'] = $depth + 1;
53 switch ( $args['style'] ) {
57 $output .= '<ol class="children">' . "\n";
61 $output .= '<ul class="children">' . "\n";
67 * End the list of items after the elements are added.
69 * @see Walker::end_lvl()
73 * @global int $comment_depth
75 * @param string $output Passed by reference. Used to append additional content.
76 * @param int $depth Depth of comment.
77 * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
79 public function end_lvl( &$output, $depth = 0, $args = array() ) {
80 $GLOBALS['comment_depth'] = $depth + 1;
82 switch ( $args['style'] ) {
86 $output .= "</ol><!-- .children -->\n";
90 $output .= "</ul><!-- .children -->\n";
96 * Traverse elements to create list from elements.
98 * This function is designed to enhance Walker::display_element() to
99 * display children of higher nesting levels than selected inline on
100 * the highest depth level displayed. This prevents them being orphaned
101 * at the end of the comment list.
103 * Example: max_depth = 2, with 5 levels of nested content.
114 * @see Walker::display_element()
115 * @see wp_list_comments()
119 * @param object $element Data object.
120 * @param array $children_elements List of elements to continue traversing.
121 * @param int $max_depth Max depth to traverse.
122 * @param int $depth Depth of current element.
123 * @param array $args An array of arguments.
124 * @param string $output Passed by reference. Used to append additional content.
126 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
130 $id_field = $this->db_fields['id'];
131 $id = $element->$id_field;
133 parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
136 * If we're at the max depth, and the current element still has children,
137 * loop over those and display them at this level. This is to prevent them
138 * being orphaned to the end of the list.
140 if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
141 foreach ( $children_elements[ $id ] as $child )
142 $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
144 unset( $children_elements[ $id ] );
150 * Start the element output.
154 * @see Walker::start_el()
155 * @see wp_list_comments()
157 * @global int $comment_depth
158 * @global WP_Comment $comment
160 * @param string $output Passed by reference. Used to append additional content.
161 * @param object $comment Comment data object.
162 * @param int $depth Depth of comment in reference to parents.
163 * @param array $args An array of arguments.
165 public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
167 $GLOBALS['comment_depth'] = $depth;
168 $GLOBALS['comment'] = $comment;
170 if ( !empty( $args['callback'] ) ) {
172 call_user_func( $args['callback'], $comment, $args, $depth );
173 $output .= ob_get_clean();
177 if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
179 $this->ping( $comment, $depth, $args );
180 $output .= ob_get_clean();
181 } elseif ( 'html5' === $args['format'] ) {
183 $this->html5_comment( $comment, $depth, $args );
184 $output .= ob_get_clean();
187 $this->comment( $comment, $depth, $args );
188 $output .= ob_get_clean();
193 * Ends the element output, if needed.
197 * @see Walker::end_el()
198 * @see wp_list_comments()
200 * @param string $output Passed by reference. Used to append additional content.
201 * @param WP_Comment $comment The comment object. Default current comment.
202 * @param int $depth Depth of comment.
203 * @param array $args An array of arguments.
205 public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
206 if ( !empty( $args['end-callback'] ) ) {
208 call_user_func( $args['end-callback'], $comment, $args, $depth );
209 $output .= ob_get_clean();
212 if ( 'div' == $args['style'] )
213 $output .= "</div><!-- #comment-## -->\n";
215 $output .= "</li><!-- #comment-## -->\n";
219 * Output a pingback comment.
224 * @see wp_list_comments()
226 * @param WP_Comment $comment The comment object.
227 * @param int $depth Depth of comment.
228 * @param array $args An array of arguments.
230 protected function ping( $comment, $depth, $args ) {
231 $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
233 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
234 <div class="comment-body">
235 <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
241 * Output a single comment.
246 * @see wp_list_comments()
248 * @param object $comment Comment to display.
249 * @param int $depth Depth of comment.
250 * @param array $args An array of arguments.
252 protected function comment( $comment, $depth, $args ) {
253 if ( 'div' == $args['style'] ) {
255 $add_below = 'comment';
258 $add_below = 'div-comment';
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">
265 <div class="comment-author vcard">
266 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
267 <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link( $comment ) ); ?>
269 <?php if ( '0' == $comment->comment_approved ) : ?>
270 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
274 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
276 /* translators: 1: comment date, 2: comment time */
277 printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' );
281 <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
284 comment_reply_link( array_merge( $args, array(
285 'add_below' => $add_below,
287 'max_depth' => $args['max_depth'],
288 'before' => '<div class="reply">',
293 <?php if ( 'div' != $args['style'] ) : ?>
300 * Output a comment in the HTML5 format.
305 * @see wp_list_comments()
307 * @param object $comment Comment to display.
308 * @param int $depth Depth of comment.
309 * @param array $args An array of arguments.
311 protected function html5_comment( $comment, $depth, $args ) {
312 $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
314 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
315 <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
316 <footer class="comment-meta">
317 <div class="comment-author vcard">
318 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
319 <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) ) ); ?>
320 </div><!-- .comment-author -->
322 <div class="comment-metadata">
323 <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
324 <time datetime="<?php comment_time( 'c' ); ?>">
326 /* translators: 1: comment date, 2: comment time */
327 printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
331 <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
332 </div><!-- .comment-metadata -->
334 <?php if ( '0' == $comment->comment_approved ) : ?>
335 <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
337 </footer><!-- .comment-meta -->
339 <div class="comment-content">
340 <?php comment_text(); ?>
341 </div><!-- .comment-content -->
344 comment_reply_link( array_merge( $args, array(
345 'add_below' => 'div-comment',
347 'max_depth' => $args['max_depth'],
348 'before' => '<div class="reply">',
352 </article><!-- .comment-body -->