Ticket #15861: 15861.patch
File 15861.patch, 4.2 KB (added by , 13 years ago) |
---|
-
wp-admin/includes/class-wp-users-list-table.php
178 178 'username' => 'login', 179 179 'name' => 'name', 180 180 'email' => 'email', 181 'posts' => 'post_count' 181 182 ); 182 183 183 184 if ( $this->is_site_users ) -
wp-includes/user.php
426 426 $this->query_where = "WHERE 1=1"; 427 427 428 428 // sorting 429 $qv['order'] = strtoupper( $qv['order'] ); 430 if ( 'ASC' == $qv['order'] ) 431 $order = 'ASC'; 432 else 433 $order = 'DESC'; 434 429 435 if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) { 430 436 $orderby = 'user_' . $qv['orderby']; 431 437 } elseif ( in_array( $qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered') ) ) { … … 433 439 } elseif ( 'name' == $qv['orderby'] || 'display_name' == $qv['orderby'] ) { 434 440 $orderby = 'display_name'; 435 441 } elseif ( 'post_count' == $qv['orderby'] ) { 436 // todo: avoid the JOIN437 442 $where = get_posts_by_author_sql('post'); 438 $this->query_from .= " LEFT OUTER JOIN ( 439 SELECT post_author, COUNT(*) as post_count 440 FROM $wpdb->posts 441 $where 442 GROUP BY post_author 443 ) p ON ({$wpdb->users}.ID = p.post_author) 444 "; 445 $orderby = 'post_count'; 443 $this->post_authors_query = "SELECT post_author FROM $wpdb->posts $where GROUP BY post_author ORDER BY COUNT(ID) $order"; 444 445 if ( false === strpos( $this->query_fields, 'ID' ) && false === strpos( $this->query_fields, '*' ) ) 446 $this->query_fields .= ", $wpdb->users.ID"; 447 448 $orderby = ''; 446 449 } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) { 447 450 $orderby = 'ID'; 448 451 } else { 449 452 $orderby = 'user_login'; 450 453 } 451 454 452 $qv['order'] = strtoupper( $qv['order'] ); 453 if ( 'ASC' == $qv['order'] ) 454 $order = 'ASC'; 455 else 456 $order = 'DESC'; 457 $this->query_orderby = "ORDER BY $orderby $order"; 455 $this->query_orderby = ( !empty($orderby) ) ? "ORDER BY $orderby $order" : ''; 458 456 459 457 // limit 460 if ( $qv['number'] ) {458 if ( $qv['number'] && 'post_count' !== $qv['orderby'] ) { 461 459 if ( $qv['offset'] ) 462 460 $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); 463 461 else … … 558 556 if ( !$this->results ) 559 557 return; 560 558 559 if ( 'post_count' == $this->query_vars['orderby'] ) 560 $this->_sort_by_post_count(); 561 561 562 if ( 'all_with_meta' == $this->query_vars['fields'] ) { 562 563 cache_users( $this->results ); 563 564 … … 620 621 function get_total() { 621 622 return $this->total_users; 622 623 } 624 625 /** 626 * Sort found users by post count 627 * 628 * @access private 629 */ 630 function _sort_by_post_count() { 631 global $wpdb; 632 633 $post_authors = $wpdb->get_col( $this->post_authors_query ); 634 635 if ( empty( $post_authors ) ) 636 return; 637 638 if ( 'DESC' == $this->query_vars['order'] ) 639 $post_authors = array_reverse( $post_authors ); 640 641 if ( is_object( $this->results[0] ) ) { 642 foreach ( $post_authors as $post_author ) { 643 $user_in_current_view = false; 644 foreach ( $this->results as $key => $user ) { 645 if ( $user->ID == $post_author ) { 646 $user_in_current_view = true; 647 break; 648 } 649 } 650 651 if ( $user_in_current_view ) { 652 unset( $this->results[$key] ); 653 654 if ( 'DESC' == $this->query_vars['order'] ) 655 array_unshift( $this->results, $user ); 656 else 657 array_push( $this->results, $user ); 658 } 659 } 660 } else { 661 foreach ( $post_authors as $post_author ) { 662 if ( false !== ( $key = array_search( $post_author, $this->results ) ) ) { 663 unset( $this->results[$key] ); 664 665 if ( 'DESC' == $this->query_vars['order'] ) 666 array_unshift( $this->results, $post_author ); 667 else 668 array_push( $this->results, $post_author ); 669 } 670 } 671 } 672 673 if ( '' !== $this->query_vars['offset'] && '' !== $this->query_vars['number'] ) 674 $this->results = array_slice( $this->results, $this->query_vars['offset'], $this->query_vars['number'] ); 675 } 623 676 } 624 677 625 678 /**