-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trac/43421 #7351
base: trunk
Are you sure you want to change the base?
Trac/43421 #7351
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to be onboard with the idea to support a numeric array, but I question the value of supporting a mix of both.
src/wp-includes/class-wp-roles.php
Outdated
foreach ( $capabilities as $key => $value ) { | ||
if ( ! is_bool( $value ) ) { | ||
$capabilities[ $value ] = true; | ||
unset( $capabilities[ $key ] ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than checking every value individually, I think it would make more sense to check the entire $capabilities
array for the shape that is provides. While there's arguably a reason to either specify a $cap => $grant
map or a list of caps, it makes no sense to me to allow a mix of the two - that would just be unintuitive.
So I think we could simplify this by using wp_is_numeric_array()
. For example:
foreach ( $capabilities as $key => $value ) { | |
if ( ! is_bool( $value ) ) { | |
$capabilities[ $value ] = true; | |
unset( $capabilities[ $key ] ); | |
} | |
} | |
if ( wp_is_numeric_array( $capabilities ) ) { | |
$capabilities = array_fill_keys( $capabilities, true ); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it were up to me, we would deprecate supporting a map entirely, as the absence of a capability implies it is not granted. Without explicitly deprecating that usage, I think it makes sense to support both, even in a single call. Is there a performance implication here? I won't disagree that it's kind of gross.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After discussion with @johnbillion we're going to move forward w/o support for a mixed format array
add_role( | ||
$role_name, | ||
'Janitor', | ||
array( | ||
'level_1', | ||
'sweep_floors' => false, | ||
) | ||
); | ||
$this->flush_roles(); | ||
|
||
// Assign a user to that role. | ||
$id = self::factory()->user->create( array( 'role' => $role_name ) ); | ||
$user = new WP_User( $id ); | ||
|
||
$this->assertTrue( $user->has_cap( 'level_1' ) ); | ||
$this->assertFalse( $user->has_cap( 'sweep_floors' ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above, I'm not sure why one would want to provide capabilities in that format. Either you should use a list of capabilities to grant, or if for some reason you want to include capabilities to set them to false
, then it makes sense to provide a map of $cap => $grant
.
In other words, IMO the test should become something like:
add_role( | |
$role_name, | |
'Janitor', | |
array( | |
'level_1', | |
'sweep_floors' => false, | |
) | |
); | |
$this->flush_roles(); | |
// Assign a user to that role. | |
$id = self::factory()->user->create( array( 'role' => $role_name ) ); | |
$user = new WP_User( $id ); | |
$this->assertTrue( $user->has_cap( 'level_1' ) ); | |
$this->assertFalse( $user->has_cap( 'sweep_floors' ) ); | |
add_role( | |
$role_name, | |
'Janitor', | |
array( | |
'level_1', | |
'sweep_floors', | |
) | |
); | |
$this->flush_roles(); | |
// Assign a user to that role. | |
$id = self::factory()->user->create( array( 'role' => $role_name ) ); | |
$user = new WP_User( $id ); | |
$this->assertTrue( $user->has_cap( 'level_1' ) ); | |
$this->assertTrue( $user->has_cap( 'sweep_floors' ) ); |
src/wp-includes/class-wp-roles.php
Outdated
@@ -160,6 +160,13 @@ public function add_role( $role, $display_name, $capabilities = array() ) { | |||
return; | |||
} | |||
|
|||
foreach( $capabilities as $key => $value ) { | |||
if(!is_bool($value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this reuses $value
as a key on lines 165 & 166, it would make the code more resilient to do: is_int( $key ) && is_string( $value )
But, I think if we are considering doing argument validation, it may warrant a dedicated method – one where we can more intentionally conclude (and document) what mutations will take place to what kind of submitted keys & values.
(The obvious benefit of a dedicated method would be reuse elsewhere that an array of caps is passed in as an argument, ensuring the same results, and ability to write unit tests for it.)
Alternatively, wp_is_numeric_array()
could be used before the new foreach
, but that function checks that all array keys are numeric, which means we’d lose support for mixed key types (which was an example in the core ticket).
src/wp-includes/class-wp-roles.php
Outdated
@@ -160,6 +160,13 @@ public function add_role( $role, $display_name, $capabilities = array() ) { | |||
return; | |||
} | |||
|
|||
foreach( $capabilities as $key => $value ) { | |||
if(!is_bool($value)) { | |||
$capabilities[$value] = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if $capabilities[$value]
already exists, and what if it is false
?
That could theoretically overwrite an intentionally blocked capability with an unintentionally allowed one.
src/wp-includes/class-wp-roles.php
Outdated
foreach( $capabilities as $key => $value ) { | ||
if(!is_bool($value)) { | ||
$capabilities[$value] = true; | ||
unset($capabilities[$key]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsetting the numeric array key does make sense to me in this scenario. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with Felix's comments. Let's get the docs for the $capabilities
parameter of the add_role()
global function and the WP_Roles::add_role()
method updated, and a @since
entry added for both.
Updates WP_Role::add_role to assume capabilities should be granted for capabilities not passed associatively.
Trac ticket: https://core.trac.wordpress.org/ticket/43421
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.