home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / comment-template.php < prev    next >
Encoding:
PHP Script  |  2017-08-20  |  85.4 KB  |  2,468 lines

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