WP_REST_Template_Autosaves_Controller::prepare_item_for_response( WP_Post $item, WP_REST_Request $request ): WP_REST_Response

In this article

Prepares the item for the REST response.

Parameters

$itemWP_Postrequired
Post revision object.
$requestWP_REST_Requestrequired
Request object.

Return

WP_REST_Response Response object.

Source

public function prepare_item_for_response( $item, $request ) {
	$template = _build_block_template_result_from_post( $item );
	$response = $this->parent_controller->prepare_item_for_response( $template, $request );

	$fields = $this->get_fields_for_response( $request );
	$data   = $response->get_data();

	if ( in_array( 'parent', $fields, true ) ) {
		$data['parent'] = (int) $item->post_parent;
	}

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

	// Wrap the data in a response object.
	$response = new WP_REST_Response( $data );

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$links = $this->prepare_links( $template );
		$response->add_links( $links );
	}

	return $response;
}

Changelog

VersionDescription
6.4.0Introduced.

User Contributed Notes

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