WP_REST_Abilities_V1_List_Controller::prepare_item_for_response( WP_Ability $ability, WP_REST_Request $request ): WP_REST_Response

In this article

Prepares an ability for response.

Parameters

$abilityWP_Abilityrequired
The ability object.
$requestWP_REST_Requestrequired
Request object.

Return

WP_REST_Response Response object.

Source

public function prepare_item_for_response( $ability, $request ) {
	$data = array(
		'name'          => $ability->get_name(),
		'label'         => $ability->get_label(),
		'description'   => $ability->get_description(),
		'category'      => $ability->get_category(),
		'input_schema'  => $this->normalize_schema_empty_object_defaults( $ability->get_input_schema() ),
		'output_schema' => $this->normalize_schema_empty_object_defaults( $ability->get_output_schema() ),
		'meta'          => $ability->get_meta(),
	);

	$context = $request['context'] ?? 'view';
	$data    = $this->add_additional_fields_to_object( $data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	$response = rest_ensure_response( $data );

	$fields = $this->get_fields_for_response( $request );
	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$links = array(
			'self'       => array(
				'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $ability->get_name() ) ),
			),
			'collection' => array(
				'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
			),
		);

		$links['wp:action-run'] = array(
			'href' => rest_url( sprintf( '%s/%s/%s/run', $this->namespace, $this->rest_base, $ability->get_name() ) ),
		);

		$response->add_links( $links );
	}

	return $response;
}

Changelog

VersionDescription
6.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.