+/**
+ * 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 = 0, $args = array() ) {
+ $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 = 0, $args = array() ) {
+ $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, $id = 0 ) {
+ $depth++;
+ $GLOBALS['comment_depth'] = $depth;
+ $GLOBALS['comment'] = $comment;
+
+ if ( !empty($args['callback']) ) {
+ call_user_func($args['callback'], $comment, $args, $depth);
+ return;
+ }
+
+ extract($args, EXTR_SKIP);
+
+ if ( 'div' == $args['style'] ) {
+ $tag = 'div';
+ $add_below = 'comment';
+ } else {
+ $tag = 'li';
+ $add_below = 'div-comment';
+ }