Ticket #25292: 25292.diff
File 25292.diff, 5.4 KB (added by , 11 years ago) |
---|
-
src/wp-includes/user.php
409 409 $this->query_fields = "$wpdb->users.ID"; 410 410 } 411 411 412 if ( $qv['count_total'] )412 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) 413 413 $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; 414 414 415 415 $this->query_from = "FROM $wpdb->users"; 416 416 $this->query_where = "WHERE 1=1"; 417 417 418 418 // sorting 419 if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) { 420 $orderby = 'user_' . $qv['orderby']; 421 } elseif ( in_array( $qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered') ) ) { 422 $orderby = $qv['orderby']; 423 } elseif ( 'name' == $qv['orderby'] || 'display_name' == $qv['orderby'] ) { 424 $orderby = 'display_name'; 425 } elseif ( 'post_count' == $qv['orderby'] ) { 426 // todo: avoid the JOIN 427 $where = get_posts_by_author_sql('post'); 428 $this->query_from .= " LEFT OUTER JOIN ( 429 SELECT post_author, COUNT(*) as post_count 430 FROM $wpdb->posts 431 $where 432 GROUP BY post_author 433 ) p ON ({$wpdb->users}.ID = p.post_author) 434 "; 435 $orderby = 'post_count'; 436 } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) { 437 $orderby = 'ID'; 438 } elseif ( 'meta_value' == $qv['orderby'] ) { 439 $orderby = "$wpdb->usermeta.meta_value"; 440 } else { 441 $orderby = 'user_login'; 419 if ( isset( $qv['orderby'] ) ) { 420 if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) { 421 $orderby = 'user_' . $qv['orderby']; 422 } elseif ( in_array( $qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered') ) ) { 423 $orderby = $qv['orderby']; 424 } elseif ( 'name' == $qv['orderby'] || 'display_name' == $qv['orderby'] ) { 425 $orderby = 'display_name'; 426 } elseif ( 'post_count' == $qv['orderby'] ) { 427 // todo: avoid the JOIN 428 $where = get_posts_by_author_sql('post'); 429 $this->query_from .= " LEFT OUTER JOIN ( 430 SELECT post_author, COUNT(*) as post_count 431 FROM $wpdb->posts 432 $where 433 GROUP BY post_author 434 ) p ON ({$wpdb->users}.ID = p.post_author) 435 "; 436 $orderby = 'post_count'; 437 } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) { 438 $orderby = 'ID'; 439 } elseif ( 'meta_value' == $qv['orderby'] ) { 440 $orderby = "$wpdb->usermeta.meta_value"; 441 } else { 442 $orderby = 'user_login'; 443 } 442 444 } 443 445 444 $qv['order'] = strtoupper( $qv['order'] ); 446 if ( empty( $orderby ) ) 447 $orderby = 'user_login'; 448 449 $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; 445 450 if ( 'ASC' == $qv['order'] ) 446 451 $order = 'ASC'; 447 452 else … … 449 454 $this->query_orderby = "ORDER BY $orderby $order"; 450 455 451 456 // limit 452 if ( $qv['number'] ) {457 if ( isset( $qv['number'] ) && $qv['number'] ) { 453 458 if ( $qv['offset'] ) 454 459 $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); 455 460 else 456 461 $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); 457 462 } 458 463 459 $search = trim( $qv['search'] ); 464 $search = ''; 465 if ( isset( $qv['search'] ) ) 466 $search = trim( $qv['search'] ); 467 460 468 if ( $search ) { 461 469 $leading_wild = ( ltrim($search, '*') != $search ); 462 470 $trailing_wild = ( rtrim($search, '*') != $search ); … … 490 498 $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); 491 499 } 492 500 493 $blog_id = absint( $qv['blog_id'] ); 501 $blog_id = 0; 502 if ( isset( $qv['blog_id'] ) ) 503 $blog_id = absint( $qv['blog_id'] ); 494 504 495 if ( 'authors' == $qv['who'] && $blog_id ) {505 if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { 496 506 $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level'; 497 507 $qv['meta_value'] = 0; 498 508 $qv['meta_compare'] = '!='; 499 509 $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query 500 510 } 501 511 502 $role = trim( $qv['role'] ); 512 $role = ''; 513 if ( isset( $qv['role'] ) ) 514 $role = trim( $qv['role'] ); 503 515 504 516 if ( $blog_id && ( $role || is_multisite() ) ) { 505 517 $cap_meta_query = array(); … … 525 537 $this->query_fields = 'DISTINCT ' . $this->query_fields; 526 538 } 527 539 528 if ( ! empty( $qv['include'] ) ) {540 if ( ! empty( $qv['include'] ) ) { 529 541 $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); 530 542 $this->query_where .= " AND $wpdb->users.ID IN ($ids)"; 531 } elseif ( ! empty($qv['exclude']) ) {543 } elseif ( ! empty( $qv['exclude'] ) ) { 532 544 $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); 533 545 $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)"; 534 546 } … … 553 565 $this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"); 554 566 } 555 567 556 if ( $qv['count_total'] )568 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) 557 569 $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); 558 570 559 571 if ( !$this->results ) -
tests/phpunit/tests/user/query.php
20 20 $users = new WP_User_Query(); 21 21 22 22 $this->assertEquals( '', $users->get( 'fields' ) ); 23 $this->assertEquals( '', $users->query_vars['fields'] );23 $this->assertEquals( '', @$users->query_vars['fields'] ); 24 24 25 25 $users->set( 'fields', 'all' ); 26 26