WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/14/2016 03:34:07 AM (4 years ago)
Author:
jeremyfelt
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ms.php

    r36679 r37200  
    850850    </table>
    851851    <?php
    852 }
    853 
    854 /**
    855  * Grants Super Admin privileges.
    856  *
    857  * @since 3.0.0
    858  *
    859  * @global array $super_admins
    860  *
    861  * @param int $user_id ID of the user to be granted Super Admin privileges.
    862  * @return bool True on success, false on failure. This can fail when the user is
    863  *              already a super admin or when the `$super_admins` global is defined.
    864  */
    865 function grant_super_admin( $user_id ) {
    866     // If global super_admins override is defined, there is nothing to do here.
    867     if ( isset( $GLOBALS['super_admins'] ) ) {
    868         return false;
    869     }
    870 
    871     /**
    872      * Fires before the user is granted Super Admin privileges.
    873      *
    874      * @since 3.0.0
    875      *
    876      * @param int $user_id ID of the user that is about to be granted Super Admin privileges.
    877      */
    878     do_action( 'grant_super_admin', $user_id );
    879 
    880     // Directly fetch site_admins instead of using get_super_admins()
    881     $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
    882 
    883     $user = get_userdata( $user_id );
    884     if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
    885         $super_admins[] = $user->user_login;
    886         update_site_option( 'site_admins' , $super_admins );
    887 
    888         /**
    889          * Fires after the user is granted Super Admin privileges.
    890          *
    891          * @since 3.0.0
    892          *
    893          * @param int $user_id ID of the user that was granted Super Admin privileges.
    894          */
    895         do_action( 'granted_super_admin', $user_id );
    896         return true;
    897     }
    898     return false;
    899 }
    900 
    901 /**
    902  * Revokes Super Admin privileges.
    903  *
    904  * @since 3.0.0
    905  *
    906  * @global array $super_admins
    907  *
    908  * @param int $user_id ID of the user Super Admin privileges to be revoked from.
    909  * @return bool True on success, false on failure. This can fail when the user's email
    910  *              is the network admin email or when the `$super_admins` global is defined.
    911  */
    912 function revoke_super_admin( $user_id ) {
    913     // If global super_admins override is defined, there is nothing to do here.
    914     if ( isset( $GLOBALS['super_admins'] ) ) {
    915         return false;
    916     }
    917 
    918     /**
    919      * Fires before the user's Super Admin privileges are revoked.
    920      *
    921      * @since 3.0.0
    922      *
    923      * @param int $user_id ID of the user Super Admin privileges are being revoked from.
    924      */
    925     do_action( 'revoke_super_admin', $user_id );
    926 
    927     // Directly fetch site_admins instead of using get_super_admins()
    928     $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
    929 
    930     $user = get_userdata( $user_id );
    931     if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
    932         if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
    933             unset( $super_admins[$key] );
    934             update_site_option( 'site_admins', $super_admins );
    935 
    936             /**
    937              * Fires after the user's Super Admin privileges are revoked.
    938              *
    939              * @since 3.0.0
    940              *
    941              * @param int $user_id ID of the user Super Admin privileges were revoked from.
    942              */
    943             do_action( 'revoked_super_admin', $user_id );
    944             return true;
    945         }
    946     }
    947     return false;
    948852}
    949853
Note: See TracChangeset for help on using the changeset viewer.