/wp-includes/comment-template.php

https://gitlab.com/juanito.abelo/nlmobile · PHP · 1486 lines · 503 code · 139 blank · 844 comment · 114 complexity · b89f7eed2548e11ce45f967bfb54cc52 MD5 · raw file

  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 em