wppaste
WordPress

WP_Ability::execute( mixed $input = null ): mixed|WP_Error

Since
6.9.0, 7.1.0, 7.1.0
Source
wp-includes/abilities-api/class-wp-ability.php:769
Executes the ability after input validation and running a permission check.

Description

Before returning the return value, it also validates the output.

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 input data for the ability. Default null.Default: null

Return value

mixed|WP_Error
The result of the ability execution, or WP_Error on failure.

Performance profile

How much work a call to WP_Ability::execute() 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
27–84

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

Plugin surface
4 hooks

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

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()called directly

Further down the call graph this can also reach option, cache, serialize, query 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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
$pre !== false27do_action(), apply_filters()
$pre === false && is_wp_error()35do_action(), apply_filters(), ->normalize_input(), is_wp_error()
$pre === false43do_action(), apply_filters(), ->normalize_input(), is_wp_error(), ->validate_input(), is_wp_error()
$pre === false && !is_wp_error() && $has_permissions is null, false, long, double, string, array, object, resource65do_action(), apply_filters(), ->normalize_input(), is_wp_error(), ->validate_input(), is_wp_error(), ->check_permissions(), is_wp_error(), __(), sprintf()
$pre === false && $has_permissions is not null, false, long, double, string, array, object, resource66do_action(), apply_filters(), ->normalize_input(), is_wp_error(), ->validate_input(), is_wp_error(), ->check_permissions(), do_action(), ->do_execute(), is_wp_error()
$pre === false && $has_permissions is not null, false, long, double, string, array, object, resource74do_action(), apply_filters(), ->normalize_input(), is_wp_error(), ->validate_input(), is_wp_error(), ->check_permissions(), do_action(), ->do_execute(), is_wp_error(), ->validate_output(), is_wp_error()
$pre === false && $has_permissions is null, false, long, double, string, array, object, resource75do_action(), apply_filters(), ->normalize_input(), is_wp_error(), ->validate_input(), is_wp_error(), ->check_permissions(), is_wp_error(), ->get_error_message(), esc_html(), _doing_it_wrong(), __(), sprintf()
$pre === false && !is_wp_error() && $has_permissions is not null, false, long, double, string, array, object, resource84do_action(), apply_filters(), ->normalize_input(), is_wp_error(), ->validate_input(), is_wp_error(), ->check_permissions(), do_action(), ->do_execute(), is_wp_error(), ->validate_output(), is_wp_error(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 116 instructions, 27–84 executed per call, 7 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 · 4

4 hooks fire while WP_Ability::execute() runs, in this order:

  1. do_action( wp_ability_invoked )actionline 783 (+14 into the body)

    Fires when an ability is invoked, before any processing takes place.

  2. apply_filters( wp_pre_execute_ability )filterline 809 (+40 into the body)

    Filters whether to short-circuit ability execution.

  3. do_action( wp_before_execute_ability )actionline 852 (+83 into the body)

    Fires before an ability gets executed, after input validation and permissions check.

  4. do_action( wp_after_execute_ability )actionline 875 (+106 into the body)

    Fires immediately after an ability finished executing.

Uses · 13

Show all 13

Source code

	public function execute( $input = null ) {		/**		 * Fires when an ability is invoked, before any processing takes place.		 *		 * This action fires for every call regardless of outcome (validation failure,		 * permission denial, short-circuit, or successful execution), and before input		 * normalization so the raw input is captured as-is.		 *		 * @since 7.1.0		 *		 * @param string     $ability_name The name of the ability.		 * @param mixed      $input        The raw input data for the ability, before normalization.		 * @param WP_Ability $ability      The ability instance.		 */		do_action( 'wp_ability_invoked', $this->name, $input, $this ); 		$pre_execute_sentinel = new WP_Filter_Sentinel(); 		/**		 * Filters whether to short-circuit ability execution.		 *		 * Returning a value other than the received default bypasses the rest of `execute()` —		 * input normalization, input validation, permission checks, the registered execute callback,		 * output validation, and the surrounding actions — and the value is returned to the caller		 * as-is. Useful for cached responses, rate limiting, maintenance mode, and test mocking.		 *		 * To continue with normal execution, return `$pre` unchanged. This preserves any value		 * (including `null`, `false`, or arbitrary objects) as a valid short-circuit result.		 *		 * Because validation is bypassed, callers that short-circuit are responsible for the		 * integrity of any value they consume from `$input`.		 *		 * @since 7.1.0		 *		 * @param mixed      $pre          The pre-computed result. Return this value unchanged to continue execution.		 *                                 Default `WP_Filter_Sentinel` instance unique to this invocation.		 * @param string     $ability_name The name of the ability.		 * @param mixed      $input        The raw input passed to `execute()`.		 * @param WP_Ability $ability      The ability instance.		 */		$pre = apply_filters( 'wp_pre_execute_ability', $pre_execute_sentinel, $this->name, $input, $this );		if ( $pre !== $pre_execute_sentinel ) {			return $pre;		} 		$input = $this->normalize_input( $input );		if ( is_wp_error( $input ) ) {			return $input;		} 		$is_valid = $this->validate_input( $input );		if ( is_wp_error( $is_valid ) ) {			return $is_valid;		} 		$has_permissions = $this->check_permissions( $input );		if ( true !== $has_permissions ) {			if ( is_wp_error( $has_permissions ) ) {				// Don't leak the permission check error to someone without the correct perms.				_doing_it_wrong(					__METHOD__,					esc_html( $has_permissions->get_error_message() ),					'6.9.0'				);			} 			return new WP_Error(				'ability_invalid_permissions',				/* translators: %s ability name. */				sprintf( __( 'Ability "%s" does not have necessary permission.' ), $this->name )			);		} 		/**		 * Fires before an ability gets executed, after input validation and permissions check.		 *		 * @since 6.9.0		 * @since 7.1.0 Added the `$ability` parameter.		 *		 * @param string     $ability_name The name of the ability.

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_pre_execute_ability filter.from the docblock
7.1.0
Added the wp_ability_invoked action.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.