wppaste
WordPress

WP_REST_Users_Controller::check_role_update( int $user_id, array $roles ): true|WP_Error

Since
4.7.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php:1256
Determines if the current user is allowed to make the desired roles change.

Compatibility

WordPress
since 4.7.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.

Parameters

$user_idint
User ID.
$rolesarray
New user roles.

Return value

true|WP_Error
True if the current user is allowed to make the role change, otherwise a WP_Error object.

Performance profile

How much work a call to WP_REST_Users_Controller::check_role_update() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–44

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 73.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_REST_Users_Controller::check_role_update()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Users_Controller::check_role_update() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–7none
!$roles && !isset($role)22__(), sprintf()
!$roles && isset($role) && !is_multisite() && $user_id === false && !->has_cap()35is_multisite(), get_current_user_id(), ->has_cap(), __(), rest_authorization_required_code(), status()
!$roles && isset($role) && !is_multisite() && $user_id !== false && empty($editable_roles[$role])36is_multisite(), get_current_user_id(), get_editable_roles(), __()
!$roles && isset($role) && is_multisite() && current_user_can() && empty($editable_roles[$role])36is_multisite(), current_user_can(), get_editable_roles(), __()
!$roles && isset($role) && is_multisite() && !current_user_can() && $user_id === false && !->has_cap()39is_multisite(), current_user_can(), get_current_user_id(), ->has_cap(), __(), rest_authorization_required_code(), status()
!$roles && isset($role) && !is_multisite() && $user_id === false && ->has_cap() && empty($editable_roles[$role])40is_multisite(), get_current_user_id(), ->has_cap(), get_editable_roles(), __()
!$roles && isset($role) && is_multisite() && !current_user_can() && $user_id !== false && empty($editable_roles[$role])40is_multisite(), current_user_can(), get_current_user_id(), get_editable_roles(), __()
!$roles && isset($role) && is_multisite() && !current_user_can() && $user_id === false && ->has_cap() && empty($editable_roles[$role])44is_multisite(), current_user_can(), get_current_user_id(), ->has_cap(), get_editable_roles(), __()

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, 6–44 executed per call, 8 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 · 7

Used by · 2

Source code

	protected function check_role_update( $user_id, $roles ) {		global $wp_roles; 		foreach ( $roles as $role ) { 			if ( ! isset( $wp_roles->role_objects[ $role ] ) ) {				return new WP_Error(					'rest_user_invalid_role',					/* translators: %s: Role key. */					sprintf( __( 'The role %s does not exist.' ), $role ),					array( 'status' => 400 )				);			} 			$potential_role = $wp_roles->role_objects[ $role ]; 			/*			 * Don't let anyone with 'edit_users' (admins) edit their own role to something without it.			 * Multisite super admins can freely edit their blog roles -- they possess all caps.			 */			if ( ! ( is_multisite()				&& current_user_can( 'manage_sites' ) )				&& get_current_user_id() === $user_id				&& ! $potential_role->has_cap( 'edit_users' )			) {				return new WP_Error(					'rest_user_invalid_role',					__( 'Sorry, you are not allowed to give users that role.' ),					array( 'status' => rest_authorization_required_code() )				);			} 			// Include user admin functions to get access to get_editable_roles().			require_once ABSPATH . 'wp-admin/includes/user.php'; 			// The new role must be editable by the logged-in user.			$editable_roles = get_editable_roles(); 			if ( empty( $editable_roles[ $role ] ) ) {				return new WP_Error(					'rest_user_invalid_role',					__( 'Sorry, you are not allowed to give users that role.' ),					array( 'status' => 403 )				);			}		} 		return true;	}

Changelog

Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.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.