Skip to content
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

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
Open

Conversation

ethanclevenger91
Copy link

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.

Copy link

github-actions bot commented Sep 13, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props eclev91, flixos90, johnjamesjacoby, johnbillion.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@felixarntz felixarntz left a 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.

Comment on lines 163 to 168
foreach ( $capabilities as $key => $value ) {
if ( ! is_bool( $value ) ) {
$capabilities[ $value ] = true;
unset( $capabilities[ $key ] );
}
}
Copy link
Member

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:

Suggested change
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 );
}

Copy link
Author

@ethanclevenger91 ethanclevenger91 Sep 17, 2024

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.

Copy link
Author

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

Comment on lines 999 to 1014
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' ) );
Copy link
Member

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:

Suggested change
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' ) );

@@ -160,6 +160,13 @@ public function add_role( $role, $display_name, $capabilities = array() ) {
return;
}

foreach( $capabilities as $key => $value ) {
if(!is_bool($value)) {
Copy link
Contributor

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).

@@ -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;
Copy link
Contributor

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.

foreach( $capabilities as $key => $value ) {
if(!is_bool($value)) {
$capabilities[$value] = true;
unset($capabilities[$key]);
Copy link
Contributor

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. 👍

Copy link
Member

@johnbillion johnbillion left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants