PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/comment-template.php

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