Ticket #21003: comment.php.patch

File comment.php.patch, 3.0 KB (added by nikolov.tmw, 11 months ago)

Patch for integrating meta_query and meta_value ordering for comments queries

  • 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 
     
    222230                        'type' => '', 
    223231                        'user_id' => '', 
    224232                        'search' => '', 
    225                         'count' => false 
     233                        'count' => false, 
     234                        'meta_key' => '', 
     235                        'meta_value' => '', 
     236                        'meta_query' => '', 
    226237                ); 
    227238 
    228239                $this->query_vars = wp_parse_args( $query_vars, $defaults ); 
     240                // Parse meta query 
     241                $this->meta_query = new WP_Meta_Query(); 
     242                $this->meta_query->parse_query_vars( $this->query_vars ); 
     243 
    229244                do_action_ref_array( 'pre_get_comments', array( &$this ) ); 
    230245                extract( $this->query_vars, EXTR_SKIP ); 
    231246 
     
    259274 
    260275                if ( ! empty( $orderby ) ) { 
    261276                        $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby); 
    262                         $ordersby = array_intersect( 
    263                                 $ordersby, 
    264                                 array( 
    265                                         'comment_agent', 
    266                                         'comment_approved', 
    267                                         'comment_author', 
    268                                         'comment_author_email', 
    269                                         'comment_author_IP', 
    270                                         'comment_author_url', 
    271                                         'comment_content', 
    272                                         'comment_date', 
    273                                         'comment_date_gmt', 
    274                                         'comment_ID', 
    275                                         'comment_karma', 
    276                                         'comment_parent', 
    277                                         'comment_post_ID', 
    278                                         'comment_type', 
    279                                         'user_id', 
    280                                 ) 
     277                        $allowed_keys = array( 
     278                                'comment_agent', 
     279                                'comment_approved', 
     280                                'comment_author', 
     281                                'comment_author_email', 
     282                                'comment_author_IP', 
     283                                'comment_author_url', 
     284                                'comment_content', 
     285                                'comment_date', 
     286                                'comment_date_gmt', 
     287                                'comment_ID', 
     288                                'comment_karma', 
     289                                'comment_parent', 
     290                                'comment_post_ID', 
     291                                'comment_type', 
     292                                'user_id', 
    281293                        ); 
     294                        if ( !empty($this->query_vars['meta_key']) ) { 
     295                                $allowed_keys[] = $q['meta_key']; 
     296                                $allowed_keys[] = 'meta_value'; 
     297                                $allowed_keys[] = 'meta_value_num'; 
     298                        } 
     299                        $ordersby = array_intersect( $ordersby, $allowed_keys ); 
     300                        foreach ($ordersby as $key => $value) { 
     301                                if ( $value == $q['meta_key'] || $value == 'meta_value' ) { 
     302                                        $ordersby[$key] = "$wpdb->commentmeta.meta_value"; 
     303                                } elseif ( $value == 'meta_value_num' ) { 
     304                                        $ordersby[$key] = "$wpdb->commentmeta.meta_value+0"; 
     305                                } 
     306                        } 
    282307                        $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); 
    283308                } else { 
    284309                        $orderby = 'comment_date_gmt'; 
     
    331356                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); 
    332357                } 
    333358 
     359                if ( !empty( $this->meta_query->queries ) ) { 
     360                        $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 
     361                        $join .= $clauses['join']; 
     362                        $where .= $clauses['where']; 
     363                } 
     364 
    334365                $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); 
    335366                $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 
    336367                foreach ( $pieces as $piece )