[wp-trac] [WordPress Trac] #38645: Improve roles reinit when switching between sites

WordPress Trac noreply at wordpress.org
Thu Aug 3 01:52:01 UTC 2017


#38645: Improve roles reinit when switching between sites
--------------------------------------+------------------------
 Reporter:  johnjamesjacoby           |       Owner:  flixos90
     Type:  defect (bug)              |      Status:  reviewing
 Priority:  normal                    |   Milestone:  4.9
Component:  Role/Capability           |     Version:  4.7
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:  multisite
--------------------------------------+------------------------
Changes (by flixos90):

 * keywords:  has-patch => has-patch has-unit-tests


Comment:

 [attachment:38645.4.diff] is an updated version of the patch with the
 primary goal of bringing it in line with the way `WP_User` works (see
 #36961 for the user-related ticket). Please have a look at particularly
 https://core.trac.wordpress.org/attachment/ticket/36961/36961.5.diff to
 see how the changes are in line with each other.

 * In addition to the `for_blog()` method, a protected `get_roles_data()`
 method has been introduced that has retrieving the roles from the database
 as its sole responsibility. This method contains a fix for a bug that
 existed in the previous patches: When calling `for_blog()` with another ID
 than the current site, the options are not switched - in the latest patch
 the `get_roles_data()` method accounts for that. A test to verify this has
 been added as well. The `WP_Roles::get_roles_data()` method aligns with
 the `WP_User::get_caps_data()` method.
 * A new `init_roles()` method has been introduced which fulfills the
 purpose of initializing the role variables based on the roles data that
 has been retrieved through `get_roles_data()`. The
 `WP_Roles::init_roles()` method aligns with the `WP_User::get_role_caps()`
 method - while they have different names, they both are responsible for
 setting up class properties based on the data retrieved from the DB.
 * The `_init()` method has been deprecated. The code that used to be in it
 has been split out into `for_blog()` and its "sub-methods"
 `get_roles_data()` and `init_roles()`. `for_blog()` is a 1-to-1
 replacement for the method.

 Some of these changes may seem rather drastic, but they improve how
 switching sites works:
 * By making the roles and user switching part a hook, developers can
 temporarily unhook it if they only want to switch options.
 * A preliminary check for `$use_db` prevents unnecessary resets of the
 other role properties.
 * The new `for_blog()` method allows for granular switching of only the
 roles, when it's needed. At the moment it still needs to call
 `switch_to_blog()` because of the option, but this technique opens up
 several possibilities in the future, for example a plugin could store the
 roles in `blogmeta` at some point, making the `switch_to_blog()` call
 entirely unnecessary for such a case. A real-world example that this could
 benefit even at this point can be seen in #36961:
 https://core.trac.wordpress.org/attachment/ticket/36961/36961.5.diff is
 based on the changes proposed here and simply calls
 `wp_roles()->for_blog()` instead of the bloated `switch_to_blog()`.

 Again, most of this is to be future-proof, but it already brings some
 benefits now.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38645#comment:24>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list