Ticket #21003: 21003.diff

File 21003.diff, 3.3 KB (added by ryan, 8 months ago)

Refresh

  • wp-includes/comment.php

     
    190190 * @since 3.1.0 
    191191 */ 
    192192class WP_Comment_Query { 
     193        /** 
     194         * Metadata query container 
     195         * 
     196         * @since 3.? 
     197         * @access public 
     198         * @var object WP_Meta_Query 
     199         */ 
     200        var $meta_query = false; 
    193201 
    194202        /** 
    195203         * Execute the query 
     
    223231                        'user_id' => '', 
    224232                        'search' => '', 
    225233                        'count' => false, 
     234                        'meta_key' => '', 
     235                        'meta_value' => '', 
     236                        'meta_query' => '', 
    226237                ); 
    227238 
    228239                $this->query_vars = wp_parse_args( $query_vars, $defaults ); 
     240 
     241                // Parse meta query 
     242                $this->meta_query = new WP_Meta_Query(); 
     243                $this->meta_query->parse_query_vars( $this->query_vars ); 
     244 
    229245                do_action_ref_array( 'pre_get_comments', array( &$this ) ); 
    230246                extract( $this->query_vars, EXTR_SKIP ); 
    231247 
    232248                // $args can be whatever, only use the args defined in defaults to compute the key 
    233249                $key = md5( serialize( compact(array_keys($defaults)) )  ); 
    234250                $last_changed = wp_cache_get('last_changed', 'comment'); 
    235                 if ( !$last_changed ) { 
     251                if ( ! $last_changed ) { 
    236252                        $last_changed = time(); 
    237253                        wp_cache_set('last_changed', $last_changed, 'comment'); 
    238254                } 
     
    257273 
    258274                if ( ! empty( $orderby ) ) { 
    259275                        $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby); 
    260                         $ordersby = array_intersect( 
    261                                 $ordersby, 
    262                                 array( 
    263                                         'comment_agent', 
    264                                         'comment_approved', 
    265                                         'comment_author', 
    266                                         'comment_author_email', 
    267                                         'comment_author_IP', 
    268                                         'comment_author_url', 
    269                                         'comment_content', 
    270                                         'comment_date', 
    271                                         'comment_date_gmt', 
    272                                         'comment_ID', 
    273                                         'comment_karma', 
    274                                         'comment_parent', 
    275                                         'comment_post_ID', 
    276                                         'comment_type', 
    277                                         'user_id', 
    278                                 ) 
     276                        $allowed_keys = array( 
     277                                'comment_agent', 
     278                                'comment_approved', 
     279                                'comment_author', 
     280                                'comment_author_email', 
     281                                'comment_author_IP', 
     282                                'comment_author_url', 
     283                                'comment_content', 
     284                                'comment_date', 
     285                                'comment_date_gmt', 
     286                                'comment_ID', 
     287                                'comment_karma', 
     288                                'comment_parent', 
     289                                'comment_post_ID', 
     290                                'comment_type', 
     291                                'user_id', 
    279292                        ); 
     293                        if ( ! empty( $this->query_vars['meta_key'] ) ) { 
     294                                $allowed_keys[] = $q['meta_key']; 
     295                                $allowed_keys[] = 'meta_value'; 
     296                                $allowed_keys[] = 'meta_value_num'; 
     297                        } 
     298                        $ordersby = array_intersect( $ordersby, $allowed_keys ); 
     299                        foreach ( $ordersby as $key => $value ) { 
     300                                if ( $value == $q['meta_key'] || $value == 'meta_value' ) { 
     301                                        $ordersby[ $key ] = "$wpdb->commentmeta.meta_value"; 
     302                                } elseif ( $value == 'meta_value_num' ) { 
     303                                        $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0"; 
     304                                } 
     305                        } 
    280306                        $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); 
    281307                } else { 
    282308                        $orderby = 'comment_date_gmt'; 
     
    329355                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); 
    330356                } 
    331357 
     358                if ( ! empty( $this->meta_query->queries ) ) { 
     359                        $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 
     360                        $join .= $clauses['join']; 
     361                        $where .= $clauses['where']; 
     362                } 
     363 
    332364                $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); 
    333365                $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 
    334366                foreach ( $pieces as $piece )