Advertisement
prettysenshi

comment-template.php

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