PageRenderTime 68ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/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

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Comment template functions
  4. *
  5. * These functions are meant to live inside of the WordPress loop.
  6. *
  7. * @package WordPress
  8. * @subpackage Template
  9. */
  10. /**
  11. * Retrieve the author of the current comment.
  12. *
  13. * If the comment has an empty comment_author field, then 'Anonymous' person is
  14. * assumed.
  15. *
  16. * @since 1.5.0
  17. *
  18. * @param int $comment_ID Optional. The ID of the comment for which to retrieve the author. Default current comment.
  19. * @return string The comment author
  20. */
  21. function get_comment_author( $comment_ID = 0 ) {
  22. $comment = get_comment( $comment_ID );
  23. if ( empty( $comment->comment_author ) ) {
  24. if ( $comment->user_id && $user = get_userdata( $comment->user_id ) )
  25. $author = $user->display_name;
  26. else
  27. $author = __('Anonymous');
  28. } else {
  29. $author = $comment->comment_author;
  30. }
  31. /**
  32. * Filter the returned comment author name.
  33. *
  34. * @since 1.5.0
  35. * @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…

Large files files are truncated, but you can click here to view the full file