+/**
+ * Retrieve HTML content for reply to comment 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 '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 $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 null|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 {@see get_comment_reply_link()}
+ * for more information on accepted arguments.
+ * @param object $comment The object of the comment being replied to.
+ * @param WP_Post $post The {@see 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 class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
+ esc_url( add_query_arg( 'replytocom', $comment->comment_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.
+ */
+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 ) {
+ global $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() . '</a>' : get_comment_author();
+ printf( $replytext, $author );
+ }
+}
+
+/**
+ * HTML comment list class.
+ *
+ * @uses Walker
+ * @since 2.7.0
+ */
+class Walker_Comment extends Walker {
+ /**
+ * What the class handles.
+ *
+ * @see Walker::$tree_type
+ *
+ * @since 2.7.0
+ * @var string
+ */
+ public $tree_type = 'comment';
+
+ /**
+ * DB fields to use.
+ *
+ * @see Walker::$db_fields
+ *
+ * @since 2.7.0
+ * @var array
+ */
+ public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
+
+ /**
+ * Start the list before the elements are added.
+ *
+ * @see Walker::start_lvl()
+ *
+ * @since 2.7.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param int $depth Depth of comment.
+ * @param array $args Uses 'style' argument for type of HTML list.
+ */
+ public function start_lvl( &$output, $depth = 0, $args = array() ) {
+ $GLOBALS['comment_depth'] = $depth + 1;
+
+ switch ( $args['style'] ) {
+ case 'div':
+ break;
+ case 'ol':
+ $output .= '<ol class="children">' . "\n";
+ break;
+ case 'ul':
+ default:
+ $output .= '<ul class="children">' . "\n";
+ break;
+ }
+ }
+
+ /**
+ * End the list of items after the elements are added.
+ *
+ * @see Walker::end_lvl()
+ *
+ * @since 2.7.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param int $depth Depth of comment.
+ * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
+ */
+ public function end_lvl( &$output, $depth = 0, $args = array() ) {
+ $GLOBALS['comment_depth'] = $depth + 1;
+
+ switch ( $args['style'] ) {
+ case 'div':
+ break;
+ case 'ol':
+ $output .= "</ol><!-- .children -->\n";
+ break;
+ case 'ul':
+ default:
+ $output .= "</ul><!-- .children -->\n";
+ break;
+ }
+ }
+
+ /**
+ * Traverse elements to create list from elements.
+ *
+ * This function is designed to enhance Walker::display_element() to
+ * display children of higher nesting levels than selected inline on
+ * the highest depth level displayed. This prevents them being orphaned
+ * at the end of the comment list.
+ *
+ * Example: max_depth = 2, with 5 levels of nested content.
+ * 1
+ * 1.1
+ * 1.1.1
+ * 1.1.1.1
+ * 1.1.1.1.1
+ * 1.1.2
+ * 1.1.2.1
+ * 2
+ * 2.2
+ *
+ * @see Walker::display_element()
+ * @see wp_list_comments()
+ *
+ * @since 2.7.0
+ *
+ * @param object $element Data object.
+ * @param array $children_elements List of elements to continue traversing.
+ * @param int $max_depth Max depth to traverse.
+ * @param int $depth Depth of current element.
+ * @param array $args An array of arguments.
+ * @param string $output Passed by reference. Used to append additional content.
+ * @return null Null on failure with no changes to parameters.
+ */
+ public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
+
+ if ( !$element )
+ return;
+
+ $id_field = $this->db_fields['id'];
+ $id = $element->$id_field;
+
+ parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
+
+ // If we're at the max depth, and the current element still has children, loop over those and display them at this level
+ // This is to prevent them being orphaned to the end of the list.
+ if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
+ foreach ( $children_elements[ $id ] as $child )
+ $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
+
+ unset( $children_elements[ $id ] );
+ }
+
+ }
+
+ /**
+ * Start the element output.
+ *
+ * @since 2.7.0
+ *
+ * @see Walker::start_el()
+ * @see wp_list_comments()
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $comment Comment data object.
+ * @param int $depth Depth of comment in reference to parents.
+ * @param array $args An array of arguments.
+ */
+ public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
+ $depth++;
+ $GLOBALS['comment_depth'] = $depth;
+ $GLOBALS['comment'] = $comment;
+
+ if ( !empty( $args['callback'] ) ) {
+ ob_start();
+ call_user_func( $args['callback'], $comment, $args, $depth );
+ $output .= ob_get_clean();
+ return;
+ }
+
+ if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
+ ob_start();
+ $this->ping( $comment, $depth, $args );
+ $output .= ob_get_clean();
+ } elseif ( 'html5' === $args['format'] ) {
+ ob_start();
+ $this->html5_comment( $comment, $depth, $args );
+ $output .= ob_get_clean();
+ } else {
+ ob_start();
+ $this->comment( $comment, $depth, $args );
+ $output .= ob_get_clean();
+ }
+ }
+
+ /**
+ * Ends the element output, if needed.
+ *
+ * @since 2.7.0
+ *
+ * @see Walker::end_el()
+ * @see wp_list_comments()
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $comment The comment object. Default current comment.
+ * @param int $depth Depth of comment.
+ * @param array $args An array of arguments.
+ */
+ public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
+ if ( !empty( $args['end-callback'] ) ) {
+ ob_start();
+ call_user_func( $args['end-callback'], $comment, $args, $depth );
+ $output .= ob_get_clean();
+ return;
+ }
+ if ( 'div' == $args['style'] )
+ $output .= "</div><!-- #comment-## -->\n";
+ else
+ $output .= "</li><!-- #comment-## -->\n";
+ }
+
+ /**
+ * Output a pingback comment.
+ *
+ * @access protected
+ * @since 3.6.0
+ *
+ * @see wp_list_comments()
+ *
+ * @param object $comment The comment object.
+ * @param int $depth Depth of comment.
+ * @param array $args An array of arguments.
+ */
+ protected function ping( $comment, $depth, $args ) {
+ $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
+?>
+ <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
+ <div class="comment-body">
+ <?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
+ </div>
+<?php
+ }
+
+ /**
+ * Output a single comment.
+ *
+ * @access protected
+ * @since 3.6.0
+ *
+ * @see wp_list_comments()
+ *
+ * @param object $comment Comment to display.
+ * @param int $depth Depth of comment.
+ * @param array $args An array of arguments.
+ */
+ protected function comment( $comment, $depth, $args ) {
+ if ( 'div' == $args['style'] ) {
+ $tag = 'div';
+ $add_below = 'comment';
+ } else {
+ $tag = 'li';
+ $add_below = 'div-comment';
+ }
+?>
+ <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '' ); ?> id="comment-<?php comment_ID(); ?>">
+ <?php if ( 'div' != $args['style'] ) : ?>
+ <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
+ <?php endif; ?>
+ <div class="comment-author vcard">
+ <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
+ <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
+ </div>
+ <?php if ( '0' == $comment->comment_approved ) : ?>
+ <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
+ <br />
+ <?php endif; ?>
+
+ <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
+ <?php
+ /* translators: 1: date, 2: time */
+ printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' );
+ ?>
+ </div>
+
+ <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
+
+ <?php
+ comment_reply_link( array_merge( $args, array(
+ 'add_below' => $add_below,
+ 'depth' => $depth,
+ 'max_depth' => $args['max_depth'],
+ 'before' => '<div class="reply">',
+ 'after' => '</div>'
+ ) ) );
+ ?>
+
+ <?php if ( 'div' != $args['style'] ) : ?>
+ </div>
+ <?php endif; ?>
+<?php
+ }
+
+ /**
+ * Output a comment in the HTML5 format.
+ *
+ * @access protected
+ * @since 3.6.0
+ *
+ * @see wp_list_comments()
+ *
+ * @param object $comment Comment to display.
+ * @param int $depth Depth of comment.
+ * @param array $args An array of arguments.
+ */
+ protected function html5_comment( $comment, $depth, $args ) {
+ $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
+?>
+ <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '' ); ?>>
+ <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
+ <footer class="comment-meta">
+ <div class="comment-author vcard">
+ <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
+ <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link() ) ); ?>
+ </div><!-- .comment-author -->
+
+ <div class="comment-metadata">
+ <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
+ <time datetime="<?php comment_time( 'c' ); ?>">
+ <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?>
+ </time>
+ </a>
+ <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
+ </div><!-- .comment-metadata -->
+
+ <?php if ( '0' == $comment->comment_approved ) : ?>
+ <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
+ <?php endif; ?>
+ </footer><!-- .comment-meta -->
+
+ <div class="comment-content">
+ <?php comment_text(); ?>
+ </div><!-- .comment-content -->
+
+ <?php
+ comment_reply_link( array_merge( $args, array(
+ 'add_below' => 'div-comment',
+ 'depth' => $depth,
+ 'max_depth' => $args['max_depth'],
+ 'before' => '<div class="reply">',
+ 'after' => '</div>'
+ ) ) );
+ ?>
+ </article><!-- .comment-body -->
+<?php
+ }
+}
+
+/**
+ * List comments.
+ *
+ * Used in the comments.php template to list comments for a particular post.
+ *
+ * @since 2.7.0
+ *
+ * @see WP_Query->comments
+ *
+ * @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 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;
+ }
+ }
+
+ 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 );
+ $wp_query->max_num_comment_pages = $walker->max_pages;
+
+ $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 'submit_button' and 'submit_fields' 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 form.
+ * Default 'Your email address will not be published.'.
+ * @type string $comment_notes_after HTML element for a message displayed after the comment form.
+ * Default 'You may use these HTML tags and attributes ...'.
+ * @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_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 $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-describedby="form-allowed-tags" 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( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
+ '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' => '<p class="form-allowed-tags" id="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
+ 'id_form' => 'commentform',
+ 'id_submit' => 'submit',
+ 'class_submit' => 'submit',
+ 'name_submit' => 'submit',
+ 'title_reply' => __( 'Leave a Reply' ),
+ 'title_reply_to' => __( 'Leave a Reply to %s' ),
+ '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">
+ <h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>
+ <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
+ <?php echo $args['must_log_in']; ?>
+ <?php
+ /**
+ * 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' );
+ ?>
+ <?php else : ?>
+ <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="comment-form"<?php echo $html5 ? ' novalidate' : ''; ?>>
+ <?php
+ /**
+ * Fires at the top of the comment form, inside the form tag.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_top' );
+ ?>
+ <?php if ( is_user_logged_in() ) : ?>
+ <?php
+ /**
+ * 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 );
+ ?>
+ <?php
+ /**
+ * 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 );
+ ?>
+ <?php else : ?>
+ <?php echo $args['comment_notes_before']; ?>
+ <?php
+ /**
+ * Fires before the comment fields in the comment form.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_before_fields' );
+ foreach ( (array) $args['fields'] as $name => $field ) {
+ /**
+ * 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";
+ }
+ /**
+ * Fires after the comment fields in the comment form.
+ *
+ * @since 3.0.0
+ */
+ do_action( 'comment_form_after_fields' );
+ ?>
+ <?php endif; ?>
+ <?php
+ /**
+ * 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', $args['comment_field'] );
+ ?>
+ <?php echo $args['comment_notes_after']; ?>
+
+ <?php
+ $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;
+}