Walker_Comment

Core walker class used to create an HTML list of comments.

Defined (1)

The class is defined in the following location(s).

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