wppaste
WordPress

WP_Ability::check_permissions( mixed $input = null ): bool|WP_Error

Since
6.9.0, 7.1.0
Source
wp-includes/abilities-api/class-wp-ability.php:623
Checks whether the ability has the necessary permissions.

Description

Please note that input is not automatically validated against the input schema.
Use validate_input() method to validate input before calling this method if needed.

The 'wp_ability_permission_result' filter fires after the registered permission_callback returns, allowing plugins to override the result.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$inputmixedoptional
The valid input data for permission checking. Default null.Default: null

Return value

bool|WP_Error
Whether the ability has the necessary permission.

Performance profile

How much work a call to WP_Ability::check_permissions() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
19–32

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

Plugin surface
1 hook

Third-party callbacks on 'wp_ability_permission_result' run inside this call, and their cost is not bounded by anything here.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()called directly

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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
!is_callable()19is_callable(), __(), sprintf()
is_callable() && is_bool($result)27is_callable(), ->invoke_callback(), apply_filters()
is_callable() && !is_bool($result)31–32is_callable(), ->invoke_callback(), apply_filters(), is_wp_error()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 45 instructions, 19–32 executed per call, 3 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.

Hooks and filters fired · 1

One hook fires while WP_Ability::check_permissions() runs, in this order:

  1. apply_filters( wp_ability_permission_result )filterline 652 (+29 into the body)

    Filters the result of an ability's permission check.

Uses · 5

Used by · 1

Source code

	public function check_permissions( $input = null ) {		if ( ! is_callable( $this->permission_callback ) ) {			return new WP_Error(				'ability_invalid_permission_callback',				/* translators: %s ability name. */				sprintf( __( 'Ability "%s" does not have a valid permission callback.' ), $this->name )			);		} 		$permission = $this->invoke_callback( $this->permission_callback, $input ); 		/**		 * Filters the result of an ability's permission check.		 *		 * Fires after the registered `permission_callback` returns. Plugins can use this to layer		 * additional authorization rules on top of the ability's own permission logic — for example,		 * multi-factor authorization gates or temporary permission elevation for trusted contexts.		 *		 * Filters can return `true` to grant, `false` to deny, or a `WP_Error` to deny with a specific		 * error code and message. The filter receives whatever the `permission_callback` produced.		 * Any other return value is coerced to `false`.		 *		 * @since 7.1.0		 *		 * @param bool|WP_Error $permission   The permission result returned by `permission_callback`.		 * @param string        $ability_name The name of the ability.		 * @param mixed         $input        The input data for the permission check.		 * @param WP_Ability    $ability      The ability instance.		 */		$result = apply_filters( 'wp_ability_permission_result', $permission, $this->name, $input, $this );		if ( ! is_bool( $result ) && ! is_wp_error( $result ) ) {			$result = false;		}		return $result;	}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

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

7.1.0
Added the wp_ability_permission_result filter.from the docblock
6.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/abilities-api/class-wp-ability.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.