From eb4db0e19c31848fae9df60db4d373dd4e5d06fb Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 30 Apr 2017 13:03:41 +0000 Subject: [PATCH] Users: Ensure user counts remain accurate if users are added to or removed from the `users` table without corresponding `usermeta` entries being added or removed. This has a slight performance impact on sites with a large number of users when the `time` strategy is used for counting users. Hopefully this impact will be negated by enhancements proposed in #38741. Props psoluch, dots, boonebgorges, ptbello, tharsheblows Fixes #29785 Built from https://develop.svn.wordpress.org/trunk@40560 git-svn-id: http://core.svn.wordpress.org/trunk@40429 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/user.php | 14 ++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/wp-includes/user.php b/wp-includes/user.php index dcd102623a..2bb4e0543e 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -857,7 +857,12 @@ function count_users($strategy = 'time') { $select_count = implode(', ', $select_count); // Add the meta_value index to the selection list, then run the query. - $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N ); + $row = $wpdb->get_row( " + SELECT {$select_count}, COUNT(*) + FROM {$wpdb->usermeta} + INNER JOIN {$wpdb->users} ON user_id = ID + WHERE meta_key = '{$blog_prefix}capabilities' + ", ARRAY_N ); // Run the previous loop again to associate results with role names. $col = 0; @@ -881,7 +886,12 @@ function count_users($strategy = 'time') { 'none' => 0, ); - $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" ); + $users_of_blog = $wpdb->get_col( " + SELECT meta_value + FROM {$wpdb->usermeta} + INNER JOIN {$wpdb->users} ON user_id = ID + WHERE meta_key = '{$blog_prefix}capabilities' + " ); foreach ( $users_of_blog as $caps_meta ) { $b_roles = maybe_unserialize($caps_meta); diff --git a/wp-includes/version.php b/wp-includes/version.php index ff5a669628..e274a983fe 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.8-alpha-40559'; +$wp_version = '4.8-alpha-40560'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.