WP_User::get_role_caps(): bool[]
- Since
- 2.0.0
- Source
wp-includes/class-wp-user.php:511
Description
All of the capabilities of the user's roles are merged with the user's individual capabilities. This means that the user can be denied specific capabilities that their role might have, but the user is specifically denied.
Compatibility
- WordPress
- since 2.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
bool[]- Array of key/value pairs where keys represent a capability name and boolean values represent whether the user has that capability.
Performance profile
How much work a call to WP_User::get_role_caps() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Scales with input
- Instructions
- 29–49
- Plugin surface
- None
- Called by
- 8
Only touches the object cache, via wp_cache_switch_to_blog().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 73.
Nothing here hands control to plugin code.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below WP_User::get_role_caps() - cacheobject cache
wp_cache_switch_to_blog()one call below WP_User::get_role_caps()
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_User::get_role_caps() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_multisite() | 29–36 | is_multisite(), wp_roles(), array_merge() |
!is_multisite() | 31–38 | is_multisite(), wp_roles(), array_merge(), restore_current_blog() |
is_multisite() | 34–41 | is_multisite(), get_current_blog_id(), wp_roles(), array_merge() |
is_multisite() | 36–43 | is_multisite(), get_current_blog_id(), wp_roles(), array_merge(), restore_current_blog() |
is_multisite() | 40–47 | is_multisite(), get_current_blog_id(), switch_to_blog(), wp_roles(), array_merge() |
is_multisite() | 42–49 | is_multisite(), get_current_blog_id(), switch_to_blog(), wp_roles(), array_merge(), restore_current_blog() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 73 instructions, 29–49 executed per call, 9 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- is_multisite()Determines whether Multisite is enabled.
- get_current_blog_id()Retrieves the current site ID.
- switch_to_blog()Switches the current blog.
- wp_roles()Retrieves the global WP_Roles instance and instantiates it if necessary.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
Used by · 8
- WP_User::_init_caps()Sets up capability object properties.
- WP_User::add_cap()Adds capability and grant or deny access to capability.
- WP_User::add_role()Adds role to user.
- WP_User::for_site()Sets the site to operate on. Defaults to the current site.
- WP_User::remove_all_caps()Removes all of the capabilities of the user.
- WP_User::remove_cap()Removes capability from user.
- WP_User::remove_role()Removes role from user.
- WP_User::set_role()Sets the role of the user.
Source code
public function get_role_caps() { $switch_site = false; if ( is_multisite() && get_current_blog_id() !== $this->site_id ) { $switch_site = true; switch_to_blog( $this->site_id ); } $wp_roles = wp_roles(); // Select caps that are role names and assign to $this->roles. if ( is_array( $this->caps ) ) { $this->roles = array(); foreach ( $this->caps as $key => $value ) { if ( $wp_roles->is_role( $key ) ) { $this->roles[] = $key; } } } // Build $allcaps from role caps, overlay user's $caps. $this->allcaps = array(); foreach ( (array) $this->roles as $role ) { $the_role = $wp_roles->get_role( $role ); $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities ); } $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); if ( $switch_site ) { restore_current_blog(); } return $this->allcaps; }Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.