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

/wp-includes/comment-template.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 2364 lines | 901 code | 208 blank | 1255 comment | 197 complexity | e13c077043f6407e90fa25ec16f67493 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0

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. $author = apply_filters( 'comment_author', $author, $comment_ID );
  62. echo $author;
  63. }
  64. /**
  65. * Retrieve the email of the author of the current comment.
  66. *
  67. * @since 1.5.0
  68. *
  69. * @param int $comment_ID Optional. The ID of the comment for which to get the author's email. Default current comment.
  70. * @return string The current comment author's email
  71. */
  72. function get_comment_author_email( $comment_ID = 0 ) {
  73. $comment = get_comment( $comment_ID );
  74. /**
  75. * Filter the comment author's returned email address.
  76. *
  77. * @since 1.5.0
  78. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  79. *
  80. * @param string $comment_author_email The comment author's email address.
  81. * @param int $comment_ID The comment ID.
  82. * @param object $comment The comment object.
  83. */
  84. return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment_ID, $comment );
  85. }
  86. /**
  87. * Display the email of the author of the current global $comment.
  88. *
  89. * Care should be taken to protect the email address and assure that email
  90. * harvesters do not capture your commentors' email address. Most assume that
  91. * their email address will not appear in raw form on the blog. Doing so will
  92. * enable anyone, including those that people don't want to get the email
  93. * address and use it for their own means good and bad.
  94. *
  95. * @since 0.71
  96. *
  97. * @param int $comment_ID Optional. The ID of the comment for which to print the author's email. Default current comment.
  98. */
  99. function comment_author_email( $comment_ID = 0 ) {
  100. $author_email = get_comment_author_email( $comment_ID );
  101. /**
  102. * Filter the comment author's email for display.
  103. *
  104. * @since 1.2.0
  105. * @since 4.1.0 The `$comment_ID` parameter was added.
  106. *
  107. * @param string $author_email The comment author's email address.
  108. * @param int $comment_ID The comment ID.
  109. */
  110. echo apply_filters( 'author_email', $author_email, $comment_ID );
  111. }
  112. /**
  113. * Display the html email link to the author of the current comment.
  114. *
  115. * Care should be taken to protect the email address and assure that email
  116. * harvesters do not capture your commentors' email address. Most assume that
  117. * their email address will not appear in raw form on the blog. Doing so will
  118. * enable anyone, including those that people don't want to get the email
  119. * address and use it for their own means good and bad.
  120. *
  121. * @since 0.71
  122. *
  123. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  124. * Default empty.
  125. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  126. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  127. */
  128. function comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
  129. if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
  130. echo $link;
  131. }
  132. /**
  133. * Return the html email link to the author of the current comment.
  134. *
  135. * Care should be taken to protect the email address and assure that email
  136. * harvesters do not capture your commentors' email address. Most assume that
  137. * their email address will not appear in raw form on the blog. Doing so will
  138. * enable anyone, including those that people don't want to get the email
  139. * address and use it for their own means good and bad.
  140. *
  141. * @global object $comment The current Comment row object.
  142. *
  143. * @since 2.7.0
  144. *
  145. * @param string $linktext Optional. Text to display instead of the comment author's email address.
  146. * Default empty.
  147. * @param string $before Optional. Text or HTML to display before the email link. Default empty.
  148. * @param string $after Optional. Text or HTML to display after the email link. Default empty.
  149. */
  150. function get_comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
  151. global $comment;
  152. /**
  153. * Filter the comment author's email for display.
  154. *
  155. * Care should be taken to protect the email address and assure that email
  156. * harvesters do not capture your commenter's email address.
  157. *
  158. * @since 1.2.0
  159. * @since 4.1.0 The `$comment` parameter was added.
  160. *
  161. * @param string $comment_author_email The comment author's email address.
  162. * @param object $comment The comment object.
  163. */
  164. $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
  165. if ((!empty($email)) && ($email != '@')) {
  166. $display = ($linktext != '') ? $linktext : $email;
  167. $return = $before;
  168. $return .= "<a href='mailto:$email'>$display</a>";
  169. $return .= $after;
  170. return $return;
  171. } else {
  172. return '';
  173. }
  174. }
  175. /**
  176. * Retrieve the HTML link to the URL of the author of the current comment.
  177. *
  178. * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
  179. * which falls back to the global comment variable if the $comment_ID argument is empty.
  180. *
  181. * @since 1.5.0
  182. *
  183. * @param int $comment_ID ID of the comment for which to get the author's link.
  184. * Default current comment.
  185. * @return string The comment author name or HTML link for author's URL.
  186. */
  187. function get_comment_author_link( $comment_ID = 0 ) {
  188. $url = get_comment_author_url( $comment_ID );
  189. $author = get_comment_author( $comment_ID );
  190. if ( empty( $url ) || 'http://' == $url )
  191. $return = $author;
  192. else
  193. $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
  194. /**
  195. * Filter the comment author's link for display.
  196. *
  197. * @since 1.5.0
  198. * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
  199. *
  200. * @param string $return The HTML-formatted comment author link.
  201. * Empty for an invalid URL.
  202. * @param string $author The comment author's username.
  203. * @param int $comment_ID The comment ID.
  204. */
  205. return apply_filters( 'get_comment_author_link', $return, $author, $comment_ID );
  206. }
  207. /**
  208. * Display the html link to the url of the author of the current comment.
  209. *
  210. * @since 0.71
  211. *
  212. * @see get_comment_author_link() Echoes result
  213. *
  214. * @param int $comment_ID ID of the comment for which to print the author's
  215. * link. Default current comment.
  216. */
  217. function comment_author_link( $comment_ID = 0 ) {
  218. echo get_comment_author_link( $comment_ID );
  219. }
  220. /**
  221. * Retrieve the IP address of the author of the current comment.
  222. *
  223. * @since 1.5.0
  224. *
  225. * @param int $comment_ID ID of the comment for which to get the author's IP
  226. * address. Default current comment.
  227. * @return string Comment author's IP address.
  228. */
  229. function get_comment_author_IP( $comment_ID = 0 ) {
  230. $comment = get_comment( $comment_ID );
  231. /**
  232. * Filter the comment author's returned IP address.
  233. *
  234. * @since 1.5.0
  235. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  236. *
  237. * @param string $comment_author_IP The comment author's IP address.
  238. * @param int $comment_ID The comment ID.
  239. * @param object $comment The comment object.
  240. */
  241. return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment_ID, $comment );
  242. }
  243. /**
  244. * Display the IP address of the author of the current comment.
  245. *
  246. * @since 0.71
  247. *
  248. * @param int $comment_ID ID of the comment for which to print the author's IP
  249. * address. Default current comment.
  250. */
  251. function comment_author_IP( $comment_ID = 0 ) {
  252. echo get_comment_author_IP( $comment_ID );
  253. }
  254. /**
  255. * Retrieve the url of the author of the current comment.
  256. *
  257. * @since 1.5.0
  258. *
  259. * @param int $comment_ID ID of the comment for which to get the author's URL.
  260. * Default current comment.
  261. * @return string
  262. */
  263. function get_comment_author_url( $comment_ID = 0 ) {
  264. $comment = get_comment( $comment_ID );
  265. $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
  266. $url = esc_url( $url, array('http', 'https') );
  267. /**
  268. * Filter the comment author's URL.
  269. *
  270. * @since 1.5.0
  271. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  272. *
  273. * @param string $url The comment author's URL.
  274. * @param int $comment_ID The comment ID.
  275. * @param object $comment The comment object.
  276. */
  277. return apply_filters( 'get_comment_author_url', $url, $comment_ID, $comment );
  278. }
  279. /**
  280. * Display the url of the author of the current comment.
  281. *
  282. * @since 0.71
  283. *
  284. * @param int $comment_ID ID of the comment for which to print the author's URL.
  285. * Default current comment.
  286. */
  287. function comment_author_url( $comment_ID = 0 ) {
  288. $author_url = get_comment_author_url( $comment_ID );
  289. /**
  290. * Filter the comment author's URL for display.
  291. *
  292. * @since 1.2.0
  293. * @since 4.1.0 The `$comment_ID` parameter was added.
  294. *
  295. * @param string $author_url The comment author's URL.
  296. * @param int $comment_ID The comment ID.
  297. */
  298. echo apply_filters( 'comment_url', $author_url, $comment_ID );
  299. }
  300. /**
  301. * Retrieves the HTML link of the url of the author of the current comment.
  302. *
  303. * $linktext parameter is only used if the URL does not exist for the comment
  304. * author. If the URL does exist then the URL will be used and the $linktext
  305. * will be ignored.
  306. *
  307. * Encapsulate the HTML link between the $before and $after. So it will appear
  308. * in the order of $before, link, and finally $after.
  309. *
  310. * @since 1.5.0
  311. *
  312. * @param string $linktext Optional. The text to display instead of the comment
  313. * author's email address. Default empty.
  314. * @param string $before Optional. The text or HTML to display before the email link.
  315. * Default empty.
  316. * @param string $after Optional. The text or HTML to display after the email link.
  317. * Default empty.
  318. * @return string The HTML link between the $before and $after parameters.
  319. */
  320. function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  321. $url = get_comment_author_url();
  322. $display = ($linktext != '') ? $linktext : $url;
  323. $display = str_replace( 'http://www.', '', $display );
  324. $display = str_replace( 'http://', '', $display );
  325. if ( '/' == substr($display, -1) ) {
  326. $display = substr($display, 0, -1);
  327. }
  328. $return = "$before<a href='$url' rel='external'>$display</a>$after";
  329. /**
  330. * Filter the comment author's returned URL link.
  331. *
  332. * @since 1.5.0
  333. *
  334. * @param string $return The HTML-formatted comment author URL link.
  335. */
  336. return apply_filters( 'get_comment_author_url_link', $return );
  337. }
  338. /**
  339. * Displays the HTML link of the url of the author of the current comment.
  340. *
  341. * @since 0.71
  342. *
  343. * @param string $linktext Optional. Text to display instead of the comment author's
  344. * email address. Default empty.
  345. * @param string $before Optional. Text or HTML to display before the email link.
  346. * Default empty.
  347. * @param string $after Optional. Text or HTML to display after the email link.
  348. * Default empty.
  349. */
  350. function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  351. echo get_comment_author_url_link( $linktext, $before, $after );
  352. }
  353. /**
  354. * Generates semantic classes for each comment element.
  355. *
  356. * @since 2.7.0
  357. *
  358. * @param string|array $class Optional. One or more classes to add to the class list.
  359. * Default empty.
  360. * @param int $comment_id Comment ID. Default current comment.
  361. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  362. * @param bool $echo Optional. Whether to cho or return the output.
  363. * Default true.
  364. */
  365. function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
  366. // Separates classes with a single space, collates classes for comment DIV
  367. $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
  368. if ( $echo)
  369. echo $class;
  370. else
  371. return $class;
  372. }
  373. /**
  374. * Returns the classes for the comment div as an array.
  375. *
  376. * @since 2.7.0
  377. *
  378. * @param string|array $class Optional. One or more classes to add to the class list. Default empty.
  379. * @param int $comment_id Comment ID. Default current comment.
  380. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  381. * @return array An array of classes.
  382. */
  383. function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
  384. global $comment_alt, $comment_depth, $comment_thread_alt;
  385. $comment = get_comment($comment_id);
  386. $classes = array();
  387. // Get the comment type (comment, trackback),
  388. $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
  389. // If the comment author has an id (registered), then print the log in name
  390. if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
  391. // For all registered users, 'byuser'
  392. $classes[] = 'byuser';
  393. $classes[] = 'comment-author-' . sanitize_html_class($user->user_nicename, $comment->user_id);
  394. // For comment authors who are the author of the post
  395. if ( $post = get_post($post_id) ) {
  396. if ( $comment->user_id === $post->post_author )
  397. $classes[] = 'bypostauthor';
  398. }
  399. }
  400. if ( empty($comment_alt) )
  401. $comment_alt = 0;
  402. if ( empty($comment_depth) )
  403. $comment_depth = 1;
  404. if ( empty($comment_thread_alt) )
  405. $comment_thread_alt = 0;
  406. if ( $comment_alt % 2 ) {
  407. $classes[] = 'odd';
  408. $classes[] = 'alt';
  409. } else {
  410. $classes[] = 'even';
  411. }
  412. $comment_alt++;
  413. // Alt for top-level comments
  414. if ( 1 == $comment_depth ) {
  415. if ( $comment_thread_alt % 2 ) {
  416. $classes[] = 'thread-odd';
  417. $classes[] = 'thread-alt';
  418. } else {
  419. $classes[] = 'thread-even';
  420. }
  421. $comment_thread_alt++;
  422. }
  423. $classes[] = "depth-$comment_depth";
  424. if ( !empty($class) ) {
  425. if ( !is_array( $class ) )
  426. $class = preg_split('#\s+#', $class);
  427. $classes = array_merge($classes, $class);
  428. }
  429. $classes = array_map('esc_attr', $classes);
  430. /**
  431. * Filter the returned CSS classes for the current comment.
  432. *
  433. * @since 2.7.0
  434. *
  435. * @param array $classes An array of comment classes.
  436. * @param string $class A comma-separated list of additional classes added to the list.
  437. * @param int $comment_id The comment id.
  438. * @param object $comment The comment
  439. * @param int|WP_Post $post_id The post ID or WP_Post object.
  440. */
  441. return apply_filters( 'comment_class', $classes, $class, $comment_id, $comment, $post_id );
  442. }
  443. /**
  444. * Retrieve the comment date of the current comment.
  445. *
  446. * @since 1.5.0
  447. *
  448. * @param string $d Optional. The format of the date. Default user's setting.
  449. * @param int $comment_ID ID of the comment for which to get the date. Default current comment.
  450. * @return string The comment's date.
  451. */
  452. function get_comment_date( $d = '', $comment_ID = 0 ) {
  453. $comment = get_comment( $comment_ID );
  454. if ( '' == $d )
  455. $date = mysql2date(get_option('date_format'), $comment->comment_date);
  456. else
  457. $date = mysql2date($d, $comment->comment_date);
  458. /**
  459. * Filter the returned comment date.
  460. *
  461. * @since 1.5.0
  462. *
  463. * @param string|int $date Formatted date string or Unix timestamp.
  464. * @param string $d The format of the date.
  465. * @param object $comment The comment object.
  466. */
  467. return apply_filters( 'get_comment_date', $date, $d, $comment );
  468. }
  469. /**
  470. * Display the comment date of the current comment.
  471. *
  472. * @since 0.71
  473. *
  474. * @param string $d Optional. The format of the date. Default user's settings.
  475. * @param int $comment_ID ID of the comment for which to print the date. Default current comment.
  476. */
  477. function comment_date( $d = '', $comment_ID = 0 ) {
  478. echo get_comment_date( $d, $comment_ID );
  479. }
  480. /**
  481. * Retrieve the excerpt of the current comment.
  482. *
  483. * Will cut each word and only output the first 20 words with '&hellip;' at the end.
  484. * If the word count is less than 20, then no truncating is done and no '&hellip;'
  485. * will appear.
  486. *
  487. * @since 1.5.0
  488. *
  489. * @param int $comment_ID ID of the comment for which to get the excerpt.
  490. * Default current comment.
  491. * @return string The maybe truncated comment with 20 words or less.
  492. */
  493. function get_comment_excerpt( $comment_ID = 0 ) {
  494. $comment = get_comment( $comment_ID );
  495. $comment_text = strip_tags($comment->comment_content);
  496. $blah = explode(' ', $comment_text);
  497. if (count($blah) > 20) {
  498. $k = 20;
  499. $use_dotdotdot = 1;
  500. } else {
  501. $k = count($blah);
  502. $use_dotdotdot = 0;
  503. }
  504. $excerpt = '';
  505. for ($i=0; $i<$k; $i++) {
  506. $excerpt .= $blah[$i] . ' ';
  507. }
  508. $excerpt .= ($use_dotdotdot) ? '&hellip;' : '';
  509. /**
  510. * Filter the retrieved comment excerpt.
  511. *
  512. * @since 1.5.0
  513. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  514. *
  515. * @param string $excerpt The comment excerpt text.
  516. * @param int $comment_ID The comment ID.
  517. * @param object $comment The comment object.
  518. */
  519. return apply_filters( 'get_comment_excerpt', $excerpt, $comment_ID, $comment );
  520. }
  521. /**
  522. * Display the excerpt of the current comment.
  523. *
  524. * @since 1.2.0
  525. *
  526. * @param int $comment_ID ID of the comment for which to print the excerpt.
  527. * Default current comment.
  528. */
  529. function comment_excerpt( $comment_ID = 0 ) {
  530. $comment_excerpt = get_comment_excerpt($comment_ID);
  531. /**
  532. * Filter the comment excerpt for display.
  533. *
  534. * @since 1.2.0
  535. * @since 4.1.0 The `$comment_ID` parameter was added.
  536. *
  537. * @param string $comment_excerpt The comment excerpt text.
  538. * @param int $comment_ID The comment ID.
  539. */
  540. echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment_ID );
  541. }
  542. /**
  543. * Retrieve the comment id of the current comment.
  544. *
  545. * @since 1.5.0
  546. *
  547. * @return int The comment ID.
  548. */
  549. function get_comment_ID() {
  550. global $comment;
  551. /**
  552. * Filter the returned comment ID.
  553. *
  554. * @since 1.5.0
  555. * @since 4.1.0 The `$comment_ID` parameter was added.
  556. *
  557. * @param int $comment_ID The current comment ID.
  558. * @param object $comment The comment object.
  559. */
  560. return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment );
  561. }
  562. /**
  563. * Display the comment id of the current comment.
  564. *
  565. * @since 0.71
  566. */
  567. function comment_ID() {
  568. echo get_comment_ID();
  569. }
  570. /**
  571. * Retrieve the link to a given comment.
  572. *
  573. * @since 1.5.0
  574. *
  575. * @see get_page_of_comment()
  576. *
  577. * @param mixed $comment Comment to retrieve. Default current comment.
  578. * @param array $args Optional. An array of arguments to override the defaults.
  579. * @return string The permalink to the given comment.
  580. */
  581. function get_comment_link( $comment = null, $args = array() ) {
  582. global $wp_rewrite, $in_comment_loop;
  583. $comment = get_comment($comment);
  584. // Backwards compat
  585. if ( ! is_array( $args ) ) {
  586. $args = array( 'page' => $args );
  587. }
  588. $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
  589. $args = wp_parse_args( $args, $defaults );
  590. if ( '' === $args['per_page'] && get_option('page_comments') )
  591. $args['per_page'] = get_option('comments_per_page');
  592. if ( empty($args['per_page']) ) {
  593. $args['per_page'] = 0;
  594. $args['page'] = 0;
  595. }
  596. if ( $args['per_page'] ) {
  597. if ( '' == $args['page'] )
  598. $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
  599. if ( $wp_rewrite->using_permalinks() )
  600. $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
  601. else
  602. $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
  603. } else {
  604. $link = get_permalink( $comment->comment_post_ID );
  605. }
  606. $link = $link . '#comment-' . $comment->comment_ID;
  607. /**
  608. * Filter the returned single comment permalink.
  609. *
  610. * @since 2.8.0
  611. *
  612. * @see get_page_of_comment()
  613. *
  614. * @param string $link The comment permalink with '#comment-$id' appended.
  615. * @param object $comment The current comment object.
  616. * @param array $args An array of arguments to override the defaults.
  617. */
  618. return apply_filters( 'get_comment_link', $link, $comment, $args );
  619. }
  620. /**
  621. * Retrieve the link to the current post comments.
  622. *
  623. * @since 1.5.0
  624. *
  625. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  626. * @return string The link to the comments.
  627. */
  628. function get_comments_link( $post_id = 0 ) {
  629. $comments_link = get_permalink( $post_id ) . '#comments';
  630. /**
  631. * Filter the returned post comments permalink.
  632. *
  633. * @since 3.6.0
  634. *
  635. * @param string $comments_link Post comments permalink with '#comments' appended.
  636. * @param int|WP_Post $post_id Post ID or WP_Post object.
  637. */
  638. return apply_filters( 'get_comments_link', $comments_link, $post_id );
  639. }
  640. /**
  641. * Display the link to the current post comments.
  642. *
  643. * @since 0.71
  644. *
  645. * @param string $deprecated Not Used.
  646. * @param string $deprecated_2 Not Used.
  647. */
  648. function comments_link( $deprecated = '', $deprecated_2 = '' ) {
  649. if ( !empty( $deprecated ) )
  650. _deprecated_argument( __FUNCTION__, '0.72' );
  651. if ( !empty( $deprecated_2 ) )
  652. _deprecated_argument( __FUNCTION__, '1.3' );
  653. echo esc_url( get_comments_link() );
  654. }
  655. /**
  656. * Retrieve the amount of comments a post has.
  657. *
  658. * @since 1.5.0
  659. *
  660. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  661. * @return int The number of comments a post has.
  662. */
  663. function get_comments_number( $post_id = 0 ) {
  664. $post = get_post( $post_id );
  665. if ( ! $post ) {
  666. $count = 0;
  667. } else {
  668. $count = $post->comment_count;
  669. $post_id = $post->ID;
  670. }
  671. /**
  672. * Filter the returned comment count for a post.
  673. *
  674. * @since 1.5.0
  675. *
  676. * @param int $count Number of comments a post has.
  677. * @param int $post_id Post ID.
  678. */
  679. return apply_filters( 'get_comments_number', $count, $post_id );
  680. }
  681. /**
  682. * Display the language string for the number of comments the current post has.
  683. *
  684. * @since 0.71
  685. *
  686. * @param string $zero Optional. Text for no comments. Default false.
  687. * @param string $one Optional. Text for one comment. Default false.
  688. * @param string $more Optional. Text for more than one comment. Default false.
  689. * @param string $deprecated Not used.
  690. */
  691. function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
  692. if ( ! empty( $deprecated ) ) {
  693. _deprecated_argument( __FUNCTION__, '1.3' );
  694. }
  695. echo get_comments_number_text( $zero, $one, $more );
  696. }
  697. /**
  698. * Display the language string for the number of comments the current post has.
  699. *
  700. * @since 4.0.0
  701. *
  702. * @param string $zero Optional. Text for no comments. Default false.
  703. * @param string $one Optional. Text for one comment. Default false.
  704. * @param string $more Optional. Text for more than one comment. Default false.
  705. */
  706. function get_comments_number_text( $zero = false, $one = false, $more = false ) {
  707. $number = get_comments_number();
  708. if ( $number > 1 ) {
  709. $output = str_replace( '%', number_format_i18n( $number ), ( false === $more ) ? __( '% Comments' ) : $more );
  710. } elseif ( $number == 0 ) {
  711. $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
  712. } else { // must be one
  713. $output = ( false === $one ) ? __( '1 Comment' ) : $one;
  714. }
  715. /**
  716. * Filter the comments count for display.
  717. *
  718. * @since 1.5.0
  719. *
  720. * @see _n()
  721. *
  722. * @param string $output A translatable string formatted based on whether the count
  723. * is equal to 0, 1, or 1+.
  724. * @param int $number The number of post comments.
  725. */
  726. return apply_filters( 'comments_number', $output, $number );
  727. }
  728. /**
  729. * Retrieve the text of the current comment.
  730. *
  731. * @since 1.5.0
  732. *
  733. * @see Walker_Comment::comment()
  734. *
  735. * @param int $comment_ID ID of the comment for which to get the text. Default current comment.
  736. * @param array $args Optional. An array of arguments. Default empty.
  737. * @return string The comment content.
  738. */
  739. function get_comment_text( $comment_ID = 0, $args = array() ) {
  740. $comment = get_comment( $comment_ID );
  741. /**
  742. * Filter the text of a comment.
  743. *
  744. * @since 1.5.0
  745. *
  746. * @see Walker_Comment::comment()
  747. *
  748. * @param string $comment_content Text of the comment.
  749. * @param object $comment The comment object.
  750. * @param array $args An array of arguments.
  751. */
  752. return apply_filters( 'get_comment_text', $comment->comment_content, $comment, $args );
  753. }
  754. /**
  755. * Display the text of the current comment.
  756. *
  757. * @since 0.71
  758. *
  759. * @see Walker_Comment::comment()
  760. *
  761. * @param int $comment_ID ID of the comment for which to print the text. Default 0.
  762. * @param array $args Optional. An array of arguments. Default empty array. Default empty.
  763. */
  764. function comment_text( $comment_ID = 0, $args = array() ) {
  765. $comment = get_comment( $comment_ID );
  766. $comment_text = get_comment_text( $comment_ID , $args );
  767. /**
  768. * Filter the text of a comment to be displayed.
  769. *
  770. * @since 1.2.0
  771. *
  772. * @see Walker_Comment::comment()
  773. *
  774. * @param string $comment_text Text of the current comment.
  775. * @param object $comment The comment object.
  776. * @param array $args An array of arguments.
  777. */
  778. echo apply_filters( 'comment_text', $comment_text, $comment, $args );
  779. }
  780. /**
  781. * Retrieve the comment time of the current comment.
  782. *
  783. * @since 1.5.0
  784. *
  785. * @param string $d Optional. The format of the time. Default user's settings.
  786. * @param bool $gmt Optional. Whether to use the GMT date. Default false.
  787. * @param bool $translate Optional. Whether to translate the time (for use in feeds).
  788. * Default true.
  789. * @return string The formatted time.
  790. */
  791. function get_comment_time( $d = '', $gmt = false, $translate = true ) {
  792. global $comment;
  793. $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
  794. if ( '' == $d )
  795. $date = mysql2date(get_option('time_format'), $comment_date, $translate);
  796. else
  797. $date = mysql2date($d, $comment_date, $translate);
  798. /**
  799. * Filter the returned comment time.
  800. *
  801. * @since 1.5.0
  802. *
  803. * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
  804. * @param string $d Date format.
  805. * @param bool $gmt Whether the GMT date is in use.
  806. * @param bool $translate Whether the time is translated.
  807. * @param object $comment The comment object.
  808. */
  809. return apply_filters( 'get_comment_time', $date, $d, $gmt, $translate, $comment );
  810. }
  811. /**
  812. * Display the comment time of the current comment.
  813. *
  814. * @since 0.71
  815. *
  816. * @param string $d Optional. The format of the time. Default user's settings.
  817. */
  818. function comment_time( $d = '' ) {
  819. echo get_comment_time($d);
  820. }
  821. /**
  822. * Retrieve the comment type of the current comment.
  823. *
  824. * @since 1.5.0
  825. *
  826. * @param int $comment_ID ID of the comment for which to get the type. Default current comment.
  827. * @return string The comment type.
  828. */
  829. function get_comment_type( $comment_ID = 0 ) {
  830. $comment = get_comment( $comment_ID );
  831. if ( '' == $comment->comment_type )
  832. $comment->comment_type = 'comment';
  833. /**
  834. * Filter the returned comment type.
  835. *
  836. * @since 1.5.0
  837. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  838. *
  839. * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
  840. * @param int $comment_ID The comment ID.
  841. * @param object $comment The comment object.
  842. */
  843. return apply_filters( 'get_comment_type', $comment->comment_type, $comment_ID, $comment );
  844. }
  845. /**
  846. * Display the comment type of the current comment.
  847. *
  848. * @since 0.71
  849. *
  850. * @param string $commenttxt Optional. String to display for comment type. Default false.
  851. * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
  852. * @param string $pingbacktxt Optional. String to display for pingback type. Default false.
  853. */
  854. function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
  855. if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
  856. if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
  857. if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
  858. $type = get_comment_type();
  859. switch( $type ) {
  860. case 'trackback' :
  861. echo $trackbacktxt;
  862. break;
  863. case 'pingback' :
  864. echo $pingbacktxt;
  865. break;
  866. default :
  867. echo $commenttxt;
  868. }
  869. }
  870. /**
  871. * Retrieve The current post's trackback URL.
  872. *
  873. * There is a check to see if permalink's have been enabled and if so, will
  874. * retrieve the pretty path. If permalinks weren't enabled, the ID of the
  875. * current post is used and appended to the correct page to go to.
  876. *
  877. * @since 1.5.0
  878. *
  879. * @return string The trackback URL after being filtered.
  880. */
  881. function get_trackback_url() {
  882. if ( '' != get_option('permalink_structure') )
  883. $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
  884. else
  885. $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . get_the_ID();
  886. /**
  887. * Filter the returned trackback URL.
  888. *
  889. * @since 2.2.0
  890. *
  891. * @param string $tb_url The trackback URL.
  892. */
  893. return apply_filters( 'trackback_url', $tb_url );
  894. }
  895. /**
  896. * Display the current post's trackback URL.
  897. *
  898. * @since 0.71
  899. *
  900. * @param bool $deprecated_echo Not used.
  901. * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
  902. * for the result instead.
  903. */
  904. function trackback_url( $deprecated_echo = true ) {
  905. if ( $deprecated_echo !== true )
  906. _deprecated_argument( __FUNCTION__, '2.5', __('Use <code>get_trackback_url()</code> instead if you do not want the value echoed.') );
  907. if ( $deprecated_echo )
  908. echo get_trackback_url();
  909. else
  910. return get_trackback_url();
  911. }
  912. /**
  913. * Generate and display the RDF for the trackback information of current post.
  914. *
  915. * Deprecated in 3.0.0, and restored in 3.0.1.
  916. *
  917. * @since 0.71
  918. *
  919. * @param int $deprecated Not used (Was $timezone = 0).
  920. */
  921. function trackback_rdf( $deprecated = '' ) {
  922. if ( ! empty( $deprecated ) ) {
  923. _deprecated_argument( __FUNCTION__, '2.5' );
  924. }
  925. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
  926. return;
  927. }
  928. echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  929. xmlns:dc="http://purl.org/dc/elements/1.1/"
  930. xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  931. <rdf:Description rdf:about="';
  932. the_permalink();
  933. echo '"'."\n";
  934. echo ' dc:identifier="';
  935. the_permalink();
  936. echo '"'."\n";
  937. echo ' dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  938. echo ' trackback:ping="'.get_trackback_url().'"'." />\n";
  939. echo '</rdf:RDF>';
  940. }
  941. /**
  942. * Whether the current post is open for comments.
  943. *
  944. * @since 1.5.0
  945. *
  946. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  947. * @return bool True if the comments are open.
  948. */
  949. function comments_open( $post_id = null ) {
  950. $_post = get_post($post_id);
  951. $open = ( 'open' == $_post->comment_status );
  952. /**
  953. * Filter whether the current post is open for comments.
  954. *
  955. * @since 2.5.0
  956. *
  957. * @param bool $open Whether the current post is open for comments.
  958. * @param int|WP_Post $post_id The post ID or WP_Post object.
  959. */
  960. return apply_filters( 'comments_open', $open, $post_id );
  961. }
  962. /**
  963. * Whether the current post is open for pings.
  964. *
  965. * @since 1.5.0
  966. *
  967. * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  968. * @return bool True if pings are accepted
  969. */
  970. function pings_open( $post_id = null ) {
  971. $_post = get_post($post_id);
  972. $open = ( 'open' == $_post->ping_status );
  973. /**
  974. * Filter whether the current post is open for pings.
  975. *
  976. * @since 2.5.0
  977. *
  978. * @param bool $open Whether the current post is open for pings.
  979. * @param int|WP_Post $post_id The post ID or WP_Post object.
  980. */
  981. return apply_filters( 'pings_open', $open, $post_id );
  982. }
  983. /**
  984. * Display form token for unfiltered comments.
  985. *
  986. * Will only display nonce token if the current user has permissions for
  987. * unfiltered html. Won't display the token for other users.
  988. *
  989. * The function was backported to 2.0.10 and was added to versions 2.1.3 and
  990. * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
  991. * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
  992. *
  993. * Backported to 2.0.10.
  994. *
  995. * @since 2.1.3
  996. */
  997. function wp_comment_form_unfiltered_html_nonce() {
  998. $post = get_post();
  999. $post_id = $post ? $post->ID : 0;
  1000. if ( current_user_can( 'unfiltered_html' ) ) {
  1001. wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
  1002. echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
  1003. }
  1004. }
  1005. /**
  1006. * Load the comment template specified in $file.
  1007. *
  1008. * Will not display the comments template if not on single post or page, or if
  1009. * the post does not have comments.
  1010. *
  1011. * Uses the WordPress database object to query for the comments. The comments
  1012. * are passed through the 'comments_array' filter hook with the list of comments
  1013. * and the post ID respectively.
  1014. *
  1015. * The $file path is passed through a filter hook called, 'comments_template'
  1016. * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  1017. * first and if it fails it will require the default comment template from the
  1018. * default theme. If either does not exist, then the WordPress process will be
  1019. * halted. It is advised for that reason, that the default theme is not deleted.
  1020. *
  1021. * @todo Document globals
  1022. * @uses $withcomments Will not try to get the comments if the post has none.
  1023. *
  1024. * @since 1.5.0
  1025. *
  1026. * @param string $file Optional. The file to load. Default '/comments.php'.
  1027. * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  1028. * Default false.
  1029. * @return null Returns null if no comments appear.
  1030. */
  1031. function comments_template( $file = '/comments.php', $separate_comments = false ) {
  1032. global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
  1033. if ( !(is_single() || is_page() || $withcomments) || empty($post) )
  1034. return;
  1035. if ( empty($file) )
  1036. $file = '/comments.php';
  1037. $req = get_option('require_name_email');
  1038. /*
  1039. * Comment author information fetched from the comment cookies.
  1040. * Uuses wp_get_current_commenter().
  1041. */
  1042. $commenter = wp_get_current_commenter();
  1043. /*
  1044. * The name of the current comment author escaped for use in attributes.
  1045. * Escaped by sanitize_comment_cookies().
  1046. */
  1047. $comment_author = $commenter['comment_author'];
  1048. /*
  1049. * The email address of the current comment author escaped for use in attributes.
  1050. * Escaped by sanitize_comment_cookies().
  1051. */
  1052. $comment_author_email = $commenter['comment_author_email'];
  1053. /*
  1054. * The url of the current comment author escaped for use in attributes.
  1055. */
  1056. $comment_author_url = esc_url($commenter['comment_author_url']);
  1057. $comment_args = array(
  1058. 'order' => 'ASC',
  1059. 'orderby' => 'comment_date_gmt',
  1060. 'status' => 'approve',
  1061. 'post_id' => $post->ID,
  1062. );
  1063. if ( $user_ID ) {
  1064. $comment_args['include_unapproved'] = array( $user_ID );
  1065. } else if ( ! empty( $comment_author_email ) ) {
  1066. $comment_args['include_unapproved'] = array( $comment_author_email );
  1067. }
  1068. $comments = get_comments( $comment_args );
  1069. /**
  1070. * Filter the comments array.
  1071. *
  1072. * @since 2.1.0
  1073. *
  1074. * @param array $comments Array of comments supplied to the comments template.
  1075. * @param int $post_ID Post ID.
  1076. */
  1077. $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
  1078. $comments = &$wp_query->comments;
  1079. $wp_query->comment_count = count($wp_query->comments);
  1080. update_comment_cache($wp_query->comments);
  1081. if ( $separate_comments ) {
  1082. $wp_query->comments_by_type = separate_comments($comments);
  1083. $comments_by_type = &$wp_query->comments_by_type;
  1084. }
  1085. $overridden_cpage = false;
  1086. if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
  1087. set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
  1088. $overridden_cpage = true;
  1089. }
  1090. if ( !defined('COMMENTS_TEMPLATE') )
  1091. define('COMMENTS_TEMPLATE', true);
  1092. $theme_template = STYLESHEETPATH . $file;
  1093. /**
  1094. * Filter the path to the theme template file used for the comments template.
  1095. *
  1096. * @since 1.5.1
  1097. *
  1098. * @param string $theme_template The path to the theme template file.
  1099. */
  1100. $include = apply_filters( 'comments_template', $theme_template );
  1101. if ( file_exists( $include ) )
  1102. require( $include );
  1103. elseif ( file_exists( TEMPLATEPATH . $file ) )
  1104. require( TEMPLATEPATH . $file );
  1105. else // Backward compat code will be removed in a future release
  1106. require( ABSPATH . WPINC . '/theme-compat/comments.php');
  1107. }
  1108. /**
  1109. * Display the JS popup script to show a comment.
  1110. *
  1111. * If the $file parameter is empty, then the home page is assumed. The defaults
  1112. * for the window are 400px by 400px.
  1113. *
  1114. * For the comment link popup to work, this function has to be called or the
  1115. * normal comment link will be assumed.
  1116. *
  1117. * @global string $wpcommentspopupfile The URL to use for the popup window.
  1118. * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
  1119. *
  1120. * @since 0.71
  1121. *
  1122. * @param int $width Optional. The width of the popup window. Default 400.
  1123. * @param int $height Optional. The height of the popup window. Default 400.
  1124. * @param string $file Optional. Sets the location of the popup window.
  1125. */
  1126. function comments_popup_script( $width = 400, $height = 400, $file = '' ) {
  1127. global $wpcommentspopupfile, $wpcommentsjavascript;
  1128. if (empty ($file)) {
  1129. $wpcommentspopupfile = ''; // Use the index.
  1130. } else {
  1131. $wpcommentspopupfile = $file;
  1132. }
  1133. $wpcommentsjavascript = 1;
  1134. $javascript = "<script type='text/javascript'>\nfunction wpopen (macagna) {\n window.open(macagna, '_blank', 'width=$width,height=$height,scrollbars=yes,status=yes');\n}\n</script>\n";
  1135. echo $javascript;
  1136. }
  1137. /**
  1138. * Displays the link to the comments popup window for the current post ID.
  1139. *
  1140. * Is not meant to be displayed on single posts and pages. Should be used
  1141. * on the lists of posts
  1142. *
  1143. * @global string $wpcommentspopupfile The URL to use for the popup window.
  1144. * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
  1145. *
  1146. * @since 0.71
  1147. *
  1148. * @param string $zero Optional. String to display when no comments. Default false.
  1149. * @param string $one Optional. String to display when only one comment is available.
  1150. * Default false.
  1151. * @param string $more Optional. String to display when there are more than one comment.
  1152. * Default false.
  1153. * @param string $css_class Optional. CSS class to use for comments. Default empty.
  1154. * @param string $none Optional. String to display when comments have been turned off.
  1155. * Default false.
  1156. * @return null Returns null on single posts and pages.
  1157. */
  1158. function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
  1159. global $wpcommentspopupfile, $wpcommentsjavascript;
  1160. $id = get_the_ID();
  1161. if ( false === $zero ) $zero = __( 'No Comments' );
  1162. if ( false === $one ) $one = __( '1 Comment' );
  1163. if ( false === $more ) $more = __( '% Comments' );
  1164. if ( false === $none ) $none = __( 'Comments Off' );
  1165. $number = get_comments_number( $id );
  1166. if ( 0 == $number && !comments_open() && !pings_open() ) {
  1167. echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
  1168. return;
  1169. }
  1170. if ( post_password_required() ) {
  1171. echo __('Enter your password to view comments.');
  1172. return;
  1173. }
  1174. echo '<a href="';
  1175. if ( $wpcommentsjavascript ) {
  1176. if ( empty( $wpcommentspopupfile ) )
  1177. $home = home_url();
  1178. else
  1179. $home = get_option('siteurl');
  1180. echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
  1181. echo '" onclick="wpopen(this.href); return false"';
  1182. } else { // if comments_popup_script() is not in the template, display simple comment link
  1183. if ( 0 == $number )
  1184. echo get_permalink() . '#respond';
  1185. else
  1186. comments_link();
  1187. echo '"';
  1188. }
  1189. if ( !empty( $css_class ) ) {
  1190. echo ' class="'.$css_class.'" ';
  1191. }
  1192. $title = the_title_attribute( array('echo' => 0 ) );
  1193. $attributes = '';
  1194. /**
  1195. * Filter the comments popup link attributes for display.
  1196. *
  1197. * @since 2.5.0
  1198. *
  1199. * @param string $attributes The comments popup link attributes. Default empty.
  1200. */
  1201. echo apply_filters( 'comments_popup_link_attributes', $attributes );
  1202. echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
  1203. comments_number( $zero, $one, $more );
  1204. echo '</a>';
  1205. }
  1206. /**
  1207. * Retrieve HTML content for reply to comment link.
  1208. *
  1209. * @since 2.7.0
  1210. *
  1211. * @param array $args {
  1212. * Optional. Override default arguments.
  1213. *
  1214. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1215. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1216. * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
  1217. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1218. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1219. * Default 'respond'.
  1220. * @type string $reply_text The text of the Reply link. Default 'Reply'.
  1221. * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
  1222. * @type int $depth' The depth of the new comment. Must be greater than 0 and less than the value
  1223. * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
  1224. * @type string $before The text or HTML to add before the reply link. Default empty.
  1225. * @type string $after The text or HTML to add after the reply link. Default empty.
  1226. * }
  1227. * @param int $comment Comment being replied to. Default current comment.
  1228. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1229. * Default current post.
  1230. * @return null|false|string Link to show comment form, if successful. False, if comments are closed.
  1231. */
  1232. function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
  1233. $defaults = array(
  1234. 'add_below' => 'comment',
  1235. 'respond_id' => 'respond',
  1236. 'reply_text' => __( 'Reply' ),
  1237. 'reply_to_text' => __( 'Reply to %s' ),
  1238. 'login_text' => __( 'Log in to Reply' ),
  1239. 'depth' => 0,
  1240. 'before' => '',
  1241. 'after' => ''
  1242. );
  1243. $args = wp_parse_args( $args, $defaults );
  1244. if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
  1245. return;
  1246. }
  1247. $comment = get_comment( $comment );
  1248. if ( empty( $post ) ) {
  1249. $post = $comment->comment_post_ID;
  1250. }
  1251. $post = get_post( $post );
  1252. if ( ! comments_open( $post->ID ) ) {
  1253. return false;
  1254. }
  1255. /**
  1256. * Filter the comment reply link arguments.
  1257. *
  1258. * @since 4.1.0
  1259. *
  1260. * @param array $args Comment reply link arguments. See {@see get_comment_reply_link()}
  1261. * for more information on accepted arguments.
  1262. * @param object $comment The object of the comment being replied to.
  1263. * @param WP_Post $post The {@see WP_Post} object.
  1264. */
  1265. $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
  1266. if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
  1267. $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
  1268. esc_url( wp_login_url( get_permalink() ) ),
  1269. $args['login_text']
  1270. );
  1271. } else {
  1272. $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
  1273. $args['add_below'], $comment->comment_ID, $args['respond_id'], $post->ID
  1274. );
  1275. $link = sprintf( "<a class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
  1276. esc_url( add_query_arg( 'replytocom', $comment->comment_ID ) ) . "#" . $args['respond_id'],
  1277. $onclick,
  1278. esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
  1279. $args['reply_text']
  1280. );
  1281. }
  1282. /**
  1283. * Filter the comment reply link.
  1284. *
  1285. * @since 2.7.0
  1286. *
  1287. * @param string $link The HTML markup for the comment reply link.
  1288. * @param array $args An array of arguments overriding the defaults.
  1289. * @param object $comment The object of the comment being replied.
  1290. * @param WP_Post $post The WP_Post object.
  1291. */
  1292. return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
  1293. }
  1294. /**
  1295. * Displays the HTML content for reply to comment link.
  1296. *
  1297. * @since 2.7.0
  1298. *
  1299. * @see get_comment_reply_link()
  1300. *
  1301. * @param array $args Optional. Override default options.
  1302. * @param int $comment Comment being replied to. Default current comment.
  1303. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
  1304. * Default current post.
  1305. * @return mixed Link to show comment form, if successful. False, if comments are closed.
  1306. */
  1307. function comment_reply_link($args = array(), $comment = null, $post = null) {
  1308. echo get_comment_reply_link($args, $comment, $post);
  1309. }
  1310. /**
  1311. * Retrieve HTML content for reply to post link.
  1312. *
  1313. * @since 2.7.0
  1314. *
  1315. * @param array $args {
  1316. * Optional. Override default arguments.
  1317. *
  1318. * @type string $add_below The first part of the selector used to identify the comment to respond below.
  1319. * The resulting value is passed as the first parameter to addComment.moveForm(),
  1320. * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
  1321. * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
  1322. * to addComment.moveForm(), and appended to the link URL as a hash value.
  1323. * Default 'respond'.
  1324. * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
  1325. * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
  1326. * @type string $before Text or HTML to add before the reply link. Default empty.
  1327. * @type string $after Text or HTML to add after the reply link. Default empty.
  1328. * }
  1329. * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
  1330. * Default current post.
  1331. * @return false|null|string Link to show comment form, if successful. False, if comments are closed.
  1332. */
  1333. function get_post_reply_link($args = array(), $post = null) {
  1334. $defaults = array(
  1335. 'add_below' => 'post',
  1336. 'respond_id' => 'respond',
  1337. 'reply_text' => __('Leave a Comment'),
  1338. 'login_text' => __('Log in to leave a Comment'),
  1339. 'before' => '',
  1340. 'after' => '',
  1341. );
  1342. $args = wp_parse_args($args, $defaults);
  1343. $post = get_post($post);
  1344. if ( ! comments_open( $post->ID ) ) {
  1345. return false;
  1346. }
  1347. if ( get_option('comment_registration') && ! is_user_logged_in() ) {
  1348. $link = sprintf( '<a rel="nofollow" href="%s">%s</a>',
  1349. wp_login_url( get_permalink() ),
  1350. $args['login_text']
  1351. );
  1352. } else {
  1353. $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
  1354. $args['add_below'], $post->ID, $args['respond_id']
  1355. );
  1356. $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
  1357. get_permalink( $post->ID ) . '#' . $args['respond_id'],
  1358. $onclick,
  1359. $args['reply_text']
  1360. );
  1361. }
  1362. $formatted_link = $args['before'] . $link . $args['after'];
  1363. /**
  1364. * Filter the formatted post comments link HTML.
  1365. *
  1366. * @since 2.7.0
  1367. *
  1368. * @param string $formatted The HTML-formatted post comments link.
  1369. * @param int|WP_Post $post The post ID or WP_Post object.
  1370. */
  1371. return apply_filters( 'post_comments_link', $formatted_link, $post );
  1372. }
  1373. /**
  1374. * Displays the HTML content for reply to post link.
  1375. *
  1376. * @since 2.7.0
  1377. *
  1378. * @see get_post_reply_link()
  1379. *
  1380. * @param array $args Optional. Override default options,
  1381. * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be dis…

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