Make WordPress Core


Ignore:
Timestamp:
09/27/2017 09:43:59 PM (9 years ago)
Author:
flixos90
Message:

Multisite: Improve initializing available roles when switch sites.

Switching the available roles and the current user's capabilities no longer happens in switch_to_blog() and restore_current_blog(), instead it has been moved to a new function wp_switch_roles_and_user() which is hooked into the site switching process. This allows to improve performance by temporarily unhooking the function when roles and capabilities do not need to be switched.

This change ensures that switching available roles now works closer to switching user capabilities, particularly the changes in [41624]. A new WP_Roles::for_site( $site_id ) method has been introduced, and the WP_Roles::_init() method has been deprecated. It is furthermore possible to retrieve the site ID for which the available roles are currently initialized through a new WP_Roles::get_site_id().

Props johnjamesjacoby, flixos90.
Fixes #38645.

File:
1 edited

Legend:

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

    r41624 r41625  
    793793 */
    794794function switch_to_blog( $new_blog, $deprecated = null ) {
    795         global $wpdb, $wp_roles;
     795        global $wpdb;
    796796
    797797        $blog_id = get_current_blog_id();
     
    848848        }
    849849
    850         if ( did_action( 'init' ) ) {
    851                 $wp_roles = new WP_Roles();
    852                 $current_user = wp_get_current_user();
    853                 $current_user->for_site( $new_blog );
    854         }
    855 
    856850        /** This filter is documented in wp-includes/ms-blogs.php */
    857851        do_action( 'switch_blog', $new_blog, $prev_blog_id );
     
    877871 */
    878872function restore_current_blog() {
    879         global $wpdb, $wp_roles;
     873        global $wpdb;
    880874
    881875        if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
     
    922916        }
    923917
    924         if ( did_action( 'init' ) ) {
    925                 $wp_roles = new WP_Roles();
    926                 $current_user = wp_get_current_user();
    927                 $current_user->for_site( $blog );
    928         }
    929 
    930918        /** This filter is documented in wp-includes/ms-blogs.php */
    931919        do_action( 'switch_blog', $blog, $prev_blog_id );
     
    935923
    936924        return true;
     925}
     926
     927/**
     928 * Switches the initialized roles and current user capabilities to another site.
     929 *
     930 * @since 4.9.0
     931 *
     932 * @param int $new_site_id New site ID.
     933 * @param int $old_site_id Old site ID.
     934 */
     935function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
     936        if ( $new_site_id == $old_site_id ) {
     937                return;
     938        }
     939
     940        if ( ! did_action( 'init' ) ) {
     941                return;
     942        }
     943
     944        wp_roles()->for_site( $new_site_id );
     945        wp_get_current_user()->for_site( $new_site_id );
    937946}
    938947
Note: See TracChangeset for help on using the changeset viewer.