PageRenderTime 35ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/comment-template.php

https://bitbucket.org/broderboy/shannonbroder-wordpress
PHP | 2234 lines | 849 code | 193 blank | 1192 comment | 194 complexity | 37e15c37627c4183c4f4fcf736ef9b5f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0
  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_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. * @since 0.71
  112. *
  113. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  114. * Default empty.
  115. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  116. * @param string $after Optional. 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.0
  134. *
  135. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  136. * Default empty.
  137. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  138. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  139. */
  140. function get_comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
  141. global $comment;
  142. /**
  143. * Filter the comment author's email for display.
  144. *
  145. * Care should be taken to protect the email address and assure that email
  146. * harvesters do not capture your commenters' email address.
  147. *
  148. * @since 1.2.0
  149. *
  150. * @param string $comment_author_email The comment author's email address.
  151. */
  152. $email = apply_filters( 'comment_email', $comment->comment_author_email );
  153. if ((!empty($email)) && ($email != '@')) {
  154. $display = ($linktext != '') ? $linktext : $email;
  155. $return = $before;
  156. $return .= "<a href='mailto:$email'>$display</a>";
  157. $return .= $after;
  158. return $return;
  159. } else {
  160. return '';
  161. }
  162. }
  163. /**
  164. * Retrieve the HTML link to the URL of the author of the current comment.
  165. *
  166. * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
  167. * which falls back to the global comment variable if the $comment_ID argument is empty.
  168. *
  169. * @since 1.5.0
  170. *
  171. * @param int $comment_ID ID of the comment for which to get the author's link.
  172. * Default current comment.
  173. * @return string The comment author name or HTML link for author's URL.
  174. */
  175. function get_comment_author_link( $comment_ID = 0 ) {
  176. $url = get_comment_author_url( $comment_ID );
  177. $author = get_comment_author( $comment_ID );
  178. if ( empty( $url ) || 'http://' == $url )
  179. $return = $author;
  180. else
  181. $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
  182. /**
  183. * Filter the comment author's link for display.
  184. *
  185. * @since 1.5.0
  186. *
  187. * @param string $return The HTML-formatted comment author link.
  188. * Empty for an invalid URL.
  189. */
  190. return apply_filters( 'get_comment_author_link', $return );
  191. }
  192. /**
  193. * Display the html link to the url of the author of the current comment.
  194. *
  195. * @since 0.71
  196. *
  197. * @see get_comment_author_link() Echoes result
  198. *
  199. * @param int $comment_ID ID of the comment for which to print the author's
  200. * link. Default current comment.
  201. */
  202. function comment_author_link( $comment_ID = 0 ) {
  203. echo get_comment_author_link( $comment_ID );
  204. }
  205. /**
  206. * Retrieve the IP address of the author of the current comment.
  207. *
  208. * @since 1.5.0
  209. *
  210. * @param int $comment_ID ID of the comment for which to get the author's IP
  211. * address. Default current comment.
  212. * @return string Comment author's IP address.
  213. */
  214. function get_comment_author_IP( $comment_ID = 0 ) {
  215. $comment = get_comment( $comment_ID );
  216. /**
  217. * Filter the comment author's returned IP address.
  218. *
  219. * @since 1.5.0
  220. *
  221. * @param string $comment_author_IP The comment author's IP address.
  222. */
  223. return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP );
  224. }
  225. /**
  226. * Display the IP address of the author of the current comment.
  227. *
  228. * @since 0.71
  229. *
  230. * @param int $comment_ID ID of the comment for which to print the author's IP
  231. * address. Default current comment.
  232. */
  233. function comment_author_IP( $comment_ID = 0 ) {
  234. echo get_comment_author_IP( $comment_ID );
  235. }
  236. /**
  237. * Retrieve the url of the author of the current comment.
  238. *
  239. * @since 1.5.0
  240. *
  241. * @param int $comment_ID ID of the comment for which to get the author's URL.
  242. * Default current comment.
  243. * @return string
  244. */
  245. function get_comment_author_url( $comment_ID = 0 ) {
  246. $comment = get_comment( $comment_ID );
  247. $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
  248. $url = esc_url( $url, array('http', 'https') );
  249. /**
  250. * Filter the comment author's URL.
  251. *
  252. * @since 1.5.0
  253. *
  254. * @param string $url The comment author's URL.
  255. */
  256. return apply_filters( 'get_comment_author_url', $url );
  257. }
  258. /**
  259. * Display the url of the author of the current comment.
  260. *
  261. * @since 0.71
  262. *
  263. * @param int $comment_ID ID of the comment for which to print the author's URL.
  264. * Default current comment.
  265. */
  266. function comment_author_url( $comment_ID = 0 ) {
  267. $author_url = get_comment_author_url( $comment_ID );
  268. /**
  269. * Filter the comment author's URL for display.
  270. *
  271. * @since 1.2.0
  272. *
  273. * @param string $author_url The comment author's URL.
  274. */
  275. echo apply_filters( 'comment_url', $author_url );
  276. }
  277. /**
  278. * Retrieves the HTML link of the url of the author of the current comment.
  279. *
  280. * $linktext parameter is only used if the URL does not exist for the comment
  281. * author. If the URL does exist then the URL will be used and the $linktext
  282. * will be ignored.
  283. *
  284. * Encapsulate the HTML link between the $before and $after. So it will appear
  285. * in the order of $before, link, and finally $after.
  286. *
  287. * @since 1.5.0
  288. *
  289. * @param string $linktext Optional. The text to display instead of the comment
  290. * author's email address. Default empty.
  291. * @param string $before Optional. The text or HTML to display before the email link.
  292. * Default empty.
  293. * @param string $after Optional. The text or HTML to display after the email link.
  294. * Default empty.
  295. * @return string The HTML link between the $before and $after parameters.
  296. */
  297. function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  298. $url = get_comment_author_url();
  299. $display = ($linktext != '') ? $linktext : $url;
  300. $display = str_replace( 'http://www.', '', $display );
  301. $display = str_replace( 'http://', '', $display );
  302. if ( '/' == substr($display, -1) )
  303. $display = substr($display, 0, -1);
  304. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  305. /**
  306. * Filter the comment author's returned URL link.
  307. *
  308. * @since 1.5.0
  309. *
  310. * @param string $return The HTML-formatted comment author URL link.
  311. */
  312. return apply_filters( 'get_comment_author_url_link', $return );
  313. }
  314. /**
  315. * Displays the HTML link of the url of the author of the current comment.
  316. *
  317. * @since 0.71
  318. *
  319. * @param string $linktext Optional. Text to display instead of the comment author's
  320. * email address. Default empty.
  321. * @param string $before Optional. Text or HTML to display before the email link.
  322. * Default empty.
  323. * @param string $after Optional. Text or HTML to display after the email link.
  324. * Default empty.
  325. */
  326. function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  327. echo get_comment_author_url_link( $linktext, $before, $after );
  328. }
  329. /**
  330. * Generates semantic classes for each comment element.
  331. *
  332. * @since 2.7.0
  333. *
  334. * @param string|array $class Optional. One or more classes to add to the class list.
  335. * Default empty.
  336. * @param int $comment_id Comment ID. Default current comment.
  337. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  338. * @param bool $echo Optional. Whether to cho or return the output.
  339. * Default true.
  340. */
  341. function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
  342. // Separates classes with a single space, collates classes for comment DIV
  343. $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
  344. if ( $echo)
  345. echo $class;
  346. else
  347. return $class;
  348. }
  349. /**
  350. * Returns the classes for the comment div as an array.
  351. *
  352. * @since 2.7.0
  353. *
  354. * @param string|array $class Optional. One or more classes to add to the class list. Default empty.
  355. * @param int $comment_id Comment ID. Default current comment.
  356. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  357. * @return array An array of classes.
  358. */
  359. function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
  360. global $comment_alt, $comment_depth, $comment_thread_alt;
  361. $comment = get_comment($comment_id);
  362. $classes = array();
  363. // Get the comment type (comment, trackback),
  364. $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
  365. // If the comment author has an id (registered), then print the log in name
  366. if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
  367. // For all registered users, 'byuser'
  368. $classes[] = 'byuser';
  369. $classes[] = 'comment-author-' . sanitize_html_class($user->user_nicename, $comment->user_id);
  370. // For comment authors who are the author of the post
  371. if ( $post = get_post($post_id) ) {
  372. if ( $comment->user_id === $post->post_author )
  373. $classes[] = 'bypostauthor';
  374. }
  375. }
  376. if ( empty($comment_alt) )
  377. $comment_alt = 0;
  378. if ( empty($comment_depth) )
  379. $comment_depth = 1;
  380. if ( empty($comment_thread_alt) )
  381. $comment_thread_alt = 0;
  382. if ( $comment_alt % 2 ) {
  383. $classes[] = 'odd';
  384. $classes[] = 'alt';
  385. } else {
  386. $classes[] = 'even';
  387. }
  388. $comment_alt++;
  389. // Alt for top-level comments
  390. if ( 1 == $comment_depth ) {
  391. if ( $comment_thread_alt % 2 ) {
  392. $classes[] = 'thread-odd';
  393. $classes[] = 'thread-alt';
  394. } else {
  395. $classes[] = 'thread-even';
  396. }
  397. $comment_thread_alt++;
  398. }
  399. $classes[] = "depth-$comment_depth";
  400. if ( !empty($class) ) {
  401. if ( !is_array( $class ) )
  402. $class = preg_split('#\s+#', $class);
  403. $classes = array_merge($classes, $class);
  404. }
  405. $classes = array_map('esc_attr', $classes);
  406. /**
  407. * Filter the returned CSS classes for the current comment.
  408. *
  409. * @since 2.7.0
  410. *
  411. * @param array $classes An array of comment classes.
  412. * @param string $class A comma-separated list of additional classes added to the list.
  413. * @param int $comment_id The comment id.
  414. * @param int|WP_Post $post_id The post ID or WP_Post object.
  415. */
  416. return apply_filters( 'comment_class', $classes, $class, $comment_id, $post_id );
  417. }
  418. /**
  419. * Retrieve the comment date of the current comment.
  420. *
  421. * @since 1.5.0
  422. *
  423. * @param string $d Optional. The format of the date. Default user's setting.
  424. * @param int $comment_ID ID of the comment for which to get the date. Default current comment.
  425. * @return string The comment's date.
  426. */
  427. function get_comment_date( $d = '', $comment_ID = 0 ) {
  428. $comment = get_comment( $comment_ID );
  429. if ( '' == $d )
  430. $date = mysql2date(get_option('date_format'), $comment->comment_date);
  431. else
  432. $date = mysql2date($d, $comment->comment_date);
  433. /**
  434. * Filter the returned comment date.
  435. *
  436. * @since 1.5.0
  437. *
  438. * @param string|int $date Formatted date string or Unix timestamp.
  439. * @param string $d The format of the date.
  440. * @param object $comment The comment object.
  441. */
  442. return apply_filters( 'get_comment_date', $date, $d, $comment );
  443. }
  444. /**
  445. * Display the comment date of the current comment.
  446. *
  447. * @since 0.71
  448. *
  449. * @param string $d Optional. The format of the date. Default user's settings.
  450. * @param int $comment_ID ID of the comment for which to print the date. Default current comment.
  451. */
  452. function comment_date( $d = '', $comment_ID = 0 ) {
  453. echo get_comment_date( $d, $comment_ID );
  454. }
  455. /**
  456. * Retrieve the excerpt of the current comment.
  457. *
  458. * Will cut each word and only output the first 20 words with '&hellip;' at the end.
  459. * If the word count is less than 20, then no truncating is done and no '&hellip;'
  460. * will appear.
  461. *
  462. * @since 1.5.0
  463. *
  464. * @param int $comment_ID ID of the comment for which to get the excerpt.
  465. * Default current comment.
  466. * @return string The maybe truncated comment with 20 words or less.
  467. */
  468. function get_comment_excerpt( $comment_ID = 0 ) {
  469. $comment = get_comment( $comment_ID );
  470. $comment_text = strip_tags($comment->comment_content);
  471. $blah = explode(' ', $comment_text);
  472. if (count($blah) > 20) {
  473. $k = 20;
  474. $use_dotdotdot = 1;
  475. } else {
  476. $k = count($blah);
  477. $use_dotdotdot = 0;
  478. }
  479. $excerpt = '';
  480. for ($i=0; $i<$k; $i++) {
  481. $excerpt .= $blah[$i] . ' ';
  482. }
  483. $excerpt .= ($use_dotdotdot) ? '&hellip;' : '';
  484. /**
  485. * Filter the retrieved comment excerpt.
  486. *
  487. * @since 1.5.0
  488. *
  489. * @param string $excerpt The comment excerpt text.
  490. */
  491. return apply_filters( 'get_comment_excerpt', $excerpt );
  492. }
  493. /**
  494. * Display the excerpt of the current comment.
  495. *
  496. * @since 1.2.0
  497. *
  498. * @param int $comment_ID ID of the comment for which to print the excerpt.
  499. * Default current comment.
  500. */
  501. function comment_excerpt( $comment_ID = 0 ) {
  502. $comment_excerpt = get_comment_excerpt($comment_ID);
  503. /**
  504. * Filter the comment excerpt for display.
  505. *
  506. * @since 1.2.0
  507. *
  508. * @param string $comment_excerpt The comment excerpt text.
  509. */
  510. echo apply_filters( 'comment_excerpt', $comment_excerpt );
  511. }
  512. /**
  513. * Retrieve the comment id of the current comment.
  514. *
  515. * @since 1.5.0
  516. *
  517. * @return int The comment ID.
  518. */
  519. function get_comment_ID() {
  520. global $comment;
  521. /**
  522. * Filter the returned comment ID.
  523. *
  524. * @since 1.5.0
  525. *
  526. * @param int $comment_ID The current comment ID.
  527. */
  528. return apply_filters( 'get_comment_ID', $comment->comment_ID );
  529. }
  530. /**
  531. * Display the comment id of the current comment.
  532. *
  533. * @since 0.71
  534. */
  535. function comment_ID() {
  536. echo get_comment_ID();
  537. }
  538. /**
  539. * Retrieve the link to a given comment.
  540. *
  541. * @since 1.5.0
  542. *
  543. * @see get_page_of_comment()
  544. *
  545. * @param mixed $comment Comment to retrieve. Default current comment.
  546. * @param array $args Optional. An array of arguments to override the defaults.
  547. * @return string The permalink to the given comment.
  548. */
  549. function get_comment_link( $comment = null, $args = array() ) {
  550. global $wp_rewrite, $in_comment_loop;
  551. $comment = get_comment($comment);
  552. // Backwards compat
  553. if ( ! is_array( $args ) ) {
  554. $args = array( 'page' => $args );
  555. }
  556. $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
  557. $args = wp_parse_args( $args, $defaults );
  558. if ( '' === $args['per_page'] && get_option('page_comments') )
  559. $args['per_page'] = get_option('comments_per_page');
  560. if ( empty($args['per_page']) ) {
  561. $args['per_page'] = 0;
  562. $args['page'] = 0;
  563. }
  564. if ( $args['per_page'] ) {
  565. if ( '' == $args['page'] )
  566. $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
  567. if ( $wp_rewrite->using_permalinks() )
  568. $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
  569. else
  570. $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
  571. } else {
  572. $link = get_permalink( $comment->comment_post_ID );
  573. }
  574. $link = $link . '#comment-' . $comment->comment_ID;
  575. /**
  576. * Filter the returned single comment permalink.
  577. *
  578. * @since 2.8.0
  579. *
  580. * @see get_page_of_comment()
  581. *
  582. * @param string $link The comment permalink with '#comment-$id' appended.
  583. * @param object $comment The current comment object.
  584. * @param array $args An array of arguments to override the defaults.
  585. */
  586. return apply_filters( 'get_comment_link', $link, $comment, $args );
  587. }
  588. /**
  589. * Retrieve the link to the current post comments.
  590. *
  591. * @since 1.5.0
  592. *
  593. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  594. * @return string The link to the comments.
  595. */
  596. function get_comments_link( $post_id = 0 ) {
  597. $comments_link = get_permalink( $post_id ) . '#comments';
  598. /**
  599. * Filter the returned post comments permalink.
  600. *
  601. * @since 3.6.0
  602. *
  603. * @param string $comments_link Post comments permalink with '#comments' appended.
  604. * @param int|WP_Post $post_id Post ID or WP_Post object.
  605. */
  606. return apply_filters( 'get_comments_link', $comments_link, $post_id );
  607. }
  608. /**
  609. * Display the link to the current post comments.
  610. *
  611. * @since 0.71
  612. *
  613. * @param string $deprecated Not Used.
  614. * @param bool $deprecated_2 Not Used.
  615. */
  616. function comments_link( $deprecated = '', $deprecated_2 = '' ) {
  617. if ( !empty( $deprecated ) )
  618. _deprecated_argument( __FUNCTION__, '0.72' );
  619. if ( !empty( $deprecated_2 ) )
  620. _deprecated_argument( __FUNCTION__, '1.3' );
  621. echo esc_url( get_comments_link() );
  622. }
  623. /**
  624. * Retrieve the amount of comments a post has.
  625. *
  626. * @since 1.5.0
  627. *
  628. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  629. * @return int The number of comments a post has.
  630. */
  631. function get_comments_number( $post_id = 0 ) {
  632. $post_id = absint( $post_id );
  633. if ( !$post_id )
  634. $post_id = get_the_ID();
  635. $post = get_post($post_id);
  636. if ( ! isset($post->comment_count) )
  637. $count = 0;
  638. else
  639. $count = $post->comment_count;
  640. /**
  641. * Filter the returned comment count for a post.
  642. *
  643. * @since 1.5.0
  644. *
  645. * @param int $count Nnumber of comments a post has.
  646. * @param int|WP_Post $post_id Post ID or WP_Post object.
  647. */
  648. return apply_filters( 'get_comments_number', $count, $post_id );
  649. }
  650. /**
  651. * Display the language string for the number of comments the current post has.
  652. *
  653. * @since 0.71
  654. *
  655. * @param string $zero Optional. Text for no comments. Default false.
  656. * @param string $one Optional. Text for one comment. Default false.
  657. * @param string $more Optional. Text for more than one comment. Default false.
  658. * @param string $deprecated Not used.
  659. */
  660. function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
  661. if ( !empty( $deprecated ) )
  662. _deprecated_argument( __FUNCTION__, '1.3' );
  663. $number = get_comments_number();
  664. if ( $number > 1 )
  665. $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
  666. elseif ( $number == 0 )
  667. $output = ( false === $zero ) ? __('No Comments') : $zero;
  668. else // must be one
  669. $output = ( false === $one ) ? __('1 Comment') : $one;
  670. /**
  671. * Filter the comments count for display.
  672. *
  673. * @since 1.5.0
  674. *
  675. * @see _n()
  676. *
  677. * @param string $output A translatable string formatted based on whether the count
  678. * is equal to 0, 1, or 1+.
  679. * @param int $number The number of post comments.
  680. */
  681. echo apply_filters( 'comments_number', $output, $number );
  682. }
  683. /**
  684. * Retrieve the text of the current comment.
  685. *
  686. * @since 1.5.0
  687. *
  688. * @see Walker_Comment::comment()
  689. *
  690. * @param int $comment_ID ID of the comment for which to get the text. Default current comment.
  691. * @param array $args Optional. An array of arguments. Default empty.
  692. * @return string The comment content.
  693. */
  694. function get_comment_text( $comment_ID = 0, $args = array() ) {
  695. $comment = get_comment( $comment_ID );
  696. /**
  697. * Filter the text of a comment.
  698. *
  699. * @since 1.5.0
  700. *
  701. * @see Walker_Comment::comment()
  702. *
  703. * @param string $comment_content Text of the comment.
  704. * @param object $comment The comment object.
  705. * @param array $args An array of arguments.
  706. */
  707. return apply_filters( 'get_comment_text', $comment->comment_content, $comment, $args );
  708. }
  709. /**
  710. * Display the text of the current comment.
  711. *
  712. * @since 0.71
  713. *
  714. * @see Walker_Comment::comment()
  715. *
  716. * @param int $comment_ID ID of the comment for which to print the text. Default 0.
  717. * @param array $args Optional. An array of arguments. Default empty array. Default empty.
  718. */
  719. function comment_text( $comment_ID = 0, $args = array() ) {
  720. $comment = get_comment( $comment_ID );
  721. $comment_text = get_comment_text( $comment_ID , $args );
  722. /**
  723. * Filter the text of a comment to be displayed.
  724. *
  725. * @since 1.2.0
  726. *
  727. * @see Walker_Comment::comment()
  728. *
  729. * @param string $comment_text Text of the current comment.
  730. * @param object $comment The comment object.
  731. * @param array $args An array of arguments.
  732. */
  733. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  734. }
  735. /**
  736. * Retrieve the comment time of the current comment.
  737. *
  738. * @since 1.5.0
  739. *
  740. * @param string $d Optional. The format of the time. Default user's settings.
  741. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  742. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  743. * Default true.
  744. * @return string The formatted time.
  745. */
  746. function get_comment_time( $d = '', $gmt = false, $translate = true ) {
  747. global $comment;
  748. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  749. if ( '' == $d )
  750. $date = mysql2date(get_option('time_format'), $comment_date, $translate);
  751. else
  752. $date = mysql2date($d, $comment_date, $translate);
  753. /**
  754. * Filter the returned comment time.
  755. *
  756. * @since 1.5.0
  757. *
  758. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  759. * @param string $d Date format.
  760. * @param bool $gmt Whether the GMT date is in use.
  761. * @param bool $translate Whether the time is translated.
  762. * @param object $comment The comment object.
  763. */
  764. return apply_filters( 'get_comment_time', $date, $d, $gmt, $translate, $comment );
  765. }
  766. /**
  767. * Display the comment time of the current comment.
  768. *
  769. * @since 0.71
  770. *
  771. * @param string $d Optional. The format of the time. Default user's settings.
  772. */
  773. function comment_time( $d = '' ) {
  774. echo get_comment_time($d);
  775. }
  776. /**
  777. * Retrieve the comment type of the current comment.
  778. *
  779. * @since 1.5.0
  780. *
  781. * @param int $comment_ID ID of the comment for which to get the type. Default current comment.
  782. * @return string The comment type.
  783. */
  784. function get_comment_type( $comment_ID = 0 ) {
  785. $comment = get_comment( $comment_ID );
  786. if ( '' == $comment->comment_type )
  787. $comment->comment_type = 'comment';
  788. /**
  789. * Filter the returned comment type.
  790. *
  791. * @since 1.5.0
  792. *
  793. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  794. */
  795. return apply_filters( 'get_comment_type', $comment->comment_type );
  796. }
  797. /**
  798. * Display the comment type of the current comment.
  799. *
  800. * @since 0.71
  801. *
  802. * @param string $commenttxt Optional. String to display for comment type. Default false.
  803. * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
  804. * @param string $pingbacktxt Optional. String to display for pingback type. Default false.
  805. */
  806. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  807. if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
  808. if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
  809. if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
  810. $type = get_comment_type();
  811. switch( $type ) {
  812. case 'trackback' :
  813. echo $trackbacktxt;
  814. break;
  815. case 'pingback' :
  816. echo $pingbacktxt;
  817. break;
  818. default :
  819. echo $commenttxt;
  820. }
  821. }
  822. /**
  823. * Retrieve The current post's trackback URL.
  824. *
  825. * There is a check to see if permalink's have been enabled and if so, will
  826. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  827. * current post is used and appended to the correct page to go to.
  828. *
  829. * @since 1.5.0
  830. *
  831. * @return string The trackback URL after being filtered.
  832. */
  833. function get_trackback_url() {
  834. if ( '' != get_option('permalink_structure') )
  835. $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
  836. else
  837. $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . get_the_ID();
  838. /**
  839. * Filter the returned trackback URL.
  840. *
  841. * @since 2.2.0
  842. *
  843. * @param string $tb_url The trackback URL.
  844. */
  845. return apply_filters( 'trackback_url', $tb_url );
  846. }
  847. /**
  848. * Display the current post's trackback URL.
  849. *
  850. * @since 0.71
  851. *
  852. * @param bool $deprecated_echo Not used.
  853. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  854. * for the result instead.
  855. */
  856. function trackback_url( $deprecated_echo = true ) {
  857. if ( $deprecated_echo !== true )
  858. _deprecated_argument( __FUNCTION__, '2.5', __('Use <code>get_trackback_url()</code> instead if you do not want the value echoed.') );
  859. if ( $deprecated_echo )
  860. echo get_trackback_url();
  861. else
  862. return get_trackback_url();
  863. }
  864. /**
  865. * Generate and display the RDF for the trackback information of current post.
  866. *
  867. * Deprecated in 3.0.0, and restored in 3.0.1.
  868. *
  869. * @since 0.71
  870. *
  871. * @param int $deprecated Not used (Was $timezone = 0).
  872. */
  873. function trackback_rdf( $deprecated = '' ) {
  874. if ( ! empty( $deprecated ) ) {
  875. _deprecated_argument( __FUNCTION__, '2.5' );
  876. }
  877. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  878. return;
  879. }
  880. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  881. xmlns:dc="http://purl.org/dc/elements/1.1/"
  882. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  883. <rdf:Description rdf:about="';
  884. the_permalink();
  885. echo '"'."\n";
  886. echo ' dc:identifier="';
  887. the_permalink();
  888. echo '"'."\n";
  889. echo ' dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  890. echo ' trackback:ping="'.get_trackback_url().'"'." />\n";
  891. echo '</rdf:RDF>';
  892. }
  893. /**
  894. * Whether the current post is open for comments.
  895. *
  896. * @since 1.5.0
  897. *
  898. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  899. * @return bool True if the comments are open.
  900. */
  901. function comments_open( $post_id = null ) {
  902. $_post = get_post($post_id);
  903. $open = ( 'open' == $_post->comment_status );
  904. /**
  905. * Filter whether the current post is open for comments.
  906. *
  907. * @since 2.5.0
  908. *
  909. * @param bool $open Whether the current post is open for comments.
  910. * @param int|WP_Post $post_id The post ID or WP_Post object.
  911. */
  912. return apply_filters( 'comments_open', $open, $post_id );
  913. }
  914. /**
  915. * Whether the current post is open for pings.
  916. *
  917. * @since 1.5.0
  918. *
  919. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  920. * @return bool True if pings are accepted
  921. */
  922. function pings_open( $post_id = null ) {
  923. $_post = get_post($post_id);
  924. $open = ( 'open' == $_post->ping_status );
  925. /**
  926. * Filter whether the current post is open for pings.
  927. *
  928. * @since 2.5.0
  929. *
  930. * @param bool $open Whether the current post is open for pings.
  931. * @param int|WP_Post $post_id The post ID or WP_Post object.
  932. */
  933. return apply_filters( 'pings_open', $open, $post_id );
  934. }
  935. /**
  936. * Display form token for unfiltered comments.
  937. *
  938. * Will only display nonce token if the current user has permissions for
  939. * unfiltered html. Won't display the token for other users.
  940. *
  941. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  942. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  943. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  944. *
  945. * Backported to 2.0.10.
  946. *
  947. * @since 2.1.3
  948. */
  949. function wp_comment_form_unfiltered_html_nonce() {
  950. $post = get_post();
  951. $post_id = $post ? $post->ID : 0;
  952. if ( current_user_can( 'unfiltered_html' ) ) {
  953. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  954. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  955. }
  956. }
  957. /**
  958. * Load the comment template specified in $file.
  959. *
  960. * Will not display the comments template if not on single post or page, or if
  961. * the post does not have comments.
  962. *
  963. * Uses the WordPress database object to query for the comments. The comments
  964. * are passed through the 'comments_array' filter hook with the list of comments
  965. * and the post ID respectively.
  966. *
  967. * The $file path is passed through a filter hook called, 'comments_template'
  968. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  969. * first and if it fails it will require the default comment template from the
  970. * default theme. If either does not exist, then the WordPress process will be
  971. * halted. It is advised for that reason, that the default theme is not deleted.
  972. *
  973. * @todo Document globals
  974. * @uses $withcomments Will not try to get the comments if the post has none.
  975. *
  976. * @since 1.5.0
  977. *
  978. * @param string $file Optional. The file to load. Default '/comments.php'.
  979. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  980. * Default false.
  981. * @return null Returns null if no comments appear.
  982. */
  983. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  984. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
  985. if ( !(is_single() || is_page() || $withcomments) || empty($post) )
  986. return;
  987. if ( empty($file) )
  988. $file = '/comments.php';
  989. $req = get_option('require_name_email');
  990. /*
  991. * Comment author information fetched from the comment cookies.
  992. * Uuses wp_get_current_commenter().
  993. */
  994. $commenter = wp_get_current_commenter();
  995. /*
  996. * The name of the current comment author escaped for use in attributes.
  997. * Escaped by sanitize_comment_cookies().
  998. */
  999. $comment_author = $commenter['comment_author'];
  1000. /*
  1001. * The email address of the current comment author escaped for use in attributes.
  1002. * Escaped by sanitize_comment_cookies().
  1003. */
  1004. $comment_author_email = $commenter['comment_author_email'];
  1005. /*
  1006. * The url of the current comment author escaped for use in attributes.
  1007. */
  1008. $comment_author_url = esc_url($commenter['comment_author_url']);
  1009. /** @todo Use API instead of SELECTs. */
  1010. if ( $user_ID) {
  1011. $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));
  1012. } else if ( empty($comment_author) ) {
  1013. $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
  1014. } else {
  1015. $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));
  1016. }
  1017. /**
  1018. * Filter the comments array.
  1019. *
  1020. * @since 2.1.0
  1021. *
  1022. * @param array $comments Array of comments supplied to the comments template.
  1023. * @param int $post_ID Post ID.
  1024. */
  1025. $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
  1026. $comments = &$wp_query->comments;
  1027. $wp_query->comment_count = count($wp_query->comments);
  1028. update_comment_cache($wp_query->comments);
  1029. if ( $separate_comments ) {
  1030. $wp_query->comments_by_type = separate_comments($comments);
  1031. $comments_by_type = &$wp_query->comments_by_type;
  1032. }
  1033. $overridden_cpage = false;
  1034. if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
  1035. set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
  1036. $overridden_cpage = true;
  1037. }
  1038. if ( !defined('COMMENTS_TEMPLATE') )
  1039. define('COMMENTS_TEMPLATE', true);
  1040. $theme_template = STYLESHEETPATH . $file;
  1041. /**
  1042. * Filter the path to the theme template file used for the comments template.
  1043. *
  1044. * @since 1.5.1
  1045. *
  1046. * @param string $theme_template The path to the theme template file.
  1047. */
  1048. $include = apply_filters( 'comments_template', $theme_template );
  1049. if ( file_exists( $include ) )
  1050. require( $include );
  1051. elseif ( file_exists( TEMPLATEPATH . $file ) )
  1052. require( TEMPLATEPATH . $file );
  1053. else // Backward compat code will be removed in a future release
  1054. require( ABSPATH . WPINC . '/theme-compat/comments.php');
  1055. }
  1056. /**
  1057. * Display the JS popup script to show a comment.
  1058. *
  1059. * If the $file parameter is empty, then the home page is assumed. The defaults
  1060. * for the window are 400px by 400px.
  1061. *
  1062. * For the comment link popup to work, this function has to be called or the
  1063. * normal comment link will be assumed.
  1064. *
  1065. * @global string $wpcommentspopupfile The URL to use for the popup window.
  1066. * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
  1067. *
  1068. * @since 0.71
  1069. *
  1070. * @param int $width Optional. The width of the popup window. Default 400.
  1071. * @param int $height Optional. The height of the popup window. Default 400.
  1072. * @param string $file Optional. Sets the location of the popup window.
  1073. */
  1074. function comments_popup_script( $width = 400, $height = 400, $file = '' ) {
  1075. global $wpcommentspopupfile, $wpcommentsjavascript;
  1076. if (empty ($file)) {
  1077. $wpcommentspopupfile = ''; // Use the index.
  1078. } else {
  1079. $wpcommentspopupfile = $file;
  1080. }
  1081. $wpcommentsjavascript = 1;
  1082. $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";
  1083. echo $javascript;
  1084. }
  1085. /**
  1086. * Displays the link to the comments popup window for the current post ID.
  1087. *
  1088. * Is not meant to be displayed on single posts and pages. Should be used
  1089. * on the lists of posts
  1090. *
  1091. * @global string $wpcommentspopupfile The URL to use for the popup window.
  1092. * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
  1093. *
  1094. * @since 0.71
  1095. *
  1096. * @param string $zero Optional. String to display when no comments. Default false.
  1097. * @param string $one Optional. String to display when only one comment is available.
  1098. * Default false.
  1099. * @param string $more Optional. String to display when there are more than one comment.
  1100. * Default false.
  1101. * @param string $css_class Optional. CSS class to use for comments. Default empty.
  1102. * @param string $none Optional. String to display when comments have been turned off.
  1103. * Default false.
  1104. * @return null Returns null on single posts and pages.
  1105. */
  1106. function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
  1107. global $wpcommentspopupfile, $wpcommentsjavascript;
  1108. $id = get_the_ID();
  1109. if ( false === $zero ) $zero = __( 'No Comments' );
  1110. if ( false === $one ) $one = __( '1 Comment' );
  1111. if ( false === $more ) $more = __( '% Comments' );
  1112. if ( false === $none ) $none = __( 'Comments Off' );
  1113. $number = get_comments_number( $id );
  1114. if ( 0 == $number && !comments_open() && !pings_open() ) {
  1115. echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
  1116. return;
  1117. }
  1118. if ( post_password_required() ) {
  1119. echo __('Enter your password to view comments.');
  1120. return;
  1121. }
  1122. echo '<a href="';
  1123. if ( $wpcommentsjavascript ) {
  1124. if ( empty( $wpcommentspopupfile ) )
  1125. $home = home_url();
  1126. else
  1127. $home = get_option('siteurl');
  1128. echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
  1129. echo '" onclick="wpopen(this.href); return false"';
  1130. } else { // if comments_popup_script() is not in the template, display simple comment link
  1131. if ( 0 == $number )
  1132. echo get_permalink() . '#respond';
  1133. else
  1134. comments_link();
  1135. echo '"';
  1136. }
  1137. if ( !empty( $css_class ) ) {
  1138. echo ' class="'.$css_class.'" ';
  1139. }
  1140. $title = the_title_attribute( array('echo' => 0 ) );
  1141. $attributes = '';
  1142. /**
  1143. * Filter the comments popup link attributes for display.
  1144. *
  1145. * @since 2.5.0
  1146. *
  1147. * @param string $attributes The comments popup link attributes. Default empty.
  1148. */
  1149. echo apply_filters( 'comments_popup_link_attributes', $attributes );
  1150. echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
  1151. comments_number( $zero, $one, $more );
  1152. echo '</a>';
  1153. }
  1154. /**
  1155. * Retrieve HTML content for reply to comment link.
  1156. *
  1157. * @since 2.7.0
  1158. *
  1159. * @param array $args {
  1160. * Optional. Override default arguments.
  1161. *
  1162. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1163. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1164. * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
  1165. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1166. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1167. * Default 'respond'.
  1168. * @type string $reply_text The text of the Reply link. Default 'Reply'.
  1169. * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
  1170. * @type int $depth' The depth of the new comment. Must be greater than 0 and less than the value
  1171. * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
  1172. * @type string $before The text or HTML to add before the reply link. Default empty.
  1173. * @type string $after The text or HTML to add after the reply link. Default empty.
  1174. * }
  1175. * @param int $comment Comment being replied to. Default current comment.
  1176. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1177. * Default current post.
  1178. * @return mixed Link to show comment form, if successful. False, if comments are closed.
  1179. */
  1180. function get_comment_reply_link($args = array(), $comment = null, $post = null) {
  1181. $defaults = array(
  1182. 'add_below' => 'comment',
  1183. 'respond_id' => 'respond',
  1184. 'reply_text' => __('Reply'),
  1185. 'login_text' => __('Log in to Reply'),
  1186. 'depth' => 0,
  1187. 'before' => '',
  1188. 'after' => ''
  1189. );
  1190. $args = wp_parse_args($args, $defaults);
  1191. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
  1192. return;
  1193. extract($args, EXTR_SKIP);
  1194. $comment = get_comment($comment);
  1195. if ( empty($post) )
  1196. $post = $comment->comment_post_ID;
  1197. $post = get_post($post);
  1198. if ( !comments_open($post->ID) )
  1199. return false;
  1200. $link = '';
  1201. if ( get_option('comment_registration') && ! is_user_logged_in() )
  1202. $link = '<a rel="nofollow" class="comment-reply-login" href="' . esc_url( wp_login_url( get_permalink() ) ) . '">' . $login_text . '</a>';
  1203. else
  1204. $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>";
  1205. /**
  1206. * Filter the comment reply link.
  1207. *
  1208. * @since 2.7.0
  1209. *
  1210. * @param string $link The HTML markup for the comment reply link.
  1211. * @param array $args An array of arguments overriding the defaults.
  1212. * @param object $comment The object of the comment being replied.
  1213. * @param WP_Post $post The WP_Post object.
  1214. */
  1215. return apply_filters( 'comment_reply_link', $before . $link . $after, $args, $comment, $post );
  1216. }
  1217. /**
  1218. * Displays the HTML content for reply to comment link.
  1219. *
  1220. * @since 2.7.0
  1221. *
  1222. * @see get_comment_reply_link()
  1223. *
  1224. * @param array $args Optional. Override default options.
  1225. * @param int $comment Comment being replied to. Default current comment.
  1226. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1227. * Default current post.
  1228. * @return mixed Link to show comment form, if successful. False, if comments are closed.
  1229. */
  1230. function comment_reply_link($args = array(), $comment = null, $post = null) {
  1231. echo get_comment_reply_link($args, $comment, $post);
  1232. }
  1233. /**
  1234. * Retrieve HTML content for reply to post link.
  1235. *
  1236. * @since 2.7.0
  1237. *
  1238. * @param array $args {
  1239. * Optional. Override default arguments.
  1240. *
  1241. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1242. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1243. * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
  1244. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1245. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1246. * Default 'respond'.
  1247. * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
  1248. * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
  1249. * @type string $before Text or HTML to add before the reply link. Default empty.
  1250. * @type string $after Text or HTML to add after the reply link. Default empty.
  1251. * }
  1252. * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
  1253. * Default current post.
  1254. * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
  1255. */
  1256. function get_post_reply_link($args = array(), $post = null) {
  1257. $defaults = array(
  1258. 'add_below' => 'post',
  1259. 'respond_id' => 'respond',
  1260. 'reply_text' => __('Leave a Comment'),
  1261. 'login_text' => __('Log in to leave a Comment'),
  1262. 'before' => '',
  1263. 'after' => '',
  1264. );
  1265. $args = wp_parse_args($args, $defaults);
  1266. extract($args, EXTR_SKIP);
  1267. $post = get_post($post);
  1268. if ( !comments_open($post->ID) )
  1269. return false;
  1270. if ( get_option('comment_registration') && ! is_user_logged_in() )
  1271. $link = '<a rel="nofollow" href="' . wp_login_url( get_permalink() ) . '">' . $login_text . '</a>';
  1272. else
  1273. $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>";
  1274. $formatted_link = $before . $link . $after;
  1275. /**
  1276. * Filter the formatted post comments link HTML.
  1277. *
  1278. * @since 2.7.0
  1279. *
  1280. * @param string $formatted The HTML-formatted post comments link.
  1281. * @param int|WP_Post $post The post ID or WP_Post object.
  1282. */
  1283. return apply_filters( 'post_comments_link', $formatted_link, $post );
  1284. }
  1285. /**
  1286. * Displays the HTML content for reply to post link.
  1287. *
  1288. * @since 2.7.0
  1289. *
  1290. * @see get_post_reply_link()
  1291. *
  1292. * @param array $args Optional. Override default options,
  1293. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1294. * Default current post.
  1295. * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
  1296. */
  1297. function post_reply_link($args = array(), $post = null) {
  1298. echo get_post_reply_link($args, $post);
  1299. }
  1300. /**
  1301. * Retrieve HTML content for cancel comment reply link.
  1302. *
  1303. * @since 2.7.0
  1304. *
  1305. * @param string $text Optional. Text to display for cancel reply link. Default empty.
  1306. */
  1307. function get_cancel_comment_reply_link( $text = '' ) {
  1308. if ( empty($text) )
  1309. $text = __('Click here to cancel reply.');
  1310. $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
  1311. $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
  1312. $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
  1313. /**
  1314. * Filter the cancel comment reply link HTML.
  1315. *
  1316. * @since 2.7.0
  1317. *
  1318. * @param string $formatted_link The HTML-formatted cancel comment reply link.
  1319. * @param string $link Cancel comment reply link URL.
  1320. * @param string $text Cancel comment reply link text.
  1321. */
  1322. return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
  1323. }
  1324. /**
  1325. * Display HTML content for cancel comment reply link.
  1326. *
  1327. * @since 2.7.0
  1328. *
  1329. * @param string $text Optional. Text to display for cancel reply link. Default empty.
  1330. */
  1331. function cancel_comment_reply_link( $text = '' ) {
  1332. echo get_cancel_comment_reply_link($text);
  1333. }
  1334. /**
  1335. * Retrieve hidden input HTML for replying to comments.
  1336. *
  1337. * @since 3.0.0
  1338. *
  1339. * @param int $id Optional. Post ID. Default current post ID.
  1340. * @return string Hidden input HTML for replying to comments
  1341. */
  1342. function get_comment_id_fields( $id = 0 ) {
  1343. if ( empty( $id ) )
  1344. $id = get_the_ID();
  1345. $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
  1346. $result = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
  1347. $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
  1348. /**
  1349. * Filter the returned comment id fields.
  1350. *
  1351. * @since 3.0.0
  1352. *
  1353. * @param string $result The HTML-formatted hidden id field comment elements.
  1354. * @param int $id The post ID.
  1355. * @param int $replytoid The id of the comment being replied to.
  1356. */
  1357. return apply_filters( 'comment_id_fields', $result, $id, $replytoid );
  1358. }
  1359. /**
  1360. * Output hidden input HTML for replying to comments.
  1361. *
  1362. * @since 2.7.0
  1363. *
  1364. * @param int $id Optional. Post ID. Default current post ID.
  1365. */
  1366. function comment_id_fields( $id = 0 ) {
  1367. echo get_comment_id_fields( $id );
  1368. }
  1369. /**
  1370. * Display text based on comment reply status.
  1371. *
  1372. * Only affects users with Javascript disabled.
  1373. *
  1374. * @since 2.7.0
  1375. *
  1376. * @param string $noreplytext Optional. Text to display when not replying to a comment.
  1377. * Default false.
  1378. * @param string $replytext Optional. Text to display when replying to a comment.
  1379. * Default false. Accepts "%s" for the author of the comment
  1380. * being replied to.
  1381. * @param string $linktoparent Optional. Boolean to control making the author's name a link
  1382. * to their comment. Default true.
  1383. */
  1384. function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) {
  1385. global $comment;
  1386. if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
  1387. if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
  1388. $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
  1389. if ( 0 == $replytoid )
  1390. echo $noreplytext;
  1391. else {
  1392. $comment = get_comment($replytoid);
  1393. $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author() . '</a>' : get_comment_author();
  1394. printf( $replytext, $author );
  1395. }
  1396. }
  1397. /**
  1398. * HTML comment list class.
  1399. *
  1400. * @uses Walker
  1401. * @since 2.7.0
  1402. */
  1403. class Walker_Comment extends Walker {
  1404. /**
  1405. * What the class handles.
  1406. *
  1407. * @see Walker::$tree_type
  1408. *
  1409. * @since 2.7.0
  1410. * @var string
  1411. */
  1412. var $tree_type = 'comment';
  1413. /**
  1414. * DB fields to use.
  1415. *
  1416. * @see Walker::$db_fields
  1417. *
  1418. * @since 2.7.0
  1419. * @var array
  1420. */
  1421. var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
  1422. /**
  1423. * Start the list before the elements are added.
  1424. *
  1425. * @see Walker::start_lvl()
  1426. *
  1427. * @since 2.7.0
  1428. *
  1429. * @param string $output Passed by reference. Used to append additional content.
  1430. * @param int $depth Depth of comment.
  1431. * @param array $args Uses 'style' argument for type of HTML list.
  1432. */
  1433. function start_lvl( &$output, $depth = 0, $args = array() ) {
  1434. $GLOBALS['comment_depth'] = $depth + 1;
  1435. switch ( $args['style'] ) {
  1436. case 'div':
  1437. break;
  1438. case 'ol':
  1439. $output .= '<ol class="children">' . "\n";
  1440. break;
  1441. default:
  1442. case 'ul':
  1443. $output .= '<ul class="children">' . "\n";
  1444. break;
  1445. }
  1446. }
  1447. /**
  1448. * End the list of items after the elements are added.
  1449. *
  1450. * @see Walker::end_lvl()
  1451. *
  1452. * @since 2.7.0
  1453. *
  1454. * @param string $output Passed by reference. Used to append additional content.
  1455. * @param int $depth Depth of comment.
  1456. * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
  1457. */
  1458. function end_lvl( &$output, $depth = 0, $args = array() ) {
  1459. $GLOBALS['comment_depth'] = $depth + 1;
  1460. switch ( $args['style'] ) {
  1461. case 'div':
  1462. break;
  1463. case 'ol':
  1464. $output .= "</ol><!-- .children -->\n";
  1465. break;
  1466. default:
  1467. case 'ul':
  1468. $output .= "</ul><!-- .children -->\n";
  1469. break;
  1470. }
  1471. }
  1472. /**
  1473. * Traverse elements to create list from elements.
  1474. *
  1475. * This function is designed to enhance Walker::display_element() to
  1476. * display children of higher nesting levels than selected inline on
  1477. * the highest depth level displayed. This prevents them being orphaned
  1478. * at the end of the comment list.
  1479. *
  1480. * Example: max_depth = 2, with 5 levels of nested content.
  1481. * 1
  1482. * 1.1
  1483. * 1.1.1
  1484. * 1.1.1.1
  1485. * 1.1.1.1.1
  1486. * 1.1.2
  1487. * 1.1.2.1
  1488. * 2
  1489. * 2.2
  1490. *
  1491. * @see Walker::display_element()
  1492. * @see wp_list_comments()
  1493. *
  1494. * @since 2.7.0
  1495. *
  1496. * @param object $element Data object.
  1497. * @param array $children_elements List of elements to continue traversing.
  1498. * @param int $max_depth Max depth to traverse.
  1499. * @param int $depth Depth of current element.
  1500. * @param array $args An array of arguments.
  1501. * @param string $output Passed by reference. Used to append additional content.
  1502. * @return null Null on failure with no changes to parameters.
  1503. */
  1504. function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  1505. if ( !$element )
  1506. return;
  1507. $id_field = $this->db_fields['id'];
  1508. $id = $element->$id_field;
  1509. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  1510. // If we're at the max depth, and the current element still has children, loop over those and display them at this level
  1511. // This is to prevent them being orphaned to the end of the list.
  1512. if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
  1513. foreach ( $children_elements[ $id ] as $child )
  1514. $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
  1515. unset( $children_elements[ $id ] );
  1516. }
  1517. }
  1518. /**
  1519. * Start the element output.
  1520. *
  1521. * @since 2.7.0
  1522. *
  1523. * @see Walker::start_el()
  1524. * @see wp_list_comments()
  1525. *
  1526. * @param string $output Passed by reference. Used to append additional content.
  1527. * @param object $comment Comment data object.
  1528. * @param int $depth Depth of comment in reference to parents.
  1529. * @param array $args An array of arguments.
  1530. */
  1531. function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
  1532. $depth++;
  1533. $GLOBALS['comment_depth'] = $depth;
  1534. $GLOBALS['comment'] = $comment;
  1535. if ( !empty( $args['callback'] ) ) {
  1536. ob_start();
  1537. call_user_func( $args['callback'], $comment, $args, $depth );
  1538. $output .= ob_get_clean();
  1539. return;
  1540. }
  1541. if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
  1542. ob_start();
  1543. $this->ping( $comment, $depth, $args );
  1544. $output .= ob_get_clean();
  1545. } elseif ( 'html5' === $args['format'] ) {
  1546. ob_start();
  1547. $this->html5_comment( $comment, $depth, $args );
  1548. $output .= ob_get_clean();
  1549. } else {
  1550. ob_start();
  1551. $this->comment( $comment, $depth, $args );
  1552. $output .= ob_get_clean();
  1553. }
  1554. }
  1555. /**
  1556. * Ends the element output, if needed.
  1557. *
  1558. * @since 2.7.0
  1559. *
  1560. * @see Walker::end_el()
  1561. * @see wp_list_comments()
  1562. *
  1563. * @param string $output Passed by reference. Used to append additional content.
  1564. * @param object $comment The comment object. Default current comment.
  1565. * @param int $depth Depth of comment.
  1566. * @param array $args An array of arguments.
  1567. */
  1568. function end_el( &$output, $comment, $depth = 0, $args = array() ) {
  1569. if ( !empty( $args['end-callback'] ) ) {
  1570. ob_start();
  1571. call_user_func( $args['end-callback'], $comment, $args, $depth );
  1572. $output .= ob_get_clean();
  1573. return;
  1574. }
  1575. if ( 'div' == $args['style'] )
  1576. $output .= "</div><!-- #comment-## -->\n";
  1577. else
  1578. $output .= "</li><!-- #comment-## -->\n";
  1579. }
  1580. /**
  1581. * Output a pingback comment.
  1582. *
  1583. * @access protected
  1584. * @since 3.6.0
  1585. *
  1586. * @see wp_list_comments()
  1587. *
  1588. * @param object $comment The comment object.
  1589. * @param int $depth Depth of comment.
  1590. * @param array $args An array of arguments.
  1591. */
  1592. protected function ping( $comment, $depth, $args ) {
  1593. $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
  1594. ?>
  1595. <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
  1596. <div class="comment-body">
  1597. <?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  1598. </div>
  1599. <?php
  1600. }
  1601. /**
  1602. * Output a single comment.
  1603. *
  1604. * @access protected
  1605. * @since 3.6.0
  1606. *
  1607. * @see wp_list_comments()
  1608. *
  1609. * @param object $comment Comment to display.
  1610. * @param int $depth Depth of comment.
  1611. * @param array $args An array of arguments.
  1612. */
  1613. protected function comment( $comment, $depth, $args ) {
  1614. if ( 'div' == $args['style'] ) {
  1615. $tag = 'div';
  1616. $add_below = 'comment';
  1617. } else {
  1618. $tag = 'li';
  1619. $add_below = 'div-comment';
  1620. }
  1621. ?>
  1622. <<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">
  1623. <?php if ( 'div' != $args['style'] ) : ?>
  1624. <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  1625. <?php endif; ?>
  1626. <div class="comment-author vcard">
  1627. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  1628. <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
  1629. </div>
  1630. <?php if ( '0' == $comment->comment_approved ) : ?>
  1631. <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
  1632. <br />
  1633. <?php endif; ?>
  1634. <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  1635. <?php
  1636. /* translators: 1: date, 2: time */
  1637. printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '&nbsp;&nbsp;', '' );
  1638. ?>
  1639. </div>
  1640. <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  1641. <div class="reply">
  1642. <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  1643. </div>
  1644. <?php if ( 'div' != $args['style'] ) : ?>
  1645. </div>
  1646. <?php endif; ?>
  1647. <?php
  1648. }
  1649. /**
  1650. * Output a comment in the HTML5 format.
  1651. *
  1652. * @access protected
  1653. * @since 3.6.0
  1654. *
  1655. * @see wp_list_comments()
  1656. *
  1657. * @param object $comment Comment to display.
  1658. * @param int $depth Depth of comment.
  1659. * @param array $args An array of arguments.
  1660. */
  1661. protected function html5_comment( $comment, $depth, $args ) {
  1662. $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
  1663. ?>
  1664. <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  1665. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  1666. <footer class="comment-meta">
  1667. <div class="comment-author vcard">
  1668. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  1669. <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link() ) ); ?>
  1670. </div><!-- .comment-author -->
  1671. <div class="comment-metadata">
  1672. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  1673. <time datetime="<?php comment_time( 'c' ); ?>">
  1674. <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?>
  1675. </time>
  1676. </a>
  1677. <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  1678. </div><!-- .comment-metadata -->
  1679. <?php if ( '0' == $comment->comment_approved ) : ?>
  1680. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
  1681. <?php endif; ?>
  1682. </footer><!-- .comment-meta -->
  1683. <div class="comment-content">
  1684. <?php comment_text(); ?>
  1685. </div><!-- .comment-content -->
  1686. <div class="reply">
  1687. <?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  1688. </div><!-- .reply -->
  1689. </article><!-- .comment-body -->
  1690. <?php
  1691. }
  1692. }
  1693. /**
  1694. * List comments.
  1695. *
  1696. * Used in the comments.php template to list comments for a particular post.
  1697. *
  1698. * @since 2.7.0
  1699. *
  1700. * @see WP_Query->comments
  1701. *
  1702. * @param string|array $args {
  1703. * Optional. Formatting options.
  1704. *
  1705. * @type string $walker The Walker class used to list comments. Default null.
  1706. * @type int $max_depth The maximum comments depth. Default empty.
  1707. * @type string $style The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
  1708. * @type string $callback Callback function to use. Default null.
  1709. * @type string $end-callback Callback function to use at the end. Default null.
  1710. * @type string $type Type of comments to list.
  1711. * Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
  1712. * @type int $page Page ID to list comments for. Default empty.
  1713. * @type int $per_page Number of comments to list per page. Default empty.
  1714. * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
  1715. * @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts 'desc', 'asc'.
  1716. * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
  1717. * @type string $format How to format the comments list.
  1718. * Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
  1719. * @type bool $short_ping Whether to output short pings. Default false.
  1720. * @type bool $echo Whether to echo the output or return it. Default true.
  1721. * }
  1722. * @param array $comments Optional. Array of comment objects.
  1723. */
  1724. function wp_list_comments( $args = array(), $comments = null ) {
  1725. global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
  1726. $in_comment_loop = true;
  1727. $comment_alt = $comment_thread_alt = 0;
  1728. $comment_depth = 1;
  1729. $defaults = array(
  1730. 'walker' => null,
  1731. 'max_depth' => '',
  1732. 'style' => 'ul',
  1733. 'callback' => null,
  1734. 'end-callback' => null,
  1735. 'type' => 'all',
  1736. 'page' => '',
  1737. 'per_page' => '',
  1738. 'avatar_size' => 32,
  1739. 'reverse_top_level' => null,
  1740. 'reverse_children' => '',
  1741. 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
  1742. 'short_ping' => false,
  1743. 'echo' => true,
  1744. );
  1745. $r = wp_parse_args( $args, $defaults );
  1746. // Figure out what comments we'll be looping through ($_comments)
  1747. if ( null !== $comments ) {
  1748. $comments = (array) $comments;
  1749. if ( empty($comments) )
  1750. return;
  1751. if ( 'all' != $r['type'] ) {
  1752. $comments_by_type = separate_comments($comments);
  1753. if ( empty($comments_by_type[$r['type']]) )
  1754. return;
  1755. $_comments = $comments_by_type[$r['type']];
  1756. } else {
  1757. $_comments = $comments;
  1758. }
  1759. } else {
  1760. if ( empty($wp_query->comments) )
  1761. return;
  1762. if ( 'all' != $r['type'] ) {
  1763. if ( empty($wp_query->comments_by_type) )
  1764. $wp_query->comments_by_type = separate_comments($wp_query->comments);
  1765. if ( empty($wp_query->comments_by_type[$r['type']]) )
  1766. return;
  1767. $_comments = $wp_query->comments_by_type[$r['type']];
  1768. } else {
  1769. $_comments = $wp_query->comments;
  1770. }
  1771. }
  1772. if ( '' === $r['per_page'] && get_option('page_comments') )
  1773. $r['per_page'] = get_query_var('comments_per_page');
  1774. if ( empty($r['per_page']) ) {
  1775. $r['per_page'] = 0;
  1776. $r['page'] = 0;
  1777. }
  1778. if ( '' === $r['max_depth'] ) {
  1779. if ( get_option('thread_comments') )
  1780. $r['max_depth'] = get_option('thread_comments_depth');
  1781. else
  1782. $r['max_depth'] = -1;
  1783. }
  1784. if ( '' === $r['page'] ) {
  1785. if ( empty($overridden_cpage) ) {
  1786. $r['page'] = get_query_var('cpage');
  1787. } else {
  1788. $threaded = ( -1 != $r['max_depth'] );
  1789. $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
  1790. set_query_var( 'cpage', $r['page'] );
  1791. }
  1792. }
  1793. // Validation check
  1794. $r['page'] = intval($r['page']);
  1795. if ( 0 == $r['page'] && 0 != $r['per_page'] )
  1796. $r['page'] = 1;
  1797. if ( null === $r['reverse_top_level'] )
  1798. $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
  1799. extract( $r, EXTR_SKIP );
  1800. if ( empty($walker) )
  1801. $walker = new Walker_Comment;
  1802. $output = $walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
  1803. $wp_query->max_num_comment_pages = $walker->max_pages;
  1804. $in_comment_loop = false;
  1805. if ( $r['echo'] )
  1806. echo $output;
  1807. else
  1808. return $output;
  1809. }
  1810. /**
  1811. * Output a complete commenting form for use within a template.
  1812. *
  1813. * Most strings and form fields may be controlled through the $args array passed
  1814. * into the function, while you may also choose to use the comment_form_default_fields
  1815. * filter to modify the array of default fields if you'd just like to add a new
  1816. * one or remove a single field. All fields are also individually passed through
  1817. * a filter of the form comment_form_field_$name where $name is the key used
  1818. * in the array of fields.
  1819. *
  1820. * @since 3.0.0
  1821. *
  1822. * @param array $args {
  1823. * Optional. Default arguments and form fields to override.
  1824. *
  1825. * @type array $fields {
  1826. * Default comment fields, filterable by default via the 'comment_form_default_fields' hook.
  1827. *
  1828. * @type string $author Comment author field HTML.
  1829. * @type string $email Comment author email field HTML.
  1830. * @type string $url Comment author URL field HTML.
  1831. * }
  1832. * @type string $comment_field The comment textarea field HTML.
  1833. * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
  1834. * @type string $logged_in_as HTML element for a 'logged in as <user>' message.
  1835. * @type string $comment_notes_before HTML element for a message displayed before the comment form.
  1836. * Default 'Your email address will not be published.'.
  1837. * @type string $comment_notes_after HTML element for a message displayed after the comment form.
  1838. * Default 'You may use these HTML tags and attributes ...'.
  1839. * @type string $id_form The comment form element id attribute. Default 'commentform'.
  1840. * @type string $id_submit The comment submit element id attribute. Default 'submit'.
  1841. * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
  1842. * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
  1843. * where %s is the author of the comment being replied to.
  1844. * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
  1845. * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
  1846. * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
  1847. * }
  1848. * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
  1849. */
  1850. function comment_form( $args = array(), $post_id = null ) {
  1851. if ( null === $post_id )
  1852. $post_id = get_the_ID();
  1853. else
  1854. $id = $post_id;
  1855. $commenter = wp_get_current_commenter();
  1856. $user = wp_get_current_user();
  1857. $user_identity = $user->exists() ? $user->display_name : '';
  1858. $args = wp_parse_args( $args );
  1859. if ( ! isset( $args['format'] ) )
  1860. $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
  1861. $req = get_option( 'require_name_email' );
  1862. $aria_req = ( $req ? " aria-required='true'" : '' );
  1863. $html5 = 'html5' === $args['format'];
  1864. $fields = array(
  1865. 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  1866. '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
  1867. 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  1868. '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
  1869. 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
  1870. '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
  1871. );
  1872. $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
  1873. /**
  1874. * Filter the default comment form fields.
  1875. *
  1876. * @since 3.0.0
  1877. *
  1878. * @param array $fields The default comment fields.
  1879. */
  1880. $fields = apply_filters( 'comment_form_default_fields', $fields );
  1881. $defaults = array(
  1882. 'fields' => $fields,
  1883. 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
  1884. /** This filter is documented in wp-includes/link-template.php */
  1885. 'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
  1886. /** This filter is documented in wp-includes/link-template.php */
  1887. 'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
  1888. 'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
  1889. 'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
  1890. 'id_form' => 'commentform',
  1891. 'id_submit' => 'submit',
  1892. 'title_reply' => __( 'Leave a Reply' ),
  1893. 'title_reply_to' => __( 'Leave a Reply to %s' ),
  1894. 'cancel_reply_link' => __( 'Cancel reply' ),
  1895. 'label_submit' => __( 'Post Comment' ),
  1896. 'format' => 'xhtml',
  1897. );
  1898. /**
  1899. * Filter the comment form default arguments.
  1900. *
  1901. * Use 'comment_form_default_fields' to filter the comment fields.
  1902. *
  1903. * @since 3.0.0
  1904. *
  1905. * @param array $defaults The default comment form arguments.
  1906. */
  1907. $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
  1908. ?>
  1909. <?php if ( comments_open( $post_id ) ) : ?>
  1910. <?php
  1911. /**
  1912. * Fires before the comment form.
  1913. *
  1914. * @since 3.0.0
  1915. */
  1916. do_action( 'comment_form_before' );
  1917. ?>
  1918. <div id="respond" class="comment-respond">
  1919. <h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>
  1920. <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
  1921. <?php echo $args['must_log_in']; ?>
  1922. <?php
  1923. /**
  1924. * Fires after the HTML-formatted 'must log in after' message in the comment form.
  1925. *
  1926. * @since 3.0.0
  1927. */
  1928. do_action( 'comment_form_must_log_in_after' );
  1929. ?>
  1930. <?php else : ?>
  1931. <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="comment-form"<?php echo $html5 ? ' novalidate' : ''; ?>>
  1932. <?php
  1933. /**
  1934. * Fires at the top of the comment form, inside the <form> tag.
  1935. *
  1936. * @since 3.0.0
  1937. */
  1938. do_action( 'comment_form_top' );
  1939. ?>
  1940. <?php if ( is_user_logged_in() ) : ?>
  1941. <?php
  1942. /**
  1943. * Filter the 'logged in' message for the comment form for display.
  1944. *
  1945. * @since 3.0.0
  1946. *
  1947. * @param string $args_logged_in The logged-in-as HTML-formatted message.
  1948. * @param array $commenter An array containing the comment author's
  1949. * username, email, and URL.
  1950. * @param string $user_identity If the commenter is a registered user,
  1951. * the display name, blank otherwise.
  1952. */
  1953. echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
  1954. ?>
  1955. <?php
  1956. /**
  1957. * Fires after the is_user_logged_in() check in the comment form.
  1958. *
  1959. * @since 3.0.0
  1960. *
  1961. * @param array $commenter An array containing the comment author's
  1962. * username, email, and URL.
  1963. * @param string $user_identity If the commenter is a registered user,
  1964. * the display name, blank otherwise.
  1965. */
  1966. do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
  1967. ?>
  1968. <?php else : ?>
  1969. <?php echo $args['comment_notes_before']; ?>
  1970. <?php
  1971. /**
  1972. * Fires before the comment fields in the comment form.
  1973. *
  1974. * @since 3.0.0
  1975. */
  1976. do_action( 'comment_form_before_fields' );
  1977. foreach ( (array) $args['fields'] as $name => $field ) {
  1978. /**
  1979. * Filter a comment form field for display.
  1980. *
  1981. * The dynamic portion of the filter hook, $name, refers to the name
  1982. * of the comment form field. Such as 'author', 'email', or 'url'.
  1983. *
  1984. * @since 3.0.0
  1985. *
  1986. * @param string $field The HTML-formatted output of the comment form field.
  1987. */
  1988. echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
  1989. }
  1990. /**
  1991. * Fires after the comment fields in the comment form.
  1992. *
  1993. * @since 3.0.0
  1994. */
  1995. do_action( 'comment_form_after_fields' );
  1996. ?>
  1997. <?php endif; ?>
  1998. <?php
  1999. /**
  2000. * Filter the content of the comment textarea field for display.
  2001. *
  2002. * @since 3.0.0
  2003. *
  2004. * @param string $args_comment_field The content of the comment textarea field.
  2005. */
  2006. echo apply_filters( 'comment_form_field_comment', $args['comment_field'] );
  2007. ?>
  2008. <?php echo $args['comment_notes_after']; ?>
  2009. <p class="form-submit">
  2010. <input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
  2011. <?php comment_id_fields( $post_id ); ?>
  2012. </p>
  2013. <?php
  2014. /**
  2015. * Fires at the bottom of the comment form, inside the closing </form> tag.
  2016. *
  2017. * @since 1.5.0
  2018. *
  2019. * @param int $post_id The post ID.
  2020. */
  2021. do_action( 'comment_form', $post_id );
  2022. ?>
  2023. </form>
  2024. <?php endif; ?>
  2025. </div><!-- #respond -->
  2026. <?php
  2027. /**
  2028. * Fires after the comment form.
  2029. *
  2030. * @since 3.0.0
  2031. */
  2032. do_action( 'comment_form_after' );
  2033. else :
  2034. /**
  2035. * Fires after the comment form if comments are closed.
  2036. *
  2037. * @since 3.0.0
  2038. */
  2039. do_action( 'comment_form_comments_closed' );
  2040. endif;
  2041. }