Make WordPress Core


Ignore:
Timestamp:
09/11/2008 07:25:50 PM (18 years ago)
Author:
ryan
Message:

wp_list_comments() and threaded comment support. First cut. see #7635

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment-template.php

    r8800 r8869  
    228228 * @param int $post_id An optional post ID
    229229 */
    230 function comment_class( $class = '', $comment_id = null, $post_id = null ) {
    231         // Separates classes with a single space, collates classes for post DIV
    232         echo 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
     230function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
     231        // Separates classes with a single space, collates classes for comment DIV
     232        $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
     233        if ( $echo)
     234                echo $class;
     235        else
     236                return $class;
    233237}
    234238
    … …  
    267271                $comment_alt = 0;
    268272
    269         if ( $comment_alt % 2 )
     273        if ( $comment_alt % 2 ) {
    270274                $classes[] = 'odd';
    271         else
     275                $classes[] = 'alt';
     276        } else {
    272277                $classes[] = 'even';
     278        }
    273279
    274280        $comment_alt++;
    … …  
    836842}
    837843
     844function comment_reply_link($args = array(), $comment = null, $post = null) {
     845        global $user_ID;
     846
     847        $defaults = array('add_below' => 'comment', 'respond_id' => 'respond', 'reply_text' => __('Reply'),
     848                'login_text' => __('Log in to Reply'));
     849
     850        $args = wp_parse_args($args, $defaults);
     851
     852        extract($args, EXTR_SKIP);
     853
     854        $comment = get_comment($comment);
     855        $post = get_post($post);
     856
     857        if ( 'open' != $post->comment_status )
     858                return false;
     859
     860        $link = '';
     861
     862        if ( get_option('comment_registration') && !$user_ID )
     863                $link = '<a href="' . site_url('wp-login.php?redirect_to=' . get_permalink()) . '">' . $login_text . '</a>';
     864        else
     865                $link = "<a href='#' onclick='moveAddCommentForm(\"$add_below-$comment->comment_ID\", $comment->comment_ID, \"$respond_id\"); return false;'>$reply_text</a>";
     866
     867        return $link;
     868}
     869
     870function cancel_comment_reply_link($text = '', $respond_id = 'respond') {
     871        if ( empty($text) )
     872                $text = __('Click here to cancel reply.');
     873        echo '<a href="#" onclick="cancelCommentReply(\'' . $respond_id . '\'); return false;">' . $text . '</a>';
     874}
     875
     876class Walker_Comment extends Walker {
     877        var $tree_type = 'comment';
     878        var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
     879
     880        function start_lvl(&$output, $depth, $args) {
     881                if ( 'div' == $args['style'] )
     882                        return;
     883
     884                echo "<ul class='children'>\n";
     885        }
     886
     887        function end_lvl(&$output, $depth, $args) {
     888                if ( 'div' == $args['style'] )
     889                        return;
     890
     891                echo "</ul>\n";
     892        }
     893
     894        function start_el(&$output, $comment, $depth, $args) {
     895                $depth++;
     896
     897                if ( !empty($args['callback']) ) {
     898                        call_user_func($args['callback'], $comment, $args, $depth);
     899                        return;
     900                }
     901
     902                $GLOBALS['comment'] = $comment;
     903                extract($args, EXTR_SKIP);
     904
     905                if ( 'div' == $args['style'] )
     906                        $tag = 'div';
     907                else
     908                        $tag = 'li';
    838909?>
     910                <<?php echo $tag ?> "<?php comment_class() ?>" id="comment-<?php comment_ID() ?>">
     911                <?php if ( 'list' == $args['style'] ) : ?>
     912                <div id="div-comment-<?php comment_ID() ?>">
     913                <?php endif; ?>
     914                <div class="comment-author vcard">
     915                <?php echo get_avatar( $comment, 32 ) ?>
     916                <?php printf(__('<cite>%s</cite> Says:'), get_comment_author_link()) ?>
     917                </div>
     918<?php if ($comment->comment_approved == '0') : ?>
     919                <em><?php _e('Your comment is awaiting moderation.') ?></em>
     920                <br />
     921<?php endif; ?>
     922
     923                <div class="comment-meta commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php printf(__('%1$s at %2$s'), get_comment_date('F jS, Y'),  get_comment_time()) ?></a><?php edit_comment_link('edit','&nbsp;&nbsp;','') ?></div>
     924
     925                <?php echo apply_filters('comment_text', get_comment_text()) ?>
     926
     927                <div class='reply'>
     928                <?php if ( $depth < $args['depth'] ) echo comment_reply_link(array('add_below' => 'div-comment')) ?>
     929                <?php if ( 'list' == $args['style'] ) : ?>
     930                </div>
     931                <?php endif; ?>
     932                </div>
     933<?php
     934        }
     935
     936        function end_el(&$output, $comment, $depth, $args) {
     937                if ( 'div' == $args['style'] )
     938                        echo "</div>\n";
     939                else
     940                        echo "</li>\n";
     941        }
     942
     943}
     944
     945/**
     946 * List comments
     947 *
     948 * Used in the comments.php template to list comments for a particular post
     949 *
     950 * @since 2.7
     951 * @uses Walker_Comment
     952 *
     953 * @param $comments array Array of comment object to list
     954 * @param $args string|array Additional arguments
     955 */
     956function wp_list_comments(&$comments, $args = array() ) {
     957        $defaults = array('walker' => null, 'depth' => 3, 'style' => 'list', 'callback' => null);
     958
     959        $r = wp_parse_args( $args, $defaults );
     960
     961        extract( $r, EXTR_SKIP );
     962
     963        if ( empty($walker) )
     964                $walker = new Walker_Comment;
     965
     966        $walker->walk($comments, $depth, $r);
     967}
     968
     969?>
Note: See TracChangeset for help on using the changeset viewer.