Make WordPress Core

Changeset 28458


Ignore:
Timestamp:
05/16/2014 07:32:05 PM (11 years ago)
Author:
wonderboymusic
Message:

Eliminate use of extract() in WP_Comment_Query::query().

See #22400.

File:
1 edited

Legend:

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

    r28457 r28458  
    278278
    279279        // $args can be whatever, only use the args defined in defaults to compute the key
    280         $key = md5( serialize( compact(array_keys($defaults)) )  );
     280        $key = md5( serialize( compact( array_keys( $defaults ) ) )  );
    281281        $last_changed = wp_cache_get( 'last_changed', 'comment' );
    282282        if ( ! $last_changed ) {
     
    286286        $cache_key = "get_comments:$key:$last_changed";
    287287
    288         if ( $cache = wp_cache_get( $cache_key, 'comment' ) )
     288        if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
    289289            return $cache;
    290 
    291         $post_id = absint($post_id);
    292 
    293         if ( 'hold' == $status )
     290        }
     291
     292        $status = $this->query_vars['status'];
     293        if ( 'hold' == $status ) {
    294294            $approved = "comment_approved = '0'";
    295         elseif ( 'approve' == $status )
     295        } elseif ( 'approve' == $status ) {
    296296            $approved = "comment_approved = '1'";
    297         elseif ( ! empty( $status ) && 'all' != $status )
     297        } elseif ( ! empty( $status ) && 'all' != $status ) {
    298298            $approved = $wpdb->prepare( "comment_approved = %s", $status );
    299         else
     299        } else {
    300300            $approved = "( comment_approved = '0' OR comment_approved = '1' )";
    301 
    302         $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
    303 
    304         if ( ! empty( $orderby ) ) {
    305             $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
     301        }
     302        $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
     303
     304        if ( ! empty( $this->query_vars['orderby'] ) ) {
     305            $ordersby = is_array( $this->query_vars['orderby'] ) ?
     306                $this->query_vars['orderby'] :
     307                preg_split( '/[,\s]/', $this->query_vars['orderby'] );
     308
    306309            $allowed_keys = array(
    307310                'comment_agent',
     
    339342        }
    340343
    341         $number = absint($number);
    342         $offset = absint($offset);
    343 
    344         if ( !empty($number) ) {
    345             if ( $offset )
     344        $number = absint( $this->query_vars['number'] );
     345        $offset = absint( $this->query_vars['offset'] );
     346
     347        if ( ! empty( $number ) ) {
     348            if ( $offset ) {
    346349                $limits = 'LIMIT ' . $offset . ',' . $number;
    347             else
     350            } else {
    348351                $limits = 'LIMIT ' . $number;
     352            }
    349353        } else {
    350354            $limits = '';
    351355        }
    352356
    353         if ( $count )
     357        if ( $this->query_vars['count'] ) {
    354358            $fields = 'COUNT(*)';
    355         else
     359        } else {
    356360            $fields = '*';
    357 
     361        }
    358362        $join = '';
    359363        $where = $approved;
    360364
    361         if ( ! empty($post_id) )
     365        $post_id = absint( $this->query_vars['post_id'] );
     366        if ( ! empty( $post_id ) ) {
    362367            $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
    363         if ( '' !== $author_email )
    364             $where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
    365         if ( '' !== $karma )
    366             $where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
    367         if ( 'comment' == $type ) {
     368        }
     369
     370        if ( '' !== $this->query_vars['author_email'] ) {
     371            $where .= $wpdb->prepare( ' AND comment_author_email = %s', $this->query_vars['author_email'] );
     372        }
     373
     374        if ( '' !== $this->query_vars['karma'] ) {
     375            $where .= $wpdb->prepare( ' AND comment_karma = %d', $this->query_vars['karma'] );
     376        }
     377
     378        if ( 'comment' == $this->query_vars['type'] ) {
    368379            $where .= " AND comment_type = ''";
    369         } elseif( 'pings' == $type ) {
     380        } elseif( 'pings' == $this->query_vars['type'] ) {
    370381            $where .= ' AND comment_type IN ("pingback", "trackback")';
    371         } elseif ( ! empty( $type ) ) {
    372             $where .= $wpdb->prepare( ' AND comment_type = %s', $type );
    373         }
    374         if ( '' !== $parent )
    375             $where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
    376 
    377         if ( is_array( $user_id ) ) {
    378             $where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $user_id ) ) . ')';
    379         } elseif ( '' !== $user_id ) {
    380             $where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
    381         }
    382 
    383         if ( '' !== $search )
    384             $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
    385 
    386         $post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) );
     382        } elseif ( ! empty( $this->query_vars['type'] ) ) {
     383            $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] );
     384        }
     385
     386        if ( '' !== $this->query_vars['parent'] ) {
     387            $where .= $wpdb->prepare( ' AND comment_parent = %d', $this->query_vars['parent'] );
     388        }
     389
     390        if ( is_array( $this->query_vars['user_id'] ) ) {
     391            $where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
     392        } elseif ( '' !== $this->query_vars['user_id'] ) {
     393            $where .= $wpdb->prepare( ' AND user_id = %d', $this->query_vars['user_id'] );
     394        }
     395
     396        if ( '' !== $this->query_vars['search'] ) {
     397            $where .= $this->get_search_sql(
     398                $this->query_vars['search'],
     399                array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
     400            );
     401        }
     402
     403        $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
     404        $post_fields = array_filter( $plucked );
     405
    387406        if ( ! empty( $post_fields ) ) {
    388407            $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
     
    398417        }
    399418
     419        $date_query = $this->query_vars['date_query'];
    400420        if ( ! empty( $date_query ) && is_array( $date_query ) ) {
    401421            $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
     
    416436            $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    417437
    418         if ( $groupby )
     438        if ( $groupby ) {
    419439            $groupby = 'GROUP BY ' . $groupby;
    420 
     440        }
    421441        $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
    422442
    423         if ( $count )
     443        if ( $this->query_vars['count'] ) {
    424444            return $wpdb->get_var( $query );
    425 
    426         $comments = $wpdb->get_results( $query );
     445        }
     446        $results = $wpdb->get_results( $query );
    427447        /**
    428448         * Filter the comment query results.
     
    430450         * @since 3.1.0
    431451         *
    432          * @param array            $comments An array of comments.
     452         * @param array            $results An array of comments.
    433453         * @param WP_Comment_Query &$this    Current instance of WP_Comment_Query, passed by reference.
    434454         */
    435         $comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) );
     455        $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
    436456
    437457        wp_cache_add( $cache_key, $comments, 'comment' );
Note: See TracChangeset for help on using the changeset viewer.