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
7 changes: 7 additions & 0 deletions src/wp-includes/class-wp-roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).

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

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

}
}

$this->roles[ $role ] = array(
'name' => $display_name,
'capabilities' => $capabilities,
Expand Down