PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/comment-template.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 1459 lines | 497 code | 136 blank | 826 comment | 111 complexity | dd06755975a74f8d12e48c2d32d1664d 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. * 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 int $comment_ID The comment ID.
  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 int $comment_ID The comment ID.
  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 int $comment_ID The comment ID.
  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 int $comment_ID The comment ID.
  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 int $comment_ID The comment ID.
  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.
  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.
  258. * @param int $comment_ID The comment ID.
  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.
  301. * @param int $comment_ID The comment ID.
  302. * @param WP_Comment $comment The comment object.
  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 int $comment_ID The comment ID.
  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|array $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="' . join( ' ', 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|array $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 A comma-separated list of additional classes added to the list.
  489. * @param int $comment_id The comment id.
  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. The format of the date. Default user's setting.
  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. if ( '' == $format ) {
  509. $date = mysql2date( get_option( 'date_format' ), $comment->comment_date );
  510. } else {
  511. $date = mysql2date( $format, $comment->comment_date );
  512. }
  513. /**
  514. * Filters the returned comment date.
  515. *
  516. * @since 1.5.0
  517. *
  518. * @param string|int $date Formatted date string or Unix timestamp.
  519. * @param string $format The format of the date.
  520. * @param WP_Comment $comment The comment object.
  521. */
  522. return apply_filters( 'get_comment_date', $date, $format, $comment );
  523. }
  524. /**
  525. * Displays the comment date of the current comment.
  526. *
  527. * @since 0.71
  528. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  529. *
  530. * @param string $format Optional. The format of the date. Default user's settings.
  531. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the date.
  532. * Default current comment.
  533. */
  534. function comment_date( $format = '', $comment_ID = 0 ) {
  535. echo get_comment_date( $format, $comment_ID );
  536. }
  537. /**
  538. * Retrieves the excerpt of the given comment.
  539. *
  540. * Returns a maximum of 20 words with an ellipsis appended if necessary.
  541. *
  542. * @since 1.5.0
  543. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  544. *
  545. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the excerpt.
  546. * Default current comment.
  547. * @return string The possibly truncated comment excerpt.
  548. */
  549. function get_comment_excerpt( $comment_ID = 0 ) {
  550. $comment = get_comment( $comment_ID );
  551. if ( ! post_password_required( $comment->comment_post_ID ) ) {
  552. $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
  553. } else {
  554. $comment_text = __( 'Password protected' );
  555. }
  556. /* translators: Maximum number of words used in a comment excerpt. */
  557. $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
  558. /**
  559. * Filters the maximum number of words used in the comment excerpt.
  560. *
  561. * @since 4.4.0
  562. *
  563. * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
  564. */
  565. $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
  566. $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '&hellip;' );
  567. /**
  568. * Filters the retrieved comment excerpt.
  569. *
  570. * @since 1.5.0
  571. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  572. *
  573. * @param string $excerpt The comment excerpt text.
  574. * @param int $comment_ID The comment ID.
  575. * @param WP_Comment $comment The comment object.
  576. */
  577. return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
  578. }
  579. /**
  580. * Displays the excerpt of the current comment.
  581. *
  582. * @since 1.2.0
  583. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  584. *
  585. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the excerpt.
  586. * Default current comment.
  587. */
  588. function comment_excerpt( $comment_ID = 0 ) {
  589. $comment = get_comment( $comment_ID );
  590. $comment_excerpt = get_comment_excerpt( $comment );
  591. /**
  592. * Filters the comment excerpt for display.
  593. *
  594. * @since 1.2.0
  595. * @since 4.1.0 The `$comment_ID` parameter was added.
  596. *
  597. * @param string $comment_excerpt The comment excerpt text.
  598. * @param int $comment_ID The comment ID.
  599. */
  600. echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID );
  601. }
  602. /**
  603. * Retrieves the comment id of the current comment.
  604. *
  605. * @since 1.5.0
  606. *
  607. * @return int The comment ID.
  608. */
  609. function get_comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  610. $comment = get_comment();
  611. /**
  612. * Filters the returned comment ID.
  613. *
  614. * @since 1.5.0
  615. * @since 4.1.0 The `$comment_ID` parameter was added.
  616. *
  617. * @param int $comment_ID The current comment ID.
  618. * @param WP_Comment $comment The comment object.
  619. */
  620. return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  621. }
  622. /**
  623. * Displays the comment id of the current comment.
  624. *
  625. * @since 0.71
  626. */
  627. function comment_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  628. echo get_comment_ID();
  629. }
  630. /**
  631. * Retrieves the link to a given comment.
  632. *
  633. * @since 1.5.0
  634. * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
  635. *
  636. * @see get_page_of_comment()
  637. *
  638. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  639. * @global bool $in_comment_loop
  640. *
  641. * @param WP_Comment|int|null $comment Comment to retrieve. Default current comment.
  642. * @param array $args {
  643. * An array of optional arguments to override the defaults.
  644. *
  645. * @type string $type Passed to get_page_of_comment().
  646. * @type int $page Current page of comments, for calculating comment pagination.
  647. * @type int $per_page Per-page value for comment pagination.
  648. * @type int $max_depth Passed to get_page_of_comment().
  649. * @type int|string $cpage Value to use for the comment's "comment-page" or "cpage" value.
  650. * If provided, this value overrides any value calculated from `$page`
  651. * and `$per_page`.
  652. * }
  653. * @return string The permalink to the given comment.
  654. */
  655. function get_comment_link( $comment = null, $args = array() ) {
  656. global $wp_rewrite, $in_comment_loop;
  657. $comment = get_comment( $comment );
  658. // Back-compat.
  659. if ( ! is_array( $args ) ) {
  660. $args = array( 'page' => $args );
  661. }
  662. $defaults = array(
  663. 'type' => 'all',
  664. 'page' => '',
  665. 'per_page' => '',
  666. 'max_depth' => '',
  667. 'cpage' => null,
  668. );
  669. $args = wp_parse_args( $args, $defaults );
  670. $link = get_permalink( $comment->comment_post_ID );
  671. // The 'cpage' param takes precedence.
  672. if ( ! is_null( $args['cpage'] ) ) {
  673. $cpage = $args['cpage'];
  674. // No 'cpage' is provided, so we calculate one.
  675. } else {
  676. if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
  677. $args['per_page'] = get_option( 'comments_per_page' );
  678. }
  679. if ( empty( $args['per_page'] ) ) {
  680. $args['per_page'] = 0;
  681. $args['page'] = 0;
  682. }
  683. $cpage = $args['page'];
  684. if ( '' == $cpage ) {
  685. if ( ! empty( $in_comment_loop ) ) {
  686. $cpage = get_query_var( 'cpage' );
  687. } else {
  688. // Requires a database hit, so we only do it when we can't figure out from context.
  689. $cpage = get_page_of_comment( $comment->comment_ID, $args );
  690. }
  691. }
  692. /*
  693. * If the default page displays the oldest comments, the permalinks for comments on the default page
  694. * do not need a 'cpage' query var.
  695. */
  696. if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
  697. $cpage = '';
  698. }
  699. }
  700. if ( $cpage && get_option( 'page_comments' ) ) {
  701. if ( $wp_rewrite->using_permalinks() ) {
  702. if ( $cpage ) {
  703. $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
  704. }
  705. $link = user_trailingslashit( $link, 'comment' );
  706. } elseif ( $cpage ) {
  707. $link = add_query_arg( 'cpage', $cpage, $link );
  708. }
  709. }
  710. if ( $wp_rewrite->using_permalinks() ) {
  711. $link = user_trailingslashit( $link, 'comment' );
  712. }
  713. $link = $link . '#comment-' . $comment->comment_ID;
  714. /**
  715. * Filters the returned single comment permalink.
  716. *
  717. * @since 2.8.0
  718. * @since 4.4.0 Added the `$cpage` parameter.
  719. *
  720. * @see get_page_of_comment()
  721. *
  722. * @param string $link The comment permalink with '#comment-$id' appended.
  723. * @param WP_Comment $comment The current comment object.
  724. * @param array $args An array of arguments to override the defaults.
  725. * @param int $cpage The calculated 'cpage' value.
  726. */
  727. return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
  728. }
  729. /**
  730. * Retrieves the link to the current post comments.
  731. *
  732. * @since 1.5.0
  733. *
  734. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  735. * @return string The link to the comments.
  736. */
  737. function get_comments_link( $post_id = 0 ) {
  738. $hash = get_comments_number( $post_id ) ? '#comments' : '#respond';
  739. $comments_link = get_permalink( $post_id ) . $hash;
  740. /**
  741. * Filters the returned post comments permalink.
  742. *
  743. * @since 3.6.0
  744. *
  745. * @param string $comments_link Post comments permalink with '#comments' appended.
  746. * @param int|WP_Post $post_id Post ID or WP_Post object.
  747. */
  748. return apply_filters( 'get_comments_link', $comments_link, $post_id );
  749. }
  750. /**
  751. * Displays the link to the current post comments.
  752. *
  753. * @since 0.71
  754. *
  755. * @param string $deprecated Not Used.
  756. * @param string $deprecated_2 Not Used.
  757. */
  758. function comments_link( $deprecated = '', $deprecated_2 = '' ) {
  759. if ( ! empty( $deprecated ) ) {
  760. _deprecated_argument( __FUNCTION__, '0.72' );
  761. }
  762. if ( ! empty( $deprecated_2 ) ) {
  763. _deprecated_argument( __FUNCTION__, '1.3.0' );
  764. }
  765. echo esc_url( get_comments_link() );
  766. }
  767. /**
  768. * Retrieves the amount of comments a post has.
  769. *
  770. * @since 1.5.0
  771. *
  772. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  773. * @return string|int If the post exists, a numeric string representing the number of comments
  774. * the post has, otherwise 0.
  775. */
  776. function get_comments_number( $post_id = 0 ) {
  777. $post = get_post( $post_id );
  778. if ( ! $post ) {
  779. $count = 0;
  780. } else {
  781. $count = $post->comment_count;
  782. $post_id = $post->ID;
  783. }
  784. /**
  785. * Filters the returned comment count for a post.
  786. *
  787. * @since 1.5.0
  788. *
  789. * @param string|int $count A string representing the number of comments a post has, otherwise 0.
  790. * @param int $post_id Post ID.
  791. */
  792. return apply_filters( 'get_comments_number', $count, $post_id );
  793. }
  794. /**
  795. * Displays the language string for the number of comments the current post has.
  796. *
  797. * @since 0.71
  798. * @since 5.4.0 The `$deprecated` parameter was changed to `$post_id`.
  799. *
  800. * @param string $zero Optional. Text for no comments. Default false.
  801. * @param string $one Optional. Text for one comment. Default false.
  802. * @param string $more Optional. Text for more than one comment. Default false.
  803. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  804. */
  805. function comments_number( $zero = false, $one = false, $more = false, $post_id = 0 ) {
  806. echo get_comments_number_text( $zero, $one, $more, $post_id );
  807. }
  808. /**
  809. * Displays the language string for the number of comments the current post has.
  810. *
  811. * @since 4.0.0
  812. * @since 5.4.0 Added the `$post_id` parameter to allow using the function outside of the loop.
  813. *
  814. * @param string $zero Optional. Text for no comments. Default false.
  815. * @param string $one Optional. Text for one comment. Default false.
  816. * @param string $more Optional. Text for more than one comment. Default false.
  817. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
  818. * @return string Language string for the number of comments a post has.
  819. */
  820. function get_comments_number_text( $zero = false, $one = false, $more = false, $post_id = 0 ) {
  821. $number = get_comments_number( $post_id );
  822. if ( $number > 1 ) {
  823. if ( false === $more ) {
  824. /* translators: %s: Number of comments. */
  825. $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
  826. } else {
  827. // % Comments
  828. /*
  829. * translators: If comment number in your language requires declension,
  830. * translate this to 'on'. Do not translate into your own language.
  831. */
  832. if ( 'on' === _x( 'off', 'Comment number declension: on or off' ) ) {
  833. $text = preg_replace( '#<span class="screen-reader-text">.+?</span>#', '', $more );
  834. $text = preg_replace( '/&.+?;/', '', $text ); // Kill entities.
  835. $text = trim( strip_tags( $text ), '% ' );
  836. // Replace '% Comments' with a proper plural form.
  837. if ( $text && ! preg_match( '/[0-9]+/', $text ) && false !== strpos( $more, '%' ) ) {
  838. /* translators: %s: Number of comments. */
  839. $new_text = _n( '%s Comment', '%s Comments', $number );
  840. $new_text = trim( sprintf( $new_text, '' ) );
  841. $more = str_replace( $text, $new_text, $more );
  842. if ( false === strpos( $more, '%' ) ) {
  843. $more = '% ' . $more;
  844. }
  845. }
  846. }
  847. $output = str_replace( '%', number_format_i18n( $number ), $more );
  848. }
  849. } elseif ( 0 == $number ) {
  850. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  851. } else { // Must be one.
  852. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  853. }
  854. /**
  855. * Filters the comments count for display.
  856. *
  857. * @since 1.5.0
  858. *
  859. * @see _n()
  860. *
  861. * @param string $output A translatable string formatted based on whether the count
  862. * is equal to 0, 1, or 1+.
  863. * @param int $number The number of post comments.
  864. */
  865. return apply_filters( 'comments_number', $output, $number );
  866. }
  867. /**
  868. * Retrieves the text of the current comment.
  869. *
  870. * @since 1.5.0
  871. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  872. * @since 5.4.0 Added 'In reply to %s.' prefix to child comments in comments feed.
  873. *
  874. * @see Walker_Comment::comment()
  875. *
  876. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the text.
  877. * Default current comment.
  878. * @param array $args Optional. An array of arguments. Default empty array.
  879. * @return string The comment content.
  880. */
  881. function get_comment_text( $comment_ID = 0, $args = array() ) {
  882. $comment = get_comment( $comment_ID );
  883. $comment_content = $comment->comment_content;
  884. if ( is_comment_feed() && $comment->comment_parent ) {
  885. $parent = get_comment( $comment->comment_parent );
  886. if ( $parent ) {
  887. $parent_link = esc_url( get_comment_link( $parent ) );
  888. $name = get_comment_author( $parent );
  889. $comment_content = sprintf(
  890. /* translators: %s: Comment link. */
  891. ent2ncr( __( 'In reply to %s.' ) ),
  892. '<a href="' . $parent_link . '">' . $name . '</a>'
  893. ) . "\n\n" . $comment_content;
  894. }
  895. }
  896. /**
  897. * Filters the text of a comment.
  898. *
  899. * @since 1.5.0
  900. *
  901. * @see Walker_Comment::comment()
  902. *
  903. * @param string $comment_content Text of the comment.
  904. * @param WP_Comment $comment The comment object.
  905. * @param array $args An array of arguments.
  906. */
  907. return apply_filters( 'get_comment_text', $comment_content, $comment, $args );
  908. }
  909. /**
  910. * Displays the text of the current comment.
  911. *
  912. * @since 0.71
  913. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  914. *
  915. * @see Walker_Comment::comment()
  916. *
  917. * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the text.
  918. * Default current comment.
  919. * @param array $args Optional. An array of arguments. Default empty array.
  920. */
  921. function comment_text( $comment_ID = 0, $args = array() ) {
  922. $comment = get_comment( $comment_ID );
  923. $comment_text = get_comment_text( $comment, $args );
  924. /**
  925. * Filters the text of a comment to be displayed.
  926. *
  927. * @since 1.2.0
  928. *
  929. * @see Walker_Comment::comment()
  930. *
  931. * @param string $comment_text Text of the current comment.
  932. * @param WP_Comment|null $comment The comment object. Null if not found.
  933. * @param array $args An array of arguments.
  934. */
  935. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  936. }
  937. /**
  938. * Retrieves the comment time of the current comment.
  939. *
  940. * @since 1.5.0
  941. *
  942. * @param string $format Optional. The format of the time. Default user's settings.
  943. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  944. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  945. * Default true.
  946. * @return string The formatted time.
  947. */
  948. function get_comment_time( $format = '', $gmt = false, $translate = true ) {
  949. $comment = get_comment();
  950. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  951. if ( '' == $format ) {
  952. $date = mysql2date( get_option( 'time_format' ), $comment_date, $translate );
  953. } else {
  954. $date = mysql2date( $format, $comment_date, $translate );
  955. }
  956. /**
  957. * Filters the returned comment time.
  958. *
  959. * @since 1.5.0
  960. *
  961. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  962. * @param string $format Date format.
  963. * @param bool $gmt Whether the GMT date is in use.
  964. * @param bool $translate Whether the time is translated.
  965. * @param WP_Comment $comment The comment object.
  966. */
  967. return apply_filters( 'get_comment_time', $date, $format, $gmt, $translate, $comment );
  968. }
  969. /**
  970. * Displays the comment time of the current comment.
  971. *
  972. * @since 0.71
  973. *
  974. * @param string $format Optional. The format of the time. Default user's settings.
  975. */
  976. function comment_time( $format = '' ) {
  977. echo get_comment_time( $format );
  978. }
  979. /**
  980. * Retrieves the comment type of the current comment.
  981. *
  982. * @since 1.5.0
  983. * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
  984. *
  985. * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type.
  986. * Default current comment.
  987. * @return string The comment type.
  988. */
  989. function get_comment_type( $comment_ID = 0 ) {
  990. $comment = get_comment( $comment_ID );
  991. if ( '' == $comment->comment_type ) {
  992. $comment->comment_type = 'comment';
  993. }
  994. /**
  995. * Filters the returned comment type.
  996. *
  997. * @since 1.5.0
  998. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  999. *
  1000. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  1001. * @param int $comment_ID The comment ID.
  1002. * @param WP_Comment $comment The comment object.
  1003. */
  1004. return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment );
  1005. }
  1006. /**
  1007. * Displays the comment type of the current comment.
  1008. *
  1009. * @since 0.71
  1010. *
  1011. * @param string $commenttxt Optional. String to display for comment type. Default false.
  1012. * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
  1013. * @param string $pingbacktxt Optional. String to display for pingback type. Default false.
  1014. */
  1015. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  1016. if ( false === $commenttxt ) {
  1017. $commenttxt = _x( 'Comment', 'noun' );
  1018. }
  1019. if ( false === $trackbacktxt ) {
  1020. $trackbacktxt = __( 'Trackback' );
  1021. }
  1022. if ( false === $pingbacktxt ) {
  1023. $pingbacktxt = __( 'Pingback' );
  1024. }
  1025. $type = get_comment_type();
  1026. switch ( $type ) {
  1027. case 'trackback':
  1028. echo $trackbacktxt;
  1029. break;
  1030. case 'pingback':
  1031. echo $pingbacktxt;
  1032. break;
  1033. default:
  1034. echo $commenttxt;
  1035. }
  1036. }
  1037. /**
  1038. * Retrieves the current post's trackback URL.
  1039. *
  1040. * There is a check to see if permalink's have been enabled and if so, will
  1041. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  1042. * current post is used and appended to the correct page to go to.
  1043. *
  1044. * @since 1.5.0
  1045. *
  1046. * @return string The trackback URL after being filtered.
  1047. */
  1048. function get_trackback_url() {
  1049. if ( '' != get_option( 'permalink_structure' ) ) {
  1050. $tb_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' );
  1051. } else {
  1052. $tb_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID();
  1053. }
  1054. /**
  1055. * Filters the returned trackback URL.
  1056. *
  1057. * @since 2.2.0
  1058. *
  1059. * @param string $tb_url The trackback URL.
  1060. */
  1061. return apply_filters( 'trackback_url', $tb_url );
  1062. }
  1063. /**
  1064. * Displays the current post's trackback URL.
  1065. *
  1066. * @since 0.71
  1067. *
  1068. * @param bool $deprecated_echo Not used.
  1069. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  1070. * for the result instead.
  1071. */
  1072. function trackback_url( $deprecated_echo = true ) {
  1073. if ( true !== $deprecated_echo ) {
  1074. _deprecated_argument(
  1075. __FUNCTION__,
  1076. '2.5.0',
  1077. sprintf(
  1078. /* translators: %s: get_trackback_url() */
  1079. __( 'Use %s instead if you do not want the value echoed.' ),
  1080. '<code>get_trackback_url()</code>'
  1081. )
  1082. );
  1083. }
  1084. if ( $deprecated_echo ) {
  1085. echo get_trackback_url();
  1086. } else {
  1087. return get_trackback_url();
  1088. }
  1089. }
  1090. /**
  1091. * Generates and displays the RDF for the trackback information of current post.
  1092. *
  1093. * Deprecated in 3.0.0, and restored in 3.0.1.
  1094. *
  1095. * @since 0.71
  1096. *
  1097. * @param int $deprecated Not used (Was $timezone = 0).
  1098. */
  1099. function trackback_rdf( $deprecated = '' ) {
  1100. if ( ! empty( $deprecated ) ) {
  1101. _deprecated_argument( __FUNCTION__, '2.5.0' );
  1102. }
  1103. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  1104. return;
  1105. }
  1106. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  1107. xmlns:dc="http://purl.org/dc/elements/1.1/"
  1108. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  1109. <rdf:Description rdf:about="';
  1110. the_permalink();
  1111. echo '"' . "\n";
  1112. echo ' dc:identifier="';
  1113. the_permalink();
  1114. echo '"' . "\n";
  1115. echo ' dc:title="' . str_replace( '--', '&#x2d;&#x2d;', wptexturize( strip_tags( get_the_title() ) ) ) . '"' . "\n";
  1116. echo ' trackback:ping="' . get_trackback_url() . '"' . " />\n";
  1117. echo '</rdf:RDF>';
  1118. }
  1119. /**
  1120. * Determines whether the current post is open for comments.
  1121. *
  1122. * For more information on this and similar theme functions, check out
  1123. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1124. * Conditional Tags} article in the Theme Developer Handbook.
  1125. *
  1126. * @since 1.5.0
  1127. *
  1128. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1129. * @return bool True if the comments are open.
  1130. */
  1131. function comments_open( $post_id = null ) {
  1132. $_post = get_post( $post_id );
  1133. $post_id = $_post ? $_post->ID : 0;
  1134. $open = ( 'open' == $_post->comment_status );
  1135. /**
  1136. * Filters whether the current post is open for comments.
  1137. *
  1138. * @since 2.5.0
  1139. *
  1140. * @param bool $open Whether the current post is open for comments.
  1141. * @param int $post_id The post ID.
  1142. */
  1143. return apply_filters( 'comments_open', $open, $post_id );
  1144. }
  1145. /**
  1146. * Determines whether the current post is open for pings.
  1147. *
  1148. * For more information on this and similar theme functions, check out
  1149. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1150. * Conditional Tags} article in the Theme Developer Handbook.
  1151. *
  1152. * @since 1.5.0
  1153. *
  1154. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  1155. * @return bool True if pings are accepted
  1156. */
  1157. function pings_open( $post_id = null ) {
  1158. $_post = get_post( $post_id );
  1159. $post_id = $_post ? $_post->ID : 0;
  1160. $open = ( 'open' == $_post->ping_status );
  1161. /**
  1162. * Filters whether the current post is open for pings.
  1163. *
  1164. * @since 2.5.0
  1165. *
  1166. * @param bool $open Whether the current post is open for pings.
  1167. * @param int $post_id The post ID.
  1168. */
  1169. return apply_filters( 'pings_open', $open, $post_id );
  1170. }
  1171. /**
  1172. * Displays form token for unfiltered comments.
  1173. *
  1174. * Will only display nonce token if the current user has permissions for
  1175. * unfiltered html. Won't display the token for other users.
  1176. *
  1177. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  1178. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  1179. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  1180. *
  1181. * Backported to 2.0.10.
  1182. *
  1183. * @since 2.1.3
  1184. */
  1185. function wp_comment_form_unfiltered_html_nonce() {
  1186. $post = get_post();
  1187. $post_id = $post ? $post->ID : 0;
  1188. if ( current_user_can( 'unfiltered_html' ) ) {
  1189. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  1190. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  1191. }
  1192. }
  1193. /**
  1194. * Loads the comment template specified in $file.
  1195. *
  1196. * Will not display the comments template if not on single post or page, or if
  1197. * the post does not have comments.
  1198. *
  1199. * Uses the WordPress database object to query for the comments. The comments
  1200. * are passed through the {@see 'comments_array'} filter hook with the list of comments
  1201. * and the post ID respectively.
  1202. *
  1203. * The `$file` path is passed through a filter hook called {@see 'comments_template'},
  1204. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  1205. * first and if it fails it will require the default comment template from the
  1206. * default theme. If either does not exist, then the WordPress process will be
  1207. * halted. It is advised for that reason, that the default theme is not deleted.
  1208. *
  1209. * Will not try to get the comments if the post has none.
  1210. *
  1211. * @since 1.5.0
  1212. *
  1213. * @global WP_Query $wp_query WordPress Query object.
  1214. * @global WP_Post $post Global post object.
  1215. * @global wpdb $wpdb WordPress database abstraction object.
  1216. * @global int $id
  1217. * @global WP_Comment $comment Global comment object.
  1218. * @global string $user_login
  1219. * @global int $user_ID
  1220. * @global string $user_identity
  1221. * @global bool $overridden_cpage
  1222. * @global bool $withcomments
  1223. *
  1224. * @param string $file Optional. The file to load. Default '/comments.php'.
  1225. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  1226. * Default false.
  1227. */
  1228. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  1229. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
  1230. if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
  1231. return;
  1232. }
  1233. if ( empty( $file ) ) {
  1234. $file = '/comments.php';
  1235. }
  1236. $req = get_option( 'require_name_email' );
  1237. /*
  1238. * Comment author information fetched from the comment cookies.
  1239. */
  1240. $commenter = wp_get_current_commenter();
  1241. /*
  1242. * The name of the current comment author escaped for use in attributes.
  1243. * Escaped by sanitize_comment_cookies().
  1244. */
  1245. $comment_author = $commenter['comment_author'];
  1246. /*
  1247. * The email address of the current comment author escaped for use in attributes.
  1248. * Escaped by sanitize_comment_cookies().
  1249. */
  1250. $comment_author_email = $commenter['comment_author_email'];
  1251. /*
  1252. * The URL of the current comment author escaped for use in attributes.
  1253. */
  1254. $comment_author_url = esc_url( $commenter['comment_author_url'] );
  1255. $comment_args = array(
  1256. 'orderby' => 'comment_date_gmt',
  1257. 'order' => 'ASC',
  1258. 'status' => 'approve',
  1259. 'post_id' => $post->ID,
  1260. 'no_found_rows' => false,
  1261. 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
  1262. );
  1263. if ( get_option( 'thread_comments' ) ) {
  1264. $comment_args['hierarchical'] = 'threaded';
  1265. } else {
  1266. $comment_args['hierarchical'] = false;
  1267. }
  1268. if ( $user_ID ) {
  1269. $comment_args['include_unapproved'] = array( $user_ID );
  1270. } else {
  1271. $unapproved_email = wp_get_unapproved_comment_author_email();
  1272. if ( $unapproved_email ) {
  1273. $comment_args['include_unapproved'] = array( $unapproved_email );
  1274. }
  1275. }
  1276. $per_page = 0;
  1277. if ( get_option( 'page_comments' ) ) {
  1278. $per_page = (int) get_query_var( 'comments_per_page' );
  1279. if ( 0 === $per_page ) {
  1280. $per_page = (int) get_option( 'comments_per_page' );
  1281. }
  1282. $comment_args['number'] = $per_page;
  1283. $page = (int) get_query_var( 'cpage' );
  1284. if ( $page ) {
  1285. $comment_args['offset'] = ( $page - 1 ) * $per_page;
  1286. } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
  1287. $comment_args['offset'] = 0;
  1288. } else {
  1289. // If fetching the first page of 'newest', we need a top-level comment count.
  1290. $top_level_query = new WP_Comment_Query();
  1291. $top_level_args = array(
  1292. 'count' => true,
  1293. 'orderby' => false,
  1294. 'post_id' => $post->ID,
  1295. 'status' => 'approve',
  1296. );
  1297. if ( $comment_args['hierarchical'] ) {
  1298. $top_level_args['parent'] = 0;
  1299. }
  1300. if ( isset( $comment_args['include_unapproved'] ) ) {
  1301. $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
  1302. }
  1303. $top_level_count = $top_level_query->query( $top_level_args );
  1304. $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
  1305. }
  1306. }
  1307. /**
  1308. * Filters the arguments used to query comments in comments_template().
  1309. *
  1310. * @since 4.5.0
  1311. *
  1312. * @see WP_Comment_Query::__construct()
  1313. *
  1314. * @param array $comment_args {
  1315. * Array of WP_Comment_Query arguments.
  1316. *
  1317. * @type string|array $orderby Field(s) to order by.
  1318. * @type string $order Order of results. Accepts 'ASC' or 'DESC'.
  1319. * @type string $status Comment status.
  1320. * @type array $include_unapproved Array of IDs or email addresses whose unapproved comments
  1321. * will be included in results.
  1322. * @type int $post_id ID of the post.
  1323. * @type bool $no_found_