PageRenderTime 67ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/comment-template.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 2264 lines | 865 code | 188 blank | 1211 comment | 198 complexity | ea1e752fc7eda295bde356faf79d76bf MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.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 Optional. Post ID or WP_Post object. Default is global $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 Optional. Post ID or WP_Post object. Default is global $post.
  629. * @return int The number of comments a post has.
  630. */
  631. function get_comments_number( $post_id = 0 ) {
  632. $post = get_post( $post_id );
  633. if ( ! $post ) {
  634. $count = 0;
  635. } else {
  636. $count = $post->comment_count;
  637. $post_id = $post->ID;
  638. }
  639. /**
  640. * Filter the returned comment count for a post.
  641. *
  642. * @since 1.5.0
  643. *
  644. * @param int $count Number of comments a post has.
  645. * @param int $post_id Post ID.
  646. */
  647. return apply_filters( 'get_comments_number', $count, $post_id );
  648. }
  649. /**
  650. * Display the language string for the number of comments the current post has.
  651. *
  652. * @since 0.71
  653. *
  654. * @param string $zero Optional. Text for no comments. Default false.
  655. * @param string $one Optional. Text for one comment. Default false.
  656. * @param string $more Optional. Text for more than one comment. Default false.
  657. * @param string $deprecated Not used.
  658. */
  659. function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
  660. if ( ! empty( $deprecated ) ) {
  661. _deprecated_argument( __FUNCTION__, '1.3' );
  662. }
  663. echo get_comments_number_text( $zero, $one, $more );
  664. }
  665. /**
  666. * Display the language string for the number of comments the current post has.
  667. *
  668. * @since 4.0.0
  669. *
  670. * @param string $zero Optional. Text for no comments. Default false.
  671. * @param string $one Optional. Text for one comment. Default false.
  672. * @param string $more Optional. Text for more than one comment. Default false.
  673. */
  674. function get_comments_number_text( $zero = false, $one = false, $more = false ) {
  675. $number = get_comments_number();
  676. if ( $number > 1 ) {
  677. $output = str_replace( '%', number_format_i18n( $number ), ( false === $more ) ? __( '% Comments' ) : $more );
  678. } elseif ( $number == 0 ) {
  679. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  680. } else { // must be one
  681. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  682. }
  683. /**
  684. * Filter the comments count for display.
  685. *
  686. * @since 1.5.0
  687. *
  688. * @see _n()
  689. *
  690. * @param string $output A translatable string formatted based on whether the count
  691. * is equal to 0, 1, or 1+.
  692. * @param int $number The number of post comments.
  693. */
  694. return apply_filters( 'comments_number', $output, $number );
  695. }
  696. /**
  697. * Retrieve the text of the current comment.
  698. *
  699. * @since 1.5.0
  700. *
  701. * @see Walker_Comment::comment()
  702. *
  703. * @param int $comment_ID ID of the comment for which to get the text. Default current comment.
  704. * @param array $args Optional. An array of arguments. Default empty.
  705. * @return string The comment content.
  706. */
  707. function get_comment_text( $comment_ID = 0, $args = array() ) {
  708. $comment = get_comment( $comment_ID );
  709. /**
  710. * Filter the text of a comment.
  711. *
  712. * @since 1.5.0
  713. *
  714. * @see Walker_Comment::comment()
  715. *
  716. * @param string $comment_content Text of the comment.
  717. * @param object $comment The comment object.
  718. * @param array $args An array of arguments.
  719. */
  720. return apply_filters( 'get_comment_text', $comment->comment_content, $comment, $args );
  721. }
  722. /**
  723. * Display the text of the current comment.
  724. *
  725. * @since 0.71
  726. *
  727. * @see Walker_Comment::comment()
  728. *
  729. * @param int $comment_ID ID of the comment for which to print the text. Default 0.
  730. * @param array $args Optional. An array of arguments. Default empty array. Default empty.
  731. */
  732. function comment_text( $comment_ID = 0, $args = array() ) {
  733. $comment = get_comment( $comment_ID );
  734. $comment_text = get_comment_text( $comment_ID , $args );
  735. /**
  736. * Filter the text of a comment to be displayed.
  737. *
  738. * @since 1.2.0
  739. *
  740. * @see Walker_Comment::comment()
  741. *
  742. * @param string $comment_text Text of the current comment.
  743. * @param object $comment The comment object.
  744. * @param array $args An array of arguments.
  745. */
  746. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  747. }
  748. /**
  749. * Retrieve the comment time of the current comment.
  750. *
  751. * @since 1.5.0
  752. *
  753. * @param string $d Optional. The format of the time. Default user's settings.
  754. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  755. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  756. * Default true.
  757. * @return string The formatted time.
  758. */
  759. function get_comment_time( $d = '', $gmt = false, $translate = true ) {
  760. global $comment;
  761. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  762. if ( '' == $d )
  763. $date = mysql2date(get_option('time_format'), $comment_date, $translate);
  764. else
  765. $date = mysql2date($d, $comment_date, $translate);
  766. /**
  767. * Filter the returned comment time.
  768. *
  769. * @since 1.5.0
  770. *
  771. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  772. * @param string $d Date format.
  773. * @param bool $gmt Whether the GMT date is in use.
  774. * @param bool $translate Whether the time is translated.
  775. * @param object $comment The comment object.
  776. */
  777. return apply_filters( 'get_comment_time', $date, $d, $gmt, $translate, $comment );
  778. }
  779. /**
  780. * Display the comment time of the current comment.
  781. *
  782. * @since 0.71
  783. *
  784. * @param string $d Optional. The format of the time. Default user's settings.
  785. */
  786. function comment_time( $d = '' ) {
  787. echo get_comment_time($d);
  788. }
  789. /**
  790. * Retrieve the comment type of the current comment.
  791. *
  792. * @since 1.5.0
  793. *
  794. * @param int $comment_ID ID of the comment for which to get the type. Default current comment.
  795. * @return string The comment type.
  796. */
  797. function get_comment_type( $comment_ID = 0 ) {
  798. $comment = get_comment( $comment_ID );
  799. if ( '' == $comment->comment_type )
  800. $comment->comment_type = 'comment';
  801. /**
  802. * Filter the returned comment type.
  803. *
  804. * @since 1.5.0
  805. *
  806. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  807. */
  808. return apply_filters( 'get_comment_type', $comment->comment_type );
  809. }
  810. /**
  811. * Display the comment type of the current comment.
  812. *
  813. * @since 0.71
  814. *
  815. * @param string $commenttxt Optional. String to display for comment type. Default false.
  816. * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
  817. * @param string $pingbacktxt Optional. String to display for pingback type. Default false.
  818. */
  819. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  820. if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
  821. if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
  822. if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
  823. $type = get_comment_type();
  824. switch( $type ) {
  825. case 'trackback' :
  826. echo $trackbacktxt;
  827. break;
  828. case 'pingback' :
  829. echo $pingbacktxt;
  830. break;
  831. default :
  832. echo $commenttxt;
  833. }
  834. }
  835. /**
  836. * Retrieve The current post's trackback URL.
  837. *
  838. * There is a check to see if permalink's have been enabled and if so, will
  839. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  840. * current post is used and appended to the correct page to go to.
  841. *
  842. * @since 1.5.0
  843. *
  844. * @return string The trackback URL after being filtered.
  845. */
  846. function get_trackback_url() {
  847. if ( '' != get_option('permalink_structure') )
  848. $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
  849. else
  850. $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . get_the_ID();
  851. /**
  852. * Filter the returned trackback URL.
  853. *
  854. * @since 2.2.0
  855. *
  856. * @param string $tb_url The trackback URL.
  857. */
  858. return apply_filters( 'trackback_url', $tb_url );
  859. }
  860. /**
  861. * Display the current post's trackback URL.
  862. *
  863. * @since 0.71
  864. *
  865. * @param bool $deprecated_echo Not used.
  866. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  867. * for the result instead.
  868. */
  869. function trackback_url( $deprecated_echo = true ) {
  870. if ( $deprecated_echo !== true )
  871. _deprecated_argument( __FUNCTION__, '2.5', __('Use <code>get_trackback_url()</code> instead if you do not want the value echoed.') );
  872. if ( $deprecated_echo )
  873. echo get_trackback_url();
  874. else
  875. return get_trackback_url();
  876. }
  877. /**
  878. * Generate and display the RDF for the trackback information of current post.
  879. *
  880. * Deprecated in 3.0.0, and restored in 3.0.1.
  881. *
  882. * @since 0.71
  883. *
  884. * @param int $deprecated Not used (Was $timezone = 0).
  885. */
  886. function trackback_rdf( $deprecated = '' ) {
  887. if ( ! empty( $deprecated ) ) {
  888. _deprecated_argument( __FUNCTION__, '2.5' );
  889. }
  890. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  891. return;
  892. }
  893. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  894. xmlns:dc="http://purl.org/dc/elements/1.1/"
  895. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  896. <rdf:Description rdf:about="';
  897. the_permalink();
  898. echo '"'."\n";
  899. echo ' dc:identifier="';
  900. the_permalink();
  901. echo '"'."\n";
  902. echo ' dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  903. echo ' trackback:ping="'.get_trackback_url().'"'." />\n";
  904. echo '</rdf:RDF>';
  905. }
  906. /**
  907. * Whether the current post is open for comments.
  908. *
  909. * @since 1.5.0
  910. *
  911. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  912. * @return bool True if the comments are open.
  913. */
  914. function comments_open( $post_id = null ) {
  915. $_post = get_post($post_id);
  916. $open = ( 'open' == $_post->comment_status );
  917. /**
  918. * Filter whether the current post is open for comments.
  919. *
  920. * @since 2.5.0
  921. *
  922. * @param bool $open Whether the current post is open for comments.
  923. * @param int|WP_Post $post_id The post ID or WP_Post object.
  924. */
  925. return apply_filters( 'comments_open', $open, $post_id );
  926. }
  927. /**
  928. * Whether the current post is open for pings.
  929. *
  930. * @since 1.5.0
  931. *
  932. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  933. * @return bool True if pings are accepted
  934. */
  935. function pings_open( $post_id = null ) {
  936. $_post = get_post($post_id);
  937. $open = ( 'open' == $_post->ping_status );
  938. /**
  939. * Filter whether the current post is open for pings.
  940. *
  941. * @since 2.5.0
  942. *
  943. * @param bool $open Whether the current post is open for pings.
  944. * @param int|WP_Post $post_id The post ID or WP_Post object.
  945. */
  946. return apply_filters( 'pings_open', $open, $post_id );
  947. }
  948. /**
  949. * Display form token for unfiltered comments.
  950. *
  951. * Will only display nonce token if the current user has permissions for
  952. * unfiltered html. Won't display the token for other users.
  953. *
  954. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  955. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  956. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  957. *
  958. * Backported to 2.0.10.
  959. *
  960. * @since 2.1.3
  961. */
  962. function wp_comment_form_unfiltered_html_nonce() {
  963. $post = get_post();
  964. $post_id = $post ? $post->ID : 0;
  965. if ( current_user_can( 'unfiltered_html' ) ) {
  966. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  967. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  968. }
  969. }
  970. /**
  971. * Load the comment template specified in $file.
  972. *
  973. * Will not display the comments template if not on single post or page, or if
  974. * the post does not have comments.
  975. *
  976. * Uses the WordPress database object to query for the comments. The comments
  977. * are passed through the 'comments_array' filter hook with the list of comments
  978. * and the post ID respectively.
  979. *
  980. * The $file path is passed through a filter hook called, 'comments_template'
  981. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  982. * first and if it fails it will require the default comment template from the
  983. * default theme. If either does not exist, then the WordPress process will be
  984. * halted. It is advised for that reason, that the default theme is not deleted.
  985. *
  986. * @todo Document globals
  987. * @uses $withcomments Will not try to get the comments if the post has none.
  988. *
  989. * @since 1.5.0
  990. *
  991. * @param string $file Optional. The file to load. Default '/comments.php'.
  992. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  993. * Default false.
  994. * @return null Returns null if no comments appear.
  995. */
  996. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  997. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
  998. if ( !(is_single() || is_page() || $withcomments) || empty($post) )
  999. return;
  1000. if ( empty($file) )
  1001. $file = '/comments.php';
  1002. $req = get_option('require_name_email');
  1003. /*
  1004. * Comment author information fetched from the comment cookies.
  1005. * Uuses wp_get_current_commenter().
  1006. */
  1007. $commenter = wp_get_current_commenter();
  1008. /*
  1009. * The name of the current comment author escaped for use in attributes.
  1010. * Escaped by sanitize_comment_cookies().
  1011. */
  1012. $comment_author = $commenter['comment_author'];
  1013. /*
  1014. * The email address of the current comment author escaped for use in attributes.
  1015. * Escaped by sanitize_comment_cookies().
  1016. */
  1017. $comment_author_email = $commenter['comment_author_email'];
  1018. /*
  1019. * The url of the current comment author escaped for use in attributes.
  1020. */
  1021. $comment_author_url = esc_url($commenter['comment_author_url']);
  1022. /** @todo Use API instead of SELECTs. */
  1023. if ( $user_ID) {
  1024. $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));
  1025. } else if ( empty($comment_author) ) {
  1026. $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
  1027. } else {
  1028. $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));
  1029. }
  1030. /**
  1031. * Filter the comments array.
  1032. *
  1033. * @since 2.1.0
  1034. *
  1035. * @param array $comments Array of comments supplied to the comments template.
  1036. * @param int $post_ID Post ID.
  1037. */
  1038. $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
  1039. $comments = &$wp_query->comments;
  1040. $wp_query->comment_count = count($wp_query->comments);
  1041. update_comment_cache($wp_query->comments);
  1042. if ( $separate_comments ) {
  1043. $wp_query->comments_by_type = separate_comments($comments);
  1044. $comments_by_type = &$wp_query->comments_by_type;
  1045. }
  1046. $overridden_cpage = false;
  1047. if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
  1048. set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
  1049. $overridden_cpage = true;
  1050. }
  1051. if ( !defined('COMMENTS_TEMPLATE') )
  1052. define('COMMENTS_TEMPLATE', true);
  1053. $theme_template = STYLESHEETPATH . $file;
  1054. /**
  1055. * Filter the path to the theme template file used for the comments template.
  1056. *
  1057. * @since 1.5.1
  1058. *
  1059. * @param string $theme_template The path to the theme template file.
  1060. */
  1061. $include = apply_filters( 'comments_template', $theme_template );
  1062. if ( file_exists( $include ) )
  1063. require( $include );
  1064. elseif ( file_exists( TEMPLATEPATH . $file ) )
  1065. require( TEMPLATEPATH . $file );
  1066. else // Backward compat code will be removed in a future release
  1067. require( ABSPATH . WPINC . '/theme-compat/comments.php');
  1068. }
  1069. /**
  1070. * Display the JS popup script to show a comment.
  1071. *
  1072. * If the $file parameter is empty, then the home page is assumed. The defaults
  1073. * for the window are 400px by 400px.
  1074. *
  1075. * For the comment link popup to work, this function has to be called or the
  1076. * normal comment link will be assumed.
  1077. *
  1078. * @global string $wpcommentspopupfile The URL to use for the popup window.
  1079. * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
  1080. *
  1081. * @since 0.71
  1082. *
  1083. * @param int $width Optional. The width of the popup window. Default 400.
  1084. * @param int $height Optional. The height of the popup window. Default 400.
  1085. * @param string $file Optional. Sets the location of the popup window.
  1086. */
  1087. function comments_popup_script( $width = 400, $height = 400, $file = '' ) {
  1088. global $wpcommentspopupfile, $wpcommentsjavascript;
  1089. if (empty ($file)) {
  1090. $wpcommentspopupfile = ''; // Use the index.
  1091. } else {
  1092. $wpcommentspopupfile = $file;
  1093. }
  1094. $wpcommentsjavascript = 1;
  1095. $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";
  1096. echo $javascript;
  1097. }
  1098. /**
  1099. * Displays the link to the comments popup window for the current post ID.
  1100. *
  1101. * Is not meant to be displayed on single posts and pages. Should be used
  1102. * on the lists of posts
  1103. *
  1104. * @global string $wpcommentspopupfile The URL to use for the popup window.
  1105. * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
  1106. *
  1107. * @since 0.71
  1108. *
  1109. * @param string $zero Optional. String to display when no comments. Default false.
  1110. * @param string $one Optional. String to display when only one comment is available.
  1111. * Default false.
  1112. * @param string $more Optional. String to display when there are more than one comment.
  1113. * Default false.
  1114. * @param string $css_class Optional. CSS class to use for comments. Default empty.
  1115. * @param string $none Optional. String to display when comments have been turned off.
  1116. * Default false.
  1117. * @return null Returns null on single posts and pages.
  1118. */
  1119. function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
  1120. global $wpcommentspopupfile, $wpcommentsjavascript;
  1121. $id = get_the_ID();
  1122. if ( false === $zero ) $zero = __( 'No Comments' );
  1123. if ( false === $one ) $one = __( '1 Comment' );
  1124. if ( false === $more ) $more = __( '% Comments' );
  1125. if ( false === $none ) $none = __( 'Comments Off' );
  1126. $number = get_comments_number( $id );
  1127. if ( 0 == $number && !comments_open() && !pings_open() ) {
  1128. echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
  1129. return;
  1130. }
  1131. if ( post_password_required() ) {
  1132. echo __('Enter your password to view comments.');
  1133. return;
  1134. }
  1135. echo '<a href="';
  1136. if ( $wpcommentsjavascript ) {
  1137. if ( empty( $wpcommentspopupfile ) )
  1138. $home = home_url();
  1139. else
  1140. $home = get_option('siteurl');
  1141. echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
  1142. echo '" onclick="wpopen(this.href); return false"';
  1143. } else { // if comments_popup_script() is not in the template, display simple comment link
  1144. if ( 0 == $number )
  1145. echo get_permalink() . '#respond';
  1146. else
  1147. comments_link();
  1148. echo '"';
  1149. }
  1150. if ( !empty( $css_class ) ) {
  1151. echo ' class="'.$css_class.'" ';
  1152. }
  1153. $title = the_title_attribute( array('echo' => 0 ) );
  1154. $attributes = '';
  1155. /**
  1156. * Filter the comments popup link attributes for display.
  1157. *
  1158. * @since 2.5.0
  1159. *
  1160. * @param string $attributes The comments popup link attributes. Default empty.
  1161. */
  1162. echo apply_filters( 'comments_popup_link_attributes', $attributes );
  1163. echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
  1164. comments_number( $zero, $one, $more );
  1165. echo '</a>';
  1166. }
  1167. /**
  1168. * Retrieve HTML content for reply to comment link.
  1169. *
  1170. * @since 2.7.0
  1171. *
  1172. * @param array $args {
  1173. * Optional. Override default arguments.
  1174. *
  1175. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1176. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1177. * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
  1178. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1179. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1180. * Default 'respond'.
  1181. * @type string $reply_text The text of the Reply link. Default 'Reply'.
  1182. * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
  1183. * @type int $depth' The depth of the new comment. Must be greater than 0 and less than the value
  1184. * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
  1185. * @type string $before The text or HTML to add before the reply link. Default empty.
  1186. * @type string $after The text or HTML to add after the reply link. Default empty.
  1187. * }
  1188. * @param int $comment Comment being replied to. Default current comment.
  1189. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1190. * Default current post.
  1191. * @return mixed Link to show comment form, if successful. False, if comments are closed.
  1192. */
  1193. function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1194. $defaults = array(
  1195. 'add_below' => 'comment',
  1196. 'respond_id' => 'respond',
  1197. 'reply_text' => __('Reply'),
  1198. 'login_text' => __('Log in to Reply'),
  1199. 'depth' => 0,
  1200. 'before' => '',
  1201. 'after' => ''
  1202. );
  1203. $args = wp_parse_args( $args, $defaults );
  1204. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
  1205. return;
  1206. }
  1207. $add_below = $args['add_below'];
  1208. $respond_id = $args['respond_id'];
  1209. $reply_text = $args['reply_text'];
  1210. $comment = get_comment( $comment );
  1211. if ( empty( $post ) ) {
  1212. $post = $comment->comment_post_ID;
  1213. }
  1214. $post = get_post( $post );
  1215. if ( ! comments_open( $post->ID ) ) {
  1216. return false;
  1217. }
  1218. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1219. $link = '<a rel="nofollow" class="comment-reply-login" href="' . esc_url( wp_login_url( get_permalink() ) ) . '">' . $args['login_text'] . '</a>';
  1220. } else {
  1221. $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>";
  1222. }
  1223. /**
  1224. * Filter the comment reply link.
  1225. *
  1226. * @since 2.7.0
  1227. *
  1228. * @param string $link The HTML markup for the comment reply link.
  1229. * @param array $args An array of arguments overriding the defaults.
  1230. * @param object $comment The object of the comment being replied.
  1231. * @param WP_Post $post The WP_Post object.
  1232. */
  1233. return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
  1234. }
  1235. /**
  1236. * Displays the HTML content for reply to comment link.
  1237. *
  1238. * @since 2.7.0
  1239. *
  1240. * @see get_comment_reply_link()
  1241. *
  1242. * @param array $args Optional. Override default options.
  1243. * @param int $comment Comment being replied to. Default current comment.
  1244. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1245. * Default current post.
  1246. * @return mixed Link to show comment form, if successful. False, if comments are closed.
  1247. */
  1248. function comment_reply_link($args = array(), $comment = null, $post = null) {
  1249. echo get_comment_reply_link($args, $comment, $post);
  1250. }
  1251. /**
  1252. * Retrieve HTML content for reply to post link.
  1253. *
  1254. * @since 2.7.0
  1255. *
  1256. * @param array $args {
  1257. * Optional. Override default arguments.
  1258. *
  1259. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1260. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1261. * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
  1262. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1263. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1264. * Default 'respond'.
  1265. * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
  1266. * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
  1267. * @type string $before Text or HTML to add before the reply link. Default empty.
  1268. * @type string $after Text or HTML to add after the reply link. Default empty.
  1269. * }
  1270. * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
  1271. * Default current post.
  1272. * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
  1273. */
  1274. function get_post_reply_link($args = array(), $post = null) {
  1275. $defaults = array(
  1276. 'add_below' => 'post',
  1277. 'respond_id' => 'respond',
  1278. 'reply_text' => __('Leave a Comment'),
  1279. 'login_text' => __('Log in to leave a Comment'),
  1280. 'before' => '',
  1281. 'after' => '',
  1282. );
  1283. $args = wp_parse_args($args, $defaults);
  1284. $add_below = $args['add_below'];
  1285. $respond_id = $args['respond_id'];
  1286. $reply_text = $args['reply_text'];
  1287. $post = get_post($post);
  1288. if ( ! comments_open( $post->ID ) ) {
  1289. return false;
  1290. }
  1291. if ( get_option('comment_registration') && ! is_user_logged_in() ) {
  1292. $link = '<a rel="nofollow" href="' . wp_login_url( get_permalink() ) . '">' . $args['login_text'] . '</a>';
  1293. } else {
  1294. $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>";
  1295. }
  1296. $formatted_link = $args['before'] . $link . $args['after'];
  1297. /**
  1298. * Filter the formatted post comments link HTML.
  1299. *
  1300. * @since 2.7.0
  1301. *
  1302. * @param string $formatted The HTML-formatted post comments link.
  1303. * @param int|WP_Post $post The post ID or WP_Post object.
  1304. */
  1305. return apply_filters( 'post_comments_link', $formatted_link, $post );
  1306. }
  1307. /**
  1308. * Displays the HTML content for reply to post link.
  1309. *
  1310. * @since 2.7.0
  1311. *
  1312. * @see get_post_reply_link()
  1313. *
  1314. * @param array $args Optional. Override default options,
  1315. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1316. * Default current post.
  1317. * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
  1318. */
  1319. function post_reply_link($args = array(), $post = null) {
  1320. echo get_post_reply_link($args, $post);
  1321. }
  1322. /**
  1323. * Retrieve HTML content for cancel comment reply link.
  1324. *
  1325. * @since 2.7.0
  1326. *
  1327. * @param string $text Optional. Text to display for cancel reply link. Default empty.
  1328. */
  1329. function get_cancel_comment_reply_link( $text = '' ) {
  1330. if ( empty($text) )
  1331. $text = __('Click here to cancel reply.');
  1332. $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
  1333. $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
  1334. $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
  1335. /**
  1336. * Filter the cancel comment reply link HTML.
  1337. *
  1338. * @since 2.7.0
  1339. *
  1340. * @param string $formatted_link The HTML-formatted cancel comment reply link.
  1341. * @param string $link Cancel comment reply link URL.
  1342. * @param string $text Cancel comment reply link text.
  1343. */
  1344. return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
  1345. }
  1346. /**
  1347. * Display HTML content for cancel comment reply link.
  1348. *
  1349. * @since 2.7.0
  1350. *
  1351. * @param string $text Optional. Text to display for cancel reply link. Default empty.
  1352. */
  1353. function cancel_comment_reply_link( $text = '' ) {
  1354. echo get_cancel_comment_reply_link($text);
  1355. }
  1356. /**
  1357. * Retrieve hidden input HTML for replying to comments.
  1358. *
  1359. * @since 3.0.0
  1360. *
  1361. * @param int $id Optional. Post ID. Default current post ID.
  1362. * @return string Hidden input HTML for replying to comments
  1363. */
  1364. function get_comment_id_fields( $id = 0 ) {
  1365. if ( empty( $id ) )
  1366. $id = get_the_ID();
  1367. $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
  1368. $result = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
  1369. $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
  1370. /**
  1371. * Filter the returned comment id fields.
  1372. *
  1373. * @since 3.0.0
  1374. *
  1375. * @param string $result The HTML-formatted hidden id field comment elements.
  1376. * @param int $id The post ID.
  1377. * @param int $replytoid The id of the comment being replied to.
  1378. */
  1379. return apply_filters( 'comment_id_fields', $result, $id, $replytoid );
  1380. }
  1381. /**
  1382. * Output hidden input HTML for replying to comments.
  1383. *
  1384. * @since 2.7.0
  1385. *
  1386. * @param int $id Optional. Post ID. Default current post ID.
  1387. */
  1388. function comment_id_fields( $id = 0 ) {
  1389. echo get_comment_id_fields( $id );
  1390. }
  1391. /**
  1392. * Display text based on comment reply status.
  1393. *
  1394. * Only affects users with Javascript disabled.
  1395. *
  1396. * @since 2.7.0
  1397. *
  1398. * @param string $noreplytext Optional. Text to display when not replying to a comment.
  1399. * Default false.
  1400. * @param string $replytext Optional. Text to display when replying to a comment.
  1401. * Default false. Accepts "%s" for the author of the comment
  1402. * being replied to.
  1403. * @param string $linktoparent Optional. Boolean to control making the author's name a link
  1404. * to their comment. Default true.
  1405. */
  1406. function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) {
  1407. global $comment;
  1408. if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
  1409. if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
  1410. $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
  1411. if ( 0 == $replytoid )
  1412. echo $noreplytext;
  1413. else {
  1414. $comment = get_comment($replytoid);
  1415. $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author() . '</a>' : get_comment_author();
  1416. printf( $replytext, $author );
  1417. }
  1418. }
  1419. /**
  1420. * HTML comment list class.
  1421. *
  1422. * @uses Walker
  1423. * @since 2.7.0
  1424. */
  1425. class Walker_Comment extends Walker {
  1426. /**
  1427. * What the class handles.
  1428. *
  1429. * @see Walker::$tree_type
  1430. *
  1431. * @since 2.7.0
  1432. * @var string
  1433. */
  1434. public $tree_type = 'comment';
  1435. /**
  1436. * DB fields to use.
  1437. *
  1438. * @see Walker::$db_fields
  1439. *
  1440. * @since 2.7.0
  1441. * @var array
  1442. */
  1443. public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
  1444. /**
  1445. * Start the list before the elements are added.
  1446. *
  1447. * @see Walker::start_lvl()
  1448. *
  1449. * @since 2.7.0
  1450. *
  1451. * @param string $output Passed by reference. Used to append additional content.
  1452. * @param int $depth Depth of comment.
  1453. * @param array $args Uses 'style' argument for type of HTML list.
  1454. */
  1455. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  1456. $GLOBALS['comment_depth'] = $depth + 1;
  1457. switch ( $args['style'] ) {
  1458. case 'div':
  1459. break;
  1460. case 'ol':
  1461. $output .= '<ol class="children">' . "\n";
  1462. break;
  1463. case 'ul':
  1464. default:
  1465. $output .= '<ul class="children">' . "\n";
  1466. break;
  1467. }
  1468. }
  1469. /**
  1470. * End the list of items after the elements are added.
  1471. *
  1472. * @see Walker::end_lvl()
  1473. *
  1474. * @since 2.7.0
  1475. *
  1476. * @param string $output Passed by reference. Used to append additional content.
  1477. * @param int $depth Depth of comment.
  1478. * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
  1479. */
  1480. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  1481. $GLOBALS['comment_depth'] = $depth + 1;
  1482. switch ( $args['style'] ) {
  1483. case 'div':
  1484. break;
  1485. case 'ol':
  1486. $output .= "</ol><!-- .children -->\n";
  1487. break;
  1488. case 'ul':
  1489. default:
  1490. $output .= "</ul><!-- .children -->\n";
  1491. break;
  1492. }
  1493. }
  1494. /**
  1495. * Traverse elements to create list from elements.
  1496. *
  1497. * This function is designed to enhance Walker::display_element() to
  1498. * display children of higher nesting levels than selected inline on
  1499. * the highest depth level displayed. This prevents them being orphaned
  1500. * at the end of the comment list.
  1501. *
  1502. * Example: max_depth = 2, with 5 levels of nested content.
  1503. * 1
  1504. * 1.1
  1505. * 1.1.1
  1506. * 1.1.1.1
  1507. * 1.1.1.1.1
  1508. * 1.1.2
  1509. * 1.1.2.1
  1510. * 2
  1511. * 2.2
  1512. *
  1513. * @see Walker::display_element()
  1514. * @see wp_list_comments()
  1515. *
  1516. * @since 2.7.0
  1517. *
  1518. * @param object $element Data object.
  1519. * @param array $children_elements List of elements to continue traversing.
  1520. * @param int $max_depth Max depth to traverse.
  1521. * @param int $depth Depth of current element.
  1522. * @param array $args An array of arguments.
  1523. * @param string $output Passed by reference. Used to append additional content.
  1524. * @return null Null on failure with no changes to parameters.
  1525. */
  1526. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  1527. if ( !$element )
  1528. return;
  1529. $id_field = $this->db_fields['id'];
  1530. $id = $element->$id_field;
  1531. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  1532. // If we're at the max depth, and the current element still has children, loop over those and display them at this level
  1533. // This is to prevent them being orphaned to the end of the list.
  1534. if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
  1535. foreach ( $children_elements[ $id ] as $child )
  1536. $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
  1537. unset( $children_elements[ $id ] );
  1538. }
  1539. }
  1540. /**
  1541. * Start the element output.
  1542. *
  1543. * @since 2.7.0
  1544. *
  1545. * @see Walker::start_el()
  1546. * @see wp_list_comments()
  1547. *
  1548. * @param string $output Passed by reference. Used to append additional content.
  1549. * @param object $comment Comment data object.
  1550. * @param int $depth Depth of comment in reference to parents.
  1551. * @param array $args An array of arguments.
  1552. */
  1553. public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
  1554. $depth++;
  1555. $GLOBALS['comment_depth'] = $depth;
  1556. $GLOBALS['comment'] = $comment;
  1557. if ( !empty( $args['callback'] ) ) {
  1558. ob_start();
  1559. call_user_func( $args['callback'], $comment, $args, $depth );
  1560. $output .= ob_get_clean();
  1561. return;
  1562. }
  1563. if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
  1564. ob_start();
  1565. $this->ping( $comment, $depth, $args );
  1566. $output .= ob_get_clean();
  1567. } elseif ( 'html5' === $args['format'] ) {
  1568. ob_start();
  1569. $this->html5_comment( $comment, $depth, $args );
  1570. $output .= ob_get_clean();
  1571. } else {
  1572. ob_start();
  1573. $this->comment( $comment, $depth, $args );
  1574. $output .= ob_get_clean();
  1575. }
  1576. }
  1577. /**
  1578. * Ends the element output, if needed.
  1579. *
  1580. * @since 2.7.0
  1581. *
  1582. * @see Walker::end_el()
  1583. * @see wp_list_comments()
  1584. *
  1585. * @param string $output Passed by reference. Used to append additional content.
  1586. * @param object $comment The comment object. Default current comment.
  1587. * @param int $depth Depth of comment.
  1588. * @param array $args An array of arguments.
  1589. */
  1590. public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
  1591. if ( !empty( $args['end-callback'] ) ) {
  1592. ob_start();
  1593. call_user_func( $args['end-callback'], $comment, $args, $depth );
  1594. $output .= ob_get_clean();
  1595. return;
  1596. }
  1597. if ( 'div' == $args['style'] )
  1598. $output .= "</div><!-- #comment-## -->\n";
  1599. else
  1600. $output .= "</li><!-- #comment-## -->\n";
  1601. }
  1602. /**
  1603. * Output a pingback comment.
  1604. *
  1605. * @access protected
  1606. * @since 3.6.0
  1607. *
  1608. * @see wp_list_comments()
  1609. *
  1610. * @param object $comment The comment object.
  1611. * @param int $depth Depth of comment.
  1612. * @param array $args An array of arguments.
  1613. */
  1614. protected function ping( $comment, $depth, $args ) {
  1615. $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
  1616. ?>
  1617. <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
  1618. <div class="comment-body">
  1619. <?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  1620. </div>
  1621. <?php
  1622. }
  1623. /**
  1624. * Output a single comment.
  1625. *
  1626. * @access protected
  1627. * @since 3.6.0
  1628. *
  1629. * @see wp_list_comments()
  1630. *
  1631. * @param object $comment Comment to display.
  1632. * @param int $depth Depth of comment.
  1633. * @param array $args An array of arguments.
  1634. */
  1635. protected function comment( $comment, $depth, $args ) {
  1636. if ( 'div' == $args['style'] ) {
  1637. $tag = 'div';
  1638. $add_below = 'comment';
  1639. } else {
  1640. $tag = 'li';
  1641. $add_below = 'div-comment';
  1642. }
  1643. ?>
  1644. <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '' ); ?> id="comment-<?php comment_ID(); ?>">
  1645. <?php if ( 'div' != $args['style'] ) : ?>
  1646. <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  1647. <?php endif; ?>
  1648. <div class="comment-author vcard">
  1649. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  1650. <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
  1651. </div>
  1652. <?php if ( '0' == $comment->comment_approved ) : ?>
  1653. <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
  1654. <br />
  1655. <?php endif; ?>
  1656. <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  1657. <?php
  1658. /* translators: 1: date, 2: time */
  1659. printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '&nbsp;&nbsp;', '' );
  1660. ?>
  1661. </div>
  1662. <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  1663. <div class="reply">
  1664. <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  1665. </div>
  1666. <?php if ( 'div' != $args['style'] ) : ?>
  1667. </div>
  1668. <?php endif; ?>
  1669. <?php
  1670. }
  1671. /**
  1672. * Output a comment in the HTML5 format.
  1673. *
  1674. * @access protected
  1675. * @since 3.6.0
  1676. *
  1677. * @see wp_list_comments()
  1678. *
  1679. * @param object $comment Comment to display.
  1680. * @param int $depth Depth of comment.
  1681. * @param array $args An array of arguments.
  1682. */
  1683. protected function html5_comment( $comment, $depth, $args ) {
  1684. $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
  1685. ?>
  1686. <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '' ); ?>>
  1687. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  1688. <footer class="comment-meta">
  1689. <div class="comment-author vcard">
  1690. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  1691. <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link() ) ); ?>
  1692. </div><!-- .comment-author -->
  1693. <div class="comment-metadata">
  1694. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  1695. <time datetime="<?php comment_time( 'c' ); ?>">
  1696. <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?>
  1697. </time>
  1698. </a>
  1699. <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
  1700. </div><!-- .comment-metadata -->
  1701. <?php if ( '0' == $comment->comment_approved ) : ?>
  1702. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
  1703. <?php endif; ?>
  1704. </footer><!-- .comment-meta -->
  1705. <div class="comment-content">
  1706. <?php comment_text(); ?>
  1707. </div><!-- .comment-content -->
  1708. <div class="reply">
  1709. <?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  1710. </div><!-- .reply -->
  1711. </article><!-- .comment-body -->
  1712. <?php
  1713. }
  1714. }
  1715. /**
  1716. * List comments.
  1717. *
  1718. * Used in the comments.php template to list comments for a particular post.
  1719. *
  1720. * @since 2.7.0
  1721. *
  1722. * @see WP_Query->comments
  1723. *
  1724. * @param string|array $args {
  1725. * Optional. Formatting options.
  1726. *
  1727. * @type object $walker Instance of a Walker class to list comments. Default null.
  1728. * @type int $max_depth The maximum comments depth. Default empty.
  1729. * @type string $style The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
  1730. * @type string $callback Callback function to use. Default null.
  1731. * @type string $end-callback Callback function to use at the end. Default null.
  1732. * @type string $type Type of comments to list.
  1733. * Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
  1734. * @type int $page Page ID to list comments for. Default empty.
  1735. * @type int $per_page Number of comments to list per page. Default empty.
  1736. * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
  1737. * @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts 'desc', 'asc'.
  1738. * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
  1739. * @type string $format How to format the comments list.
  1740. * Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
  1741. * @type bool $short_ping Whether to output short pings. Default false.
  1742. * @type bool $echo Whether to echo the output or return it. Default true.
  1743. * }
  1744. * @param array $comments Optional. Array of comment objects.
  1745. */
  1746. function wp_list_comments( $args = array(), $comments = null ) {
  1747. global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
  1748. $in_comment_loop = true;
  1749. $comment_alt = $comment_thread_alt = 0;
  1750. $comment_depth = 1;
  1751. $defaults = array(
  1752. 'walker' => null,
  1753. 'max_depth' => '',
  1754. 'style' => 'ul',
  1755. 'callback' => null,
  1756. 'end-callback' => null,
  1757. 'type' => 'all',
  1758. 'page' => '',
  1759. 'per_page' => '',
  1760. 'avatar_size' => 32,
  1761. 'reverse_top_level' => null,
  1762. 'reverse_children' => '',
  1763. 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
  1764. 'short_ping' => false,
  1765. 'echo' => true,
  1766. );
  1767. $r = wp_parse_args( $args, $defaults );
  1768. /**
  1769. * Filter the arguments used in retrieving the comment list.
  1770. *
  1771. * @since 4.0.0
  1772. *
  1773. * @see wp_list_comments()
  1774. *
  1775. * @param array $r An array of arguments for displaying comments.
  1776. */
  1777. $r = apply_filters( 'wp_list_comments_args', $r );
  1778. // Figure out what comments we'll be looping through ($_comments)
  1779. if ( null !== $comments ) {
  1780. $comments = (array) $comments;
  1781. if ( empty($comments) )
  1782. return;
  1783. if ( 'all' != $r['type'] ) {
  1784. $comments_by_type = separate_comments($comments);
  1785. if ( empty($comments_by_type[$r['type']]) )
  1786. return;
  1787. $_comments = $comments_by_type[$r['type']];
  1788. } else {
  1789. $_comments = $comments;
  1790. }
  1791. } else {
  1792. if ( empty($wp_query->comments) )
  1793. return;
  1794. if ( 'all' != $r['type'] ) {
  1795. if ( empty($wp_query->comments_by_type) )
  1796. $wp_query->comments_by_type = separate_comments($wp_query->comments);
  1797. if ( empty($wp_query->comments_by_type[$r['type']]) )
  1798. return;
  1799. $_comments = $wp_query->comments_by_type[$r['type']];
  1800. } else {
  1801. $_comments = $wp_query->comments;
  1802. }
  1803. }
  1804. if ( '' === $r['per_page'] && get_option('page_comments') )
  1805. $r['per_page'] = get_query_var('comments_per_page');
  1806. if ( empty($r['per_page']) ) {
  1807. $r['per_page'] = 0;
  1808. $r['page'] = 0;
  1809. }
  1810. if ( '' === $r['max_depth'] ) {
  1811. if ( get_option('thread_comments') )
  1812. $r['max_depth'] = get_option('thread_comments_depth');
  1813. else
  1814. $r['max_depth'] = -1;
  1815. }
  1816. if ( '' === $r['page'] ) {
  1817. if ( empty($overridden_cpage) ) {
  1818. $r['page'] = get_query_var('cpage');
  1819. } else {
  1820. $threaded = ( -1 != $r['max_depth'] );
  1821. $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
  1822. set_query_var( 'cpage', $r['page'] );
  1823. }
  1824. }
  1825. // Validation check
  1826. $r['page'] = intval($r['page']);
  1827. if ( 0 == $r['page'] && 0 != $r['per_page'] )
  1828. $r['page'] = 1;
  1829. if ( null === $r['reverse_top_level'] )
  1830. $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
  1831. if ( empty( $r['walker'] ) ) {
  1832. $walker = new Walker_Comment;
  1833. } else {
  1834. $walker = $r['walker'];
  1835. }
  1836. $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r );
  1837. $wp_query->max_num_comment_pages = $walker->max_pages;
  1838. $in_comment_loop = false;
  1839. if ( $r['echo'] ) {
  1840. echo $output;
  1841. } else {
  1842. return $output;
  1843. }
  1844. }
  1845. /**
  1846. * Output a complete commenting form for use within a template.
  1847. *
  1848. * Most strings and form fields may be controlled through the $args array passed
  1849. * into the function, while you may also choose to use the comment_form_default_fields
  1850. * filter to modify the array of default fields if you'd just like to add a new
  1851. * one or remove a single field. All fields are also individually passed through
  1852. * a filter of the form comment_form_field_$name where $name is the key used
  1853. * in the array of fields.
  1854. *
  1855. * @since 3.0.0
  1856. *
  1857. * @param array $args {
  1858. * Optional. Default arguments and form fields to override.
  1859. *
  1860. * @type array $fields {
  1861. * Default comment fields, filterable by default via the 'comment_form_default_fields' hook.
  1862. *
  1863. * @type string $author Comment author field HTML.
  1864. * @type string $email Comment author email field HTML.
  1865. * @type string $url Comment author URL field HTML.
  1866. * }
  1867. * @type string $comment_field The comment textarea field HTML.
  1868. * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
  1869. * @type string $logged_in_as HTML element for a 'logged in as <user>' message.
  1870. * @type string $comment_notes_before HTML element for a message displayed before the comment form.
  1871. * Default 'Your email address will not be published.'.
  1872. * @type string $comment_notes_after HTML element for a message displayed after the comment form.
  1873. * Default 'You may use these HTML tags and attributes ...'.
  1874. * @type string $id_form The comment form element id attribute. Default 'commentform'.
  1875. * @type string $id_submit The comment submit element id attribute. Default 'submit'.
  1876. * @type string $name_submit The comment submit element name attribute. Default 'submit'.
  1877. * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
  1878. * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
  1879. * where %s is the author of the comment being replied to.
  1880. * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
  1881. * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
  1882. * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
  1883. * }
  1884. * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
  1885. */
  1886. function comment_form( $args = array(), $post_id = null ) {
  1887. if ( null === $post_id )
  1888. $post_id = get_the_ID();
  1889. $commenter = wp_get_current_commenter();
  1890. $user = wp_get_current_user();
  1891. $user_identity = $user->exists() ? $user->display_name : '';
  1892. $args = wp_parse_args( $args );
  1893. if ( ! isset( $args['format'] ) )
  1894. $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
  1895. $req = get_option( 'require_name_email' );
  1896. $aria_req = ( $req ? " aria-required='true'" : '' );
  1897. $html5 = 'html5' === $args['format'];
  1898. $fields = array(
  1899. 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  1900. '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
  1901. 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
  1902. '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
  1903. 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
  1904. '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
  1905. );
  1906. $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
  1907. /**
  1908. * Filter the default comment form fields.
  1909. *
  1910. * @since 3.0.0
  1911. *
  1912. * @param array $fields The default comment fields.
  1913. */
  1914. $fields = apply_filters( 'comment_form_default_fields', $fields );
  1915. $defaults = array(
  1916. 'fields' => $fields,
  1917. '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>',
  1918. /** This filter is documented in wp-includes/link-template.php */
  1919. '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>',
  1920. /** This filter is documented in wp-includes/link-template.php */
  1921. '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>',
  1922. 'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
  1923. '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>',
  1924. 'id_form' => 'commentform',
  1925. 'id_submit' => 'submit',
  1926. 'name_submit' => 'submit',
  1927. 'title_reply' => __( 'Leave a Reply' ),
  1928. 'title_reply_to' => __( 'Leave a Reply to %s' ),
  1929. 'cancel_reply_link' => __( 'Cancel reply' ),
  1930. 'label_submit' => __( 'Post Comment' ),
  1931. 'format' => 'xhtml',
  1932. );
  1933. /**
  1934. * Filter the comment form default arguments.
  1935. *
  1936. * Use 'comment_form_default_fields' to filter the comment fields.
  1937. *
  1938. * @since 3.0.0
  1939. *
  1940. * @param array $defaults The default comment form arguments.
  1941. */
  1942. $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
  1943. ?>
  1944. <?php if ( comments_open( $post_id ) ) : ?>
  1945. <?php
  1946. /**
  1947. * Fires before the comment form.
  1948. *
  1949. * @since 3.0.0
  1950. */
  1951. do_action( 'comment_form_before' );
  1952. ?>
  1953. <div id="respond" class="comment-respond">
  1954. <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>
  1955. <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
  1956. <?php echo $args['must_log_in']; ?>
  1957. <?php
  1958. /**
  1959. * Fires after the HTML-formatted 'must log in after' message in the comment form.
  1960. *
  1961. * @since 3.0.0
  1962. */
  1963. do_action( 'comment_form_must_log_in_after' );
  1964. ?>
  1965. <?php else : ?>
  1966. <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' : ''; ?>>
  1967. <?php
  1968. /**
  1969. * Fires at the top of the comment form, inside the <form> tag.
  1970. *
  1971. * @since 3.0.0
  1972. */
  1973. do_action( 'comment_form_top' );
  1974. ?>
  1975. <?php if ( is_user_logged_in() ) : ?>
  1976. <?php
  1977. /**
  1978. * Filter the 'logged in' message for the comment form for display.
  1979. *
  1980. * @since 3.0.0
  1981. *
  1982. * @param string $args_logged_in The logged-in-as HTML-formatted message.
  1983. * @param array $commenter An array containing the comment author's
  1984. * username, email, and URL.
  1985. * @param string $user_identity If the commenter is a registered user,
  1986. * the display name, blank otherwise.
  1987. */
  1988. echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
  1989. ?>
  1990. <?php
  1991. /**
  1992. * Fires after the is_user_logged_in() check in the comment form.
  1993. *
  1994. * @since 3.0.0
  1995. *
  1996. * @param array $commenter An array containing the comment author's
  1997. * username, email, and URL.
  1998. * @param string $user_identity If the commenter is a registered user,
  1999. * the display name, blank otherwise.
  2000. */
  2001. do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
  2002. ?>
  2003. <?php else : ?>
  2004. <?php echo $args['comment_notes_before']; ?>
  2005. <?php
  2006. /**
  2007. * Fires before the comment fields in the comment form.
  2008. *
  2009. * @since 3.0.0
  2010. */
  2011. do_action( 'comment_form_before_fields' );
  2012. foreach ( (array) $args['fields'] as $name => $field ) {
  2013. /**
  2014. * Filter a comment form field for display.
  2015. *
  2016. * The dynamic portion of the filter hook, $name, refers to the name
  2017. * of the comment form field. Such as 'author', 'email', or 'url'.
  2018. *
  2019. * @since 3.0.0
  2020. *
  2021. * @param string $field The HTML-formatted output of the comment form field.
  2022. */
  2023. echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
  2024. }
  2025. /**
  2026. * Fires after the comment fields in the comment form.
  2027. *
  2028. * @since 3.0.0
  2029. */
  2030. do_action( 'comment_form_after_fields' );
  2031. ?>
  2032. <?php endif; ?>
  2033. <?php
  2034. /**
  2035. * Filter the content of the comment textarea field for display.
  2036. *
  2037. * @since 3.0.0
  2038. *
  2039. * @param string $args_comment_field The content of the comment textarea field.
  2040. */
  2041. echo apply_filters( 'comment_form_field_comment', $args['comment_field'] );
  2042. ?>
  2043. <?php echo $args['comment_notes_after']; ?>
  2044. <p class="form-submit">
  2045. <input name="<?php echo esc_attr( $args['name_submit'] ); ?>" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
  2046. <?php comment_id_fields( $post_id ); ?>
  2047. </p>
  2048. <?php
  2049. /**
  2050. * Fires at the bottom of the comment form, inside the closing </form> tag.
  2051. *
  2052. * @since 1.5.0
  2053. *
  2054. * @param int $post_id The post ID.
  2055. */
  2056. do_action( 'comment_form', $post_id );
  2057. ?>
  2058. </form>
  2059. <?php endif; ?>
  2060. </div><!-- #respond -->
  2061. <?php
  2062. /**
  2063. * Fires after the comment form.
  2064. *
  2065. * @since 3.0.0
  2066. */
  2067. do_action( 'comment_form_after' );
  2068. else :
  2069. /**
  2070. * Fires after the comment form if comments are closed.
  2071. *
  2072. * @since 3.0.0
  2073. */
  2074. do_action( 'comment_form_comments_closed' );
  2075. endif;
  2076. }