WordPress.org

Make WordPress Core

Ticket #8120: 8120.getting-started.diff

File 8120.getting-started.diff, 3.8 KB (added by markjaquith, 10 years ago)

This is merely a start... completely untested.

  • wp-includes/comment.php

     
    180180function get_comments( $args = '' ) {
    181181        global $wpdb;
    182182
    183         $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
     183        $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0, 'paged' => FALSE, 'page' => 1, 'comments_per_page' => get_option( 'comments_per_page' ), 'threaded' => FALSE, 'child_of' => 0, $passed_comments = NULL );
    184184
    185185        $args = wp_parse_args( $args, $defaults );
    186186        extract( $args, EXTR_SKIP );
     
    198198                return $cache;
    199199        }
    200200
    201         $post_id = absint($post_id);
     201        $post_id = absint( $post_id );
     202        $page = absint( $page );
     203        $comments_per_page = absint( $comments_per_page );
    202204
     205        if ( is_string( $child_of) || is_int( $child_of ) )
     206                $child_of = explode( ',', $child_of );
     207
     208        if ( is_array( $child_of ) ) {
     209                $_child_of = array();
     210                foreach ( (array) $child_of as $_child )
     211                        $_child_of[] = absint( trim( $_child, " '" ) );
     212                $child_of = implode( ',', $_child_of );
     213        } else {
     214                $child_of = 0;
     215        }
     216
    203217        if ( 'hold' == $status )
    204218                $approved = "comment_approved = '0'";
    205219        elseif ( 'approve' == $status )
     
    209223        else
    210224                $approved = "( comment_approved = '0' OR comment_approved = '1' )";
    211225
    212         $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
     226        $order = ( 'ASC' == strtoupper( $order ) ) ? 'ASC' : 'DESC';
    213227
    214228        $orderby = 'comment_date_gmt';  // Hard code for now
    215229
    216230        $number = absint($number);
    217231        $offset = absint($offset);
    218232
    219         if ( !empty($number) ) {
     233        $threaded_where = '';
     234
     235        if ( $paged && $post_id && !$threaded ) {
     236                $number = 'LIMIT ' . ( $page - 1 ) * $comments_per_page . ',' . $comments_per_page;
     237        } elseif ( $paged && $post_id ) {
     238                if ( ( $comments_per_page * 2 ) >= get_comment_count( $post_id ) ) { // just query them all
     239                        $threaded = false; // we'll get them all in one query, so cancel any recursive queries
     240                        $number = '';
     241                } elseif ( 0 == $child_of ) { // first level of recursion
     242                        $threaded_where = 'comment_parent = 0 AND ';
     243                } else { // further level of recursion
     244                        $threaded_where = "comment_parent IN( $child_of ) AND ";
     245                }
     246        } elseif ( !empty($number) ) {
    220247                if ( $offset )
    221248                        $number = 'LIMIT ' . $offset . ',' . $number;
    222249                else
     
    227254        }
    228255
    229256        if ( ! empty($post_id) )
    230                 $post_where = "comment_post_ID = $post_id AND";
     257                $post_where = $wpdb->prepare( 'comment_post_ID = %d AND', $post_id );
    231258        else
    232259                $post_where = '';
    233260
    234         $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
    235         wp_cache_add( $cache_key, $comments, 'comment' );
     261        $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $threaded_where $post_where $approved ORDER BY $orderby $order $number" );
    236262
     263        if ( $threaded && $paged ) {
     264                if ( count( $comments ) ) { // have to recurse
     265                        if ( count( $passed_comments ) )
     266                                $passed_comments = array_merge( (array) $passed_comments, (array) $comments );
     267                        else
     268                                $passed_comments = $comments;
     269                        $_new_ids = array();
     270                        foreach ( (array) $comments as $_c )
     271                                $_new_ids[] = $_c->comment_ID;
     272                        $_args = compact( 'status', 'orderby', 'order', 'number', 'offset', 'post_id', 'paged', 'page', 'comments_per_page', 'threaded', 'passed_comments');
     273                        $_args['child_of'] = $_new_ids;
     274                        $comments = get_comments( $_args );
     275                } else { // found them all!
     276                        $comments = $passed_comments;
     277                }
     278        }
     279
     280        // we only add to the cache when we've gathered the full comment tree
     281        if ( ! ( $threaded && $paged && $child_of != 0 ) )
     282                wp_cache_add( $cache_key, $comments, 'comment' );
     283
    237284        return $comments;
    238285}
    239286