WP_AI_Client_Ability_Function_Resolver::execute_abilitypublicWP 7.0.0

Executes a WordPress ability from a function call.

Only abilities that were specified in the constructor are allowed to be executed. If the ability is not in the allowed list, an error response with code ability_not_allowed is returned.

Method of the class: WP_AI_Client_Ability_Function_Resolver{}

No Hooks.

Returns

FunctionResponse. The response from executing the ability.

Usage

$WP_AI_Client_Ability_Function_Resolver = new WP_AI_Client_Ability_Function_Resolver();
$WP_AI_Client_Ability_Function_Resolver->execute_ability( $call ): FunctionResponse;
$call(FunctionCall) (required)
The function call to execute.

Changelog

Since 7.0.0 Introduced.

WP_AI_Client_Ability_Function_Resolver::execute_ability() code WP 7.0

public function execute_ability( FunctionCall $call ): FunctionResponse {
	$function_name = $call->getName() ?? 'unknown';
	$function_id   = $call->getId() ?? 'unknown';

	if ( ! $this->is_ability_call( $call ) ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				'error' => __( 'Not an ability function call' ),
				'code'  => 'invalid_ability_call',
			)
		);
	}

	$ability_name = self::function_name_to_ability_name( $function_name );

	if ( ! isset( $this->allowed_abilities[ $ability_name ] ) ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				/* translators: %s: ability name */
				'error' => sprintf( __( 'Ability "%s" was not specified in the allowed abilities list.' ), $ability_name ),
				'code'  => 'ability_not_allowed',
			)
		);
	}

	$ability = wp_get_ability( $ability_name );

	if ( ! $ability instanceof WP_Ability ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				/* translators: %s: ability name */
				'error' => sprintf( __( 'Ability "%s" not found' ), $ability_name ),
				'code'  => 'ability_not_found',
			)
		);
	}

	$args   = $call->getArgs();
	$result = $ability->execute( ! empty( $args ) ? $args : null );

	if ( is_wp_error( $result ) ) {
		return new FunctionResponse(
			$function_id,
			$function_name,
			array(
				'error' => $result->get_error_message(),
				'code'  => $result->get_error_code(),
				'data'  => $result->get_error_data(),
			)
		);
	}

	return new FunctionResponse(
		$function_id,
		$function_name,
		$result
	);
}