WP_REST_Abilities_V1_List_Controller::prepare_item_for_responsepublicWP 6.9.0

Prepares an ability for response.

Method of the class: WP_REST_Abilities_V1_List_Controller{}

No Hooks.

Returns

WP_REST_Response. Response object.

Usage

$WP_REST_Abilities_V1_List_Controller = new WP_REST_Abilities_V1_List_Controller();
$WP_REST_Abilities_V1_List_Controller->prepare_item_for_response( $ability, $request );
$ability(WP_Ability) (required)
The ability object.
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 6.9.0 Introduced.

WP_REST_Abilities_V1_List_Controller::prepare_item_for_response() code WP 7.0

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->strip_internal_schema_keywords(
			$this->normalize_schema_empty_object_defaults( $ability->get_input_schema() )
		),
		'output_schema' => $this->strip_internal_schema_keywords(
			$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;
}