WP_Ability::executepublicWP 6.9.0

Executes the ability after input validation and running a permission check. Before returning the return value, it also validates the output.

Method of the class: WP_Ability{}

Returns

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

Usage

$WP_Ability = new WP_Ability();
$WP_Ability->execute( $input );
$input(mixed)
The input data for the ability.
Default: null

Changelog

Since 6.9.0 Introduced.

WP_Ability::execute() code WP 6.9.1

public function execute( $input = null ) {
	$input    = $this->normalize_input( $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.' ), esc_html( $this->name ) )
		);
	}

	/**
	 * Fires before an ability gets executed, after input validation and permissions check.
	 *
	 * @since 6.9.0
	 *
	 * @param string $ability_name The name of the ability.
	 * @param mixed  $input        The input data for the ability.
	 */
	do_action( 'wp_before_execute_ability', $this->name, $input );

	$result = $this->do_execute( $input );
	if ( is_wp_error( $result ) ) {
		return $result;
	}

	$is_valid = $this->validate_output( $result );
	if ( is_wp_error( $is_valid ) ) {
		return $is_valid;
	}

	/**
	 * Fires immediately after an ability finished executing.
	 *
	 * @since 6.9.0
	 *
	 * @param string $ability_name The name of the ability.
	 * @param mixed  $input        The input data for the ability.
	 * @param mixed  $result       The result of the ability execution.
	 */
	do_action( 'wp_after_execute_ability', $this->name, $input, $result );

	return $result;
}