Make WordPress Core

Ticket #19623: 19623-1.diff

File 19623-1.diff, 4.3 KB (added by simonwheatley, 14 years ago)

Enhance WP_Comment_Query and use the new functionality in comments_template (now without error_log call, and without repeated approve arg)

  • wp-includes/comment.php

     
    212212                        'order' => 'DESC',
    213213                        'parent' => '',
    214214                        'post_ID' => '',
    215                         'post_id' => 0,
     215                        'post_id' => '',
     216                        'post__in' => '',
    216217                        'post_author' => '',
    217218                        'post_name' => '',
    218219                        'post_parent' => '',
     
    220221                        'post_type' => '',
    221222                        'status' => '',
    222223                        'type' => '',
     224                        'unapproved_author' => '',
     225                        'unapproved_author_email' => '',
     226                        'unapproved_user_id' => '',
    223227                        'user_id' => '',
    224228                        'search' => '',
    225                         'count' => false
     229                        'count' => false,
    226230                );
    227231
    228232                $this->query_vars = wp_parse_args( $query_vars, $defaults );
     
    242246                        return $cache;
    243247                }
    244248
     249                if ( empty( $post_id ) && empty( $post__in ) )
     250                        $post_id = 0;
     251
    245252                $post_id = absint($post_id);
    246253
     254                $where = '';
     255
     256                $show_unapproved = ( '' != $unapproved_user_id || '' !== $unapproved_author || '' != $unapproved_author_email );
     257
     258                if ( $show_unapproved ) {
     259                        $where .= ' ( ';
     260                }
     261               
    247262                if ( 'hold' == $status )
    248                         $approved = "comment_approved = '0'";
     263                        $where .= "comment_approved = '0'";
    249264                elseif ( 'approve' == $status )
    250                         $approved = "comment_approved = '1'";
     265                        $where .= "comment_approved = '1'";
    251266                elseif ( 'spam' == $status )
    252                         $approved = "comment_approved = 'spam'";
     267                        $where .= "comment_approved = 'spam'";
    253268                elseif ( 'trash' == $status )
    254                         $approved = "comment_approved = 'trash'";
     269                        $where .= "comment_approved = 'trash'";
    255270                else
    256                         $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     271                        $where .= "( comment_approved = '0' OR comment_approved = '1' )";
    257272
     273                if ( $show_unapproved ) {
     274                        $where .= ' OR ( comment_approved = 0 ';
     275                        if ( '' !== $unapproved_author )
     276                                $where .= $wpdb->prepare( ' AND comment_author = %s', $unapproved_author );
     277                        if ( '' !== $unapproved_author_email )
     278                                $where .= $wpdb->prepare( ' AND comment_author_email = %s', $unapproved_author_email );
     279                        if ( '' !== $unapproved_user_id )
     280                                $where .= $wpdb->prepare( ' AND user_id = %d', $unapproved_user_id );
     281                        $where .= ' ) ) ';
     282                }
     283               
    258284                $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
    259285
    260286                if ( ! empty( $orderby ) ) {
     
    302328                        $fields = '*';
    303329
    304330                $join = '';
    305                 $where = $approved;
    306331
    307332                if ( ! empty($post_id) )
    308333                        $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
     334                if ( '' != $post__in ) {
     335                        $_post__in = implode(',', array_map( 'absint', $post__in ));
     336                        $where .= " AND comment_post_ID IN ($_post__in)";
     337                }
    309338                if ( '' !== $author_email )
    310339                        $where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
    311340                if ( '' !== $karma )
  • wp-includes/comment-template.php

     
     
    879879         */
    880880        $comment_author_url = esc_url($commenter['comment_author_url']);
    881881
    882         /** @todo Use API instead of SELECTs. */
     882        $query = new WP_Comment_Query;
     883        $args = array(
     884                'order' => 'ASC',
     885                'post_id' => $post->ID,
     886                'status' => 'approve',
     887        );
    883888        if ( $user_ID) {
    884                 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
    885         } else if ( empty($comment_author) ) {
    886                 $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
    887         } else {
    888                 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
     889                $args[ 'unapproved_user_id' ] = $user_ID;
     890        } else if ( ! empty($comment_author) ) {
     891                $args[ 'unapproved_author' ] = wp_specialchars_decode($comment_author,ENT_QUOTES);
     892                $args[ 'unapproved_author_email' ] = $comment_author_email;
    889893        }
     894        $args = apply_filters( 'comments_template_args', $args );
     895        $comments = $query->query( $args );
    890896
    891897        // keep $comments for legacy's sake
    892898        $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );