/wp-includes/comment-template.php

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