[wp-trac] [WordPress Trac] #14998: Allow WP_User->has_cap() to accept TRUE or FALSE as capability

WordPress Trac wp-trac at lists.automattic.com
Thu Sep 30 19:14:41 UTC 2010


#14998: Allow WP_User->has_cap() to accept TRUE or FALSE as capability
-----------------------------+----------------------------------------------
 Reporter:  coffee2code      |       Owner:                 
     Type:  enhancement      |      Status:  new            
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Role/Capability  |     Version:  3.0.1          
 Severity:  minor            |    Keywords:  has-patch      
-----------------------------+----------------------------------------------
 It would be handy (and in some instances potentially eliminate some
 redundancy) if `WP_User->has_cap()` could be made to immediately return
 TRUE or FALSE if passed a boolean as the capability.  The primary use case
 for this is for invoking the various menu-adding functions
 (`add_menu_page()`, `add_submenu_page()`, etc) after having determined the
 current user already has sufficient capabilities to view the menu.  In
 such cases, we can bypass any further capability checks and simply pass
 TRUE or FALSE as the capability.

 A simplistic example:

 {{{
    if ( current_user_can( 'manage_options' ) ) {
       add_options_page( 'My Plugin', 'Item A', 'manage_options', 'my-
 plugin-a', array( &$this, 'my_menu_a' ) );
       add_options_page( 'My Plugin', 'Item B', 'manage_options', 'my-
 plugin-b', array( &$this, 'my_menu_b' ) );
       // do some other stuff here for someone with 'manage_options'
 capabilities
    }
 }}}

 In the above, the current user is being checked three times for the
 'manage_options' capability.  With the attached patch applied, it'd be:

 {{{
    if ( current_user_can( 'manage_options' ) ) {
       add_options_page( 'My Plugin', 'Item A', true, 'my-plugin-a', array(
 &$this, 'my_menu_a' ) );
       add_options_page( 'My Plugin', 'Item B', true, 'my-plugin-b', array(
 &$this, 'my_menu_b' ) );
       // do some other stuff here for someone with 'manage_options'
 capabilities
    }
 }}}


 The patch should allow for a boolean to be specified as a capability in
 any capability-accepting function.  If true or false is a capability (or
 in the list of capabilities), then `has_cap()` will return that boolean
 without further capability checking.  In the event a capabilities array
 contains both, false takes precendence over true (err on the side of
 restriction rather than permission).

 Patch attached.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/14998>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list