WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/08/2015 10:06:46 PM (6 years ago)
Author:
johnbillion
Message:

Introduce the ability to filter the Users admin listing screen by users with no role, if such users exist.

Fixes #22993
Props spmlucas, johnbillion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r34923 r34965  
    736736 *
    737737 * @since 3.0.0
     738 * @since 4.4.0 The number of users with no role is now included in the `none` element.
    738739 *
    739740 * @global wpdb $wpdb
     
    776777        $total_users = (int) $row[$col];
    777778
     779        $role_counts['none'] = ( $total_users - array_sum( $role_counts ) );
     780
    778781        $result['total_users'] = $total_users;
    779782        $result['avail_roles'] =& $role_counts;
    780783    } else {
    781         $avail_roles = array();
     784        $avail_roles = array(
     785            'none' => 0,
     786        );
    782787
    783788        $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
     
    787792            if ( ! is_array( $b_roles ) )
    788793                continue;
     794            if ( empty( $b_roles ) ) {
     795                $avail_roles['none']++;
     796            }
    789797            foreach ( $b_roles as $b_role => $val ) {
    790798                if ( isset($avail_roles[$b_role]) ) {
     
    798806        $result['total_users'] = count( $users_of_blog );
    799807        $result['avail_roles'] =& $avail_roles;
     808    }
     809
     810    if ( is_multisite() ) {
     811        $result['avail_roles']['none'] = 0;
    800812    }
    801813
     
    22322244    $manager->destroy_all();
    22332245}
     2246
     2247/**
     2248 * Get the user IDs of all users with no role on this site.
     2249 *
     2250 * This function returns an empty array when used on Multisite.
     2251 *
     2252 * @since 4.4.0
     2253 *
     2254 * @return array Array of user IDs.
     2255 */
     2256function wp_get_users_with_no_role() {
     2257    global $wpdb;
     2258
     2259    if ( is_multisite() ) {
     2260        return array();
     2261    }
     2262
     2263    $prefix = $wpdb->get_blog_prefix();
     2264    $regex  = implode( '|', wp_roles()->get_names() );
     2265    $regex  = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
     2266    $users  = $wpdb->get_col( $wpdb->prepare( "
     2267        SELECT user_id
     2268        FROM $wpdb->usermeta
     2269        WHERE meta_key = '{$prefix}capabilities'
     2270        AND meta_value NOT REGEXP %s
     2271    ", $regex ) );
     2272
     2273    return $users;
     2274}
Note: See TracChangeset for help on using the changeset viewer.