Created
June 16, 2021 18:12
-
-
Save Clorith/6abbbf167e6e5ef7bdd79cd9739e74f2 to your computer and use it in GitHub Desktop.
A script to be used with WP-CLI's `wp eval-file` to remove super-admins from all sub-sites in a network (except the primary site in the network when applicable).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Fetch all superadmins on a site. | |
$super_admins = get_super_admins(); | |
// Loop over all super admins | |
foreach ( $super_admins as $admin_slug ) { | |
// Get a user object for this user. | |
$user = get_user_by( 'slug', $admin_slug ); | |
// Fail-safe, maybe the slug somehow couldn't be identified. | |
if ( ! $user ) { | |
echo "Could not find the user object for `" . $admin_slug . "`\n"; | |
continue; | |
} | |
echo "Processing the user `" . $admin_slug . "` (user id: " . $user->ID . ")\n"; | |
// Get all sites for this user. | |
$sites = get_blogs_of_user( $user->ID, true ); | |
// Loop over the users sites. | |
foreach ( $sites as $site ) { | |
$site_id = $site->userblog_id; | |
/* | |
* Skip the main site in the network, for consistency. | |
* Not neccesarely required, a network admin will exist even if removed from all sites, | |
* but it makes a more consistent admin UI experience for many. | |
*/ | |
if ( $site_id === get_main_site_id() ) { | |
continue; | |
} | |
// Check that the user has no posts on this site, or else content could be lost. | |
$posts = get_posts( array( | |
'posts_per_page' => 1, // We only need a single post to know this user has content that needs moved. | |
'author' => $user->ID, | |
'post_type' => 'any', // We use WordPress' keyword `any` to mean any post type that may exist as a catchall. | |
'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private', 'inherit' ), // There are more post stati, but these are the known ones that represent "real" content. | |
) ); | |
// If posts are found, do not remove this user. | |
if ( ! empty( $posts ) ) { | |
echo "Site " . $site->siteurl . " (#" . $site_id . ") has content by the author `" . $admin_slug . "`\n"; | |
continue; | |
} | |
// Remove the user from this site (aka "blog" in WordPress). | |
$remove = remove_user_from_blog( $user->ID, $site_id ); | |
if ( is_wp_error( $remove ) ) { | |
echo "Error when removing user from site #" . $site_id . ": " . $remove->get_error_message() . "\n"; | |
} | |
// The `remove_user_from_blog` function returns a boolean `true` if it was successful, this is just a nice to have section. | |
if ( true !== $remove ) { | |
echo "Something went wrong, and we could not remove the user from the site " . $site->siteurl . " (#" . $site_id . ")`\n"; | |
continue; | |
} | |
// Clear the cache for this site. | |
wp_cache_delete( $site_id . '_user_count', 'blog-details' ); | |
} | |
// Clear the user cache when done, it's nice to be tidy. | |
clean_user_cache( $user->ID ); | |
echo "Done processing this user...\n"; | |
} | |
echo "-----\n"; | |
echo "Finished removing super-admins from sub-sites in the network.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment