class-walker-comment.php 12 KB

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