PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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