Make WordPress Core

Changeset 22074


Ignore:
Timestamp:
09/27/2012 09:53:14 PM (14 years ago)
Author:
ryan
Message:

Support meta queries in WP_Comment_Query.

Props nikolov.tmw
fixes #21003

File:
1 edited

Legend:

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

    r22068 r22074  
    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        /**
     
    224232                        'search' => '',
    225233                        'count' => false,
     234                        'meta_key' => '',
     235                        'meta_value' => '',
     236                        'meta_query' => '',
    226237                );
    227238
     239                $groupby = '';
     240
    228241                $this->query_vars = wp_parse_args( $query_vars, $defaults );
     242
     243                // Parse meta query
     244                $this->meta_query = new WP_Meta_Query();
     245                $this->meta_query->parse_query_vars( $this->query_vars );
     246
    229247                do_action_ref_array( 'pre_get_comments', array( &$this ) );
    230248                extract( $this->query_vars, EXTR_SKIP );
     
    233251                $key = md5( serialize( compact(array_keys($defaults)) )  );
    234252                $last_changed = wp_cache_get('last_changed', 'comment');
    235                 if ( !$last_changed ) {
     253                if ( ! $last_changed ) {
    236254                        $last_changed = time();
    237255                        wp_cache_set('last_changed', $last_changed, 'comment');
     
    240258
    241259                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
    242                         return $cache;
     260                        //return $cache;
    243261                }
    244262
     
    258276                if ( ! empty( $orderby ) ) {
    259277                        $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                                 )
     278                        $allowed_keys = array(
     279                                'comment_agent',
     280                                'comment_approved',
     281                                'comment_author',
     282                                'comment_author_email',
     283                                'comment_author_IP',
     284                                'comment_author_url',
     285                                'comment_content',
     286                                'comment_date',
     287                                'comment_date_gmt',
     288                                'comment_ID',
     289                                'comment_karma',
     290                                'comment_parent',
     291                                'comment_post_ID',
     292                                'comment_type',
     293                                'user_id',
    279294                        );
     295                        if ( ! empty( $this->query_vars['meta_key'] ) ) {
     296                                $allowed_keys[] = $q['meta_key'];
     297                                $allowed_keys[] = 'meta_value';
     298                                $allowed_keys[] = 'meta_value_num';
     299                        }
     300                        $ordersby = array_intersect( $ordersby, $allowed_keys );
     301                        foreach ( $ordersby as $key => $value ) {
     302                                if ( $value == $q['meta_key'] || $value == 'meta_value' ) {
     303                                        $ordersby[ $key ] = "$wpdb->commentmeta.meta_value";
     304                                } elseif ( $value == 'meta_value_num' ) {
     305                                        $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0";
     306                                }
     307                        }
    280308                        $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
    281309                } else {
     
    330358                }
    331359
    332                 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
     360                if ( ! empty( $this->meta_query->queries ) ) {
     361                        $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
     362                        $join .= $clauses['join'];
     363                        $where .= $clauses['where'];
     364                        $groupby = "{$wpdb->comments}.comment_ID";
     365                }
     366
     367                $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
    333368                $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
    334369                foreach ( $pieces as $piece )
    335370                        $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    336371
    337                 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits";
     372                if ( $groupby )
     373                        $groupby = 'GROUP BY ' . $groupby;
     374
     375                $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
    338376
    339377                if ( $count )
Note: See TracChangeset for help on using the changeset viewer.