[wp-trac] [WordPress Trac] #43421: The $capabilities argument in the `add_role()` function is incompatible with `user_can`

WordPress Trac noreply at wordpress.org
Sat Mar 10 17:38:45 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           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+------------------------------

Comment (by soulseekah):

 The `add_cap` method has the following signature: `add_cap( $cap, $grant =
 true )`

 Notice the 2nd argument. To add a capability you need two things: a
 capability name, and a grant status (`true`, `false`).

 When adding capabilities using `add_role`, the third argument needs a way
 to pass both the capability name and the grant status. How does it do it?
 A key-value array.

 As stated in the docblock:


 {{{
 * @param array $capabilities List of capabilities, e.g. array(
 'edit_posts' => true, 'delete_posts' => false );
 }}}

 You have to pass both these things. When you pass `$userCaps = [ 'read',
 'my_cap', ];` you are effectively passing in the following array: `[ 0 =>
 'read', 1 => 'my_cap' ]`, which in turn tries to perform two calls:
 `add_cap( 0, 'read' )` and `add_cap( 1, 'my_cap' )`, which is, indeed,
 incompatible with what you're trying to achieve.

 This is not a bug, in my opinion.

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


More information about the wp-trac mailing list