PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wp-includes/class-walker-comment.php

https://gitlab.com/morganestes/wordpress-develop
PHP | 400 lines | 214 code | 28 blank | 158 comment | 32 complexity | 0b73bbc91896b0a57395e48efcdff78d MD5 | raw file
  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
  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. ?>
  248. <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
  249. <?php if ( 'div' != $args['style'] ) : ?>
  250. <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  251. <?php endif; ?>
  252. <div class="comment-author vcard">
  253. <?php
  254. if ( 0 != $args['avatar_size'] ) {
  255. echo get_avatar( $comment, $args['avatar_size'] );}
  256. ?>
  257. <?php
  258. /* translators: %s: comment author link */
  259. printf(
  260. __( '%s <span class="says">says:</span>' ),
  261. sprintf( '<cite class="fn">%s</cite>', get_comment_author_link( $comment ) )
  262. );
  263. ?>
  264. </div>
  265. <?php if ( '0' == $comment->comment_approved ) : ?>
  266. <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
  267. <br />
  268. <?php endif; ?>
  269. <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
  270. <?php
  271. /* translators: 1: comment date, 2: comment time */
  272. printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
  273. ?>
  274. </a>
  275. <?php
  276. edit_comment_link( __( '(Edit)' ), '&nbsp;&nbsp;', '' );
  277. ?>
  278. </div>
  279. <?php
  280. comment_text(
  281. $comment, array_merge(
  282. $args, array(
  283. 'add_below' => $add_below,
  284. 'depth' => $depth,
  285. 'max_depth' => $args['max_depth'],
  286. )
  287. )
  288. );
  289. ?>
  290. <?php
  291. comment_reply_link(
  292. array_merge(
  293. $args, array(
  294. 'add_below' => $add_below,
  295. 'depth' => $depth,
  296. 'max_depth' => $args['max_depth'],
  297. 'before' => '<div class="reply">',
  298. 'after' => '</div>',
  299. )
  300. )
  301. );
  302. ?>
  303. <?php if ( 'div' != $args['style'] ) : ?>
  304. </div>
  305. <?php endif; ?>
  306. <?php
  307. }
  308. /**
  309. * Outputs a comment in the HTML5 format.
  310. *
  311. * @since 3.6.0
  312. *
  313. * @see wp_list_comments()
  314. *
  315. * @param WP_Comment $comment Comment to display.
  316. * @param int $depth Depth of the current comment.
  317. * @param array $args An array of arguments.
  318. */
  319. protected function html5_comment( $comment, $depth, $args ) {
  320. $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
  321. ?>
  322. <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
  323. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  324. <footer class="comment-meta">
  325. <div class="comment-author vcard">
  326. <?php
  327. if ( 0 != $args['avatar_size'] ) {
  328. echo get_avatar( $comment, $args['avatar_size'] );}
  329. ?>
  330. <?php
  331. /* translators: %s: comment author link */
  332. printf(
  333. __( '%s <span class="says">says:</span>' ),
  334. sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) )
  335. );
  336. ?>
  337. </div><!-- .comment-author -->
  338. <div class="comment-metadata">
  339. <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
  340. <time datetime="<?php comment_time( 'c' ); ?>">
  341. <?php
  342. /* translators: 1: comment date, 2: comment time */
  343. printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
  344. ?>
  345. </time>
  346. </a>
  347. <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  348. </div><!-- .comment-metadata -->
  349. <?php if ( '0' == $comment->comment_approved ) : ?>
  350. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
  351. <?php endif; ?>
  352. </footer><!-- .comment-meta -->
  353. <div class="comment-content">
  354. <?php comment_text(); ?>
  355. </div><!-- .comment-content -->
  356. <?php
  357. comment_reply_link(
  358. array_merge(
  359. $args, array(
  360. 'add_below' => 'div-comment',
  361. 'depth' => $depth,
  362. 'max_depth' => $args['max_depth'],
  363. 'before' => '<div class="reply">',
  364. 'after' => '</div>',
  365. )
  366. )
  367. );
  368. ?>
  369. </article><!-- .comment-body -->
  370. <?php
  371. }
  372. }