class-walker-comment.php 11 KB

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