PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/comment-template.php

https://github.com/markjaquith/WordPress
PHP | 2746 lines | 1634 code | 180 blank | 932 comment | 132 complexity | 7da42de87acac1e9e729d253dcaf6bc2 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full 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. * Retrieves 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. $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
  27. if ( $user ) {
  28. $author = $user->display_name;
  29. } else {
  30. $author = __( 'Anonymous' );
  31. }
  32. } else {
  33. $author = $comment->comment_author;
  34. }
  35. /**
  36. * Filters the returned comment author name.
  37. *
  38. * @since 1.5.0
  39. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  40. *
  41. * @param string $author The comment author's username.
  42. * @param string $comment_ID The comment ID as a numeric string.
  43. * @param WP_Comment $comment The comment object.
  44. */
  45. return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
  46. }
  47. /**
  48. * Displays the author of the current comment.
  49. *
  50. * @since 0.71
  51. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  52. *
  53. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author.
  54. * Default current comment.
  55. */
  56. function comment_author( $comment_ID = 0 ) {
  57. $comment = get_comment( $comment_ID );
  58. $author = get_comment_author( $comment );
  59. /**
  60. * Filters the comment author's name for display.
  61. *
  62. * @since 1.2.0
  63. * @since 4.1.0 The `$comment_ID` parameter was added.
  64. *
  65. * @param string $author The comment author's username.
  66. * @param string $comment_ID The comment ID as a numeric string.
  67. */
  68. echo apply_filters( 'comment_author', $author, $comment->comment_ID );
  69. }
  70. /**
  71. * Retrieves the email of the author of the current comment.
  72. *
  73. * @since 1.5.0
  74. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  75. *
  76. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's email.
  77. * Default current comment.
  78. * @return string The current comment author's email
  79. */
  80. function get_comment_author_email( $comment_ID = 0 ) {
  81. $comment = get_comment( $comment_ID );
  82. /**
  83. * Filters the comment author's returned email address.
  84. *
  85. * @since 1.5.0
  86. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  87. *
  88. * @param string $comment_author_email The comment author's email address.
  89. * @param string $comment_ID The comment ID as a numeric string.
  90. * @param WP_Comment $comment The comment object.
  91. */
  92. return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
  93. }
  94. /**
  95. * Displays the email of the author of the current global $comment.
  96. *
  97. * Care should be taken to protect the email address and assure that email
  98. * harvesters do not capture your commenter's email address. Most assume that
  99. * their email address will not appear in raw form on the site. Doing so will
  100. * enable anyone, including those that people don't want to get the email
  101. * address and use it for their own means good and bad.
  102. *
  103. * @since 0.71
  104. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  105. *
  106. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's email.
  107. * Default current comment.
  108. */
  109. function comment_author_email( $comment_ID = 0 ) {
  110. $comment = get_comment( $comment_ID );
  111. $author_email = get_comment_author_email( $comment );
  112. /**
  113. * Filters the comment author's email for display.
  114. *
  115. * @since 1.2.0
  116. * @since 4.1.0 The `$comment_ID` parameter was added.
  117. *
  118. * @param string $author_email The comment author's email address.
  119. * @param string $comment_ID The comment ID as a numeric string.
  120. */
  121. echo apply_filters( 'author_email', $author_email, $comment->comment_ID );
  122. }
  123. /**
  124. * Displays the HTML email link to the author of the current comment.
  125. *
  126. * Care should be taken to protect the email address and assure that email
  127. * harvesters do not capture your commenter's email address. Most assume that
  128. * their email address will not appear in raw form on the site. Doing so will
  129. * enable anyone, including those that people don't want to get the email
  130. * address and use it for their own means good and bad.
  131. *
  132. * @since 0.71
  133. * @since 4.6.0 Added the `$comment` parameter.
  134. *
  135. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  136. * Default empty.
  137. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  138. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  139. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
  140. */
  141. function comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
  142. $link = get_comment_author_email_link( $linktext, $before, $after, $comment );
  143. if ( $link ) {
  144. echo $link;
  145. }
  146. }
  147. /**
  148. * Returns the HTML email link to the author of the current comment.
  149. *
  150. * Care should be taken to protect the email address and assure that email
  151. * harvesters do not capture your commenter's email address. Most assume that
  152. * their email address will not appear in raw form on the site. Doing so will
  153. * enable anyone, including those that people don't want to get the email
  154. * address and use it for their own means good and bad.
  155. *
  156. * @since 2.7.0
  157. * @since 4.6.0 Added the `$comment` parameter.
  158. *
  159. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  160. * Default empty.
  161. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  162. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  163. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment.
  164. * @return string HTML markup for the comment author email link. By default, the email address is obfuscated
  165. * via the {@see 'comment_email'} filter with antispambot().
  166. */
  167. function get_comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) {
  168. $comment = get_comment( $comment );
  169. /**
  170. * Filters the comment author's email for display.
  171. *
  172. * Care should be taken to protect the email address and assure that email
  173. * harvesters do not capture your commenter's email address.
  174. *
  175. * @since 1.2.0
  176. * @since 4.1.0 The `$comment` parameter was added.
  177. *
  178. * @param string $comment_author_email The comment author's email address.
  179. * @param WP_Comment $comment The comment object.
  180. */
  181. $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
  182. if ( ( ! empty( $email ) ) && ( '@' !== $email ) ) {
  183. $display = ( '' !== $linktext ) ? $linktext : $email;
  184. $return = $before;
  185. $return .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( 'mailto:' . $email ), esc_html( $display ) );
  186. $return .= $after;
  187. return $return;
  188. } else {
  189. return '';
  190. }
  191. }
  192. /**
  193. * Retrieves the HTML link to the URL of the author of the current comment.
  194. *
  195. * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
  196. * which falls back to the global comment variable if the $comment_ID argument is empty.
  197. *
  198. * @since 1.5.0
  199. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  200. *
  201. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's link.
  202. * Default current comment.
  203. * @return string The comment author name or HTML link for author's URL.
  204. */
  205. function get_comment_author_link( $comment_ID = 0 ) {
  206. $comment = get_comment( $comment_ID );
  207. $url = get_comment_author_url( $comment );
  208. $author = get_comment_author( $comment );
  209. if ( empty( $url ) || 'http://' === $url ) {
  210. $return = $author;
  211. } else {
  212. $return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
  213. }
  214. /**
  215. * Filters the comment author's link for display.
  216. *
  217. * @since 1.5.0
  218. * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
  219. *
  220. * @param string $return The HTML-formatted comment author link.
  221. * Empty for an invalid URL.
  222. * @param string $author The comment author's username.
  223. * @param string $comment_ID The comment ID as a numeric string.
  224. */
  225. return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
  226. }
  227. /**
  228. * Displays the HTML link to the URL of the author of the current comment.
  229. *
  230. * @since 0.71
  231. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  232. *
  233. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's link.
  234. * Default current comment.
  235. */
  236. function comment_author_link( $comment_ID = 0 ) {
  237. echo get_comment_author_link( $comment_ID );
  238. }
  239. /**
  240. * Retrieve the IP address of the author of the current comment.
  241. *
  242. * @since 1.5.0
  243. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  244. *
  245. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's IP address.
  246. * Default current comment.
  247. * @return string Comment author's IP address, or an empty string if it's not available.
  248. */
  249. function get_comment_author_IP( $comment_ID = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  250. $comment = get_comment( $comment_ID );
  251. /**
  252. * Filters the comment author's returned IP address.
  253. *
  254. * @since 1.5.0
  255. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  256. *
  257. * @param string $comment_author_IP The comment author's IP address, or an empty string if it's not available.
  258. * @param string $comment_ID The comment ID as a numeric string.
  259. * @param WP_Comment $comment The comment object.
  260. */
  261. return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  262. }
  263. /**
  264. * Displays the IP address of the author of the current comment.
  265. *
  266. * @since 0.71
  267. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  268. *
  269. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's IP address.
  270. * Default current comment.
  271. */
  272. function comment_author_IP( $comment_ID = 0 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  273. echo esc_html( get_comment_author_IP( $comment_ID ) );
  274. }
  275. /**
  276. * Retrieves the URL of the author of the current comment, not linked.
  277. *
  278. * @since 1.5.0
  279. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  280. *
  281. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's URL.
  282. * Default current comment.
  283. * @return string Comment author URL, if provided, an empty string otherwise.
  284. */
  285. function get_comment_author_url( $comment_ID = 0 ) {
  286. $comment = get_comment( $comment_ID );
  287. $url = '';
  288. $id = 0;
  289. if ( ! empty( $comment ) ) {
  290. $author_url = ( 'http://' === $comment->comment_author_url ) ? '' : $comment->comment_author_url;
  291. $url = esc_url( $author_url, array( 'http', 'https' ) );
  292. $id = $comment->comment_ID;
  293. }
  294. /**
  295. * Filters the comment author's URL.
  296. *
  297. * @since 1.5.0
  298. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  299. *
  300. * @param string $url The comment author's URL, or an empty string.
  301. * @param string|int $comment_ID The comment ID as a numeric string, or 0 if not found.
  302. * @param WP_Comment|null $comment The comment object, or null if not found.
  303. */
  304. return apply_filters( 'get_comment_author_url', $url, $id, $comment );
  305. }
  306. /**
  307. * Displays the URL of the author of the current comment, not linked.
  308. *
  309. * @since 0.71
  310. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  311. *
  312. * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's URL.
  313. * Default current comment.
  314. */
  315. function comment_author_url( $comment_ID = 0 ) {
  316. $comment = get_comment( $comment_ID );
  317. $author_url = get_comment_author_url( $comment );
  318. /**
  319. * Filters the comment author's URL for display.
  320. *
  321. * @since 1.2.0
  322. * @since 4.1.0 The `$comment_ID` parameter was added.
  323. *
  324. * @param string $author_url The comment author's URL.
  325. * @param string $comment_ID The comment ID as a numeric string.
  326. */
  327. echo apply_filters( 'comment_url', $author_url, $comment->comment_ID );
  328. }
  329. /**
  330. * Retrieves the HTML link of the URL of the author of the current comment.
  331. *
  332. * $linktext parameter is only used if the URL does not exist for the comment
  333. * author. If the URL does exist then the URL will be used and the $linktext
  334. * will be ignored.
  335. *
  336. * Encapsulate the HTML link between the $before and $after. So it will appear
  337. * in the order of $before, link, and finally $after.
  338. *
  339. * @since 1.5.0
  340. * @since 4.6.0 Added the `$comment` parameter.
  341. *
  342. * @param string $linktext Optional. The text to display instead of the comment
  343. * author's email address. Default empty.
  344. * @param string $before Optional. The text or HTML to display before the email link.
  345. * Default empty.
  346. * @param string $after Optional. The text or HTML to display after the email link.
  347. * Default empty.
  348. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
  349. * Default is the current comment.
  350. * @return string The HTML link between the $before and $after parameters.
  351. */
  352. function get_comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
  353. $url = get_comment_author_url( $comment );
  354. $display = ( '' !== $linktext ) ? $linktext : $url;
  355. $display = str_replace( 'http://www.', '', $display );
  356. $display = str_replace( 'http://', '', $display );
  357. if ( '/' === substr( $display, -1 ) ) {
  358. $display = substr( $display, 0, -1 );
  359. }
  360. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  361. /**
  362. * Filters the comment author's returned URL link.
  363. *
  364. * @since 1.5.0
  365. *
  366. * @param string $return The HTML-formatted comment author URL link.
  367. */
  368. return apply_filters( 'get_comment_author_url_link', $return );
  369. }
  370. /**
  371. * Displays the HTML link of the URL of the author of the current comment.
  372. *
  373. * @since 0.71
  374. * @since 4.6.0 Added the `$comment` parameter.
  375. *
  376. * @param string $linktext Optional. Text to display instead of the comment author's
  377. * email address. Default empty.
  378. * @param string $before Optional. Text or HTML to display before the email link.
  379. * Default empty.
  380. * @param string $after Optional. Text or HTML to display after the email link.
  381. * Default empty.
  382. * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object.
  383. * Default is the current comment.
  384. */
  385. function comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
  386. echo get_comment_author_url_link( $linktext, $before, $after, $comment );
  387. }
  388. /**
  389. * Generates semantic classes for each comment element.
  390. *
  391. * @since 2.7.0
  392. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
  393. *
  394. * @param string|string[] $class Optional. One or more classes to add to the class list.
  395. * Default empty.
  396. * @param int|WP_Comment $comment Comment ID or WP_Comment object. Default current comment.
  397. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  398. * @param bool $echo Optional. Whether to echo or return the output.
  399. * Default true.
  400. * @return void|string Void if `$echo` argument is true, comment classes if `$echo` is false.
  401. */
  402. function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
  403. // Separates classes with a single space, collates classes for comment DIV.
  404. $class = 'class="' . implode( ' ', get_comment_class( $class, $comment, $post_id ) ) . '"';
  405. if ( $echo ) {
  406. echo $class;
  407. } else {
  408. return $class;
  409. }
  410. }
  411. /**
  412. * Returns the classes for the comment div as an array.
  413. *
  414. * @since 2.7.0
  415. * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
  416. *
  417. * @global int $comment_alt
  418. * @global int $comment_depth
  419. * @global int $comment_thread_alt
  420. *
  421. * @param string|string[] $class Optional. One or more classes to add to the class list. Default empty.
  422. * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. Default current comment.
  423. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  424. * @return string[] An array of classes.
  425. */
  426. function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
  427. global $comment_alt, $comment_depth, $comment_thread_alt;
  428. $classes = array();
  429. $comment = get_comment( $comment_id );
  430. if ( ! $comment ) {
  431. return $classes;
  432. }
  433. // Get the comment type (comment, trackback).
  434. $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
  435. // Add classes for comment authors that are registered users.
  436. $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
  437. if ( $user ) {
  438. $classes[] = 'byuser';
  439. $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
  440. // For comment authors who are the author of the post.
  441. $post = get_post( $post_id );
  442. if ( $post ) {
  443. if ( $comment->user_id === $post->post_author ) {
  444. $classes[] = 'bypostauthor';
  445. }
  446. }
  447. }
  448. if ( empty( $comment_alt ) ) {
  449. $comment_alt = 0;
  450. }
  451. if ( empty( $comment_depth ) ) {
  452. $comment_depth = 1;
  453. }
  454. if ( empty( $comment_thread_alt ) ) {
  455. $comment_thread_alt = 0;
  456. }
  457. if ( $comment_alt % 2 ) {
  458. $classes[] = 'odd';
  459. $classes[] = 'alt';
  460. } else {
  461. $classes[] = 'even';
  462. }
  463. $comment_alt++;
  464. // Alt for top-level comments.
  465. if ( 1 == $comment_depth ) {
  466. if ( $comment_thread_alt % 2 ) {
  467. $classes[] = 'thread-odd';
  468. $classes[] = 'thread-alt';
  469. } else {
  470. $classes[] = 'thread-even';
  471. }
  472. $comment_thread_alt++;
  473. }
  474. $classes[] = "depth-$comment_depth";
  475. if ( ! empty( $class ) ) {
  476. if ( ! is_array( $class ) ) {
  477. $class = preg_split( '#\s+#', $class );
  478. }
  479. $classes = array_merge( $classes, $class );
  480. }
  481. $classes = array_map( 'esc_attr', $classes );
  482. /**
  483. * Filters the returned CSS classes for the current comment.
  484. *
  485. * @since 2.7.0
  486. *
  487. * @param string[] $classes An array of comment classes.
  488. * @param string[] $class An array of additional classes added to the list.
  489. * @param string $comment_id The comment ID as a numeric string.
  490. * @param WP_Comment $comment The comment object.
  491. * @param int|WP_Post $post_id The post ID or WP_Post object.
  492. */
  493. return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );
  494. }
  495. /**
  496. * Retrieves the comment date of the current comment.
  497. *
  498. * @since 1.5.0
  499. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  500. *
  501. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  502. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the date.
  503. * Default current comment.
  504. * @return string The comment's date.
  505. */
  506. function get_comment_date( $format = '', $comment_ID = 0 ) {
  507. $comment = get_comment( $comment_ID );
  508. $_format = ! empty( $format ) ? $format : get_option( 'date_format' );
  509. $date = mysql2date( $_format, $comment->comment_date );
  510. /**
  511. * Filters the returned comment date.
  512. *
  513. * @since 1.5.0
  514. *
  515. * @param string|int $date Formatted date string or Unix timestamp.
  516. * @param string $format PHP date format.
  517. * @param WP_Comment $comment The comment object.
  518. */
  519. return apply_filters( 'get_comment_date', $date, $format, $comment );
  520. }
  521. /**
  522. * Displays the comment date of the current comment.
  523. *
  524. * @since 0.71
  525. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  526. *
  527. * @param string $format Optional. PHP date format. Defaults to the 'date_format' option.
  528. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the date.
  529. * Default current comment.
  530. */
  531. function comment_date( $format = '', $comment_ID = 0 ) {
  532. echo get_comment_date( $format, $comment_ID );
  533. }
  534. /**
  535. * Retrieves the excerpt of the given comment.
  536. *
  537. * Returns a maximum of 20 words with an ellipsis appended if necessary.
  538. *
  539. * @since 1.5.0
  540. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  541. *
  542. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the excerpt.
  543. * Default current comment.
  544. * @return string The possibly truncated comment excerpt.
  545. */
  546. function get_comment_excerpt( $comment_ID = 0 ) {
  547. $comment = get_comment( $comment_ID );
  548. if ( ! post_password_required( $comment->comment_post_ID ) ) {
  549. $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
  550. } else {
  551. $comment_text = __( 'Password protected' );
  552. }
  553. /* translators: Maximum number of words used in a comment excerpt. */
  554. $comment_excerpt_length = (int) _x( '20', 'comment_excerpt_length' );
  555. /**
  556. * Filters the maximum number of words used in the comment excerpt.
  557. *
  558. * @since 4.4.0
  559. *
  560. * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
  561. */
  562. $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
  563. $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '&hellip;' );
  564. /**
  565. * Filters the retrieved comment excerpt.
  566. *
  567. * @since 1.5.0
  568. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  569. *
  570. * @param string $excerpt The comment excerpt text.
  571. * @param string $comment_ID The comment ID as a numeric string.
  572. * @param WP_Comment $comment The comment object.
  573. */
  574. return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
  575. }
  576. /**
  577. * Displays the excerpt of the current comment.
  578. *
  579. * @since 1.2.0
  580. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  581. *
  582. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the excerpt.
  583. * Default current comment.
  584. */
  585. function comment_excerpt( $comment_ID = 0 ) {
  586. $comment = get_comment( $comment_ID );
  587. $comment_excerpt = get_comment_excerpt( $comment );
  588. /**
  589. * Filters the comment excerpt for display.
  590. *
  591. * @since 1.2.0
  592. * @since 4.1.0 The `$comment_ID` parameter was added.
  593. *
  594. * @param string $comment_excerpt The comment excerpt text.
  595. * @param string $comment_ID The comment ID as a numeric string.
  596. */
  597. echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID );
  598. }
  599. /**
  600. * Retrieves the comment ID of the current comment.
  601. *
  602. * @since 1.5.0
  603. *
  604. * @return string The comment ID as a numeric string.
  605. */
  606. function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  607. $comment = get_comment();
  608. /**
  609. * Filters the returned comment ID.
  610. *
  611. * @since 1.5.0
  612. * @since 4.1.0 The `$comment` parameter was added.
  613. *
  614. * @param string $comment_ID The current comment ID as a numeric string.
  615. * @param WP_Comment $comment The comment object.
  616. */
  617. return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  618. }
  619. /**
  620. * Displays the comment ID of the current comment.
  621. *
  622. * @since 0.71
  623. */
  624. function comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  625. echo get_comment_ID();
  626. }
  627. /**
  628. * Retrieves the link to a given comment.
  629. *
  630. * @since 1.5.0
  631. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
  632. *
  633. * @see get_page_of_comment()
  634. *
  635. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  636. * @global bool $in_comment_loop
  637. *
  638. * @param WP_Comment|int|null $comment Comment to retrieve. Default current comment.
  639. * @param array $args {
  640. * An array of optional arguments to override the defaults.
  641. *
  642. * @type string $type Passed to get_page_of_comment().
  643. * @type int $page Current page of comments, for calculating comment pagination.
  644. * @type int $per_page Per-page value for comment pagination.
  645. * @type int $max_depth Passed to get_page_of_comment().
  646. * @type int|string $cpage Value to use for the comment's "comment-page" or "cpage" value.
  647. * If provided, this value overrides any value calculated from `$page`
  648. * and `$per_page`.
  649. * }
  650. * @return string The permalink to the given comment.
  651. */
  652. function get_comment_link( $comment = null, $args = array() ) {
  653. global $wp_rewrite, $in_comment_loop;
  654. $comment = get_comment( $comment );
  655. // Back-compat.
  656. if ( ! is_array( $args ) ) {
  657. $args = array( 'page' => $args );
  658. }
  659. $defaults = array(
  660. 'type' => 'all',
  661. 'page' => '',
  662. 'per_page' => '',
  663. 'max_depth' => '',
  664. 'cpage' => null,
  665. );
  666. $args = wp_parse_args( $args, $defaults );
  667. $link = get_permalink( $comment->comment_post_ID );
  668. // The 'cpage' param takes precedence.
  669. if ( ! is_null( $args['cpage'] ) ) {
  670. $cpage = $args['cpage'];
  671. // No 'cpage' is provided, so we calculate one.
  672. } else {
  673. if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
  674. $args['per_page'] = get_option( 'comments_per_page' );
  675. }
  676. if ( empty( $args['per_page'] ) ) {
  677. $args['per_page'] = 0;
  678. $args['page'] = 0;
  679. }
  680. $cpage = $args['page'];
  681. if ( '' == $cpage ) {
  682. if ( ! empty( $in_comment_loop ) ) {
  683. $cpage = get_query_var( 'cpage' );
  684. } else {
  685. // Requires a database hit, so we only do it when we can't figure out from context.
  686. $cpage = get_page_of_comment( $comment->comment_ID, $args );
  687. }
  688. }
  689. /*
  690. * If the default page displays the oldest comments, the permalinks for comments on the default page
  691. * do not need a 'cpage' query var.
  692. */
  693. if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
  694. $cpage = '';
  695. }
  696. }
  697. if ( $cpage && get_option( 'page_comments' ) ) {
  698. if ( $wp_rewrite->using_permalinks() ) {
  699. if ( $cpage ) {
  700. $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
  701. }
  702. $link = user_trailingslashit( $link, 'comment' );
  703. } elseif ( $cpage ) {
  704. $link = add_query_arg( 'cpage', $cpage, $link );
  705. }
  706. }
  707. if ( $wp_rewrite->using_permalinks() ) {
  708. $link = user_trailingslashit( $link, 'comment' );
  709. }
  710. $link = $link . '#comment-' . $comment->comment_ID;
  711. /**
  712. * Filters the returned single comment permalink.
  713. *
  714. * @since 2.8.0
  715. * @since 4.4.0 Added the `$cpage` parameter.
  716. *
  717. * @see get_page_of_comment()
  718. *
  719. * @param string $link The comment permalink with '#comment-$id' appended.
  720. * @param WP_Comment $comment The current comment object.
  721. * @param array $args An array of arguments to override the defaults.
  722. * @param int $cpage The calculated 'cpage' value.
  723. */
  724. return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
  725. }
  726. /**
  727. * Retrieves the link to the current post comments.
  728. *
  729. * @since 1.5.0
  730. *
  731. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  732. * @return string The link to the comments.
  733. */
  734. function get_comments_link( $post_id = 0 ) {
  735. $hash = get_comments_number( $post_id ) ? '#comments' : '#respond';
  736. $comments_link = get_permalink( $post_id ) . $hash;
  737. /**
  738. * Filters the returned post comments permalink.
  739. *
  740. * @since 3.6.0
  741. *
  742. * @param string $comments_link Post comments permalink with '#comments' appended.
  743. * @param int|WP_Post $post_id Post ID or WP_Post object.
  744. */
  745. return apply_filters( 'get_comments_link', $comments_link, $post_id );
  746. }
  747. /**
  748. * Displays the link to the current post comments.
  749. *
  750. * @since 0.71
  751. *
  752. * @param string $deprecated Not Used.
  753. * @param string $deprecated_2 Not Used.
  754. */
  755. function comments_link( $deprecated = '', $deprecated_2 = '' ) {
  756. if ( ! empty( $deprecated ) ) {
  757. _deprecated_argument( __FUNCTION__, '0.72' );
  758. }
  759. if ( ! empty( $deprecated_2 ) ) {
  760. _deprecated_argument( __FUNCTION__, '1.3.0' );
  761. }
  762. echo esc_url( get_comments_link() );
  763. }
  764. /**
  765. * Retrieves the amount of comments a post has.
  766. *
  767. * @since 1.5.0
  768. *
  769. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  770. * @return string|int If the post exists, a numeric string representing the number of comments
  771. * the post has, otherwise 0.
  772. */
  773. function get_comments_number( $post_id = 0 ) {
  774. $post = get_post( $post_id );
  775. if ( ! $post ) {
  776. $count = 0;
  777. } else {
  778. $count = $post->comment_count;
  779. $post_id = $post->ID;
  780. }
  781. /**
  782. * Filters the returned comment count for a post.
  783. *
  784. * @since 1.5.0
  785. *
  786. * @param string|int $count A string representing the number of comments a post has, otherwise 0.
  787. * @param int $post_id Post ID.
  788. */
  789. return apply_filters( 'get_comments_number', $count, $post_id );
  790. }
  791. /**
  792. * Displays the language string for the number of comments the current post has.
  793. *
  794. * @since 0.71
  795. * @since 5.4.0 The `$deprecated` parameter was changed to `$post_id`.
  796. *
  797. * @param string|false $zero Optional. Text for no comments. Default false.
  798. * @param string|false $one Optional. Text for one comment. Default false.
  799. * @param string|false $more Optional. Text for more than one comment. Default false.
  800. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  801. */
  802. function comments_number( $zero = false, $one = false, $more = false, $post_id = 0 ) {
  803. echo get_comments_number_text( $zero, $one, $more, $post_id );
  804. }
  805. /**
  806. * Displays the language string for the number of comments the current post has.
  807. *
  808. * @since 4.0.0
  809. * @since 5.4.0 Added the `$post_id` parameter to allow using the function outside of the loop.
  810. *
  811. * @param string $zero Optional. Text for no comments. Default false.
  812. * @param string $one Optional. Text for one comment. Default false.
  813. * @param string $more Optional. Text for more than one comment. Default false.
  814. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  815. * @return string Language string for the number of comments a post has.
  816. */
  817. function get_comments_number_text( $zero = false, $one = false, $more = false, $post_id = 0 ) {
  818. $number = get_comments_number( $post_id );
  819. if ( $number > 1 ) {
  820. if ( false === $more ) {
  821. /* translators: %s: Number of comments. */
  822. $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
  823. } else {
  824. // % Comments
  825. /*
  826. * translators: If comment number in your language requires declension,
  827. * translate this to 'on'. Do not translate into your own language.
  828. */
  829. if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {
  830. $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );
  831. $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities.
  832. $text = trim( strip_tags( $text ), '% ' );
  833. // Replace '% Comments' with a proper plural form.
  834. if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) {
  835. /* translators: %s: Number of comments. */
  836. $new_text = _n( '%s Comment', '%s Comments', $number );
  837. $new_text = trim( sprintf( $new_text, '' ) );
  838. $more = str_replace( $text, $new_text, $more );
  839. if ( false === strpos( $more, '%' ) ) {
  840. $more = '% ' . $more;
  841. }
  842. }
  843. }
  844. $output = str_replace( '%', number_format_i18n( $number ), $more );
  845. }
  846. } elseif ( 0 == $number ) {
  847. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  848. } else { // Must be one.
  849. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  850. }
  851. /**
  852. * Filters the comments count for display.
  853. *
  854. * @since 1.5.0
  855. *
  856. * @see _n()
  857. *
  858. * @param string $output A translatable string formatted based on whether the count
  859. * is equal to 0, 1, or 1+.
  860. * @param int $number The number of post comments.
  861. */
  862. return apply_filters( 'comments_number', $output, $number );
  863. }
  864. /**
  865. * Retrieves the text of the current comment.
  866. *
  867. * @since 1.5.0
  868. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  869. * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed.
  870. *
  871. * @see Walker_Comment::comment()
  872. *
  873. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the text.
  874. * Default current comment.
  875. * @param array $args Optional. An array of arguments. Default empty array.
  876. * @return string The comment content.
  877. */
  878. function get_comment_text( $comment_ID = 0, $args = array() ) {
  879. $comment = get_comment( $comment_ID );
  880. $comment_content = $comment->comment_content;
  881. if ( is_comment_feed() && $comment->comment_parent ) {
  882. $parent = get_comment( $comment->comment_parent );
  883. if ( $parent ) {
  884. $parent_link = esc_url( get_comment_link( $parent ) );
  885. $name = get_comment_author( $parent );
  886. $comment_content = sprintf(
  887. /* translators: %s: Comment link. */
  888. ent2ncr( __( 'In reply to %s.' ) ),
  889. '<a href="' . $parent_link . '">' . $name . '</a>'
  890. ) . "\n\n" . $comment_content;
  891. }
  892. }
  893. /**
  894. * Filters the text of a comment.
  895. *
  896. * @since 1.5.0
  897. *
  898. * @see Walker_Comment::comment()
  899. *
  900. * @param string $comment_content Text of the comment.
  901. * @param WP_Comment $comment The comment object.
  902. * @param array $args An array of arguments.
  903. */
  904. return apply_filters( 'get_comment_text', $comment_content, $comment, $args );
  905. }
  906. /**
  907. * Displays the text of the current comment.
  908. *
  909. * @since 0.71
  910. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  911. *
  912. * @see Walker_Comment::comment()
  913. *
  914. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the text.
  915. * Default current comment.
  916. * @param array $args Optional. An array of arguments. Default empty array.
  917. */
  918. function comment_text( $comment_ID = 0, $args = array() ) {
  919. $comment = get_comment( $comment_ID );
  920. $comment_text = get_comment_text( $comment, $args );
  921. /**
  922. * Filters the text of a comment to be displayed.
  923. *
  924. * @since 1.2.0
  925. *
  926. * @see Walker_Comment::comment()
  927. *
  928. * @param string $comment_text Text of the current comment.
  929. * @param WP_Comment|null $comment The comment object. Null if not found.
  930. * @param array $args An array of arguments.
  931. */
  932. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  933. }
  934. /**
  935. * Retrieves the comment time of the current comment.
  936. *
  937. * @since 1.5.0
  938. *
  939. * @param string $format Optional. PHP time format. Defaults to the 'time_format' option.
  940. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  941. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  942. * Default true.
  943. * @return string The formatted time.
  944. */
  945. function get_comment_time( $format = '', $gmt = false, $translate = true ) {
  946. $comment = get_comment();
  947. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  948. $_format = ! empty( $format ) ? $format : get_option( 'time_format' );
  949. $date = mysql2date( $_format, $comment_date, $translate );
  950. /**
  951. * Filters the returned comment time.
  952. *
  953. * @since 1.5.0
  954. *
  955. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  956. * @param string $format PHP date format.
  957. * @param bool $gmt Whether the GMT date is in use.
  958. * @param bool $translate Whether the time is translated.
  959. * @param WP_Comment $comment The comment object.
  960. */
  961. return apply_filters( 'get_comment_time', $date, $format, $gmt, $translate, $comment );
  962. }
  963. /**
  964. * Displays the comment time of the current comment.
  965. *
  966. * @since 0.71
  967. *
  968. * @param string $format Optional. PHP time format. Defaults to the 'time_format' option.
  969. */
  970. function comment_time( $format = '' ) {
  971. echo get_comment_time( $format );
  972. }
  973. /**
  974. * Retrieves the comment type of the current comment.
  975. *
  976. * @since 1.5.0
  977. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  978. *
  979. * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type.
  980. * Default current comment.
  981. * @return string The comment type.
  982. */
  983. function get_comment_type( $comment_ID = 0 ) {
  984. $comment = get_comment( $comment_ID );
  985. if ( '' === $comment->comment_type ) {
  986. $comment->comment_type = 'comment';
  987. }
  988. /**
  989. * Filters the returned comment type.
  990. *
  991. * @since 1.5.0
  992. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  993. *
  994. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  995. * @param string $comment_ID The comment ID as a numeric string.
  996. * @param WP_Comment $comment The comment object.
  997. */
  998. return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment );
  999. }
  1000. /**
  1001. * Displays the comment type of the current comment.
  1002. *
  1003. * @since 0.71
  1004. *
  1005. * @param string|false $commenttxt Optional. String to display for comment type. Default false.
  1006. * @param string|false $trackbacktxt Optional. String to display for trackback type. Default false.
  1007. * @param string|false $pingbacktxt Optional. String to display for pingback type. Default false.
  1008. */
  1009. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  1010. if ( false === $commenttxt ) {
  1011. $commenttxt = _x( 'Comment', 'noun' );
  1012. }
  1013. if ( false === $trackbacktxt ) {
  1014. $trackbacktxt = __( 'Trackback' );
  1015. }
  1016. if ( false === $pingbacktxt ) {
  1017. $pingbacktxt = __( 'Pingback' );
  1018. }
  1019. $type = get_comment_type();
  1020. switch ( $type ) {
  1021. case 'trackback':
  1022. echo $trackbacktxt;
  1023. break;
  1024. case 'pingback':
  1025. echo $pingbacktxt;
  1026. break;
  1027. default:
  1028. echo $commenttxt;
  1029. }
  1030. }
  1031. /**
  1032. * Retrieves the current post's trackback URL.
  1033. *
  1034. * There is a check to see if permalink's have been enabled and if so, will
  1035. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  1036. * current post is used and appended to the correct page to go to.
  1037. *
  1038. * @since 1.5.0
  1039. *
  1040. * @return string The trackback URL after being filtered.
  1041. */
  1042. function get_trackback_url() {
  1043. if ( get_option( 'permalink_structure' ) ) {
  1044. $tb_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' );
  1045. } else {
  1046. $tb_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID();
  1047. }
  1048. /**
  1049. * Filters the returned trackback URL.
  1050. *
  1051. * @since 2.2.0
  1052. *
  1053. * @param string $tb_url The trackback URL.
  1054. */
  1055. return apply_filters( 'trackback_url', $tb_url );
  1056. }
  1057. /**
  1058. * Displays the current post's trackback URL.
  1059. *
  1060. * @since 0.71
  1061. *
  1062. * @param bool $deprecated_echo Not used.
  1063. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  1064. * for the result instead.
  1065. */
  1066. function trackback_url( $deprecated_echo = true ) {
  1067. if ( true !== $deprecated_echo ) {
  1068. _deprecated_argument(
  1069. __FUNCTION__,
  1070. '2.5.0',
  1071. sprintf(
  1072. /* translators: %s: get_trackback_url() */
  1073. __( 'Use %s instead if you do not want the value echoed.' ),
  1074. '<code>get_trackback_url()</code>'
  1075. )
  1076. );
  1077. }
  1078. if ( $deprecated_echo ) {
  1079. echo get_trackback_url();
  1080. } else {
  1081. return get_trackback_url();
  1082. }
  1083. }
  1084. /**
  1085. * Generates and displays the RDF for the trackback information of current post.
  1086. *
  1087. * Deprecated in 3.0.0, and restored in 3.0.1.
  1088. *
  1089. * @since 0.71
  1090. *
  1091. * @param int|string $deprecated Not used (Was $timezone = 0).
  1092. */
  1093. function trackback_rdf( $deprecated = '' ) {
  1094. if ( ! empty( $deprecated ) ) {
  1095. _deprecated_argument( __FUNCTION__, '2.5.0' );
  1096. }
  1097. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  1098. return;
  1099. }
  1100. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  1101. xmlns:dc="http://purl.org/dc/elements/1.1/"
  1102. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  1103. <rdf:Description rdf:about="';
  1104. the_permalink();
  1105. echo '"' . "\n";
  1106. echo ' dc:identifier="';
  1107. the_permalink();
  1108. echo '"' . "\n";
  1109. echo ' dc:title="' . str_replace( '--', '&#x2d;&#x2d;', wptexturize( strip_tags( get_the_title() ) ) ) . '"' . "\n";
  1110. echo ' trackback:ping="' . get_trackback_url() . '"' . " />\n";
  1111. echo '</rdf:RDF>';
  1112. }
  1113. /**
  1114. * Determines whether the current post is open for comments.
  1115. *
  1116. * For more information on this and similar theme functions, check out
  1117. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1118. * Conditional Tags} article in the Theme Developer Handbook.
  1119. *
  1120. * @since 1.5.0
  1121. *
  1122. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1123. * @return bool True if the comments are open.
  1124. */
  1125. function comments_open( $post_id = null ) {
  1126. $_post = get_post( $post_id );
  1127. $post_id = $_post ? $_post->ID : 0;
  1128. $open = ( $_post && ( 'open' === $_post->comment_status ) );
  1129. /**
  1130. * Filters whether the current post is open for comments.
  1131. *
  1132. * @since 2.5.0
  1133. *
  1134. * @param bool $open Whether the current post is open for comments.
  1135. * @param int $post_id The post ID.
  1136. */
  1137. return apply_filters( 'comments_open', $open, $post_id );
  1138. }
  1139. /**
  1140. * Determines whether the current post is open for pings.
  1141. *
  1142. * For more information on this and similar theme functions, check out
  1143. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1144. * Conditional Tags} article in the Theme Developer Handbook.
  1145. *
  1146. * @since 1.5.0
  1147. *
  1148. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1149. * @return bool True if pings are accepted
  1150. */
  1151. function pings_open( $post_id = null ) {
  1152. $_post = get_post( $post_id );
  1153. $post_id = $_post ? $_post->ID : 0;
  1154. $open = ( $_post && ( 'open' === $_post->ping_status ) );
  1155. /**
  1156. * Filters whether the current post is open for pings.
  1157. *
  1158. * @since 2.5.0
  1159. *
  1160. * @param bool $open Whether the current post is open for pings.
  1161. * @param int $post_id The post ID.
  1162. */
  1163. return apply_filters( 'pings_open', $open, $post_id );
  1164. }
  1165. /**
  1166. * Displays form token for unfiltered comments.
  1167. *
  1168. * Will only display nonce token if the current user has permissions for
  1169. * unfiltered html. Won't display the token for other users.
  1170. *
  1171. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  1172. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  1173. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  1174. *
  1175. * Backported to 2.0.10.
  1176. *
  1177. * @since 2.1.3
  1178. */
  1179. function wp_comment_form_unfiltered_html_nonce() {
  1180. $post = get_post();
  1181. $post_id = $post ? $post->ID : 0;
  1182. if ( current_user_can( 'unfiltered_html' ) ) {
  1183. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  1184. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  1185. }
  1186. }
  1187. /**
  1188. * Loads the comment template specified in $file.
  1189. *
  1190. * Will not display the comments template if not on single post or page, or if
  1191. * the post does not have comments.
  1192. *
  1193. * Uses the WordPress database object to query for the comments. The comments
  1194. * are passed through the {@see 'comments_array'} filter hook with the list of comments
  1195. * and the post ID respectively.
  1196. *
  1197. * The `$file` path is passed through a filter hook called {@see 'comments_template'},
  1198. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  1199. * first and if it fails it will require the default comment template from the
  1200. * default theme. If either does not exist, then the WordPress process will be
  1201. * halted. It is advised for that reason, that the default theme is not deleted.
  1202. *
  1203. * Will not try to get the comments if the post has none.
  1204. *
  1205. * @since 1.5.0
  1206. *
  1207. * @global WP_Query $wp_query WordPress Query object.
  1208. * @global WP_Post $post Global post object.
  1209. * @global wpdb $wpdb WordPress database abstraction object.
  1210. * @global int $id
  1211. * @global WP_Comment $comment Global comment object.
  1212. * @global string $user_login
  1213. * @global string $user_identity
  1214. * @global bool $overridden_cpage
  1215. * @global bool $withcomments
  1216. *
  1217. * @param string $file Optional. The file to load. Default '/comments.php'.
  1218. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  1219. * Default false.
  1220. */
  1221. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  1222. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;
  1223. if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
  1224. return;
  1225. }
  1226. if ( empty( $file ) ) {
  1227. $file = '/comments.php';
  1228. }
  1229. $req = get_option( 'require_name_email' );
  1230. /*
  1231. * Comment author information fetched from the comment cookies.
  1232. */
  1233. $commenter = wp_get_current_commenter();
  1234. /*
  1235. * The name of the current comment author escaped for use in attributes.
  1236. * Escaped by sanitize_comment_cookies().
  1237. */
  1238. $comment_author = $commenter['comment_author'];
  1239. /*
  1240. * The email address of the current comment author escaped for use in attributes.
  1241. * Escaped by sanitize_comment_cookies().
  1242. */
  1243. $comment_author_email = $commenter['comment_author_email'];
  1244. /*
  1245. * The URL of the current comment author escaped for use in attributes.
  1246. */
  1247. $comment_author_url = esc_url( $commenter['comment_author_url'] );
  1248. $comment_args = array(
  1249. 'orderby' => 'comment_date_gmt',
  1250. 'order' => 'ASC',
  1251. 'status' => 'approve',
  1252. 'post_id' => $post->ID,
  1253. 'no_found_rows' => false,
  1254. 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
  1255. );
  1256. if ( get_option( 'thread_comments' ) ) {
  1257. $comment_args['hierarchical'] = 'threaded';
  1258. } else {
  1259. $comment_args['hierarchical'] = false;
  1260. }
  1261. if ( is_user_logged_in() ) {
  1262. $comment_args['include_unapproved'] = array( get_current_user_id() );
  1263. } else {
  1264. $unapproved_email = wp_get_unapproved_comment_author_email();
  1265. if ( $unapproved_email ) {
  1266. $comment_args['include_unapproved'] = array( $unapproved_email );
  1267. }
  1268. }
  1269. $per_page = 0;
  1270. if ( get_option( 'page_comments' ) ) {
  1271. $per_page = (int) get_query_var( 'comments_per_page' );
  1272. if ( 0 === $per_page ) {
  1273. $per_page = (int) get_option( 'comments_per_page' );
  1274. }
  1275. $comment_args['number'] = $per_page;
  1276. $page = (int) get_query_var( 'cpage' );
  1277. if ( $page ) {
  1278. $comment_args['offset'] = ( $page - 1 ) * $per_page;
  1279. } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
  1280. $comment_args['offset'] = 0;
  1281. } else {
  1282. // If fetching the first page of 'newest', we need a top-level comment count.
  1283. $top_level_query = new WP_Comment_Query();
  1284. $top_level_args = array(
  1285. 'count' => true,
  1286. 'orderby' => false,
  1287. 'post_id' => $post->ID,
  1288. 'status' => 'approve',
  1289. );
  1290. if ( $comment_args['hierarchical'] ) {
  1291. $top_level_args['parent'] = 0;
  1292. }
  1293. if ( isset( $comment_args['include_unapproved'] ) ) {
  1294. $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
  1295. }
  1296. /**
  1297. * Filters the arguments used in the top level comments query.
  1298. *
  1299. * @since 5.6.0
  1300. *
  1301. * @see WP_Comment_Query::__construct()
  1302. *
  1303. * @param array $top_level_args {
  1304. * The top level query arguments for the comments template.
  1305. *
  1306. * @type bool $count Whether to return a comment count.
  1307. * @type string|array $orderby The field(s) to order by.
  1308. * @type int $post_id The post ID.
  1309. * @type string|array $status The comment status to limit results by.
  1310. * }

Large files files are truncated, but you can click here to view the full file