[wp-trac] [WordPress Trac] #43421: The $capabilities argument in the `add_role()` function is incompatible with `user_can`
WordPress Trac
noreply at wordpress.org
Mon Feb 26 17:19:27 UTC 2018
#43421: The $capabilities argument in the `add_role()` function is incompatible
with `user_can`
-----------------------------+-----------------------------
Reporter: eclev91 | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Role/Capability | Version: 4.9.4
Severity: normal | Keywords:
Focuses: |
-----------------------------+-----------------------------
Reproduce:
Add a role to WP using `add_role()`, and pass it custom capabilities using
the third argument. Like:
{{{#!php
<?php
$userCaps = [
'read',
'my_cap',
];
add_role('my_new_role', 'My New Role', $userCaps);
}}}
Create a user of said role. Then:
{{{#!php
<?php
$user = new WP_User($id_of_created_user);
return user_can($user, 'my_cap'); // returns false, expected return true
}}}
If, alternatively, I create my role this way:
{{{#!php
<?php
$userCaps = [
'read',
'my_cap',
];
$role = add_role('my_new_role', 'My New Role');
foreach($userCaps as $cap) {
$role->add_cap($cap);
}
}}}
And repeat testing, it returns true as expected.
This has to do with how the caps are saved. The first way, when
capabilities are retrieved in `WP_User::has_cap()`, they look like this:
{{{#!php
<?php
array(4) {
[0]=>
"read",
[1]=>
"my_cap",
["my_new_role"]=>
bool(true)
["exist"]=>
bool(true)
}
}}}
And when done via `add_cap`, they look like this:
{{{#!php
<?php
array(4) {
["read"]=>
bool(true)
["my_cap"]=>
bool(true)
["my_new_role"]=>
bool(true)
["exist"]=>
bool(true)
}
}}}
The subsequent `empty` array check is true for the first, and false for
the second.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/43421>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list