Make WordPress Core

Ticket #38741: 38741.3.diff

File 38741.3.diff, 6.3 KB (added by tharsheblows, 8 years ago)

with tests and cleaned up a bit

  • src/wp-admin/includes/class-wp-users-list-table.php

     
    173173                global $role;
    174174
    175175                $wp_roles = wp_roles();
     176                $count_users = true;
    176177
     178                if ( wp_is_large_site() ) {
     179                        $count_users = false;
     180                } elseif ( is_multisite() && wp_is_large_network( 'users' ) ) {
     181                        $count_users = false;
     182                }
     183
    177184                if ( $this->is_site_users ) {
    178185                        $url = 'site-users.php?id=' . $this->site_id;
    179                         switch_to_blog( $this->site_id );
    180                         $users_of_blog = count_users();
    181                         restore_current_blog();
     186                        if ( $count_users ) {
     187                                switch_to_blog( $this->site_id );
     188                                $users_of_blog = count_users();
     189                                restore_current_blog();
     190                        }
    182191                } else {
    183192                        $url = 'users.php';
    184                         $users_of_blog = count_users();
     193                        if ( $count_users ) {
     194                                $users_of_blog = count_users();
     195                        }
    185196                }
    186197
    187                 $total_users = $users_of_blog['total_users'];
    188                 $avail_roles =& $users_of_blog['avail_roles'];
    189                 unset($users_of_blog);
     198                if ( $count_users ) {
     199                        $total_users =  $users_of_blog['total_users'];
     200                        $avail_roles =& $users_of_blog['avail_roles'];
     201                        unset( $users_of_blog );
     202                } else {
     203                        $avail_roles = array();
     204                }
    190205
    191206                $class = empty($role) ? ' class="current"' : '';
    192207                $role_links = array();
    193                 $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>';
     208
     209                if ( $count_users ) {
     210                        $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>';
     211                } else {
     212                        $role_links['all'] = "<a href='$url'$class>" . _x( 'All', 'users' ) . '</a>';
     213                }
     214
    194215                foreach ( $wp_roles->get_names() as $this_role => $name ) {
    195                         if ( !isset($avail_roles[$this_role]) )
     216                        if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
    196217                                continue;
     218                        }
    197219
    198220                        $class = '';
    199221
     
    202224                        }
    203225
    204226                        $name = translate_user_role( $name );
    205                         /* translators: User role name with count */
    206                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
     227                        if ( $count_users ) {
     228                                /* translators: User role name with count */
     229                                $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
     230                        }
    207231                        $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
    208232                }
    209233
    210                 if ( ! empty( $avail_roles['none' ] ) ) {
     234                if ( ! $count_users || ! empty( $avail_roles['none' ] ) ) {
    211235
    212236                        $class = '';
    213237
     
    216240                        }
    217241
    218242                        $name = __( 'No role' );
    219                         /* translators: User role name with count */
    220                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
     243                        if ( $count_users ) {
     244                                /* translators: User role name with count */
     245                                $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
     246                        }
    221247                        $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
    222248
    223249                }
  • src/wp-includes/functions.php

     
    56425642
    56435643        return $last_changed;
    56445644}
     5645
     5646/**
     5647 * Whether or not we have a large site.
     5648 *
     5649 * The default criteria for a large site is more than 10,000 users.
     5650 *
     5651 * @since x.x.x
     5652 *
     5653 * @return bool True if the site meets the criteria for large. False otherwise.
     5654 */
     5655function wp_is_large_site() {
     5656        $count = wp_get_active_user_count();
     5657        /**
     5658         * Filters whether the site is considered large.
     5659         *
     5660         * @since x.x.x
     5661         *
     5662         * @param bool   $is_large_site Whether the site has more than 10000 users.
     5663         * @param int    $count         The count of items for the component.
     5664         */
     5665        return apply_filters( 'wp_is_large_site', $count > 10000, $count );
     5666}
     5667
     5668/**
     5669 * Update the active user count.
     5670 *
     5671 * @since x.x.x
     5672 *
     5673 * @global wpdb $wpdb WordPress database abstraction object.
     5674 */
     5675function wp_active_user_count() {
     5676        global $wpdb;
     5677
     5678        $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users" );
     5679        return $count;
     5680}
     5681
     5682/**
     5683 * The number of active users.
     5684 *
     5685 * The count is cached for a minimum of twelve hours. This is not a live count.
     5686 *
     5687 * @since x.x.x
     5688 *
     5689 * @return int
     5690 */
     5691function wp_get_active_user_count() {
     5692        $count = get_transient( 'active_user_count' );
     5693
     5694        if ( false === $count ) {
     5695                $count = wp_active_user_count();
     5696                set_transient( 'active_user_count', $count, 12 * HOUR_IN_SECONDS );
     5697        }
     5698
     5699        return (int) $count;
     5700}
  • src/wp-includes/update.php

     
    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

     
    11141114
    11151115                return $data;
    11161116        }
     1117
     1118        /*
     1119         * @ticket 38741
     1120         */
     1121       
     1122        public function test_wp_get_active_user_count(){
     1123
     1124                // make twenty additional users to make 21 users
     1125                for( $i = 0; $i < 20; $i++ ){
     1126                        self::factory()->user->create();
     1127                }
     1128
     1129                $user_count = wp_active_user_count();
     1130
     1131                $this->assertEquals( $user_count, 21 );
     1132        }
     1133
     1134        /*
     1135         * @ticket 38741
     1136         */
     1137        public function test_wp_is_large_site(){
     1138
     1139                if ( wp_using_ext_object_cache() ) {
     1140                        $this->markTestSkipped( 'Not testable with an external object cache.' );
     1141                }
     1142
     1143                // set the 'active_user_count' transient to over 10000 to emulate a large site
     1144                set_transient( 'active_user_count', 10001 );
     1145                $this->assertTrue( wp_is_large_site() );
     1146                delete_transient( 'active_user_count' );
     1147        }
    11171148}