Make WordPress Core


Ignore:
Timestamp:
09/27/2017 09:43:59 PM (7 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/tests/phpunit/tests/user/capabilities.php

    r41624 r41625  
    19401940        $this->assertSame( 333, $user->get_site_id() );
    19411941    }
     1942
     1943    /**
     1944     * @ticket 38645
     1945     * @group ms-required
     1946     */
     1947    function test_init_roles_for_different_site() {
     1948        global $wpdb;
     1949
     1950        $site_id = self::factory()->blog->create();
     1951
     1952        switch_to_blog( $site_id );
     1953
     1954        $role_name = 'uploader';
     1955        add_role( $role_name, 'Uploader', array(
     1956            'read'         => true,
     1957            'upload_files' => true,
     1958        ) );
     1959
     1960        restore_current_blog();
     1961
     1962        $wp_roles = wp_roles();
     1963        $wp_roles->for_site( $site_id );
     1964
     1965        $this->assertTrue( isset( $wp_roles->role_objects[ $role_name ] ) );
     1966    }
     1967
     1968    /**
     1969     * @ticket 38645
     1970     */
     1971    function test_get_roles_data() {
     1972        global $wpdb;
     1973
     1974        $custom_roles = array(
     1975            'test_role' => array(
     1976                'name'         => 'Test Role',
     1977                'capabilities' => array(
     1978                    'do_foo' => true,
     1979                    'do_bar' => false,
     1980                ),
     1981            ),
     1982        );
     1983
     1984        // Test `WP_Roles::get_roles_data()` by manually setting the roles option.
     1985        update_option( $wpdb->get_blog_prefix( get_current_blog_id() ) . 'user_roles', $custom_roles );
     1986
     1987        $roles = new WP_Roles();
     1988        $this->assertSame( $custom_roles, $roles->roles );
     1989    }
     1990
     1991    /**
     1992     * @ticket 38645
     1993     */
     1994    function test_roles_get_site_id_default() {
     1995        $roles = new WP_Roles();
     1996        $this->assertSame( get_current_blog_id(), $roles->get_site_id() );
     1997    }
     1998
     1999    /**
     2000     * @ticket 38645
     2001     */
     2002    function test_roles_get_site_id() {
     2003        global $wpdb;
     2004
     2005        // Suppressing errors here allows to get around creating an actual site,
     2006        // which is unnecessary for this test.
     2007        $suppress = $wpdb->suppress_errors();
     2008        $roles = new WP_Roles( 333 );
     2009        $wpdb->suppress_errors( $suppress );
     2010
     2011        $this->assertSame( 333, $roles->get_site_id() );
     2012    }
    19422013}
Note: See TracChangeset for help on using the changeset viewer.