WordPress.org

Make WordPress Core

Ticket #38645: 38645.patch

File 38645.patch, 5.9 KB (added by johnjamesjacoby, 3 years ago)
  • src/wp-includes/class-wp-roles.php

    diff --git src/wp-includes/class-wp-roles.php src/wp-includes/class-wp-roles.php
    index 23e05d7..d9654f6 100644
     
    7070        public $use_db = true;
    7171
    7272        /**
     73         * What role type to get from the database
     74         *
     75         * @since 4.8.0
     76         *
     77         * @access public
     78         * @var string
     79         */
     80        public $role_type = 'user_roles';
     81
     82        /**
    7383         * Constructor
    7484         *
    7585         * @since 2.0.0
    7686         */
    77         public function __construct() {
    78                 $this->_init();
     87        public function __construct( $blog_id = null ) {
     88                $this->use_db = empty( $GLOBALS['wp_user_roles'] );
     89
     90                $this->for_blog( $blog_id );
    7991        }
    8092
    8193        /**
     
    99111         * Set up the object properties.
    100112         *
    101113         * The role key is set to the current prefix for the $wpdb object with
    102          * 'user_roles' appended. If the $wp_user_roles global is set, then it will
     114         * the role_type appended. If the $wp_user_roles global is set, then it will
    103115         * be used and the role option will not be updated or used.
    104116         *
    105117         * @since 2.1.0
    106118         * @access protected
    107119         *
    108          * @global array $wp_user_roles Used to set the 'roles' property value.
     120         * @param string $role_key Optional role key.
    109121         */
    110         protected function _init() {
    111                 global $wp_user_roles, $wpdb;
     122        protected function _init( $role_key = '' ) {
     123                global $wpdb;
    112124
    113                 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
    114                 if ( ! empty( $wp_user_roles ) ) {
    115                         $this->roles = $wp_user_roles;
    116                         $this->use_db = false;
     125                // Fallback on current site's prefix if no role key was passed
     126                if ( empty( $role_key ) ) {
     127                        $this->role_key = $wpdb->get_blog_prefix() . $this->role_type;
     128                } else {
     129                        $this->role_key = $role_key;
     130                }
     131
     132                // Use the $wp_user_roles global if one was set
     133                if ( false === $this->use_db ) {
     134                        $this->roles = $GLOBALS['wp_user_roles'];
    117135                } else {
    118136                        $this->roles = get_option( $this->role_key );
    119137                }
    120138
    121                 if ( empty( $this->roles ) )
    122                         return;
     139                // Set role names & objects from the roles array
     140                if ( ! empty( $this->roles ) ) {
     141                        $this->role_objects = array();
     142                        $this->role_names   = array();
    123143
    124                 $this->role_objects = array();
    125                 $this->role_names =  array();
    126                 foreach ( array_keys( $this->roles ) as $role ) {
    127                         $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
    128                         $this->role_names[$role] = $this->roles[$role]['name'];
     144                        foreach ( array_keys( $this->roles ) as $role ) {
     145                                $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
     146                                $this->role_names[ $role ]   = $this->roles[ $role ]['name'];
     147                        }
    129148                }
    130149
    131150                /**
     
    142161         * Reinitialize the object
    143162         *
    144163         * Recreates the role objects. This is typically called only by switch_to_blog()
    145          * after switching wpdb to a new site ID.
     164         * after switching wpdb to a new site ID, and is skipped entirely when the
     165         * $wp_user_roles omega global is used.
    146166         *
    147167         * @since 3.5.0
    148          * @deprecated 4.7.0 Use new WP_Roles()
    149168         * @access public
    150169         */
    151170        public function reinit() {
    152                 _deprecated_function( __METHOD__, '4.7.0', 'new WP_Roles()' );
    153                 $this->_init();
     171                if ( $this->use_db ) {
     172                        $this->_init();
     173                }
     174        }
     175
     176        /**
     177         * Set the site to operate on. Defaults to the current site.
     178         *
     179         * @since 4.8.0
     180         *
     181         * @global WPDB $wpdb
     182         *
     183         * @param int $blog_id Optional. The site ID to initialize for.
     184         */
     185        public function for_blog( $blog_id = null ) {
     186                global $wpdb;
     187
     188                // Set the role_key based on the prefix for the blog_id
     189                $role_key = $wpdb->get_blog_prefix( $blog_id ) . $this->role_type;
     190
     191                // Initialize roles based on key
     192                $this->_init( $role_key );
    154193        }
    155194
    156195        /**
  • src/wp-includes/ms-blogs.php

    diff --git src/wp-includes/ms-blogs.php src/wp-includes/ms-blogs.php
    index 9188750..875a6ce 100644
     
    766766 * @return true Always returns True.
    767767 */
    768768function switch_to_blog( $new_blog, $deprecated = null ) {
    769         global $wpdb, $wp_roles;
     769        global $wpdb;
    770770
    771771        $blog_id = get_current_blog_id();
    772772        if ( empty( $new_blog ) ) {
     
    821821                }
    822822        }
    823823
    824         if ( did_action( 'init' ) ) {
    825                 $wp_roles = new WP_Roles();
    826                 $current_user = wp_get_current_user();
    827                 $current_user->for_blog( $new_blog );
    828         }
    829 
    830824        /** This filter is documented in wp-includes/ms-blogs.php */
    831825        do_action( 'switch_blog', $new_blog, $prev_blog_id );
    832826        $GLOBALS['switched'] = true;
     
    850844 * @return bool True on success, false if we're already on the current blog
    851845 */
    852846function restore_current_blog() {
    853         global $wpdb, $wp_roles;
     847        global $wpdb;
    854848
    855849        if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
    856850                return false;
     
    895889                }
    896890        }
    897891
    898         if ( did_action( 'init' ) ) {
    899                 $wp_roles = new WP_Roles();
    900                 $current_user = wp_get_current_user();
    901                 $current_user->for_blog( $blog );
    902         }
    903 
    904892        /** This filter is documented in wp-includes/ms-blogs.php */
    905893        do_action( 'switch_blog', $blog, $prev_blog_id );
    906894
     
    911899}
    912900
    913901/**
     902 * Switch WP_Roles & current_user roles & caps, usually when switching sites.
     903 *
     904 * @since 4.8.0
     905 *
     906 * @param int $new_blog_id
     907 * @param int $old_blog_id
     908 */
     909function switch_wp_roles( $new_blog_id = 0, $old_blog_id = 0 ) {
     910        if ( $new_blog_id != $old_blog_id ) {
     911                wp_roles()->for_blog( $new_blog_id );
     912                wp_get_current_user()->for_blog( $new_blog_id );
     913        }
     914}
     915
     916/**
    914917 * Determines if switch_to_blog() is in effect
    915918 *
    916919 * @since 3.5.0
  • src/wp-includes/ms-default-filters.php

    diff --git src/wp-includes/ms-default-filters.php src/wp-includes/ms-default-filters.php
    old mode 100644
    new mode 100755
    index 08d752a..c3e218b
     
    3232add_action( 'network_user_new_created_user',   'wp_send_new_user_notifications' );
    3333add_filter( 'sanitize_user', 'strtolower' );
    3434
     35// Roles
     36add_action( 'switch_blog', 'switch_wp_roles', 8, 2 );
     37
    3538// Blogs
    3639add_filter( 'wpmu_validate_blog_signup', 'signup_nonce_check' );
    3740add_action( 'wpmu_new_blog', 'wpmu_log_new_registrations', 10, 2 );