WP_REST_Post_Statuses_Controller::prepare_item_for_response( stdClass $item, WP_REST_Request $request ): WP_REST_Response

In this article

Prepares a post status object for serialization.

Parameters

$itemstdClassrequired
Post status data.
$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response Post status data.

Source

public function prepare_item_for_response( $item, $request ) {
	// Restores the more descriptive, specific name for use within this method.
	$status = $item;

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

	if ( in_array( 'name', $fields, true ) ) {
		$data['name'] = $status->label;
	}

	if ( in_array( 'private', $fields, true ) ) {
		$data['private'] = (bool) $status->private;
	}

	if ( in_array( 'protected', $fields, true ) ) {
		$data['protected'] = (bool) $status->protected;
	}

	if ( in_array( 'public', $fields, true ) ) {
		$data['public'] = (bool) $status->public;
	}

	if ( in_array( 'queryable', $fields, true ) ) {
		$data['queryable'] = (bool) $status->publicly_queryable;
	}

	if ( in_array( 'show_in_list', $fields, true ) ) {
		$data['show_in_list'] = (bool) $status->show_in_admin_all_list;
	}

	if ( in_array( 'slug', $fields, true ) ) {
		$data['slug'] = $status->name;
	}

	if ( in_array( 'date_floating', $fields, true ) ) {
		$data['date_floating'] = $status->date_floating;
	}

	$context = ! empty( $request['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 );

	$rest_url = rest_url( rest_get_route_for_post_type_items( 'post' ) );
	if ( 'publish' === $status->name ) {
		$response->add_link( 'archives', $rest_url );
	} else {
		$response->add_link( 'archives', add_query_arg( 'status', $status->name, $rest_url ) );
	}

	/**
	 * Filters a post status returned from the REST API.
	 *
	 * Allows modification of the status data right before it is returned.
	 *
	 * @since 4.7.0
	 *
	 * @param WP_REST_Response $response The response object.
	 * @param object           $status   The original post status object.
	 * @param WP_REST_Request  $request  Request used to generate the response.
	 */
	return apply_filters( 'rest_prepare_status', $response, $status, $request );
}

Hooks

apply_filters( ‘rest_prepare_status’, WP_REST_Response $response, object $status, WP_REST_Request $request )

Filters a post status returned from the REST API.

Changelog

VersionDescription
5.9.0Renamed $status to $item to match parent class for PHP 8 named parameter support.
4.7.0Introduced.

User Contributed Notes

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