Make WordPress Core

Ticket #12904: get_comments.diff

File get_comments.diff, 1.6 KB (added by zeo, 15 years ago)

refresh patch

  • wp-includes/comment.php

     
    204204                'status' => '',
    205205                'type' => '',
    206206                'user_id' => '',
     207                'post_status' => '',
     208                'post_type' => ''
    207209        );
    208210
    209211        $args = wp_parse_args( $args, $defaults );
     
    294296        if ( '' !== $user_id )
    295297                $post_where .= $wpdb->prepare( 'user_id = %d AND ', $user_id );
    296298
    297         $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
     299        if ( ! empty( $post_status ) ) {
     300                $post_status = is_array( $post_status ) ? $post_status : preg_split( '/[,\s]/', $post_status );
     301                if ( array_intersect( $post_status,  get_post_stati() ) )
     302                        $post_status = stripslashes( $wpdb->prepare( "AND post_status IN ( '%s' )", implode( "','", $post_status ) ) );
     303        }
     304
     305        if ( ! empty( $post_type ) ) {
     306                $post_type = is_array( $post_type ) ? $post_type : preg_split( '/[,\s]/', $post_type );
     307                if ( array_intersect( $post_type, get_post_types() ) )
     308                        $post_type = stripslashes( $wpdb->prepare( "AND post_type IN ( '%s' )", implode( "','", $post_type ) ) );
     309        }
     310
     311        $join = '';
     312
     313        if ( ! empty( $post_status ) || ! empty( $post_type ) )
     314                $join = $wpdb->prepare( "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID" );
     315
     316        $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments $join WHERE $post_where $approved $post_status $post_type ORDER BY $orderby $order $number" );
    298317        wp_cache_add( $cache_key, $comments, 'comment' );
    299318
    300319        return $comments;