Ticket #21003: 21003.2.diff
File 21003.2.diff, 3.9 KB (added by , 12 years ago) |
---|
-
wp-includes/comment.php
190 190 * @since 3.1.0 191 191 */ 192 192 class 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; 193 201 194 202 /** 195 203 * Execute the query … … 223 231 'user_id' => '', 224 232 'search' => '', 225 233 'count' => false, 234 'meta_key' => '', 235 'meta_value' => '', 236 'meta_query' => '', 226 237 ); 227 238 239 $groupby = ''; 240 228 241 $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 229 247 do_action_ref_array( 'pre_get_comments', array( &$this ) ); 230 248 extract( $this->query_vars, EXTR_SKIP ); 231 249 232 250 // $args can be whatever, only use the args defined in defaults to compute the key 233 251 $key = md5( serialize( compact(array_keys($defaults)) ) ); 234 252 $last_changed = wp_cache_get('last_changed', 'comment'); 235 if ( ! $last_changed ) {253 if ( ! $last_changed ) { 236 254 $last_changed = time(); 237 255 wp_cache_set('last_changed', $last_changed, 'comment'); 238 256 } … … 257 275 258 276 if ( ! empty( $orderby ) ) { 259 277 $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', 279 294 ); 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 } 280 308 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); 281 309 } else { 282 310 $orderby = 'comment_date_gmt'; … … 329 357 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); 330 358 } 331 359 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' ); 333 368 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 334 369 foreach ( $pieces as $piece ) 335 370 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 336 371 337 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits"; 372 if ( $groupby ) 373 $groupby = 'GROUP BY ' . $groupby; 338 374 375 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits"; 376 339 377 if ( $count ) 340 378 return $wpdb->get_var( $query ); 341 379