PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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