/wp-includes/comment-template.php

https://gitlab.com/webkod3r/tripolis · PHP · 1517 lines · 524 code · 144 blank · 849 comment · 121 complexity · 34d0faa5a33a5b148079071992ed0f4e MD5 · raw file

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