+/**
+ * Retrieve HTML content for reply to comment link.
+ *
+ * The default arguments that can be override are 'add_below', 'respond_id',
+ * 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
+ * used, if the user must log in or register first before posting a comment. The
+ * 'reply_text' will be used, if they can post a reply. The 'add_below' and
+ * 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
+ * parameters.
+ *
+ * @since 2.7.0
+ *
+ * @param array $args Optional. Override default options.
+ * @param int $comment Optional. Comment being replied to.
+ * @param int $post Optional. Post that the comment is going to be displayed on.
+ * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
+ */
+function get_comment_reply_link($args = array(), $comment = null, $post = null) {
+ global $user_ID;
+
+ $defaults = array('add_below' => 'comment', 'respond_id' => 'respond', 'reply_text' => __('Reply'),
+ '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;
+
+ extract($args, EXTR_SKIP);
+
+ $comment = get_comment($comment);
+ if ( empty($post) )
+ $post = $comment->comment_post_ID;
+ $post = get_post($post);
+
+ if ( !comments_open($post->ID) )
+ return false;
+
+ $link = '';
+
+ if ( get_option('comment_registration') && !$user_ID )
+ $link = '<a rel="nofollow" class="comment-reply-login" href="' . esc_url( wp_login_url( get_permalink() ) ) . '">' . $login_text . '</a>';
+ else
+ $link = "<a class='comment-reply-link' href='" . esc_url( add_query_arg( 'replytocom', $comment->comment_ID ) ) . "#" . $respond_id . "' onclick='return addComment.moveForm(\"$add_below-$comment->comment_ID\", \"$comment->comment_ID\", \"$respond_id\", \"$post->ID\")'>$reply_text</a>";
+ return apply_filters('comment_reply_link', $before . $link . $after, $args, $comment, $post);
+}
+
+/**
+ * Displays the HTML content for reply to comment link.
+ *
+ * @since 2.7.0
+ * @see get_comment_reply_link() Echoes result
+ *
+ * @param array $args Optional. Override default options.
+ * @param int $comment Optional. Comment being replied to.
+ * @param int $post Optional. Post that the comment is going to be displayed on.
+ * @return string|bool|null 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.
+ *
+ * The default arguments that can be override are 'add_below', 'respond_id',
+ * 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
+ * used, if the user must log in or register first before posting a comment. The
+ * 'reply_text' will be used, if they can post a reply. The 'add_below' and
+ * 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
+ * parameters.
+ *
+ * @since 2.7.0
+ *
+ * @param array $args Optional. Override default options.
+ * @param int|object $post Optional. Post that the comment is going to be displayed on. Defaults to current post.
+ * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
+ */
+function get_post_reply_link($args = array(), $post = null) {
+ global $user_ID;
+
+ $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);
+ extract($args, EXTR_SKIP);
+ $post = get_post($post);
+
+ if ( !comments_open($post->ID) )
+ return false;
+
+ if ( get_option('comment_registration') && !$user_ID ) {
+ $link = '<a rel="nofollow" href="' . wp_login_url( get_permalink() ) . '">' . $login_text . '</a>';
+ } else {
+ $link = "<a rel='nofollow' class='comment-reply-link' href='" . get_permalink($post->ID) . "#$respond_id' onclick='return addComment.moveForm(\"$add_below-$post->ID\", \"0\", \"$respond_id\", \"$post->ID\")'>$reply_text</a>";
+ }
+ return apply_filters('post_comments_link', $before . $link . $after, $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|object $post Optional. Post that the comment is going to be displayed on.
+ * @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.
+ */
+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';
+ return apply_filters('cancel_comment_reply_link', '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>', $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.
+ */
+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
+ *
+ * @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";
+ return apply_filters('comment_id_fields', $result, $id, $replytoid);
+}
+
+/**
+ * Output hidden input HTML for replying to comments.
+ *
+ * @since 2.7.0
+ * @see get_comment_id_fields() Echoes result
+ */
+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.
+ * @param string $replytext Optional. Text to display when replying to a comment. 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.
+ */
+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.
+ *
+ * @package WordPress
+ * @uses Walker
+ * @since 2.7.0
+ */
+class Walker_Comment extends Walker {
+ /**
+ * @see Walker::$tree_type
+ * @since 2.7.0
+ * @var string
+ */
+ var $tree_type = 'comment';
+
+ /**
+ * @see Walker::$db_fields
+ * @since 2.7.0
+ * @var array
+ */
+ var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
+
+ /**
+ * @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.
+ */
+ function start_lvl(&$output, $depth, $args) {
+ $GLOBALS['comment_depth'] = $depth + 1;
+
+ switch ( $args['style'] ) {
+ case 'div':
+ break;
+ case 'ol':
+ echo "<ol class='children'>\n";
+ break;
+ default:
+ case 'ul':
+ echo "<ul class='children'>\n";
+ break;
+ }
+ }
+
+ /**
+ * @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'.
+ */
+ function end_lvl(&$output, $depth, $args) {
+ $GLOBALS['comment_depth'] = $depth + 1;
+
+ switch ( $args['style'] ) {
+ case 'div':
+ break;
+ case 'ol':
+ echo "</ol>\n";
+ break;
+ default:
+ case 'ul':
+ echo "</ul>\n";
+ break;
+ }
+ }
+
+ /**
+ * 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
+ *
+ */
+ function display_element( $element, &$children_elements, $max_depth, $depth=0, $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 ] );
+ }
+
+ }
+
+ /**
+ * @see Walker::start_el()
+ * @since 2.7.0
+ *
+ * @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
+ */
+ function start_el(&$output, $comment, $depth, $args) {
+ $depth++;
+ $GLOBALS['comment_depth'] = $depth;
+
+ if ( !empty($args['callback']) ) {
+ call_user_func($args['callback'], $comment, $args, $depth);
+ return;
+ }
+
+ $GLOBALS['comment'] = $comment;
+ extract($args, EXTR_SKIP);
+
+ if ( 'div' == $args['style'] ) {
+ $tag = 'div';
+ $add_below = 'comment';
+ } else {
+ $tag = 'li';
+ $add_below = 'div-comment';
+ }
+?>
+ <<?php echo $tag ?> <?php comment_class(empty( $args['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 ($args['avatar_size'] != 0) 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 ($comment->comment_approved == '0') : ?>
+ <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 htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
+ <?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() ?>
+
+ <div class="reply">
+ <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
+ </div>
+ <?php if ( 'div' != $args['style'] ) : ?>
+ </div>
+ <?php endif; ?>
+<?php
+ }
+
+ /**
+ * @see Walker::end_el()
+ * @since 2.7.0
+ *
+ * @param string $output Passed by reference. Used to append additional content.
+ * @param object $comment
+ * @param int $depth Depth of comment.
+ * @param array $args
+ */
+ function end_el(&$output, $comment, $depth, $args) {
+ if ( !empty($args['end-callback']) ) {
+ call_user_func($args['end-callback'], $comment, $args, $depth);
+ return;
+ }
+ if ( 'div' == $args['style'] )
+ echo "</div>\n";
+ else
+ echo "</li>\n";
+ }
+
+}
+
+/**
+ * List comments
+ *
+ * Used in the comments.php template to list comments for a particular post
+ *
+ * @since 2.7.0
+ * @uses Walker_Comment
+ *
+ * @param string|array $args Formatting options
+ * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
+ */
+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' => '');
+
+ $r = wp_parse_args( $args, $defaults );
+
+ // 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') );
+
+ extract( $r, EXTR_SKIP );
+
+ if ( empty($walker) )
+ $walker = new Walker_Comment;
+
+ $walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
+ $wp_query->max_num_comment_pages = $walker->max_pages;
+
+ $in_comment_loop = false;
+}
+
+/**
+ * Outputs 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
+ * @param array $args Options for strings, fields etc in the form
+ * @param mixed $post_id Post ID to generate the form for, uses the current post if null
+ * @return void
+ */
+function comment_form( $args = array(), $post_id = null ) {
+ global $id;
+
+ if ( null === $post_id )
+ $post_id = $id;
+ else
+ $id = $post_id;
+
+ $commenter = wp_get_current_commenter();
+ $user = wp_get_current_user();
+ $user_identity = ! empty( $user->ID ) ? $user->display_name : '';
+
+ $req = get_option( 'require_name_email' );
+ $aria_req = ( $req ? " aria-required='true'" : '' );
+ $fields = array(
+ 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
+ '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
+ 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
+ '<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
+ 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
+ '<input id="url" name="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>' );
+ $defaults = array(
+ 'fields' => apply_filters( 'comment_form_default_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"></textarea></p>',
+ '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>',
+ '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>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
+ 'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
+ 'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
+ 'id_form' => 'commentform',
+ 'id_submit' => 'submit',
+ 'title_reply' => __( 'Leave a Reply' ),
+ 'title_reply_to' => __( 'Leave a Reply to %s' ),
+ 'cancel_reply_link' => __( 'Cancel reply' ),
+ 'label_submit' => __( 'Post Comment' ),
+ );
+
+ $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
+
+ ?>
+ <?php if ( comments_open() ) : ?>
+ <?php do_action( 'comment_form_before' ); ?>
+ <div id="respond">
+ <h3 id="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 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'] ); ?>">
+ <?php do_action( 'comment_form_top' ); ?>
+ <?php if ( is_user_logged_in() ) : ?>
+ <?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
+ <?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
+ <?php else : ?>
+ <?php echo $args['comment_notes_before']; ?>
+ <?php
+ do_action( 'comment_form_before_fields' );
+ foreach ( (array) $args['fields'] as $name => $field ) {
+ echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
+ }
+ do_action( 'comment_form_after_fields' );
+ ?>
+ <?php endif; ?>
+ <?php echo apply_filters( 'comment_form_field_comment', $args['comment_field'] ); ?>
+ <?php echo $args['comment_notes_after']; ?>
+ <p class="form-submit">
+ <input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
+ <?php comment_id_fields( $post_id ); ?>
+ </p>
+ <?php do_action( 'comment_form', $post_id ); ?>
+ </form>
+ <?php endif; ?>
+ </div><!-- #respond -->
+ <?php do_action( 'comment_form_after' ); ?>
+ <?php else : ?>
+ <?php do_action( 'comment_form_comments_closed' ); ?>
+ <?php endif; ?>
+ <?php
+}
+