WordPress.org

Make WordPress Core

Changeset 34965


Ignore:
Timestamp:
10/08/2015 10:06:46 PM (4 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

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-users-list-table.php

    r34963 r34965  
    8888        $paged = $this->get_pagenum();
    8989
    90         $args = array(
    91             'number' => $users_per_page,
    92             'offset' => ( $paged-1 ) * $users_per_page,
    93             'role' => $role,
    94             'search' => $usersearch,
    95             'fields' => 'all_with_meta'
    96         );
     90        if ( 'none' === $role ) {
     91            $args = array(
     92                'number' => $users_per_page,
     93                'offset' => ( $paged-1 ) * $users_per_page,
     94                'include' => wp_get_users_with_no_role(),
     95                'search' => $usersearch,
     96                'fields' => 'all_with_meta'
     97            );
     98        } else {
     99            $args = array(
     100                'number' => $users_per_page,
     101                'offset' => ( $paged-1 ) * $users_per_page,
     102                'role' => $role,
     103                'search' => $usersearch,
     104                'fields' => 'all_with_meta'
     105            );
     106        }
    97107
    98108        if ( '' !== $args['search'] )
     
    167177            $users_of_blog = count_users();
    168178        }
     179
    169180        $total_users = $users_of_blog['total_users'];
    170181        $avail_roles =& $users_of_blog['avail_roles'];
     
    188199            $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
    189200            $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
     201        }
     202
     203        if ( ! empty( $avail_roles['none' ] ) ) {
     204
     205            $class = '';
     206
     207            if ( 'none' === $role ) {
     208                $class = ' class="current"';
     209            }
     210
     211            $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' ] ) );
     214            $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
     215
    190216        }
    191217
  • 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.