Make WordPress Core

Ticket #29785: 29785.4.diff

File 29785.4.diff, 2.0 KB (added by johnbillion, 7 years ago)
  • src/wp-includes/user.php

     
    857857                $select_count = implode(', ', $select_count);
    858858
    859859                // Add the meta_value index to the selection list, then run the query.
    860                 $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
     860                $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta JOIN $wpdb->users ON user_id = ID WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
    861861
    862862                // Run the previous loop again to associate results with role names.
    863863                $col = 0;
     
    881881                        'none' => 0,
    882882                );
    883883
    884                 $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
     884                $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta JOIN $wpdb->users ON user_id = ID WHERE meta_key = '{$blog_prefix}capabilities'" );
    885885
    886886                foreach ( $users_of_blog as $caps_meta ) {
    887887                        $b_roles = maybe_unserialize($caps_meta);
  • tests/phpunit/tests/user/countUsers.php

     
    186186                );
    187187        }
    188188
     189        /**
     190         * @ticket 29785
     191         *
     192         * @dataProvider data_count_users_strategies
     193         */
     194        public function test_should_not_count_users_who_are_not_in_posts_table( $strategy ) {
     195                global $wpdb;
     196
     197                // Get a 'before' count for comparison.
     198                $count = count_users( $strategy );
     199
     200                $u = self::factory()->user->create( array(
     201                        'role' => 'editor',
     202                ) );
     203
     204                // Manually delete the user, but leave the capabilities usermeta.
     205                $wpdb->delete( $wpdb->users, array(
     206                        'ID' => $u,
     207                ) );
     208
     209                $count2 = count_users( $strategy );
     210
     211                $this->assertEqualSets( $count, $count2 );
     212        }
     213
    189214}