Make WordPress Core

Ticket #38741: 38741.6.diff

File 38741.6.diff, 8.4 KB (added by johnbillion, 7 years ago)
  • src/wp-admin/includes/class-wp-users-list-table.php

    diff --git src/wp-admin/includes/class-wp-users-list-table.php src/wp-admin/includes/class-wp-users-list-table.php
    index 61ea897c5a..6825bca5f0 100644
    class WP_Users_List_Table extends WP_List_Table { 
    166166                global $role;
    167167
    168168                $wp_roles = wp_roles();
     169                $count_users = true;
     170
     171                if ( wp_is_large_user_count() ) {
     172                        $count_users = false;
     173                } elseif ( is_multisite() && wp_is_large_network( 'users' ) ) {
     174                        $count_users = false;
     175                }
    169176
    170177                if ( $this->is_site_users ) {
    171178                        $url = 'site-users.php?id=' . $this->site_id;
    172                         switch_to_blog( $this->site_id );
    173                         $users_of_blog = count_users( 'time', $this->site_id );
    174                         restore_current_blog();
     179                        if ( $count_users ) {
     180                                switch_to_blog( $this->site_id );
     181                                $users_of_blog = count_users( 'time', $this->site_id );
     182                                restore_current_blog();
     183                        }
    175184                } else {
    176185                        $url = 'users.php';
    177                         $users_of_blog = count_users();
     186                        if ( $count_users ) {
     187                                $users_of_blog = count_users();
     188                        }
    178189                }
    179190
    180                 $total_users = $users_of_blog['total_users'];
    181                 $avail_roles =& $users_of_blog['avail_roles'];
    182                 unset($users_of_blog);
     191                if ( $count_users ) {
     192                        $total_users = $users_of_blog['total_users'];
     193                        $avail_roles =& $users_of_blog['avail_roles'];
     194                        unset($users_of_blog);
     195                } else {
     196                        $avail_roles = array();
     197                }
    183198
    184199                $class = empty($role) ? ' class="current"' : '';
    185200                $role_links = array();
    186                 $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
     201
     202                if ( $count_users ) {
     203                        $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
     204                } else {
     205                        $role_links['all'] = "<a href='$url'$class>" . _x( 'All', 'users' ) . '</a>';
     206                }
    187207                foreach ( $wp_roles->get_names() as $this_role => $name ) {
    188                         if ( !isset($avail_roles[$this_role]) )
     208                        if ( $count_users && !isset($avail_roles[$this_role]) ) {
    189209                                continue;
     210                        }
    190211
    191212                        $class = '';
    192213
    class WP_Users_List_Table extends WP_List_Table { 
    195216                        }
    196217
    197218                        $name = translate_user_role( $name );
    198                         /* translators: User role name with count */
    199                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
     219                        if ( $count_users ) {
     220                                /* translators: User role name with count */
     221                                $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
     222                        }
    200223                        $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
    201224                }
    202225
    203                 if ( ! empty( $avail_roles['none' ] ) ) {
     226                if ( ! $count_users || ! empty( $avail_roles['none' ] ) ) {
    204227
    205228                        $class = '';
    206229
    class WP_Users_List_Table extends WP_List_Table { 
    209232                        }
    210233
    211234                        $name = __( 'No role' );
    212                         /* translators: User role name with count */
    213                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
     235                        if ( $count_users ) {
     236                                /* translators: User role name with count */
     237                                $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
     238                        }
    214239                        $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
    215240
    216241                }
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index a196f4ff19..23355d0759 100644
    add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 ); 
    464464// Widgets
    465465add_action( 'init', 'wp_widgets_init', 1 );
    466466
     467// User counts
     468foreach ( array( 'user_register', 'deleted_user' ) as $action ){
     469        add_action( $action, 'wp_update_active_user_count' );
     470}
     471
    467472// Admin Bar
    468473// Don't remove. Wrong way to disable.
    469474add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 03c6126d5b..22314b2910 100644
    All at ###SITENAME### 
    57345734                $site_name
    57355735        ), $email_change_email['message'], $email_change_email['headers'] );
    57365736}
     5737
     5738/**
     5739 * Whether or not we have a large site, based on its number of users.
     5740 *
     5741 * The default criteria for a large site is more than 10,000 users.
     5742 *
     5743 * @since x.x.x
     5744 *
     5745 * @return bool True if the site meets the criteria for large. False otherwise.
     5746 */
     5747function wp_is_large_user_count() {
     5748        $count = wp_get_active_user_count();
     5749return true;
     5750        /**
     5751         * Filters whether the site is considered large, based on its number of users.
     5752         *
     5753         * @since x.x.x
     5754         *
     5755         * @param bool   $is_large_user_count Whether the site has more than 10000 users.
     5756         * @param int    $count         The count of items for the component.
     5757         */
     5758        return apply_filters( 'wp_is_large_user_count', $count > 10000, $count );
     5759}
     5760
     5761/**
     5762 * Update the active user count.
     5763 *
     5764 * @since x.x.x
     5765 *
     5766 * @global wpdb $wpdb WordPress database abstraction object.
     5767 *
     5768 * @return int The active user count.
     5769 */
     5770function wp_update_active_user_count() {
     5771        global $wpdb;
     5772
     5773        $count = $wpdb->get_var( "
     5774                SELECT COUNT(ID) as c
     5775                FROM $wpdb->users
     5776        " );
     5777        update_option( 'active_user_count', $count );
     5778
     5779        return (int) $count;
     5780}
     5781
     5782/**
     5783 * The number of active users.
     5784 *
     5785 * @since x.x.x
     5786 *
     5787 * @return int The active user count.
     5788 */
     5789function wp_get_active_user_count() {
     5790        $count = get_option( 'active_user_count', false );
     5791
     5792        if ( false === $count ) {
     5793                $count = wp_update_active_user_count();
     5794        }
     5795        return (int) $count;
     5796}
  • src/wp-includes/ms-functions.php

    diff --git src/wp-includes/ms-functions.php src/wp-includes/ms-functions.php
    index 7c28844e0c..2dd90d0432 100644
    function wp_is_large_network( $using = 'sites', $network_id = null ) { 
    25502550
    25512551        if ( 'users' == $using ) {
    25522552                $count = get_user_count( $network_id );
     2553                $is_large = ( $count > 10000 );
     2554
     2555                /** This filter is documented in wp-includes/functions.php */
     2556                $is_large = apply_filters( 'wp_is_large_user_count', $is_large, $count );
     2557
    25532558                /**
    25542559                 * Filters whether the network is considered large.
    25552560                 *
    function wp_is_large_network( $using = 'sites', $network_id = null ) { 
    25612566                 * @param int    $count            The count of items for the component.
    25622567                 * @param int    $network_id       The ID of the network being checked.
    25632568                 */
    2564                 return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count, $network_id );
     2569                return apply_filters( 'wp_is_large_network', $is_large, 'users', $count, $network_id );
    25652570        }
    25662571
    25672572        $count = get_blog_count( $network_id );
  • src/wp-includes/update.php

    diff --git src/wp-includes/update.php src/wp-includes/update.php
    index f25e644d62..a14e050bd3 100644
    function wp_version_check( $extra_stats = array(), $force_check = false ) { 
    7878                $wp_install = network_site_url();
    7979                $multisite_enabled = 1;
    8080        } else {
    81                 $user_count = count_users();
    82                 $user_count = $user_count['total_users'];
     81                $user_count = wp_get_active_user_count();
    8382                $multisite_enabled = 0;
    8483                $num_blogs = 1;
    8584                $wp_install = home_url( '/' );
  • tests/phpunit/tests/functions.php

    diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php
    index 252d890bbb..4f231cc286 100644
    class Tests_Functions extends WP_UnitTestCase { 
    11101110
    11111111                return $data;
    11121112        }
     1113
     1114        /*
     1115         * @ticket 38741
     1116         */
     1117       
     1118        public function test_wp_get_active_user_count(){
     1119
     1120                // make twenty additional users to make 21 users
     1121                for( $i = 0; $i < 20; $i++ ){
     1122                        self::factory()->user->create();
     1123                }
     1124
     1125                $user_count = wp_get_active_user_count();
     1126
     1127                $this->assertEquals( $user_count, 21 );
     1128        }
     1129
     1130        /*
     1131         * @ticket 38741
     1132         */
     1133        public function test_wp_is_large_user_count(){
     1134
     1135                if ( wp_using_ext_object_cache() ) {
     1136                        $this->markTestSkipped( 'Not testable with an external object cache.' );
     1137                }
     1138
     1139                // set the 'active_user_count' transient to over 10000 to emulate a large site
     1140                update_option( 'active_user_count', 10001 );
     1141                $this->assertTrue( wp_is_large_user_count() );
     1142                delete_option( 'active_user_count' );
     1143        }
    11131144}