WordPress.org

Make WordPress Core

Ticket #15861: 15861.patch

File 15861.patch, 4.2 KB (added by SergeyBiryukov, 22 months ago)
  • wp-admin/includes/class-wp-users-list-table.php

     
    178178                        'username' => 'login', 
    179179                        'name'     => 'name', 
    180180                        'email'    => 'email', 
     181                        'posts'    => 'post_count' 
    181182                ); 
    182183 
    183184                if ( $this->is_site_users ) 
  • wp-includes/user.php

     
    426426                $this->query_where = "WHERE 1=1"; 
    427427 
    428428                // sorting 
     429                $qv['order'] = strtoupper( $qv['order'] ); 
     430                if ( 'ASC' == $qv['order'] ) 
     431                        $order = 'ASC'; 
     432                else 
     433                        $order = 'DESC'; 
     434 
    429435                if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) { 
    430436                        $orderby = 'user_' . $qv['orderby']; 
    431437                } elseif ( in_array( $qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered') ) ) { 
     
    433439                } elseif ( 'name' == $qv['orderby'] || 'display_name' == $qv['orderby'] ) { 
    434440                        $orderby = 'display_name'; 
    435441                } elseif ( 'post_count' == $qv['orderby'] ) { 
    436                         // todo: avoid the JOIN 
    437442                        $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 = ''; 
    446449                } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) { 
    447450                        $orderby = 'ID'; 
    448451                } else { 
    449452                        $orderby = 'user_login'; 
    450453                } 
    451454 
    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" : ''; 
    458456 
    459457                // limit 
    460                 if ( $qv['number'] ) { 
     458                if ( $qv['number'] && 'post_count' !== $qv['orderby'] ) { 
    461459                        if ( $qv['offset'] ) 
    462460                                $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); 
    463461                        else 
     
    558556                if ( !$this->results ) 
    559557                        return; 
    560558 
     559                if ( 'post_count' == $this->query_vars['orderby'] ) 
     560                        $this->_sort_by_post_count(); 
     561 
    561562                if ( 'all_with_meta' == $this->query_vars['fields'] ) { 
    562563                        cache_users( $this->results ); 
    563564 
     
    620621        function get_total() { 
    621622                return $this->total_users; 
    622623        } 
     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        } 
    623676} 
    624677 
    625678/**