[wp-trac] [WordPress Trac] #53131: Disjunctive normal form for WP_User::has_cap

WordPress Trac noreply at wordpress.org
Sun May 2 03:06:38 UTC 2021


#53131: Disjunctive normal form for WP_User::has_cap
-----------------------------+-----------------------------
 Reporter:  manfcarlo        |      Owner:  (none)
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Users            |    Version:
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 The following block of code is from the `has_cap` method on the `WP_User`
 class:

 {{{
     // Must have ALL requested caps.
     foreach ( (array) $caps as $cap ) {
         if ( empty( $capabilities[ $cap ] ) ) {
             return false;
         }
     }

     return true;
 }}}

 `$caps` is an array of capabilities that are returned from the
 `map_meta_cap`
 [https://developer.wordpress.org/reference/hooks/map_meta_cap/ filter
 hook.]

 This assumes there is only one possible set of capabilities that can pass
 the capability check.

 In reality, you might want it to be possible to pass the capability check
 in multiple ways. One possible way to implement this is an array of arrays
 that represent disjunctive normal form:

 {{{
     $has_cap_set = true;

     // Must have ALL caps in some set.
     foreach ( $caps as $cap_set ) {
         foreach ( $cap_set as $cap ) {
             if ( empty( $capabilities[ $cap ] ) ) {
                 $has_cap_set = false;
                 break;
             }
         }

         if ( $has_cap_set ) {
             return true;
         }
     }

     return false;
 }}}

 Currently, plugins can simulate this behaviour by calling `user_can` from
 within the `map_meta_cap` filter and returning `exist` if `user_can`
 returns true, but this is not ideal for two reasons:

 1. `map_meta_cap` is meant to simply map to capabilities, not check
 whether the user actually has them
 2. Simply returning `exist` might be misleading to other plugins that use
 the `map_meta_cap` or `user_has_cap` filter

 It would make this a much simpler job if core offered support for
 `map_meta_cap` to return a disjunctive normal form of capabilities.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53131>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list