User objects with extra role, site_name and blog_id properties. */ function mulist_get_users_across_sites( $role = '', $search = '' ) { $all_users = array(); $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0, // All sites; the default would stop at 100. 'deleted' => 0, 'archived' => 0, 'spam' => 0, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); $args = array( 'fields' => 'all_with_meta' ); if ( '' !== $role ) { $args['role'] = $role; } if ( '' !== $search ) { $args['search'] = '*' . $search . '*'; $args['search_columns'] = array( 'user_email', 'user_login', 'display_name' ); } $site_name = get_bloginfo( 'name' ); foreach ( get_users( $args ) as $user ) { $all_users[] = (object) array( 'ID' => (int) $user->ID, 'display_name' => $user->display_name, 'user_login' => $user->user_login, 'user_email' => $user->user_email, 'role' => implode( ', ', array_map( 'mulist_role_name', (array) $user->roles ) ), 'site_name' => $site_name, 'blog_id' => (int) $site_id, ); } restore_current_blog(); } return $all_users; } /** * Translated role name for a role slug. * * @param string $role Role slug. * @return string */ function mulist_role_name( $role ) { $names = wp_roles()->get_names(); return isset( $names[ $role ] ) ? translate_user_role( $names[ $role ] ) : (string) $role; } /** * Remove users from sites (bulk action). Super admins and the current user are skipped. * * @param array $items List of "user_id,blog_id" strings. * @return array{removed: int, skipped: int} */ function mulist_remove_users( array $items ) { $result = array( 'removed' => 0, 'skipped' => 0, ); foreach ( $items as $item ) { if ( ! preg_match( '/^\s*(\d+)\s*,\s*(\d+)\s*$/', (string) $item, $m ) ) { continue; } $user_id = absint( $m[1] ); $blog_id = absint( $m[2] ); $protected = get_current_user_id() === $user_id || is_super_admin( $user_id ); if ( $protected || ! get_site( $blog_id ) || ! is_user_member_of_blog( $user_id, $blog_id ) ) { ++$result['skipped']; continue; } $removed = remove_user_from_blog( $user_id, $blog_id ); if ( true === $removed ) { ++$result['removed']; } else { ++$result['skipped']; } } return $result; } /** * Render the screen. * * @return void */ function mulist_render_page() { if ( ! current_user_can( 'manage_network_users' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'firsencode-network-users' ) ); } require_once __DIR__ . '/class-mulist-users-role-list-table.php'; $notice = null; // Bulk action from the top (action) or bottom (action2) selector of the table. $bulk_action = isset( $_POST['action'] ) ? sanitize_key( wp_unslash( $_POST['action'] ) ) : ''; if ( 'remove' !== $bulk_action && isset( $_POST['action2'] ) ) { $bulk_action = sanitize_key( wp_unslash( $_POST['action2'] ) ); } if ( 'remove' === $bulk_action ) { // WP_List_Table prints this nonce field (bulk-{plural}). check_admin_referer( 'bulk-users' ); $items = isset( $_POST['user'] ) && is_array( $_POST['user'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['user'] ) ) : array(); $notice = mulist_remove_users( $items ); } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only list filters. $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; $role = isset( $_GET['role'] ) ? sanitize_key( wp_unslash( $_GET['role'] ) ) : ''; $paged = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; $per_page = isset( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : 20; // phpcs:enable WordPress.Security.NonceVerification.Recommended $roles = wp_roles()->get_names(); $role = isset( $roles[ $role ] ) ? $role : ''; $per_page = min( MULIST_MAX_PER_PAGE, max( 1, $per_page ) ); $all_users = mulist_get_users_across_sites( $role, $search ); $total = count( $all_users ); $users = array_slice( $all_users, ( $paged - 1 ) * $per_page, $per_page ); $base_url = network_admin_url( 'users.php?page=' . MULIST_PAGE_SLUG ); echo '
'; echo esc_html( sprintf( /* translators: 1: number of removed memberships, 2: number of skipped ones. */ __( 'Removed from sites: %1$d. Skipped (super admins, yourself or not a member): %2$d.', 'firsencode-network-users' ), $notice['removed'], $notice['skipped'] ) ); echo '