WP_REST_Abilities_V1_Categories_Controller::prepare_item_for_responsepublicWP 6.9.0

Prepares an ability category for response.

Method of the class: WP_REST_Abilities_V1_Categories_Controller{}

No Hooks.

Returns

WP_REST_Response. Response object.

Usage

$WP_REST_Abilities_V1_Categories_Controller = new WP_REST_Abilities_V1_Categories_Controller();
$WP_REST_Abilities_V1_Categories_Controller->prepare_item_for_response( $category, $request );
$category(WP_Ability_Category) (required)
The ability category object.
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 6.9.0 Introduced.

WP_REST_Abilities_V1_Categories_Controller::prepare_item_for_response() code WP 6.9

public function prepare_item_for_response( $category, $request ) {
	$data = array(
		'slug'        => $category->get_slug(),
		'label'       => $category->get_label(),
		'description' => $category->get_description(),
		'meta'        => $category->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, $category->get_slug() ) ),
			),
			'collection' => array(
				'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
			),
			'abilities'  => array(
				'href' => rest_url( sprintf( '%s/abilities?category=%s', $this->namespace, $category->get_slug() ) ),
			),
		);

		$response->add_links( $links );
	}

	return $response;
}