Multisite: Relocate `revoke_super_admin()` and `grant_super_admin()`

Moving these functions to `wp-includes/capabilities.php` allows plugins to access more than just `is_super_admin()` and `get_super_admin()` from the front end without including a file via `wp-admin/`. Add an `is_multisite()` check to prevent use of these on single site environments now that they have left `ms.php`..

Props johnjamesjacoby.
Fixes #21788.

Built from https://develop.svn.wordpress.org/trunk@37200


git-svn-id: http://core.svn.wordpress.org/trunk@37166 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2016-04-14 03:35:27 +00:00
parent efee62a316
commit 6cc19b5e2e
3 changed files with 97 additions and 97 deletions

View File

@ -851,102 +851,6 @@ function choose_primary_blog() {
<?php
}
/**
* Grants Super Admin privileges.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @param int $user_id ID of the user to be granted Super Admin privileges.
* @return bool True on success, false on failure. This can fail when the user is
* already a super admin or when the `$super_admins` global is defined.
*/
function grant_super_admin( $user_id ) {
// If global super_admins override is defined, there is nothing to do here.
if ( isset( $GLOBALS['super_admins'] ) ) {
return false;
}
/**
* Fires before the user is granted Super Admin privileges.
*
* @since 3.0.0
*
* @param int $user_id ID of the user that is about to be granted Super Admin privileges.
*/
do_action( 'grant_super_admin', $user_id );
// Directly fetch site_admins instead of using get_super_admins()
$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
$user = get_userdata( $user_id );
if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
$super_admins[] = $user->user_login;
update_site_option( 'site_admins' , $super_admins );
/**
* Fires after the user is granted Super Admin privileges.
*
* @since 3.0.0
*
* @param int $user_id ID of the user that was granted Super Admin privileges.
*/
do_action( 'granted_super_admin', $user_id );
return true;
}
return false;
}
/**
* Revokes Super Admin privileges.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @param int $user_id ID of the user Super Admin privileges to be revoked from.
* @return bool True on success, false on failure. This can fail when the user's email
* is the network admin email or when the `$super_admins` global is defined.
*/
function revoke_super_admin( $user_id ) {
// If global super_admins override is defined, there is nothing to do here.
if ( isset( $GLOBALS['super_admins'] ) ) {
return false;
}
/**
* Fires before the user's Super Admin privileges are revoked.
*
* @since 3.0.0
*
* @param int $user_id ID of the user Super Admin privileges are being revoked from.
*/
do_action( 'revoke_super_admin', $user_id );
// Directly fetch site_admins instead of using get_super_admins()
$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
$user = get_userdata( $user_id );
if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
unset( $super_admins[$key] );
update_site_option( 'site_admins', $super_admins );
/**
* Fires after the user's Super Admin privileges are revoked.
*
* @since 3.0.0
*
* @param int $user_id ID of the user Super Admin privileges were revoked from.
*/
do_action( 'revoked_super_admin', $user_id );
return true;
}
}
return false;
}
/**
* Whether or not we can edit this network from this page.
*

View File

@ -619,3 +619,99 @@ function is_super_admin( $user_id = false ) {
return false;
}
/**
* Grants Super Admin privileges.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @param int $user_id ID of the user to be granted Super Admin privileges.
* @return bool True on success, false on failure. This can fail when the user is
* already a super admin or when the `$super_admins` global is defined.
*/
function grant_super_admin( $user_id ) {
// If global super_admins override is defined, there is nothing to do here.
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
return false;
}
/**
* Fires before the user is granted Super Admin privileges.
*
* @since 3.0.0
*
* @param int $user_id ID of the user that is about to be granted Super Admin privileges.
*/
do_action( 'grant_super_admin', $user_id );
// Directly fetch site_admins instead of using get_super_admins()
$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
$user = get_userdata( $user_id );
if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
$super_admins[] = $user->user_login;
update_site_option( 'site_admins' , $super_admins );
/**
* Fires after the user is granted Super Admin privileges.
*
* @since 3.0.0
*
* @param int $user_id ID of the user that was granted Super Admin privileges.
*/
do_action( 'granted_super_admin', $user_id );
return true;
}
return false;
}
/**
* Revokes Super Admin privileges.
*
* @since 3.0.0
*
* @global array $super_admins
*
* @param int $user_id ID of the user Super Admin privileges to be revoked from.
* @return bool True on success, false on failure. This can fail when the user's email
* is the network admin email or when the `$super_admins` global is defined.
*/
function revoke_super_admin( $user_id ) {
// If global super_admins override is defined, there is nothing to do here.
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
return false;
}
/**
* Fires before the user's Super Admin privileges are revoked.
*
* @since 3.0.0
*
* @param int $user_id ID of the user Super Admin privileges are being revoked from.
*/
do_action( 'revoke_super_admin', $user_id );
// Directly fetch site_admins instead of using get_super_admins()
$super_admins = get_site_option( 'site_admins', array( 'admin' ) );
$user = get_userdata( $user_id );
if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
unset( $super_admins[$key] );
update_site_option( 'site_admins', $super_admins );
/**
* Fires after the user's Super Admin privileges are revoked.
*
* @since 3.0.0
*
* @param int $user_id ID of the user Super Admin privileges were revoked from.
*/
do_action( 'revoked_super_admin', $user_id );
return true;
}
}
return false;
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37199';
$wp_version = '4.6-alpha-37200';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.