You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

2465 lines
85 KiB

  1. <?php
  2. /**
  3. * Comment template functions
  4. *
  5. * These functions are meant to live inside of the WordPress loop.
  6. *
  7. * @package WordPress
  8. * @subpackage Template
  9. */
  10. /**
  11. * Retrieve the author of the current comment.
  12. *
  13. * If the comment has an empty comment_author field, then 'Anonymous' person is
  14. * assumed.
  15. *
  16. * @since 1.5.0
  17. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  18. *
  19. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author.
  20. * Default current comment.
  21. * @return string The comment author
  22. */
  23. function get_comment_author( $comment_ID = 0 ) {
  24. $comment = get_comment( $comment_ID );
  25. if ( empty( $comment->comment_author ) ) {
  26. if ( $comment->user_id && $user = get_userdata( $comment->user_id ) )
  27. $author = $user->display_name;
  28. else
  29. $author = __('Anonymous');
  30. } else {
  31. $author = $comment->comment_author;
  32. }
  33. /**
  34. * Filters the returned comment author name.
  35. *
  36. * @since 1.5.0
  37. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  38. *
  39. * @param string $author The comment author's username.
  40. * @param int $comment_ID The comment ID.
  41. * @param WP_Comment $comment The comment object.
  42. */
  43. return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
  44. }
  45. /**
  46. * Displays the author of the current comment.
  47. *
  48. * @since 0.71
  49. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  50. *
  51. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author.
  52. * Default current comment.
  53. */
  54. function comment_author( $comment_ID = 0 ) {
  55. $comment = get_comment( $comment_ID );
  56. $author = get_comment_author( $comment );
  57. /**
  58. * Filters the comment author's name for display.
  59. *
  60. * @since 1.2.0
  61. * @since 4.1.0 The `$comment_ID` parameter was added.
  62. *
  63. * @param string $author The comment author's username.
  64. * @param int $comment_ID The comment ID.
  65. */
  66. echo apply_filters( 'comment_author', $author, $comment->comment_ID );
  67. }
  68. /**
  69. * Retrieve the email of the author of the current comment.
  70. *
  71. * @since 1.5.0
  72. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  73. *
  74. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's email.
  75. * Default current comment.
  76. * @return string The current comment author's email
  77. */
  78. function get_comment_author_email( $comment_ID = 0 ) {
  79. $comment = get_comment( $comment_ID );
  80. /**
  81. * Filters the comment author's returned email address.
  82. *
  83. * @since 1.5.0
  84. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  85. *
  86. * @param string $comment_author_email The comment author's email address.
  87. * @param int $comment_ID The comment ID.
  88. * @param WP_Comment $comment The comment object.
  89. */
  90. return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
  91. }
  92. /**
  93. * Display the email of the author of the current global $comment.
  94. *
  95. * Care should be taken to protect the email address and assure that email
  96. * harvesters do not capture your commentors' email address. Most assume that
  97. * their email address will not appear in raw form on the site. Doing so will
  98. * enable anyone, including those that people don't want to get the email
  99. * address and use it for their own means good and bad.
  100. *
  101. * @since 0.71
  102. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  103. *
  104. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's email.
  105. * Default current comment.
  106. */
  107. function comment_author_email( $comment_ID = 0 ) {
  108. $comment = get_comment( $comment_ID );
  109. $author_email = get_comment_author_email( $comment );
  110. /**
  111. * Filters the comment author's email for display.
  112. *
  113. * @since 1.2.0
  114. * @since 4.1.0 The `$comment_ID` parameter was added.
  115. *
  116. * @param string $author_email The comment author's email address.
  117. * @param int $comment_ID The comment ID.
  118. */
  119. echo apply_filters( 'author_email', $author_email, $comment->comment_ID );
  120. }
  121. /**
  122. * Display the html email link to the author of the current comment.
  123. *
  124. * Care should be taken to protect the email address and assure that email
  125. * harvesters do not capture your commentors' email address. Most assume that
  126. * their email address will not appear in raw form on the site. Doing so will
  127. * enable anyone, including those that people don't want to get the email
  128. * address and use it for their own means good and bad.
  129. *
  130. * @since 0.71
  131. * @since 4.6.0 Added the `$comment` parameter.
  132. *
  133. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  134. * Default empty.
  135. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  136. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  137. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
  138. */
  139. function comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
  140. if ( $link = get_comment_author_email_link( $linktext, $before, $after, $comment ) ) {
  141. echo $link;
  142. }
  143. }
  144. /**
  145. * Return the html email link to the author of the current comment.
  146. *
  147. * Care should be taken to protect the email address and assure that email
  148. * harvesters do not capture your commentors' email address. Most assume that
  149. * their email address will not appear in raw form on the site. Doing so will
  150. * enable anyone, including those that people don't want to get the email
  151. * address and use it for their own means good and bad.
  152. *
  153. * @since 2.7.0
  154. * @since 4.6.0 Added the `$comment` parameter.
  155. *
  156. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  157. * Default empty.
  158. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  159. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  160. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
  161. * @return string HTML markup for the comment author email link. By default, the email address is obfuscated
  162. * via the {@see 'comment_email'} filter with antispambot().
  163. */
  164. function get_comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
  165. $comment = get_comment( $comment );
  166. /**
  167. * Filters the comment author's email for display.
  168. *
  169. * Care should be taken to protect the email address and assure that email
  170. * harvesters do not capture your commenter's email address.
  171. *
  172. * @since 1.2.0
  173. * @since 4.1.0 The `$comment` parameter was added.
  174. *
  175. * @param string $comment_author_email The comment author's email address.
  176. * @param WP_Comment $comment The comment object.
  177. */
  178. $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
  179. if ((!empty($email)) && ($email != '@')) {
  180. $display = ($linktext != '') ? $linktext : $email;
  181. $return = $before;
  182. $return .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( 'mailto:' . $email ), esc_html( $display ) );
  183. $return .= $after;
  184. return $return;
  185. } else {
  186. return '';
  187. }
  188. }
  189. /**
  190. * Retrieve the HTML link to the URL of the author of the current comment.
  191. *
  192. * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
  193. * which falls back to the global comment variable if the $comment_ID argument is empty.
  194. *
  195. * @since 1.5.0
  196. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  197. *
  198. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's link.
  199. * Default current comment.
  200. * @return string The comment author name or HTML link for author's URL.
  201. */
  202. function get_comment_author_link( $comment_ID = 0 ) {
  203. $comment = get_comment( $comment_ID );
  204. $url = get_comment_author_url( $comment );
  205. $author = get_comment_author( $comment );
  206. if ( empty( $url ) || 'http://' == $url )
  207. $return = $author;
  208. else
  209. $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
  210. /**
  211. * Filters the comment author's link for display.
  212. *
  213. * @since 1.5.0
  214. * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
  215. *
  216. * @param string $return The HTML-formatted comment author link.
  217. * Empty for an invalid URL.
  218. * @param string $author The comment author's username.
  219. * @param int $comment_ID The comment ID.
  220. */
  221. return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
  222. }
  223. /**
  224. * Display the html link to the url of the author of the current comment.
  225. *
  226. * @since 0.71
  227. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  228. *
  229. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's link.
  230. * Default current comment.
  231. */
  232. function comment_author_link( $comment_ID = 0 ) {
  233. echo get_comment_author_link( $comment_ID );
  234. }
  235. /**
  236. * Retrieve the IP address of the author of the current comment.
  237. *
  238. * @since 1.5.0
  239. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  240. *
  241. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's IP address.
  242. * Default current comment.
  243. * @return string Comment author's IP address.
  244. */
  245. function get_comment_author_IP( $comment_ID = 0 ) {
  246. $comment = get_comment( $comment_ID );
  247. /**
  248. * Filters the comment author's returned IP address.
  249. *
  250. * @since 1.5.0
  251. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  252. *
  253. * @param string $comment_author_IP The comment author's IP address.
  254. * @param int $comment_ID The comment ID.
  255. * @param WP_Comment $comment The comment object.
  256. */
  257. return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment );
  258. }
  259. /**
  260. * Display the IP address of the author of the current comment.
  261. *
  262. * @since 0.71
  263. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  264. *
  265. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's IP address.
  266. * Default current comment.
  267. */
  268. function comment_author_IP( $comment_ID = 0 ) {
  269. echo esc_html( get_comment_author_IP( $comment_ID ) );
  270. }
  271. /**
  272. * Retrieve the url of the author of the current comment.
  273. *
  274. * @since 1.5.0
  275. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  276. *
  277. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's URL.
  278. * Default current comment.
  279. * @return string Comment author URL.
  280. */
  281. function get_comment_author_url( $comment_ID = 0 ) {
  282. $comment = get_comment( $comment_ID );
  283. $url = '';
  284. $id = 0;
  285. if ( ! empty( $comment ) ) {
  286. $author_url = ( 'http://' == $comment->comment_author_url ) ? '' : $comment->comment_author_url;
  287. $url = esc_url( $author_url, array( 'http', 'https' ) );
  288. $id = $comment->ID;
  289. }
  290. /**
  291. * Filters the comment author's URL.
  292. *
  293. * @since 1.5.0
  294. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  295. *
  296. * @param string $url The comment author's URL.
  297. * @param int $comment_ID The comment ID.
  298. * @param WP_Comment $comment The comment object.
  299. */
  300. return apply_filters( 'get_comment_author_url', $url, $id, $comment );
  301. }
  302. /**
  303. * Display the url of the author of the current comment.
  304. *
  305. * @since 0.71
  306. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  307. *
  308. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's URL.
  309. * Default current comment.
  310. */
  311. function comment_author_url( $comment_ID = 0 ) {
  312. $comment = get_comment( $comment_ID );
  313. $author_url = get_comment_author_url( $comment );
  314. /**
  315. * Filters the comment author's URL for display.
  316. *
  317. * @since 1.2.0
  318. * @since 4.1.0 The `$comment_ID` parameter was added.
  319. *
  320. * @param string $author_url The comment author's URL.
  321. * @param int $comment_ID The comment ID.
  322. */
  323. echo apply_filters( 'comment_url', $author_url, $comment->comment_ID );
  324. }
  325. /**
  326. * Retrieves the HTML link of the url of the author of the current comment.
  327. *
  328. * $linktext parameter is only used if the URL does not exist for the comment
  329. * author. If the URL does exist then the URL will be used and the $linktext
  330. * will be ignored.
  331. *
  332. * Encapsulate the HTML link between the $before and $after. So it will appear
  333. * in the order of $before, link, and finally $after.
  334. *
  335. * @since 1.5.0
  336. * @since 4.6.0 Added the `$comment` parameter.
  337. *
  338. * @param string $linktext Optional. The text to display instead of the comment
  339. * author's email address. Default empty.
  340. * @param string $before Optional. The text or HTML to display before the email link.
  341. * Default empty.
  342. * @param string $after Optional. The text or HTML to display after the email link.
  343. * Default empty.
  344. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
  345. * Default is the current comment.
  346. * @return string The HTML link between the $before and $after parameters.
  347. */
  348. function get_comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
  349. $url = get_comment_author_url( $comment );
  350. $display = ($linktext != '') ? $linktext : $url;
  351. $display = str_replace( 'http://www.', '', $display );
  352. $display = str_replace( 'http://', '', $display );
  353. if ( '/' == substr($display, -1) ) {
  354. $display = substr($display, 0, -1);
  355. }
  356. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  357. /**
  358. * Filters the comment author's returned URL link.
  359. *
  360. * @since 1.5.0
  361. *
  362. * @param string $return The HTML-formatted comment author URL link.
  363. */
  364. return apply_filters( 'get_comment_author_url_link', $return );
  365. }
  366. /**
  367. * Displays the HTML link of the url of the author of the current comment.
  368. *
  369. * @since 0.71
  370. * @since 4.6.0 Added the `$comment` parameter.
  371. *
  372. * @param string $linktext Optional. Text to display instead of the comment author's
  373. * email address. Default empty.
  374. * @param string $before Optional. Text or HTML to display before the email link.
  375. * Default empty.
  376. * @param string $after Optional. Text or HTML to display after the email link.
  377. * Default empty.
  378. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
  379. * Default is the current comment.
  380. */
  381. function comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
  382. echo get_comment_author_url_link( $linktext, $before, $after, $comment );
  383. }
  384. /**
  385. * Generates semantic classes for each comment element.
  386. *
  387. * @since 2.7.0
  388. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
  389. *
  390. * @param string|array $class Optional. One or more classes to add to the class list.
  391. * Default empty.
  392. * @param int|WP_Comment $comment Comment ID or WP_Comment object. Default current comment.
  393. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  394. * @param bool $echo Optional. Whether to cho or return the output.
  395. * Default true.
  396. * @return string If `$echo` is false, the class will be returned. Void otherwise.
  397. */
  398. function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
  399. // Separates classes with a single space, collates classes for comment DIV
  400. $class = 'class="' . join( ' ', get_comment_class( $class, $comment, $post_id ) ) . '"';
  401. if ( $echo)
  402. echo $class;
  403. else
  404. return $class;
  405. }
  406. /**
  407. * Returns the classes for the comment div as an array.
  408. *
  409. * @since 2.7.0
  410. * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
  411. *
  412. * @global int $comment_alt
  413. * @global int $comment_depth
  414. * @global int $comment_thread_alt
  415. *
  416. * @param string|array $class Optional. One or more classes to add to the class list. Default empty.
  417. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. Default current comment.
  418. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  419. * @return array An array of classes.
  420. */
  421. function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
  422. global $comment_alt, $comment_depth, $comment_thread_alt;
  423. $classes = array();
  424. $comment = get_comment( $comment_id );
  425. if ( ! $comment ) {
  426. return $classes;
  427. }
  428. // Get the comment type (comment, trackback),
  429. $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
  430. // Add classes for comment authors that are registered users.
  431. if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) {
  432. $classes[] = 'byuser';
  433. $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
  434. // For comment authors who are the author of the post
  435. if ( $post = get_post($post_id) ) {
  436. if ( $comment->user_id === $post->post_author ) {
  437. $classes[] = 'bypostauthor';
  438. }
  439. }
  440. }
  441. if ( empty($comment_alt) )
  442. $comment_alt = 0;
  443. if ( empty($comment_depth) )
  444. $comment_depth = 1;
  445. if ( empty($comment_thread_alt) )
  446. $comment_thread_alt = 0;
  447. if ( $comment_alt % 2 ) {
  448. $classes[] = 'odd';
  449. $classes[] = 'alt';
  450. } else {
  451. $classes[] = 'even';
  452. }
  453. $comment_alt++;
  454. // Alt for top-level comments
  455. if ( 1 == $comment_depth ) {
  456. if ( $comment_thread_alt % 2 ) {
  457. $classes[] = 'thread-odd';
  458. $classes[] = 'thread-alt';
  459. } else {
  460. $classes[] = 'thread-even';
  461. }
  462. $comment_thread_alt++;
  463. }
  464. $classes[] = "depth-$comment_depth";
  465. if ( !empty($class) ) {
  466. if ( !is_array( $class ) )
  467. $class = preg_split('#\s+#', $class);
  468. $classes = array_merge($classes, $class);
  469. }
  470. $classes = array_map('esc_attr', $classes);
  471. /**
  472. * Filters the returned CSS classes for the current comment.
  473. *
  474. * @since 2.7.0
  475. *
  476. * @param array $classes An array of comment classes.
  477. * @param string $class A comma-separated list of additional classes added to the list.
  478. * @param int $comment_id The comment id.
  479. * @param WP_Comment $comment The comment object.
  480. * @param int|WP_Post $post_id The post ID or WP_Post object.
  481. */
  482. return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );
  483. }
  484. /**
  485. * Retrieve the comment date of the current comment.
  486. *
  487. * @since 1.5.0
  488. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  489. *
  490. * @param string $d Optional. The format of the date. Default user's setting.
  491. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the date.
  492. * Default current comment.
  493. * @return string The comment's date.
  494. */
  495. function get_comment_date( $d = '', $comment_ID = 0 ) {
  496. $comment = get_comment( $comment_ID );
  497. if ( '' == $d )
  498. $date = mysql2date(get_option('date_format'), $comment->comment_date);
  499. else
  500. $date = mysql2date($d, $comment->comment_date);
  501. /**
  502. * Filters the returned comment date.
  503. *
  504. * @since 1.5.0
  505. *
  506. * @param string|int $date Formatted date string or Unix timestamp.
  507. * @param string $d The format of the date.
  508. * @param WP_Comment $comment The comment object.
  509. */
  510. return apply_filters( 'get_comment_date', $date, $d, $comment );
  511. }
  512. /**
  513. * Display the comment date of the current comment.
  514. *
  515. * @since 0.71
  516. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  517. *
  518. * @param string $d Optional. The format of the date. Default user's settings.
  519. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the date.
  520. * Default current comment.
  521. */
  522. function comment_date( $d = '', $comment_ID = 0 ) {
  523. echo get_comment_date( $d, $comment_ID );
  524. }
  525. /**
  526. * Retrieve the excerpt of the current comment.
  527. *
  528. * Will cut each word and only output the first 20 words with '&hellip;' at the end.
  529. * If the word count is less than 20, then no truncating is done and no '&hellip;'
  530. * will appear.
  531. *
  532. * @since 1.5.0
  533. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  534. *
  535. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the excerpt.
  536. * Default current comment.
  537. * @return string The maybe truncated comment with 20 words or less.
  538. */
  539. function get_comment_excerpt( $comment_ID = 0 ) {
  540. $comment = get_comment( $comment_ID );
  541. $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
  542. $words = explode( ' ', $comment_text );
  543. /**
  544. * Filters the amount of words used in the comment excerpt.
  545. *
  546. * @since 4.4.0
  547. *
  548. * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
  549. */
  550. $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
  551. $use_ellipsis = count( $words ) > $comment_excerpt_length;
  552. if ( $use_ellipsis ) {
  553. $words = array_slice( $words, 0, $comment_excerpt_length );
  554. }
  555. $excerpt = trim( join( ' ', $words ) );
  556. if ( $use_ellipsis ) {
  557. $excerpt .= '&hellip;';
  558. }
  559. /**
  560. * Filters the retrieved comment excerpt.
  561. *
  562. * @since 1.5.0
  563. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  564. *
  565. * @param string $excerpt The comment excerpt text.
  566. * @param int $comment_ID The comment ID.
  567. * @param WP_Comment $comment The comment object.
  568. */
  569. return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
  570. }
  571. /**
  572. * Display the excerpt of the current comment.
  573. *
  574. * @since 1.2.0
  575. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  576. *
  577. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the excerpt.
  578. * Default current comment.
  579. */
  580. function comment_excerpt( $comment_ID = 0 ) {
  581. $comment = get_comment( $comment_ID );
  582. $comment_excerpt = get_comment_excerpt( $comment );
  583. /**
  584. * Filters the comment excerpt for display.
  585. *
  586. * @since 1.2.0
  587. * @since 4.1.0 The `$comment_ID` parameter was added.
  588. *
  589. * @param string $comment_excerpt The comment excerpt text.
  590. * @param int $comment_ID The comment ID.
  591. */
  592. echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID );
  593. }
  594. /**
  595. * Retrieve the comment id of the current comment.
  596. *
  597. * @since 1.5.0
  598. *
  599. * @return int The comment ID.
  600. */
  601. function get_comment_ID() {
  602. $comment = get_comment();
  603. /**
  604. * Filters the returned comment ID.
  605. *
  606. * @since 1.5.0
  607. * @since 4.1.0 The `$comment_ID` parameter was added.
  608. *
  609. * @param int $comment_ID The current comment ID.
  610. * @param WP_Comment $comment The comment object.
  611. */
  612. return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment );
  613. }
  614. /**
  615. * Display the comment id of the current comment.
  616. *
  617. * @since 0.71
  618. */
  619. function comment_ID() {
  620. echo get_comment_ID();
  621. }
  622. /**
  623. * Retrieve the link to a given comment.
  624. *
  625. * @since 1.5.0
  626. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
  627. *
  628. * @see get_page_of_comment()
  629. *
  630. * @global WP_Rewrite $wp_rewrite
  631. * @global bool $in_comment_loop
  632. *
  633. * @param WP_Comment|int|null $comment Comment to retrieve. Default current comment.
  634. * @param array $args {
  635. * An array of optional arguments to override the defaults.
  636. *
  637. * @type string $type Passed to get_page_of_comment().
  638. * @type int $page Current page of comments, for calculating comment pagination.
  639. * @type int $per_page Per-page value for comment pagination.
  640. * @type int $max_depth Passed to get_page_of_comment().
  641. * @type int|string $cpage Value to use for the comment's "comment-page" or "cpage" value.
  642. * If provided, this value overrides any value calculated from `$page`
  643. * and `$per_page`.
  644. * }
  645. * @return string The permalink to the given comment.
  646. */
  647. function get_comment_link( $comment = null, $args = array() ) {
  648. global $wp_rewrite, $in_comment_loop;
  649. $comment = get_comment($comment);
  650. // Back-compat.
  651. if ( ! is_array( $args ) ) {
  652. $args = array( 'page' => $args );
  653. }
  654. $defaults = array(
  655. 'type' => 'all',
  656. 'page' => '',
  657. 'per_page' => '',
  658. 'max_depth' => '',
  659. 'cpage' => null,
  660. );
  661. $args = wp_parse_args( $args, $defaults );
  662. $link = get_permalink( $comment->comment_post_ID );
  663. // The 'cpage' param takes precedence.
  664. if ( ! is_null( $args['cpage'] ) ) {
  665. $cpage = $args['cpage'];
  666. // No 'cpage' is provided, so we calculate one.
  667. } else {
  668. if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
  669. $args['per_page'] = get_option('comments_per_page');
  670. }
  671. if ( empty( $args['per_page'] ) ) {
  672. $args['per_page'] = 0;
  673. $args['page'] = 0;
  674. }
  675. $cpage = $args['page'];
  676. if ( '' == $cpage ) {
  677. if ( ! empty( $in_comment_loop ) ) {
  678. $cpage = get_query_var( 'cpage' );
  679. } else {
  680. // Requires a database hit, so we only do it when we can't figure out from context.
  681. $cpage = get_page_of_comment( $comment->comment_ID, $args );
  682. }
  683. }
  684. /*
  685. * If the default page displays the oldest comments, the permalinks for comments on the default page
  686. * do not need a 'cpage' query var.
  687. */
  688. if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
  689. $cpage = '';
  690. }
  691. }
  692. if ( $cpage && get_option( 'page_comments' ) ) {
  693. if ( $wp_rewrite->using_permalinks() ) {
  694. if ( $cpage ) {
  695. $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
  696. }
  697. $link = user_trailingslashit( $link, 'comment' );
  698. } elseif ( $cpage ) {
  699. $link = add_query_arg( 'cpage', $cpage, $link );
  700. }
  701. }
  702. if ( $wp_rewrite->using_permalinks() ) {
  703. $link = user_trailingslashit( $link, 'comment' );
  704. }
  705. $link = $link . '#comment-' . $comment->comment_ID;
  706. /**
  707. * Filters the returned single comment permalink.
  708. *
  709. * @since 2.8.0
  710. * @since 4.4.0 Added the `$cpage` parameter.
  711. *
  712. * @see get_page_of_comment()
  713. *
  714. * @param string $link The comment permalink with '#comment-$id' appended.
  715. * @param WP_Comment $comment The current comment object.
  716. * @param array $args An array of arguments to override the defaults.
  717. * @param int $cpage The calculated 'cpage' value.
  718. */
  719. return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
  720. }
  721. /**
  722. * Retrieves the link to the current post comments.
  723. *
  724. * @since 1.5.0
  725. *
  726. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  727. * @return string The link to the comments.
  728. */
  729. function get_comments_link( $post_id = 0 ) {
  730. $hash = get_comments_number( $post_id ) ? '#comments' : '#respond';
  731. $comments_link = get_permalink( $post_id ) . $hash;
  732. /**
  733. * Filters the returned post comments permalink.
  734. *
  735. * @since 3.6.0
  736. *
  737. * @param string $comments_link Post comments permalink with '#comments' appended.
  738. * @param int|WP_Post $post_id Post ID or WP_Post object.
  739. */
  740. return apply_filters( 'get_comments_link', $comments_link, $post_id );
  741. }
  742. /**
  743. * Display the link to the current post comments.
  744. *
  745. * @since 0.71
  746. *
  747. * @param string $deprecated Not Used.
  748. * @param string $deprecated_2 Not Used.
  749. */
  750. function comments_link( $deprecated = '', $deprecated_2 = '' ) {
  751. if ( !empty( $deprecated ) )
  752. _deprecated_argument( __FUNCTION__, '0.72' );
  753. if ( !empty( $deprecated_2 ) )
  754. _deprecated_argument( __FUNCTION__, '1.3.0' );
  755. echo esc_url( get_comments_link() );
  756. }
  757. /**
  758. * Retrieve the amount of comments a post has.
  759. *
  760. * @since 1.5.0
  761. *
  762. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  763. * @return int The number of comments a post has.
  764. */
  765. function get_comments_number( $post_id = 0 ) {
  766. $post = get_post( $post_id );
  767. if ( ! $post ) {
  768. $count = 0;
  769. } else {
  770. $count = $post->comment_count;
  771. $post_id = $post->ID;
  772. }
  773. /**
  774. * Filters the returned comment count for a post.
  775. *
  776. * @since 1.5.0
  777. *
  778. * @param int $count Number of comments a post has.
  779. * @param int $post_id Post ID.
  780. */
  781. return apply_filters( 'get_comments_number', $count, $post_id );
  782. }
  783. /**
  784. * Display the language string for the number of comments the current post has.
  785. *
  786. * @since 0.71
  787. *
  788. * @param string $zero Optional. Text for no comments. Default false.
  789. * @param string $one Optional. Text for one comment. Default false.
  790. * @param string $more Optional. Text for more than one comment. Default false.
  791. * @param string $deprecated Not used.
  792. */
  793. function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
  794. if ( ! empty( $deprecated ) ) {
  795. _deprecated_argument( __FUNCTION__, '1.3.0' );
  796. }
  797. echo get_comments_number_text( $zero, $one, $more );
  798. }
  799. /**
  800. * Display the language string for the number of comments the current post has.
  801. *
  802. * @since 4.0.0
  803. *
  804. * @param string $zero Optional. Text for no comments. Default false.
  805. * @param string $one Optional. Text for one comment. Default false.
  806. * @param string $more Optional. Text for more than one comment. Default false.
  807. */
  808. function get_comments_number_text( $zero = false, $one = false, $more = false ) {
  809. $number = get_comments_number();
  810. if ( $number > 1 ) {
  811. if ( false === $more ) {
  812. /* translators: %s: number of comments */
  813. $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
  814. } else {
  815. // % Comments
  816. /* translators: If comment number in your language requires declension,
  817. * translate this to 'on'. Do not translate into your own language.
  818. */
  819. if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {
  820. $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );
  821. $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities
  822. $text = trim( strip_tags( $text ), '% ' );
  823. // Replace '% Comments' with a proper plural form
  824. if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) {
  825. /* translators: %s: number of comments */
  826. $new_text = _n( '%s Comment', '%s Comments', $number );
  827. $new_text = trim( sprintf( $new_text, '' ) );
  828. $more = str_replace( $text, $new_text, $more );
  829. if ( false === strpos( $more, '%' ) ) {
  830. $more = '% ' . $more;
  831. }
  832. }
  833. }
  834. $output = str_replace( '%', number_format_i18n( $number ), $more );
  835. }
  836. } elseif ( $number == 0 ) {
  837. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  838. } else { // must be one
  839. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  840. }
  841. /**
  842. * Filters the comments count for display.
  843. *
  844. * @since 1.5.0
  845. *
  846. * @see _n()
  847. *
  848. * @param string $output A translatable string formatted based on whether the count
  849. * is equal to 0, 1, or 1+.
  850. * @param int $number The number of post comments.
  851. */
  852. return apply_filters( 'comments_number', $output, $number );
  853. }
  854. /**
  855. * Retrieve the text of the current comment.
  856. *
  857. * @since 1.5.0
  858. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  859. *
  860. * @see Walker_Comment::comment()
  861. *
  862. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the text.
  863. * Default current comment.
  864. * @param array $args Optional. An array of arguments. Default empty.
  865. * @return string The comment content.
  866. */
  867. function get_comment_text( $comment_ID = 0, $args = array() ) {
  868. $comment = get_comment( $comment_ID );
  869. /**
  870. * Filters the text of a comment.
  871. *
  872. * @since 1.5.0
  873. *
  874. * @see Walker_Comment::comment()
  875. *
  876. * @param string $comment_content Text of the comment.
  877. * @param WP_Comment $comment The comment object.
  878. * @param array $args An array of arguments.
  879. */
  880. return apply_filters( 'get_comment_text', $comment->comment_content, $comment, $args );
  881. }
  882. /**
  883. * Display the text of the current comment.
  884. *
  885. * @since 0.71
  886. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  887. *
  888. * @see Walker_Comment::comment()
  889. *
  890. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the text.
  891. * Default current comment.
  892. * @param array $args Optional. An array of arguments. Default empty array. Default empty.
  893. */
  894. function comment_text( $comment_ID = 0, $args = array() ) {
  895. $comment = get_comment( $comment_ID );
  896. $comment_text = get_comment_text( $comment, $args );
  897. /**
  898. * Filters the text of a comment to be displayed.
  899. *
  900. * @since 1.2.0
  901. *
  902. * @see Walker_Comment::comment()
  903. *
  904. * @param string $comment_text Text of the current comment.
  905. * @param WP_Comment $comment The comment object.
  906. * @param array $args An array of arguments.
  907. */
  908. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  909. }
  910. /**
  911. * Retrieve the comment time of the current comment.
  912. *
  913. * @since 1.5.0
  914. *
  915. * @param string $d Optional. The format of the time. Default user's settings.
  916. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  917. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  918. * Default true.
  919. * @return string The formatted time.
  920. */
  921. function get_comment_time( $d = '', $gmt = false, $translate = true ) {
  922. $comment = get_comment();
  923. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  924. if ( '' == $d )
  925. $date = mysql2date(get_option('time_format'), $comment_date, $translate);
  926. else
  927. $date = mysql2date($d, $comment_date, $translate);
  928. /**
  929. * Filters the returned comment time.
  930. *
  931. * @since 1.5.0
  932. *
  933. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  934. * @param string $d Date format.
  935. * @param bool $gmt Whether the GMT date is in use.
  936. * @param bool $translate Whether the time is translated.
  937. * @param WP_Comment $comment The comment object.
  938. */
  939. return apply_filters( 'get_comment_time', $date, $d, $gmt, $translate, $comment );
  940. }
  941. /**
  942. * Display the comment time of the current comment.
  943. *
  944. * @since 0.71
  945. *
  946. * @param string $d Optional. The format of the time. Default user's settings.
  947. */
  948. function comment_time( $d = '' ) {
  949. echo get_comment_time($d);
  950. }
  951. /**
  952. * Retrieve the comment type of the current comment.
  953. *
  954. * @since 1.5.0
  955. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  956. *
  957. * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type.
  958. * Default current comment.
  959. * @return string The comment type.
  960. */
  961. function get_comment_type( $comment_ID = 0 ) {
  962. $comment = get_comment( $comment_ID );
  963. if ( '' == $comment->comment_type )
  964. $comment->comment_type = 'comment';
  965. /**
  966. * Filters the returned comment type.
  967. *
  968. * @since 1.5.0
  969. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  970. *
  971. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  972. * @param int $comment_ID The comment ID.
  973. * @param WP_Comment $comment The comment object.
  974. */
  975. return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment );
  976. }
  977. /**
  978. * Display the comment type of the current comment.
  979. *
  980. * @since 0.71
  981. *
  982. * @param string $commenttxt Optional. String to display for comment type. Default false.
  983. * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
  984. * @param string $pingbacktxt Optional. String to display for pingback type. Default false.
  985. */
  986. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  987. if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
  988. if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
  989. if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
  990. $type = get_comment_type();
  991. switch( $type ) {
  992. case 'trackback' :
  993. echo $trackbacktxt;
  994. break;
  995. case 'pingback' :
  996. echo $pingbacktxt;
  997. break;
  998. default :
  999. echo $commenttxt;
  1000. }
  1001. }
  1002. /**
  1003. * Retrieve The current post's trackback URL.
  1004. *
  1005. * There is a check to see if permalink's have been enabled and if so, will
  1006. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  1007. * current post is used and appended to the correct page to go to.
  1008. *
  1009. * @since 1.5.0
  1010. *
  1011. * @return string The trackback URL after being filtered.
  1012. */
  1013. function get_trackback_url() {
  1014. if ( '' != get_option('permalink_structure') )
  1015. $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
  1016. else
  1017. $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . get_the_ID();
  1018. /**
  1019. * Filters the returned trackback URL.
  1020. *
  1021. * @since 2.2.0
  1022. *
  1023. * @param string $tb_url The trackback URL.
  1024. */
  1025. return apply_filters( 'trackback_url', $tb_url );
  1026. }
  1027. /**
  1028. * Display the current post's trackback URL.
  1029. *
  1030. * @since 0.71
  1031. *
  1032. * @param bool $deprecated_echo Not used.
  1033. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  1034. * for the result instead.
  1035. */
  1036. function trackback_url( $deprecated_echo = true ) {
  1037. if ( true !== $deprecated_echo ) {
  1038. _deprecated_argument( __FUNCTION__, '2.5.0',
  1039. /* translators: %s: get_trackback_url() */
  1040. sprintf( __( 'Use %s instead if you do not want the value echoed.' ),
  1041. '<code>get_trackback_url()</code>'
  1042. )
  1043. );
  1044. }
  1045. if ( $deprecated_echo ) {
  1046. echo get_trackback_url();
  1047. } else {
  1048. return get_trackback_url();
  1049. }
  1050. }
  1051. /**
  1052. * Generate and display the RDF for the trackback information of current post.
  1053. *
  1054. * Deprecated in 3.0.0, and restored in 3.0.1.
  1055. *
  1056. * @since 0.71
  1057. *
  1058. * @param int $deprecated Not used (Was $timezone = 0).
  1059. */
  1060. function trackback_rdf( $deprecated = '' ) {
  1061. if ( ! empty( $deprecated ) ) {
  1062. _deprecated_argument( __FUNCTION__, '2.5.0' );
  1063. }
  1064. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  1065. return;
  1066. }
  1067. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  1068. xmlns:dc="http://purl.org/dc/elements/1.1/"
  1069. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  1070. <rdf:Description rdf:about="';
  1071. the_permalink();
  1072. echo '"'."\n";
  1073. echo ' dc:identifier="';
  1074. the_permalink();
  1075. echo '"'."\n";
  1076. echo ' dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  1077. echo ' trackback:ping="'.get_trackback_url().'"'." />\n";
  1078. echo '</rdf:RDF>';
  1079. }
  1080. /**
  1081. * Whether the current post is open for comments.
  1082. *
  1083. * @since 1.5.0
  1084. *
  1085. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1086. * @return bool True if the comments are open.
  1087. */
  1088. function comments_open( $post_id = null ) {
  1089. $_post = get_post($post_id);
  1090. $open = ( 'open' == $_post->comment_status );
  1091. /**
  1092. * Filters whether the current post is open for comments.
  1093. *
  1094. * @since 2.5.0
  1095. *
  1096. * @param bool $open Whether the current post is open for comments.
  1097. * @param int|WP_Post $post_id The post ID or WP_Post object.
  1098. */
  1099. return apply_filters( 'comments_open', $open, $post_id );
  1100. }
  1101. /**
  1102. * Whether the current post is open for pings.
  1103. *
  1104. * @since 1.5.0
  1105. *
  1106. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1107. * @return bool True if pings are accepted
  1108. */
  1109. function pings_open( $post_id = null ) {
  1110. $_post = get_post($post_id);
  1111. $open = ( 'open' == $_post->ping_status );
  1112. /**
  1113. * Filters whether the current post is open for pings.
  1114. *
  1115. * @since 2.5.0
  1116. *
  1117. * @param bool $open Whether the current post is open for pings.
  1118. * @param int|WP_Post $post_id The post ID or WP_Post object.
  1119. */
  1120. return apply_filters( 'pings_open', $open, $post_id );
  1121. }
  1122. /**
  1123. * Display form token for unfiltered comments.
  1124. *
  1125. * Will only display nonce token if the current user has permissions for
  1126. * unfiltered html. Won't display the token for other users.
  1127. *
  1128. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  1129. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  1130. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  1131. *
  1132. * Backported to 2.0.10.
  1133. *
  1134. * @since 2.1.3
  1135. */
  1136. function wp_comment_form_unfiltered_html_nonce() {
  1137. $post = get_post();
  1138. $post_id = $post ? $post->ID : 0;
  1139. if ( current_user_can( 'unfiltered_html' ) ) {
  1140. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  1141. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  1142. }
  1143. }
  1144. /**
  1145. * Load the comment template specified in $file.
  1146. *
  1147. * Will not display the comments template if not on single post or page, or if
  1148. * the post does not have comments.
  1149. *
  1150. * Uses the WordPress database object to query for the comments. The comments
  1151. * are passed through the {@see 'comments_array'} filter hook with the list of comments
  1152. * and the post ID respectively.
  1153. *
  1154. * The `$file` path is passed through a filter hook called {@see 'comments_template'},
  1155. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  1156. * first and if it fails it will require the default comment template from the
  1157. * default theme. If either does not exist, then the WordPress process will be
  1158. * halted. It is advised for that reason, that the default theme is not deleted.
  1159. *
  1160. * Will not try to get the comments if the post has none.
  1161. *
  1162. * @since 1.5.0
  1163. *
  1164. * @global WP_Query $wp_query
  1165. * @global WP_Post $post
  1166. * @global wpdb $wpdb
  1167. * @global int $id
  1168. * @global WP_Comment $comment
  1169. * @global string $user_login
  1170. * @global int $user_ID
  1171. * @global string $user_identity
  1172. * @global bool $overridden_cpage
  1173. * @global bool $withcomments
  1174. *
  1175. * @param string $file Optional. The file to load. Default '/comments.php'.
  1176. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  1177. * Default false.
  1178. */
  1179. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  1180. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
  1181. if ( !(is_single() || is_page() || $withcomments) || empty($post) )
  1182. return;
  1183. if ( empty($file) )
  1184. $file = '/comments.php';
  1185. $req = get_option('require_name_email');
  1186. /*
  1187. * Comment author information fetched from the comment cookies.
  1188. */
  1189. $commenter = wp_get_current_commenter();
  1190. /*
  1191. * The name of the current comment author escaped for use in attributes.
  1192. * Escaped by sanitize_comment_cookies().
  1193. */
  1194. $comment_author = $commenter['comment_author'];
  1195. /*
  1196. * The email address of the current comment author escaped for use in attributes.
  1197. * Escaped by sanitize_comment_cookies().
  1198. */
  1199. $comment_author_email = $commenter['comment_author_email'];
  1200. /*
  1201. * The url of the current comment author escaped for use in attributes.
  1202. */
  1203. $comment_author_url = esc_url($commenter['comment_author_url']);
  1204. $comment_args = array(
  1205. 'orderby' => 'comment_date_gmt',
  1206. 'order' => 'ASC',
  1207. 'status' => 'approve',
  1208. 'post_id' => $post->ID,
  1209. 'no_found_rows' => false,
  1210. 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
  1211. );
  1212. if ( get_option('thread_comments') ) {
  1213. $comment_args['hierarchical'] = 'threaded';
  1214. } else {
  1215. $comment_args['hierarchical'] = false;
  1216. }
  1217. if ( $user_ID ) {
  1218. $comment_args['include_unapproved'] = array( $user_ID );
  1219. } elseif ( ! empty( $comment_author_email ) ) {
  1220. $comment_args['include_unapproved'] = array( $comment_author_email );
  1221. }
  1222. $per_page = 0;
  1223. if ( get_option( 'page_comments' ) ) {
  1224. $per_page = (int) get_query_var( 'comments_per_page' );
  1225. if ( 0 === $per_page ) {
  1226. $per_page = (int) get_option( 'comments_per_page' );
  1227. }
  1228. $comment_args['number'] = $per_page;
  1229. $page = (int) get_query_var( 'cpage' );
  1230. if ( $page ) {
  1231. $comment_args['offset'] = ( $page - 1 ) * $per_page;
  1232. } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
  1233. $comment_args['offset'] = 0;
  1234. } else {
  1235. // If fetching the first page of 'newest', we need a top-level comment count.
  1236. $top_level_query = new WP_Comment_Query();
  1237. $top_level_args = array(
  1238. 'count' => true,
  1239. 'orderby' => false,
  1240. 'post_id' => $post->ID,
  1241. 'status' => 'approve',
  1242. );
  1243. if ( $comment_args['hierarchical'] ) {
  1244. $top_level_args['parent'] = 0;
  1245. }
  1246. if ( isset( $comment_args['include_unapproved'] ) ) {
  1247. $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
  1248. }
  1249. $top_level_count = $top_level_query->query( $top_level_args );
  1250. $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
  1251. }
  1252. }
  1253. /**
  1254. * Filters the arguments used to query comments in comments_template().
  1255. *
  1256. * @since 4.5.0
  1257. *
  1258. * @see WP_Comment_Query::__construct()
  1259. *
  1260. * @param array $comment_args {
  1261. * Array of WP_Comment_Query arguments.
  1262. *
  1263. * @type string|array $orderby Field(s) to order by.
  1264. * @type string $order Order of results. Accepts 'ASC' or 'DESC'.
  1265. * @type string $status Comment status.
  1266. * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments
  1267. * will be included in results.
  1268. * @type int $post_id ID of the post.
  1269. * @type bool $no_found_rows Whether to refrain from querying for found rows.
  1270. * @type bool $update_comment_meta_cache Whether to prime cache for comment meta.
  1271. * @type bool|string $hierarchical Whether to query for comments hierarchically.
  1272. * @type int $offset Comment offset.
  1273. * @type int $number Number of comments to fetch.
  1274. * }
  1275. */
  1276. $comment_args = apply_filters( 'comments_template_query_args', $comment_args );
  1277. $comment_query = new WP_Comment_Query( $comment_args );
  1278. $_comments = $comment_query->comments;
  1279. // Trees must be flattened before they're passed to the walker.
  1280. if ( $comment_args['hierarchical'] ) {
  1281. $comments_flat = array();
  1282. foreach ( $_comments as $_comment ) {
  1283. $comments_flat[] = $_comment;
  1284. $comment_children = $_comment->get_children( array(
  1285. 'format' => 'flat',
  1286. 'status' => $comment_args['status'],
  1287. 'orderby' => $comment_args['orderby']
  1288. ) );
  1289. foreach ( $comment_children as $comment_child ) {
  1290. $comments_flat[] = $comment_child;
  1291. }
  1292. }
  1293. } else {
  1294. $comments_flat = $_comments;
  1295. }
  1296. /**
  1297. * Filters the comments array.
  1298. *
  1299. * @since 2.1.0
  1300. *
  1301. * @param array $comments Array of comments supplied to the comments template.
  1302. * @param int $post_ID Post ID.
  1303. */
  1304. $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
  1305. $comments = &$wp_query->comments;
  1306. $wp_query->comment_count = count($wp_query->comments);
  1307. $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
  1308. if ( $separate_comments ) {
  1309. $wp_query->comments_by_type = separate_comments($comments);
  1310. $comments_by_type = &$wp_query->comments_by_type;
  1311. } else {
  1312. $wp_query->comments_by_type = array();
  1313. }
  1314. $overridden_cpage = false;
  1315. if ( '' == get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) {
  1316. set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
  1317. $overridden_cpage = true;
  1318. }
  1319. if ( !defined('COMMENTS_TEMPLATE') )
  1320. define('COMMENTS_TEMPLATE', true);
  1321. $theme_template = STYLESHEETPATH . $file;
  1322. /**
  1323. * Filters the path to the theme template file used for the comments template.
  1324. *
  1325. * @since 1.5.1
  1326. *
  1327. * @param string $theme_template The path to the theme template file.
  1328. */
  1329. $include = apply_filters( 'comments_template', $theme_template );
  1330. if ( file_exists( $include ) )
  1331. require( $include );
  1332. elseif ( file_exists( TEMPLATEPATH . $file ) )
  1333. require( TEMPLATEPATH . $file );
  1334. else // Backward compat code will be removed in a future release
  1335. require( ABSPATH . WPINC . '/theme-compat/comments.php');
  1336. }
  1337. /**
  1338. * Displays the link to the comments for the current post ID.
  1339. *
  1340. * @since 0.71
  1341. *
  1342. * @param string $zero Optional. String to display when no comments. Default false.
  1343. * @param string $one Optional. String to display when only one comment is available.
  1344. * Default false.
  1345. * @param string $more Optional. String to display when there are more than one comment.
  1346. * Default false.
  1347. * @param string $css_class Optional. CSS class to use for comments. Default empty.
  1348. * @param string $none Optional. String to display when comments have been turned off.
  1349. * Default false.
  1350. */
  1351. function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
  1352. $id = get_the_ID();
  1353. $title = get_the_title();
  1354. $number = get_comments_number( $id );
  1355. if ( false === $zero ) {
  1356. /* translators: %s: post title */
  1357. $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $title );
  1358. }
  1359. if ( false === $one ) {
  1360. /* translators: %s: post title */
  1361. $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $title );
  1362. }
  1363. if ( false === $more ) {
  1364. /* translators: 1: Number of comments 2: post title */
  1365. $more = _n( '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', $number );
  1366. $more = sprintf( $more, number_format_i18n( $number ), $title );
  1367. }
  1368. if ( false === $none ) {
  1369. /* translators: %s: post title */
  1370. $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $title );
  1371. }
  1372. if ( 0 == $number && !comments_open() && !pings_open() ) {
  1373. echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
  1374. return;
  1375. }
  1376. if ( post_password_required() ) {
  1377. _e( 'Enter your password to view comments.' );
  1378. return;
  1379. }
  1380. echo '<a href="';
  1381. if ( 0 == $number ) {
  1382. $respond_link = get_permalink() . '#respond';
  1383. /**
  1384. * Filters the respond link when a post has no comments.
  1385. *
  1386. * @since 4.4.0
  1387. *
  1388. * @param string $respond_link The default response link.
  1389. * @param integer $id The post ID.
  1390. */
  1391. echo apply_filters( 'respond_link', $respond_link, $id );
  1392. } else {
  1393. comments_link();
  1394. }
  1395. echo '"';
  1396. if ( !empty( $css_class ) ) {
  1397. echo ' class="'.$css_class.'" ';
  1398. }
  1399. $attributes = '';
  1400. /**
  1401. * Filters the comments link attributes for display.
  1402. *
  1403. * @since 2.5.0
  1404. *
  1405. * @param string $attributes The comments link attributes. Default empty.
  1406. */
  1407. echo apply_filters( 'comments_popup_link_attributes', $attributes );
  1408. echo '>';
  1409. comments_number( $zero, $one, $more );
  1410. echo '</a>';
  1411. }
  1412. /**
  1413. * Retrieve HTML content for reply to comment link.
  1414. *
  1415. * @since 2.7.0
  1416. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
  1417. *
  1418. * @param array $args {
  1419. * Optional. Override default arguments.
  1420. *
  1421. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1422. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1423. * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
  1424. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1425. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1426. * Default 'respond'.
  1427. * @type string $reply_text The text of the Reply link. Default 'Reply'.
  1428. * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
  1429. * @type int $max_depth The max depth of the comment tree. Default 0.
  1430. * @type int $depth The depth of the new comment. Must be greater than 0 and less than the value
  1431. * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
  1432. * @type string $before The text or HTML to add before the reply link. Default empty.
  1433. * @type string $after The text or HTML to add after the reply link. Default empty.
  1434. * }
  1435. * @param int|WP_Comment $comment Comment being replied to. Default current comment.
  1436. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1437. * Default current post.
  1438. * @return void|false|string Link to show comment form, if successful. False, if comments are closed.
  1439. */
  1440. function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1441. $defaults = array(
  1442. 'add_below' => 'comment',
  1443. 'respond_id' => 'respond',
  1444. 'reply_text' => __( 'Reply' ),
  1445. /* translators: Comment reply button text. 1: Comment author name */
  1446. 'reply_to_text' => __( 'Reply to %s' ),
  1447. 'login_text' => __( 'Log in to Reply' ),
  1448. 'max_depth' => 0,
  1449. 'depth' => 0,
  1450. 'before' => '',
  1451. 'after' => ''
  1452. );
  1453. $args = wp_parse_args( $args, $defaults );
  1454. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
  1455. return;
  1456. }
  1457. $comment = get_comment( $comment );
  1458. if ( empty( $post ) ) {
  1459. $post = $comment->comment_post_ID;
  1460. }
  1461. $post = get_post( $post );
  1462. if ( ! comments_open( $post->ID ) ) {
  1463. return false;
  1464. }
  1465. /**
  1466. * Filters the comment reply link arguments.
  1467. *
  1468. * @since 4.1.0
  1469. *
  1470. * @param array $args Comment reply link arguments. See get_comment_reply_link()
  1471. * for more information on accepted arguments.
  1472. * @param WP_Comment $comment The object of the comment being replied to.
  1473. * @param WP_Post $post The WP_Post object.
  1474. */
  1475. $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
  1476. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1477. $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1478. esc_url( wp_login_url( get_permalink() ) ),
  1479. $args['login_text']
  1480. );
  1481. } else {
  1482. $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
  1483. $args['add_below'], $comment->comment_ID, $args['respond_id'], $post->ID
  1484. );
  1485. $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
  1486. esc_url( add_query_arg( 'replytocom', $comment->comment_ID, get_permalink( $post->ID ) ) ) . "#" . $args['respond_id'],
  1487. $onclick,
  1488. esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
  1489. $args['reply_text']
  1490. );
  1491. }
  1492. /**
  1493. * Filters the comment reply link.
  1494. *
  1495. * @since 2.7.0
  1496. *
  1497. * @param string $link The HTML markup for the comment reply link.
  1498. * @param array $args An array of arguments overriding the defaults.
  1499. * @param object $comment The object of the comment being replied.
  1500. * @param WP_Post $post The WP_Post object.
  1501. */
  1502. return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
  1503. }
  1504. /**
  1505. * Displays the HTML content for reply to comment link.
  1506. *
  1507. * @since 2.7.0
  1508. *
  1509. * @see get_comment_reply_link()
  1510. *
  1511. * @param array $args Optional. Override default options.
  1512. * @param int $comment Comment being replied to. Default current comment.
  1513. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1514. * Default current post.
  1515. * @return mixed Link to show comment form, if successful. False, if comments are closed.
  1516. */
  1517. function comment_reply_link($args = array(), $comment = null, $post = null) {
  1518. echo get_comment_reply_link($args, $comment, $post);
  1519. }
  1520. /**
  1521. * Retrieve HTML content for reply to post link.
  1522. *
  1523. * @since 2.7.0
  1524. *
  1525. * @param array $args {
  1526. * Optional. Override default arguments.
  1527. *
  1528. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1529. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1530. * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
  1531. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1532. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1533. * Default 'respond'.
  1534. * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
  1535. * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
  1536. * @type string $before Text or HTML to add before the reply link. Default empty.
  1537. * @type string $after Text or HTML to add after the reply link. Default empty.
  1538. * }
  1539. * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
  1540. * Default current post.
  1541. * @return false|null|string Link to show comment form, if successful. False, if comments are closed.
  1542. */
  1543. function get_post_reply_link($args = array(), $post = null) {
  1544. $defaults = array(
  1545. 'add_below' => 'post',
  1546. 'respond_id' => 'respond',
  1547. 'reply_text' => __('Leave a Comment'),
  1548. 'login_text' => __('Log in to leave a Comment'),
  1549. 'before' => '',
  1550. 'after' => '',
  1551. );
  1552. $args = wp_parse_args($args, $defaults);
  1553. $post = get_post($post);
  1554. if ( ! comments_open( $post->ID ) ) {
  1555. return false;
  1556. }
  1557. if ( get_option('comment_registration') && ! is_user_logged_in() ) {
  1558. $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1559. wp_login_url( get_permalink() ),
  1560. $args['login_text']
  1561. );
  1562. } else {
  1563. $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
  1564. $args['add_below'], $post->ID, $args['respond_id']
  1565. );
  1566. $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
  1567. get_permalink( $post->ID ) . '#' . $args['respond_id'],
  1568. $onclick,
  1569. $args['reply_text']
  1570. );
  1571. }
  1572. $formatted_link = $args['before'] . $link . $args['after'];
  1573. /**
  1574. * Filters the formatted post comments link HTML.
  1575. *
  1576. * @since 2.7.0
  1577. *
  1578. * @param string $formatted The HTML-formatted post comments link.
  1579. * @param int|WP_Post $post The post ID or WP_Post object.
  1580. */
  1581. return apply_filters( 'post_comments_link', $formatted_link, $post );
  1582. }
  1583. /**
  1584. * Displays the HTML content for reply to post link.
  1585. *
  1586. * @since 2.7.0
  1587. *
  1588. * @see get_post_reply_link()
  1589. *
  1590. * @param array $args Optional. Override default options,
  1591. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1592. * Default current post.
  1593. * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
  1594. */
  1595. function post_reply_link($args = array(), $post = null) {
  1596. echo get_post_reply_link($args, $post);
  1597. }
  1598. /**
  1599. * Retrieve HTML content for cancel comment reply link.
  1600. *
  1601. * @since 2.7.0
  1602. *
  1603. * @param string $text Optional. Text to display for cancel reply link. Default empty.
  1604. * @return string
  1605. */
  1606. function get_cancel_comment_reply_link( $text = '' ) {
  1607. if ( empty($text) )
  1608. $text = __('Click here to cancel reply.');
  1609. $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
  1610. $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
  1611. $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
  1612. /**
  1613. * Filters the cancel comment reply link HTML.
  1614. *
  1615. * @since 2.7.0
  1616. *
  1617. * @param string $formatted_link The HTML-formatted cancel comment reply link.
  1618. * @param string $link Cancel comment reply link URL.
  1619. * @param string $text Cancel comment reply link text.
  1620. */
  1621. return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
  1622. }
  1623. /**
  1624. * Display HTML content for cancel comment reply link.
  1625. *
  1626. * @since 2.7.0
  1627. *
  1628. * @param string $text Optional. Text to display for cancel reply link. Default empty.
  1629. */
  1630. function cancel_comment_reply_link( $text = '' ) {
  1631. echo get_cancel_comment_reply_link($text);
  1632. }
  1633. /**
  1634. * Retrieve hidden input HTML for replying to comments.
  1635. *
  1636. * @since 3.0.0
  1637. *
  1638. * @param int $id Optional. Post ID. Default current post ID.
  1639. * @return string Hidden input HTML for replying to comments
  1640. */
  1641. function get_comment_id_fields( $id = 0 ) {
  1642. if ( empty( $id ) )
  1643. $id = get_the_ID();
  1644. $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
  1645. $result = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
  1646. $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
  1647. /**
  1648. * Filters the returned comment id fields.
  1649. *
  1650. * @since 3.0.0
  1651. *
  1652. * @param string $result The HTML-formatted hidden id field comment elements.
  1653. * @param int $id The post ID.
  1654. * @param int $replytoid The id of the comment being replied to.
  1655. */
  1656. return apply_filters( 'comment_id_fields', $result, $id, $replytoid );
  1657. }
  1658. /**
  1659. * Output hidden input HTML for replying to comments.
  1660. *
  1661. * @since 2.7.0
  1662. *
  1663. * @param int $id Optional. Post ID. Default current post ID.
  1664. */
  1665. function comment_id_fields( $id = 0 ) {
  1666. echo get_comment_id_fields( $id );
  1667. }
  1668. /**
  1669. * Display text based on comment reply status.
  1670. *
  1671. * Only affects users with JavaScript disabled.
  1672. *
  1673. * @internal The $comment global must be present to allow template tags access to the current
  1674. * comment. See https://core.trac.wordpress.org/changeset/36512.
  1675. *
  1676. * @since 2.7.0
  1677. *
  1678. * @global WP_Comment $comment Current comment.
  1679. *
  1680. * @param string $noreplytext Optional. Text to display when not replying to a comment.
  1681. * Default false.
  1682. * @param string $replytext Optional. Text to display when replying to a comment.
  1683. * Default false. Accepts "%s" for the author of the comment
  1684. * being replied to.
  1685. * @param string $linktoparent Optional. Boolean to control making the author's name a link
  1686. * to their comment. Default true.
  1687. */
  1688. function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) {
  1689. global $comment;
  1690. if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
  1691. if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
  1692. $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
  1693. if ( 0 == $replytoid )
  1694. echo $noreplytext;
  1695. else {
  1696. // Sets the global so that template tags can be used in the comment form.
  1697. $comment = get_comment($replytoid);
  1698. $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author( $comment ) . '</a>' : get_comment_author( $comment );
  1699. printf( $replytext, $author );
  1700. }
  1701. }
  1702. /**
  1703. * List comments.
  1704. *
  1705. * Used in the comments.php template to list comments for a particular post.
  1706. *
  1707. * @since 2.7.0
  1708. *
  1709. * @see WP_Query->comments
  1710. *
  1711. * @global WP_Query $wp_query
  1712. * @global int $comment_alt
  1713. * @global int $comment_depth
  1714. * @global int $comment_thread_alt
  1715. * @global bool $overridden_cpage
  1716. * @global bool $in_comment_loop
  1717. *
  1718. * @param string|array $args {
  1719. * Optional. Formatting options.
  1720. *
  1721. * @type object $walker Instance of a Walker class to list comments. Default null.
  1722. * @type int $max_depth The maximum comments depth. Default empty.
  1723. * @type string $style The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
  1724. * @type string $callback Callback function to use. Default null.
  1725. * @type string $end-callback Callback function to use at the end. Default null.
  1726. * @type string $type Type of comments to list.
  1727. * Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
  1728. * @type int $page Page ID to list comments for. Default empty.
  1729. * @type int $per_page Number of comments to list per page. Default empty.
  1730. * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
  1731. * @type bool $reverse_top_level Ordering of the listed comments. If true, will display newest comments first.
  1732. * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
  1733. * @type string $format How to format the comments list.
  1734. * Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
  1735. * @type bool $short_ping Whether to output short pings. Default false.
  1736. * @type bool $echo Whether to echo the output or return it. Default true.
  1737. * }
  1738. * @param array $comments Optional. Array of WP_Comment objects.
  1739. */
  1740. function wp_list_comments( $args = array(), $comments = null ) {
  1741. global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
  1742. $in_comment_loop = true;
  1743. $comment_alt = $comment_thread_alt = 0;
  1744. $comment_depth = 1;
  1745. $defaults = array(
  1746. 'walker' => null,
  1747. 'max_depth' => '',
  1748. 'style' => 'ul',
  1749. 'callback' => null,
  1750. 'end-callback' => null,
  1751. 'type' => 'all',
  1752. 'page' => '',
  1753. 'per_page' => '',
  1754. 'avatar_size' => 32,
  1755. 'reverse_top_level' => null,
  1756. 'reverse_children' => '',
  1757. 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
  1758. 'short_ping' => false,
  1759. 'echo' => true,
  1760. );
  1761. $r = wp_parse_args( $args, $defaults );
  1762. /**
  1763. * Filters the arguments used in retrieving the comment list.
  1764. *
  1765. * @since 4.0.0
  1766. *
  1767. * @see wp_list_comments()
  1768. *
  1769. * @param array $r An array of arguments for displaying comments.
  1770. */
  1771. $r = apply_filters( 'wp_list_comments_args', $r );
  1772. // Figure out what comments we'll be looping through ($_comments)
  1773. if ( null !== $comments ) {
  1774. $comments = (array) $comments;
  1775. if ( empty($comments) )
  1776. return;
  1777. if ( 'all' != $r['type'] ) {
  1778. $comments_by_type = separate_comments($comments);
  1779. if ( empty($comments_by_type[$r['type']]) )
  1780. return;
  1781. $_comments = $comments_by_type[$r['type']];
  1782. } else {
  1783. $_comments = $comments;
  1784. }
  1785. } else {
  1786. /*
  1787. * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
  1788. * perform a separate comment query and allow Walker_Comment to paginate.
  1789. */
  1790. if ( $r['page'] || $r['per_page'] ) {
  1791. $current_cpage = get_query_var( 'cpage' );
  1792. if ( ! $current_cpage ) {
  1793. $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
  1794. }
  1795. $current_per_page = get_query_var( 'comments_per_page' );
  1796. if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
  1797. $comment_args = array(
  1798. 'post_id' => get_the_ID(),
  1799. 'orderby' => 'comment_date_gmt',
  1800. 'order' => 'ASC',
  1801. 'status' => 'approve',
  1802. );
  1803. if ( is_user_logged_in() ) {
  1804. $comment_args['include_unapproved'] = get_current_user_id();
  1805. } else {
  1806. $commenter = wp_get_current_commenter();
  1807. if ( $commenter['comment_author_email'] ) {
  1808. $comment_args['include_unapproved'] = $commenter['comment_author_email'];
  1809. }
  1810. }
  1811. $comments = get_comments( $comment_args );
  1812. if ( 'all' != $r['type'] ) {
  1813. $comments_by_type = separate_comments( $comments );
  1814. if ( empty( $comments_by_type[ $r['type'] ] ) ) {
  1815. return;
  1816. }
  1817. $_comments = $comments_by_type[ $r['type'] ];
  1818. } else {
  1819. $_comments = $comments;
  1820. }
  1821. }
  1822. // Otherwise, fall back on the comments from `$wp_query->comments`.
  1823. } else {
  1824. if ( empty($wp_query->comments) )
  1825. return;
  1826. if ( 'all' != $r['type'] ) {
  1827. if ( empty($wp_query->comments_by_type) )
  1828. $wp_query->comments_by_type = separate_comments($wp_query->comments);
  1829. if ( empty($wp_query->comments_by_type[$r['type']]) )
  1830. return;
  1831. $_comments = $wp_query->comments_by_type[$r['type']];
  1832. } else {
  1833. $_comments = $wp_query->comments;
  1834. }
  1835. if ( $wp_query->max_num_comment_pages ) {
  1836. $default_comments_page = get_option( 'default_comments_page' );
  1837. $cpage = get_query_var( 'cpage' );
  1838. if ( 'newest' === $default_comments_page ) {
  1839. $r['cpage'] = $cpage;
  1840. /*
  1841. * When first page shows oldest comments, post permalink is the same as
  1842. * the comment permalink.
  1843. */
  1844. } elseif ( $cpage == 1 ) {
  1845. $r['cpage'] = '';
  1846. } else {
  1847. $r['cpage'] = $cpage;
  1848. }
  1849. $r['page'] = 0;
  1850. $r['per_page'] = 0;
  1851. }
  1852. }
  1853. }
  1854. if ( '' === $r['per_page'] && get_option( 'page_comments' ) ) {
  1855. $r['per_page'] = get_query_var('comments_per_page');
  1856. }
  1857. if ( empty($r['per_page']) ) {
  1858. $r['per_page'] = 0;
  1859. $r['page'] = 0;
  1860. }
  1861. if ( '' === $r['max_depth'] ) {
  1862. if ( get_option('thread_comments') )
  1863. $r['max_depth'] = get_option('thread_comments_depth');
  1864. else
  1865. $r['max_depth'] = -1;
  1866. }
  1867. if ( '' === $r['page'] ) {
  1868. if ( empty($overridden_cpage) ) {
  1869. $r['page'] = get_query_var('cpage');
  1870. } else {
  1871. $threaded = ( -1 != $r['max_depth'] );
  1872. $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
  1873. set_query_var( 'cpage', $r['page'] );
  1874. }
  1875. }
  1876. // Validation check
  1877. $r['page'] = intval($r['page']);
  1878. if ( 0 == $r['page'] && 0 != $r['per_page'] )
  1879. $r['page'] = 1;
  1880. if ( null === $r['reverse_top_level'] )
  1881. $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
  1882. wp_queue_comments_for_comment_meta_lazyload( $_comments );
  1883. if ( empty( $r['walker'] ) ) {
  1884. $walker = new Walker_Comment;
  1885. } else {
  1886. $walker = $r['walker'];
  1887. }
  1888. $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r );
  1889. $in_comment_loop = false;
  1890. if ( $r['echo'] ) {
  1891. echo $output;
  1892. } else {
  1893. return $output;
  1894. }
  1895. }
  1896. /**
  1897. * Outputs a complete commenting form for use within a template.
  1898. *
  1899. * Most strings and form fields may be controlled through the $args array passed
  1900. * into the function, while you may also choose to use the {@see 'comment_form_default_fields'}
  1901. * filter to modify the array of default fields if you'd just like to add a new
  1902. * one or remove a single field. All fields are also individually passed through
  1903. * a filter of the {@see 'comment_form_field_$name'} where $name is the key used
  1904. * in the array of fields.
  1905. *
  1906. * @since 3.0.0
  1907. * @since 4.1.0 Introduced the 'class_submit' argument.
  1908. * @since 4.2.0 Introduced the 'submit_button' and 'submit_fields' arguments.
  1909. * @since 4.4.0 Introduced the 'class_form', 'title_reply_before', 'title_reply_after',
  1910. * 'cancel_reply_before', and 'cancel_reply_after' arguments.
  1911. * @since 4.5.0 The 'author', 'email', and 'url' form fields are limited to 245, 100,
  1912. * and 200 characters, respectively.
  1913. * @since 4.6.0 Introduced the 'action' argument.
  1914. *
  1915. * @param array $args {
  1916. * Optional. Default arguments and form fields to override.
  1917. *
  1918. * @type array $fields {
  1919. * Default comment fields, filterable by default via the {@see 'comment_form_default_fields'} hook.
  1920. *
  1921. * @type string $author Comment author field HTML.
  1922. * @type string $email Comment author email field HTML.
  1923. * @type string $url Comment author URL field HTML.
  1924. * }
  1925. * @type string $comment_field The comment textarea field HTML.
  1926. * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
  1927. * @type string $logged_in_as HTML element for a 'logged in as [user]' message.
  1928. * @type string $comment_notes_before HTML element for a message displayed before the comment fields
  1929. * if the user is not logged in.
  1930. * Default 'Your email address will not be published.'.
  1931. * @type string $comment_notes_after HTML element for a message displayed after the textarea field.
  1932. * @type string $action The comment form element action attribute. Default '/wp-comments-post.php'.
  1933. * @type string $id_form The comment form element id attribute. Default 'commentform'.
  1934. * @type string $id_submit The comment submit element id attribute. Default 'submit'.
  1935. * @type string $class_form The comment form element class attribute. Default 'comment-form'.
  1936. * @type string $class_submit The comment submit element class attribute. Default 'submit'.
  1937. * @type string $name_submit The comment submit element name attribute. Default 'submit'.
  1938. * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
  1939. * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
  1940. * where %s is the author of the comment being replied to.
  1941. * @type string $title_reply_before HTML displayed before the comment form title.
  1942. * Default: '<h3 id="reply-title" class="comment-reply-title">'.
  1943. * @type string $title_reply_after HTML displayed after the comment form title.
  1944. * Default: '</h3>'.
  1945. * @type string $cancel_reply_before HTML displayed before the cancel reply link.
  1946. * @type string $cancel_reply_after HTML displayed after the cancel reply link.
  1947. * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
  1948. * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
  1949. * @type string $submit_button HTML format for the Submit button.
  1950. * Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
  1951. * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden
  1952. * fields. Default: '<p class="form-submit">%1$s %2$s</a>', where %1$s is the
  1953. * submit button markup and %2$s is the comment hidden fields.
  1954. * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
  1955. * }
  1956. * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
  1957. */
  1958. function comment_form( $args = array(), $post_id = null ) {
  1959. if ( null === $post_id )
  1960. $post_id = get_the_ID();
  1961. // Exit the function when comments for the post are closed.
  1962. if ( ! comments_open( $post_id ) ) {
  1963. /**
  1964. * Fires after the comment form if comments are closed.
  1965. *
  1966. * @since 3.0.0
  1967. */
  1968. do_action( 'comment_form_comments_closed' );
  1969. return;
  1970. }
  1971. $commenter = wp_get_current_commenter();
  1972. $user = wp_get_current_user();
  1973. $user_identity = $user->exists() ? $user->display_name : '';
  1974. $args = wp_parse_args( $args );
  1975. if ( ! isset( $args['format'] ) )
  1976. $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
  1977. $req = get_option( 'require_name_email' );
  1978. $aria_req = ( $req ? " aria-required='true'" : '' );
  1979. $html_req = ( $req ? " required='required'" : '' );
  1980. $html5 = 'html5' === $args['format'];
  1981. $fields = array(
  1982. 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  1983. '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245"' . $aria_req . $html_req . ' /></p>',
  1984. 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  1985. '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" maxlength="100" aria-describedby="email-notes"' . $aria_req . $html_req . ' /></p>',
  1986. 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
  1987. '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>',
  1988. );
  1989. $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
  1990. /**
  1991. * Filters the default comment form fields.
  1992. *
  1993. * @since 3.0.0
  1994. *
  1995. * @param array $fields The default comment fields.
  1996. */
  1997. $fields = apply_filters( 'comment_form_default_fields', $fields );
  1998. $defaults = array(
  1999. 'fields' => $fields,
  2000. 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>',
  2001. /** This filter is documented in wp-includes/link-template.php */
  2002. 'must_log_in' => '<p class="must-log-in">' . sprintf(
  2003. /* translators: %s: login URL */
  2004. __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
  2005. wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) )
  2006. ) . '</p>',
  2007. /** This filter is documented in wp-includes/link-template.php */
  2008. 'logged_in_as' => '<p class="logged-in-as">' . sprintf(
  2009. /* translators: 1: edit user link, 2: accessibility text, 3: user name, 4: logout URL */
  2010. __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
  2011. get_edit_user_link(),
  2012. /* translators: %s: user name */
  2013. esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.' ), $user_identity ) ),
  2014. $user_identity,
  2015. wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) )
  2016. ) . '</p>',
  2017. 'comment_notes_before' => '<p class="comment-notes"><span id="email-notes">' . __( 'Your email address will not be published.' ) . '</span>'. ( $req ? $required_text : '' ) . '</p>',
  2018. 'comment_notes_after' => '',
  2019. 'action' => site_url( '/wp-comments-post.php' ),
  2020. 'id_form' => 'commentform',
  2021. 'id_submit' => 'submit',
  2022. 'class_form' => 'comment-form',
  2023. 'class_submit' => 'submit',
  2024. 'name_submit' => 'submit',
  2025. 'title_reply' => __( 'Leave a Reply' ),
  2026. 'title_reply_to' => __( 'Leave a Reply to %s' ),
  2027. 'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">',
  2028. 'title_reply_after' => '</h3>',
  2029. 'cancel_reply_before' => ' <small>',
  2030. 'cancel_reply_after' => '</small>',
  2031. 'cancel_reply_link' => __( 'Cancel reply' ),
  2032. 'label_submit' => __( 'Post Comment' ),
  2033. 'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
  2034. 'submit_field' => '<p class="form-submit">%1$s %2$s</p>',
  2035. 'format' => 'xhtml',
  2036. );
  2037. /**
  2038. * Filters the comment form default arguments.
  2039. *
  2040. * Use {@see 'comment_form_default_fields'} to filter the comment fields.
  2041. *
  2042. * @since 3.0.0
  2043. *
  2044. * @param array $defaults The default comment form arguments.
  2045. */
  2046. $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
  2047. // Ensure that the filtered args contain all required default values.
  2048. $args = array_merge( $defaults, $args );
  2049. /**
  2050. * Fires before the comment form.
  2051. *
  2052. * @since 3.0.0
  2053. */
  2054. do_action( 'comment_form_before' );
  2055. ?>
  2056. <div id="respond" class="comment-respond">
  2057. <?php
  2058. echo $args['title_reply_before'];
  2059. comment_form_title( $args['title_reply'], $args['title_reply_to'] );
  2060. echo $args['cancel_reply_before'];
  2061. cancel_comment_reply_link( $args['cancel_reply_link'] );
  2062. echo $args['cancel_reply_after'];
  2063. echo $args['title_reply_after'];
  2064. if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) :
  2065. echo $args['must_log_in'];
  2066. /**
  2067. * Fires after the HTML-formatted 'must log in after' message in the comment form.
  2068. *
  2069. * @since 3.0.0
  2070. */
  2071. do_action( 'comment_form_must_log_in_after' );
  2072. else : ?>
  2073. <form action="<?php echo esc_url( $args['action'] ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="<?php echo esc_attr( $args['class_form'] ); ?>"<?php echo $html5 ? ' novalidate' : ''; ?>>
  2074. <?php
  2075. /**
  2076. * Fires at the top of the comment form, inside the form tag.
  2077. *
  2078. * @since 3.0.0
  2079. */
  2080. do_action( 'comment_form_top' );
  2081. if ( is_user_logged_in() ) :
  2082. /**
  2083. * Filters the 'logged in' message for the comment form for display.
  2084. *
  2085. * @since 3.0.0
  2086. *
  2087. * @param string $args_logged_in The logged-in-as HTML-formatted message.
  2088. * @param array $commenter An array containing the comment author's
  2089. * username, email, and URL.
  2090. * @param string $user_identity If the commenter is a registered user,
  2091. * the display name, blank otherwise.
  2092. */
  2093. echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
  2094. /**
  2095. * Fires after the is_user_logged_in() check in the comment form.
  2096. *
  2097. * @since 3.0.0
  2098. *
  2099. * @param array $commenter An array containing the comment author's
  2100. * username, email, and URL.
  2101. * @param string $user_identity If the commenter is a registered user,
  2102. * the display name, blank otherwise.
  2103. */
  2104. do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
  2105. else :
  2106. echo $args['comment_notes_before'];
  2107. endif;
  2108. // Prepare an array of all fields, including the textarea
  2109. $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];
  2110. /**
  2111. * Filters the comment form fields, including the textarea.
  2112. *
  2113. * @since 4.4.0
  2114. *
  2115. * @param array $comment_fields The comment fields.
  2116. */
  2117. $comment_fields = apply_filters( 'comment_form_fields', $comment_fields );
  2118. // Get an array of field names, excluding the textarea
  2119. $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) );
  2120. // Get the first and the last field name, excluding the textarea
  2121. $first_field = reset( $comment_field_keys );
  2122. $last_field = end( $comment_field_keys );
  2123. foreach ( $comment_fields as $name => $field ) {
  2124. if ( 'comment' === $name ) {
  2125. /**
  2126. * Filters the content of the comment textarea field for display.
  2127. *
  2128. * @since 3.0.0
  2129. *
  2130. * @param string $args_comment_field The content of the comment textarea field.
  2131. */
  2132. echo apply_filters( 'comment_form_field_comment', $field );
  2133. echo $args['comment_notes_after'];
  2134. } elseif ( ! is_user_logged_in() ) {
  2135. if ( $first_field === $name ) {
  2136. /**
  2137. * Fires before the comment fields in the comment form, excluding the textarea.
  2138. *
  2139. * @since 3.0.0
  2140. */
  2141. do_action( 'comment_form_before_fields' );
  2142. }
  2143. /**
  2144. * Filters a comment form field for display.
  2145. *
  2146. * The dynamic portion of the filter hook, `$name`, refers to the name
  2147. * of the comment form field. Such as 'author', 'email', or 'url'.
  2148. *
  2149. * @since 3.0.0
  2150. *
  2151. * @param string $field The HTML-formatted output of the comment form field.
  2152. */
  2153. echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
  2154. if ( $last_field === $name ) {
  2155. /**
  2156. * Fires after the comment fields in the comment form, excluding the textarea.
  2157. *
  2158. * @since 3.0.0
  2159. */
  2160. do_action( 'comment_form_after_fields' );
  2161. }
  2162. }
  2163. }
  2164. $submit_button = sprintf(
  2165. $args['submit_button'],
  2166. esc_attr( $args['name_submit'] ),
  2167. esc_attr( $args['id_submit'] ),
  2168. esc_attr( $args['class_submit'] ),
  2169. esc_attr( $args['label_submit'] )
  2170. );
  2171. /**
  2172. * Filters the submit button for the comment form to display.
  2173. *
  2174. * @since 4.2.0
  2175. *
  2176. * @param string $submit_button HTML markup for the submit button.
  2177. * @param array $args Arguments passed to `comment_form()`.
  2178. */
  2179. $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );
  2180. $submit_field = sprintf(
  2181. $args['submit_field'],
  2182. $submit_button,
  2183. get_comment_id_fields( $post_id )
  2184. );
  2185. /**
  2186. * Filters the submit field for the comment form to display.
  2187. *
  2188. * The submit field includes the submit button, hidden fields for the
  2189. * comment form, and any wrapper markup.
  2190. *
  2191. * @since 4.2.0
  2192. *
  2193. * @param string $submit_field HTML markup for the submit field.
  2194. * @param array $args Arguments passed to comment_form().
  2195. */
  2196. echo apply_filters( 'comment_form_submit_field', $submit_field, $args );
  2197. /**
  2198. * Fires at the bottom of the comment form, inside the closing </form> tag.
  2199. *
  2200. * @since 1.5.0
  2201. *
  2202. * @param int $post_id The post ID.
  2203. */
  2204. do_action( 'comment_form', $post_id );
  2205. ?>
  2206. </form>
  2207. <?php endif; ?>
  2208. </div><!-- #respond -->
  2209. <?php
  2210. /**
  2211. * Fires after the comment form.
  2212. *
  2213. * @since 3.0.0
  2214. */
  2215. do_action( 'comment_form_after' );
  2216. }