PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/comment-template.php

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