+/**
+ * Retrieve HTML content for reply to comment link.
+ *
+ * @since 2.7.0
+ * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
+ *
+ * @param array $args {
+ * Optional. Override default arguments.
+ *
+ * @type string $add_below The first part of the selector used to identify the comment to respond below.
+ * The resulting value is passed as the first parameter to addComment.moveForm(),
+ * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
+ * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
+ * to addComment.moveForm(), and appended to the link URL as a hash value.
+ * Default 'respond'.
+ * @type string $reply_text The text of the Reply link. Default 'Reply'.
+ * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
+ * @type int $depth' The depth of the new comment. Must be greater than 0 and less than the value
+ * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
+ * @type string $before The text or HTML to add before the reply link. Default empty.
+ * @type string $after The text or HTML to add after the reply link. Default empty.
+ * }
+ * @param int|WP_Comment $comment Comment being replied to. Default current comment.
+ * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
+ * Default current post.
+ * @return void|false|string Link to show comment form, if successful. False, if comments are closed.
+ */
+function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
+ $defaults = array(
+ 'add_below' => 'comment',
+ 'respond_id' => 'respond',
+ 'reply_text' => __( 'Reply' ),
+ 'reply_to_text' => __( 'Reply to %s' ),
+ 'login_text' => __( 'Log in to Reply' ),
+ 'depth' => 0,
+ 'before' => '',
+ 'after' => ''
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+
+ if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
+ return;
+ }
+
+ $comment = get_comment( $comment );
+
+ if ( empty( $post ) ) {
+ $post = $comment->comment_post_ID;
+ }
+
+ $post = get_post( $post );
+
+ if ( ! comments_open( $post->ID ) ) {
+ return false;
+ }
+
+ /**
+ * Filter the comment reply link arguments.
+ *
+ * @since 4.1.0
+ *
+ * @param array $args Comment reply link arguments. See get_comment_reply_link()
+ * for more information on accepted arguments.
+ * @param WP_Comment $comment The object of the comment being replied to.
+ * @param WP_Post $post The WP_Post object.
+ */
+ $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
+
+ if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
+ $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
+ esc_url( wp_login_url( get_permalink() ) ),
+ $args['login_text']
+ );
+ } else {
+ $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
+ $args['add_below'], $comment->comment_ID, $args['respond_id'], $post->ID
+ );
+
+ $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
+ esc_url( add_query_arg( 'replytocom', $comment->comment_ID, get_permalink( $post->ID ) ) ) . "#" . $args['respond_id'],
+ $onclick,
+ esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
+ $args['reply_text']
+ );
+ }
+
+ /**
+ * Filter the comment reply link.
+ *
+ * @since 2.7.0
+ *
+ * @param string $link The HTML markup for the comment reply link.
+ * @param array $args An array of arguments overriding the defaults.
+ * @param object $comment The object of the comment being replied.
+ * @param WP_Post $post The WP_Post object.
+ */
+ return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
+}
+
+/**
+ * Displays the HTML content for reply to comment link.
+ *
+ * @since 2.7.0
+ *
+ * @see get_comment_reply_link()
+ *
+ * @param array $args Optional. Override default options.
+ * @param int $comment Comment being replied to. Default current comment.
+ * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
+ * Default current post.
+ * @return mixed Link to show comment form, if successful. False, if comments are closed.
+ */
+function comment_reply_link($args = array(), $comment = null, $post = null) {
+ echo get_comment_reply_link($args, $comment, $post);
+}
+
+/**
+ * Retrieve HTML content for reply to post link.
+ *
+ * @since 2.7.0
+ *
+ * @param array $args {
+ * Optional. Override default arguments.
+ *
+ * @type string $add_below The first part of the selector used to identify the comment to respond below.
+ * The resulting value is passed as the first parameter to addComment.moveForm(),
+ * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
+ * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
+ * to addComment.moveForm(), and appended to the link URL as a hash value.
+ * Default 'respond'.
+ * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
+ * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
+ * @type string $before Text or HTML to add before the reply link. Default empty.
+ * @type string $after Text or HTML to add after the reply link. Default empty.
+ * }
+ * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
+ * Default current post.
+ * @return false|null|string Link to show comment form, if successful. False, if comments are closed.
+ */
+function get_post_reply_link($args = array(), $post = null) {
+ $defaults = array(
+ 'add_below' => 'post',
+ 'respond_id' => 'respond',
+ 'reply_text' => __('Leave a Comment'),
+ 'login_text' => __('Log in to leave a Comment'),
+ 'before' => '',
+ 'after' => '',
+ );
+
+ $args = wp_parse_args($args, $defaults);
+
+ $post = get_post($post);
+
+ if ( ! comments_open( $post->ID ) ) {
+ return false;
+ }
+
+ if ( get_option('comment_registration') && ! is_user_logged_in() ) {
+ $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
+ wp_login_url( get_permalink() ),
+ $args['login_text']
+ );
+ } else {
+ $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
+ $args['add_below'], $post->ID, $args['respond_id']
+ );
+
+ $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
+ get_permalink( $post->ID ) . '#' . $args['respond_id'],
+ $onclick,
+ $args['reply_text']
+ );
+ }
+ $formatted_link = $args['before'] . $link . $args['after'];
+
+ /**
+ * Filter the formatted post comments link HTML.
+ *
+ * @since 2.7.0
+ *
+ * @param string $formatted The HTML-formatted post comments link.
+ * @param int|WP_Post $post The post ID or WP_Post object.
+ */
+ return apply_filters( 'post_comments_link', $formatted_link, $post );
+}
+
+/**
+ * Displays the HTML content for reply to post link.
+ *
+ * @since 2.7.0
+ *
+ * @see get_post_reply_link()
+ *
+ * @param array $args Optional. Override default options,
+ * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
+ * Default current post.
+ * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
+ */
+function post_reply_link($args = array(), $post = null) {
+ echo get_post_reply_link($args, $post);
+}
+
+/**
+ * Retrieve HTML content for cancel comment reply link.
+ *
+ * @since 2.7.0
+ *
+ * @param string $text Optional. Text to display for cancel reply link. Default empty.
+ * @return string
+ */
+function get_cancel_comment_reply_link( $text = '' ) {
+ if ( empty($text) )
+ $text = __('Click here to cancel reply.');
+
+ $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
+ $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
+
+ $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
+
+ /**
+ * Filter the cancel comment reply link HTML.
+ *
+ * @since 2.7.0
+ *
+ * @param string $formatted_link The HTML-formatted cancel comment reply link.
+ * @param string $link Cancel comment reply link URL.
+ * @param string $text Cancel comment reply link text.
+ */
+ return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
+}
+
+/**
+ * Display HTML content for cancel comment reply link.
+ *
+ * @since 2.7.0
+ *
+ * @param string $text Optional. Text to display for cancel reply link. Default empty.
+ */
+function cancel_comment_reply_link( $text = '' ) {
+ echo get_cancel_comment_reply_link($text);
+}
+
+/**
+ * Retrieve hidden input HTML for replying to comments.
+ *
+ * @since 3.0.0
+ *
+ * @param int $id Optional. Post ID. Default current post ID.
+ * @return string Hidden input HTML for replying to comments
+ */
+function get_comment_id_fields( $id = 0 ) {
+ if ( empty( $id ) )
+ $id = get_the_ID();
+
+ $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
+ $result = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
+ $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
+
+ /**
+ * Filter the returned comment id fields.
+ *
+ * @since 3.0.0
+ *
+ * @param string $result The HTML-formatted hidden id field comment elements.
+ * @param int $id The post ID.
+ * @param int $replytoid The id of the comment being replied to.
+ */
+ return apply_filters( 'comment_id_fields', $result, $id, $replytoid );
+}
+
+/**
+ * Output hidden input HTML for replying to comments.
+ *
+ * @since 2.7.0
+ *
+ * @param int $id Optional. Post ID. Default current post ID.
+ */
+function comment_id_fields( $id = 0 ) {
+ echo get_comment_id_fields( $id );
+}
+
+/**
+ * Display text based on comment reply status.
+ *
+ * Only affects users with JavaScript disabled.
+ *
+ * @since 2.7.0
+ *
+ * @param string $noreplytext Optional. Text to display when not replying to a comment.
+ * Default false.
+ * @param string $replytext Optional. Text to display when replying to a comment.
+ * Default false. Accepts "%s" for the author of the comment
+ * being replied to.
+ * @param string $linktoparent Optional. Boolean to control making the author's name a link
+ * to their comment. Default true.
+ */
+function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) {
+ $comment = get_comment();
+
+ if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
+ if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
+
+ $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
+
+ if ( 0 == $replytoid )
+ echo $noreplytext;
+ else {
+ $comment = get_comment($replytoid);
+ $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author( $comment ) . '</a>' : get_comment_author( $comment );
+ printf( $replytext, $author );
+ }
+}
+
+/**
+ * List comments.
+ *
+ * Used in the comments.php template to list comments for a particular post.
+ *
+ * @since 2.7.0
+ *
+ * @see WP_Query->comments
+ *
+ * @global WP_Query $wp_query
+ * @global int $comment_alt
+ * @global int $comment_depth
+ * @global int $comment_thread_alt
+ * @global bool $overridden_cpage
+ * @global bool $in_comment_loop
+ *
+ * @param string|array $args {
+ * Optional. Formatting options.
+ *
+ * @type object $walker Instance of a Walker class to list comments. Default null.
+ * @type int $max_depth The maximum comments depth. Default empty.
+ * @type string $style The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
+ * @type string $callback Callback function to use. Default null.
+ * @type string $end-callback Callback function to use at the end. Default null.
+ * @type string $type Type of comments to list.
+ * Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
+ * @type int $page Page ID to list comments for. Default empty.
+ * @type int $per_page Number of comments to list per page. Default empty.
+ * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
+ * @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts 'desc', 'asc'.
+ * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
+ * @type string $format How to format the comments list.
+ * Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
+ * @type bool $short_ping Whether to output short pings. Default false.
+ * @type bool $echo Whether to echo the output or return it. Default true.
+ * }
+ * @param array $comments Optional. Array of WP_Comment objects.
+ */
+function wp_list_comments( $args = array(), $comments = null ) {
+ global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
+
+ $in_comment_loop = true;
+
+ $comment_alt = $comment_thread_alt = 0;
+ $comment_depth = 1;
+
+ $defaults = array(
+ 'walker' => null,
+ 'max_depth' => '',
+ 'style' => 'ul',
+ 'callback' => null,
+ 'end-callback' => null,
+ 'type' => 'all',
+ 'page' => '',
+ 'per_page' => '',
+ 'avatar_size' => 32,
+ 'reverse_top_level' => null,
+ 'reverse_children' => '',
+ 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
+ 'short_ping' => false,
+ 'echo' => true,
+ );
+
+ $r = wp_parse_args( $args, $defaults );
+
+ /**
+ * Filter the arguments used in retrieving the comment list.
+ *
+ * @since 4.0.0
+ *
+ * @see wp_list_comments()
+ *
+ * @param array $r An array of arguments for displaying comments.
+ */
+ $r = apply_filters( 'wp_list_comments_args', $r );
+
+ // Figure out what comments we'll be looping through ($_comments)
+ if ( null !== $comments ) {
+ $comments = (array) $comments;
+ if ( empty($comments) )
+ return;
+ if ( 'all' != $r['type'] ) {
+ $comments_by_type = separate_comments($comments);
+ if ( empty($comments_by_type[$r['type']]) )
+ return;
+ $_comments = $comments_by_type[$r['type']];
+ } else {
+ $_comments = $comments;
+ }
+ } else {
+ if ( empty($wp_query->comments) )
+ return;
+ if ( 'all' != $r['type'] ) {
+ if ( empty($wp_query->comments_by_type) )
+ $wp_query->comments_by_type = separate_comments($wp_query->comments);
+ if ( empty($wp_query->comments_by_type[$r['type']]) )
+ return;
+ $_comments = $wp_query->comments_by_type[$r['type']];
+ } else {
+ $_comments = $wp_query->comments;
+ }
+
+ // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
+ if ( $wp_query->max_num_comment_pages ) {
+ $default_comments_page = get_option( 'default_comments_page' );
+ $cpage = get_query_var( 'cpage' );
+ if ( 'newest' === $default_comments_page ) {
+ $r['cpage'] = $cpage;
+
+ // When first page shows oldest comments, post permalink is the same as the comment permalink.
+ } elseif ( $cpage == 1 ) {
+ $r['cpage'] = '';
+ } else {
+ $r['cpage'] = $cpage;
+ }
+
+ $r['page'] = 0;
+ $r['per_page'] = 0;
+ }
+ }
+
+ if ( '' === $r['per_page'] && get_option( 'page_comments' ) ) {
+ $r['per_page'] = get_query_var('comments_per_page');
+ }
+
+ if ( empty($r['per_page']) ) {
+ $r['per_page'] = 0;
+ $r['page'] = 0;
+ }
+
+ if ( '' === $r['max_depth'] ) {
+ if ( get_option('thread_comments') )
+ $r['max_depth'] = get_option('thread_comments_depth');
+ else
+ $r['max_depth'] = -1;
+ }
+
+ if ( '' === $r['page'] ) {
+ if ( empty($overridden_cpage) ) {
+ $r['page'] = get_query_var('cpage');
+ } else {
+ $threaded = ( -1 != $r['max_depth'] );
+ $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
+ set_query_var( 'cpage', $r['page'] );
+ }
+ }
+ // Validation check
+ $r['page'] = intval($r['page']);
+ if ( 0 == $r['page'] && 0 != $r['per_page'] )
+ $r['page'] = 1;
+
+ if ( null === $r['reverse_top_level'] )
+ $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
+
+ if ( empty( $r['walker'] ) ) {
+ $walker = new Walker_Comment;
+ } else {
+ $walker = $r['walker'];
+ }
+
+ $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r );
+
+ $in_comment_loop = false;
+
+ if ( $r['echo'] ) {
+ echo $output;
+ } else {
+ return $output;
+ }
+}
+
+/**
+ * Output a complete commenting form for use within a template.
+ *
+ * Most strings and form fields may be controlled through the $args array passed
+ * into the function, while you may also choose to use the comment_form_default_fields
+ * filter to modify the array of default fields if you'd just like to add a new
+ * one or remove a single field. All fields are also individually passed through
+ * a filter of the form comment_form_field_$name where $name is the key used
+ * in the array of fields.
+ *
+ * @since 3.0.0
+ * @since 4.1.0 Introduced the 'class_submit' argument.
+ * @since 4.2.0 Introduced the 'submit_button' and 'submit_fields' arguments.
+ * @since 4.4.0 Introduced the 'class_form', 'title_reply_before', 'title_reply_after',
+ * 'cancel_reply_before', and 'cancel_reply_after' arguments.
+ *
+ * @param array $args {
+ * Optional. Default arguments and form fields to override.
+ *
+ * @type array $fields {
+ * Default comment fields, filterable by default via the 'comment_form_default_fields' hook.
+ *
+ * @type string $author Comment author field HTML.
+ * @type string $email Comment author email field HTML.
+ * @type string $url Comment author URL field HTML.
+ * }
+ * @type string $comment_field The comment textarea field HTML.
+ * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
+ * @type string $logged_in_as HTML element for a 'logged in as [user]' message.
+ * @type string $comment_notes_before HTML element for a message displayed before the comment fields
+ * if the user is not logged in.
+ * Default 'Your email address will not be published.'.
+ * @type string $comment_notes_after HTML element for a message displayed after the textarea field.
+ * @type string $id_form The comment form element id attribute. Default 'commentform'.
+ * @type string $id_submit The comment submit element id attribute. Default 'submit'.
+ * @type string $class_form The comment form element class attribute. Default 'comment-form'.
+ * @type string $class_submit The comment submit element class attribute. Default 'submit'.
+ * @type string $name_submit The comment submit element name attribute. Default 'submit'.
+ * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
+ * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
+ * where %s is the author of the comment being replied to.
+ * @type string $title_reply_before HTML displayed before the comment form title.
+ * Default: '<h3 id="reply-title" class="comment-reply-title">'.
+ * @type string $title_reply_after HTML displayed after the comment form title.
+ * Default: '</h3>'.
+ * @type string $cancel_reply_before HTML displayed before the cancel reply link.
+ * @type string $cancel_reply_after HTML displayed after the cancel reply link.
+ * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
+ * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
+ * @type string $submit_button HTML format for the Submit button.
+ * Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
+ * @type string $submit_field HTML format for the markup surrounding the Submit button and comment hidden
+ * fields. Default: '<p class="form-submit">%1$s %2$s</a>', where %1$s is the
+ * submit button markup and %2$s is the comment hidden fields.
+ * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
+ * }
+ * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
+ */
+function comment_form( $args = array(), $post_id = null ) {
+ if ( null === $post_id )
+ $post_id = get_the_ID();
+
+ $commenter = wp_get_current_commenter();
+ $user = wp_get_current_user();
+ $user_identity = $user->exists() ? $user->display_name : '';
+
+ $args = wp_parse_args( $args );
+ if ( ! isset( $args['format'] ) )
+ $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
+
+ $req = get_option( 'require_name_email' );
+ $aria_req = ( $req ? " aria-required='true'" : '' );
+ $html_req = ( $req ? " required='required'" : '' );
+ $html5 = 'html5' === $args['format'];
+ $fields = array(
+ 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
+ '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . $html_req . ' /></p>',
+ 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
+ '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" aria-describedby="email-notes"' . $aria_req . $html_req . ' /></p>',
+ 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
+ '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
+ );
+
+ $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
+
+ /**
+ * Filter the default comment form fields.
+ *
+ * @since 3.0.0
+ *
+ * @param array $fields The default comment fields.
+ */
+ $fields = apply_filters( 'comment_form_default_fields', $fields );
+ $defaults = array(
+ 'fields' => $fields,
+ 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required="required"></textarea></p>',
+ /** This filter is documented in wp-includes/link-template.php */
+ 'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
+ /** This filter is documented in wp-includes/link-template.php */
+ 'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( '<a href="%1$s" aria-label="Logged in as %2$s. Edit your profile.">Logged in as %2$s</a>. <a href="%3$s">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
+ 'comment_notes_before' => '<p class="comment-notes"><span id="email-notes">' . __( 'Your email address will not be published.' ) . '</span>'. ( $req ? $required_text : '' ) . '</p>',
+ 'comment_notes_after' => '',
+ 'id_form' => 'commentform',
+ 'id_submit' => 'submit',
+ 'class_form' => 'comment-form',
+ 'class_submit' => 'submit',
+ 'name_submit' => 'submit',
+ 'title_reply' => __( 'Leave a Reply' ),
+ 'title_reply_to' => __( 'Leave a Reply to %s' ),
+ 'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">',
+ 'title_reply_after' => '</h3>',
+ 'cancel_reply_before' => ' <small>',
+ 'cancel_reply_after' => '</small>',
+ 'cancel_reply_link' => __( 'Cancel reply' ),
+ 'label_submit' => __( 'Post Comment' ),
+ 'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
+ 'submit_field' => '<p class="form-submit">%1$s %2$s</p>',
+ 'format' => 'xhtml',
+ );
+
+ /**
+ * Filter the comment form default arguments.
+ *
+ * Use 'comment_form_default_fields' to filter the comment fields.
+ *
+ * @since 3.0.0
+ *
+ * @param array $defaults The default comment form arguments.
+ */
+ $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
+
+ // Ensure that the filtered args contain all required default values.
+ $args = array_merge( $defaults, $args );
+
+ if ( comments_open( $post_id ) ) : ?>
+ <?php
+ /**
+ * Fires before the comment form.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_before' );
+ ?>
+ <div id="respond" class="comment-respond">
+ <?php
+ echo $args['title_reply_before'];
+
+ comment_form_title( $args['title_reply'], $args['title_reply_to'] );
+
+ echo $args['cancel_reply_before'];
+
+ cancel_comment_reply_link( $args['cancel_reply_link'] );
+
+ echo $args['cancel_reply_after'];
+
+ echo $args['title_reply_after'];
+
+ if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) :
+ echo $args['must_log_in'];
+ /**
+ * Fires after the HTML-formatted 'must log in after' message in the comment form.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_must_log_in_after' );
+ else : ?>
+ <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="<?php echo esc_attr( $args['class_form'] ); ?>"<?php echo $html5 ? ' novalidate' : ''; ?>>
+ <?php
+ /**
+ * Fires at the top of the comment form, inside the form tag.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_top' );
+
+ if ( is_user_logged_in() ) :
+ /**
+ * Filter the 'logged in' message for the comment form for display.
+ *
+ * @since 3.0.0
+ *
+ * @param string $args_logged_in The logged-in-as HTML-formatted message.
+ * @param array $commenter An array containing the comment author's
+ * username, email, and URL.
+ * @param string $user_identity If the commenter is a registered user,
+ * the display name, blank otherwise.
+ */
+ echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
+
+ /**
+ * Fires after the is_user_logged_in() check in the comment form.
+ *
+ * @since 3.0.0
+ *
+ * @param array $commenter An array containing the comment author's
+ * username, email, and URL.
+ * @param string $user_identity If the commenter is a registered user,
+ * the display name, blank otherwise.
+ */
+ do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
+
+ else :
+
+ echo $args['comment_notes_before'];
+
+ endif;
+
+ // Prepare an array of all fields, including the textarea
+ $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];
+
+ /**
+ * Filter the comment form fields.
+ *
+ * @since 4.4.0
+ *
+ * @param array $comment_fields The comment fields.
+ */
+ $comment_fields = apply_filters( 'comment_form_fields', $comment_fields );
+
+ // Get an array of field names, excluding the textarea
+ $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) );
+
+ // Get the first and the last field name, excluding the textarea
+ $first_field = reset( $comment_field_keys );
+ $last_field = end( $comment_field_keys );
+
+ foreach ( $comment_fields as $name => $field ) {
+
+ if ( 'comment' === $name ) {
+
+ /**
+ * Filter the content of the comment textarea field for display.
+ *
+ * @since 3.0.0
+ *
+ * @param string $args_comment_field The content of the comment textarea field.
+ */
+ echo apply_filters( 'comment_form_field_comment', $field );
+
+ echo $args['comment_notes_after'];
+
+ } elseif ( ! is_user_logged_in() ) {
+
+ if ( $first_field === $name ) {
+ /**
+ * Fires before the comment fields in the comment form, excluding the textarea.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_before_fields' );
+ }
+
+ /**
+ * Filter a comment form field for display.
+ *
+ * The dynamic portion of the filter hook, `$name`, refers to the name
+ * of the comment form field. Such as 'author', 'email', or 'url'.
+ *
+ * @since 3.0.0
+ *
+ * @param string $field The HTML-formatted output of the comment form field.
+ */
+ echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
+
+ if ( $last_field === $name ) {
+ /**
+ * Fires after the comment fields in the comment form, excluding the textarea.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_after_fields' );
+ }
+ }
+ }
+
+ $submit_button = sprintf(
+ $args['submit_button'],
+ esc_attr( $args['name_submit'] ),
+ esc_attr( $args['id_submit'] ),
+ esc_attr( $args['class_submit'] ),
+ esc_attr( $args['label_submit'] )
+ );
+
+ /**
+ * Filter the submit button for the comment form to display.
+ *
+ * @since 4.2.0
+ *
+ * @param string $submit_button HTML markup for the submit button.
+ * @param array $args Arguments passed to `comment_form()`.
+ */
+ $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );
+
+ $submit_field = sprintf(
+ $args['submit_field'],
+ $submit_button,
+ get_comment_id_fields( $post_id )
+ );
+
+ /**
+ * Filter the submit field for the comment form to display.
+ *
+ * The submit field includes the submit button, hidden fields for the
+ * comment form, and any wrapper markup.
+ *
+ * @since 4.2.0
+ *
+ * @param string $submit_field HTML markup for the submit field.
+ * @param array $args Arguments passed to comment_form().
+ */
+ echo apply_filters( 'comment_form_submit_field', $submit_field, $args );
+
+ /**
+ * Fires at the bottom of the comment form, inside the closing </form> tag.
+ *
+ * @since 1.5.0
+ *
+ * @param int $post_id The post ID.
+ */
+ do_action( 'comment_form', $post_id );
+ ?>
+ </form>
+ <?php endif; ?>
+ </div><!-- #respond -->
+ <?php
+ /**
+ * Fires after the comment form.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_after' );
+ else :
+ /**
+ * Fires after the comment form if comments are closed.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_comments_closed' );
+ endif;
+}