Opened 15 years ago
Closed 15 years ago
#14602 closed enhancement (fixed)
Create user_can, refactor current_user_can, author_can, current_user_can_for_blog
| Reported by: | | Owned by: | |
|---|---|---|---|
| Milestone: | 3.1 | Priority: | normal |
| Severity: | normal | Version: | 3.0.1 |
| Component: | Role/Capability | Keywords: | has-patch needs-testing |
| Focuses: | Cc: |
Description
Currently we have no function to pass a user ID and a capability/rolewhich will return whether the user can perform that role or has that capability. Further, the functions current_user_can, author_can, and current_user_can_for_blog have some code duplication.
I suggest adding a new function user_can, which accepts any user_ID or a user object and a capability, and returns a boolean. The other *_can* functions can then be refactored to use the new function.
See attached patch.
Attachments (1)
Change History (8)
#2 follow-up: ↓ 4
@
15 years ago
- Component changed from Users to Role/Capability
- Milestone changed from Awaiting Review to 3.1
Looking good. +1
#4 in reply to: ↑ 2 ; follow-up: ↓ 5
@
15 years ago
Replying to scribu:
Looking good. +1
Should the user_can function be using get_userdata rather than instantiating a new WP_User object every time, so it takes advantage of the cache?
#5 in reply to: ↑ 4
@
15 years ago
Replying to simonwheatley:
Replying to scribu:
Looking good. +1
Should the
user_canfunction be usingget_userdatarather than instantiating a new WP_User object every time, so it takes advantage of the cache?
No, because WP_User calls get_userdata() internally.
Looking at the current patch I wonder whether the
is_intcheck in the first line of the newuser_canfunction ought to be more like this:function user_can( $user, $capability ) { if ( ! is_object( $user ) ) $user = new WP_User( (int) $user ); if ( ! $user || ! $user->ID ) return false; $args = array_slice( func_get_args(), 2 ); $args = array_merge( array( $capability ), $args ); return call_user_func_array( array( &$user, 'has_cap' ), $args ); }