[wp-trac] [WordPress Trac] #23016: Allow plugins to manipulate WP_Roles ($wp_roles global) on construct

WordPress Trac noreply at wordpress.org
Wed Nov 2 17:25:07 UTC 2016


#23016: Allow plugins to manipulate WP_Roles ($wp_roles global) on construct
-------------------------------------------------+-------------------------
 Reporter:  johnjamesjacoby                      |       Owner:  pento
     Type:  defect (bug)                         |      Status:  closed
 Priority:  normal                               |   Milestone:  4.7
Component:  Role/Capability                      |     Version:  3.5
 Severity:  normal                               |  Resolution:  fixed
 Keywords:  has-patch has-unit-tests commit      |     Focuses:
  needs-dev-note                                 |
-------------------------------------------------+-------------------------

Comment (by mnelson4):

 `new WP_Roles()` isn't an exact replacement for `$wp_roles->reinit()`
 because it returns a NEW `WP_Roles` object, whereas `$wp_roles->reinit()`
 returned the same old `WP_Roles` object.

 So if code anywhere stores a reference to the global `$wp_roles` object,
 it can easily become stale if other code anywhere else changes that global
 in order to reset roles.

 Eg
 {{{#!php
 global $wp_roles;
 $my_reference_to_wp_roles = $wp_roles;
 $wp_roles = new WP_Roles();
 }}}

 Now `$my_reference_to_wp_roles` still points to the old `WP_Roles` object,
 not the new `WP_Roles` object we just created, and I'll get into trouble
 if I use it.

 Whereas using the now-deprecated code

 {{{#!php
 global $wp_roles;
 $my_reference_to_wp_roles = $wp_roles;
 $wp_roles->reinit();
 }}}

 doesn't have this problem: `$my_reference_to_wp_roles` stills points to
 the same thing as`$wp_roles`, so I can continue to use
 `$my_reference_to_wp_roles` just fine.

 Granted, before these changes, plugin or theme code could have switched
 `$wp_roles` and created this problem. But now the *recommended* way of
 resetting `$wp_roles` is to do `$wp_roles = new WP_Roles()`, which can
 cause problems for any code who holds a reference to the old global before
 it got changed.

 Maybe we could say "Well, if you ever created a variable referencing the
 global `$wp_roles`, you were doing_it_wrong()`, but is that mentioned in
 documentation anywhere? If so, ya then they're doing it wrong and its
 their problem. If not, we should probably be a bit more accommodating.

 So I think these changes are fine, except I don't think deprecating
 `WP_Roles::reinit()` is good, and we should continue to use
 `WP_Roles::reinit()` instead of calling `$wp_roles = new WP_Roles()`.

 Thoughts?

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


More information about the wp-trac mailing list