WP_REST_Templates_Controller::prepare_item_for_response( WP_Block_Template $item, WP_REST_Request $request ): WP_REST_Response

In this article

Prepare a single template output for response

Parameters

$itemWP_Block_Templaterequired
Template instance.
$requestWP_REST_Requestrequired
Request object.

Return

WP_REST_Response Response object.

Source

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

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

	// Base fields for every template.
	$data = array();

	if ( rest_is_field_included( 'id', $fields ) ) {
		$data['id'] = $template->id;
	}

	if ( rest_is_field_included( 'theme', $fields ) ) {
		$data['theme'] = $template->theme;
	}

	if ( rest_is_field_included( 'content', $fields ) ) {
		$data['content'] = array();
	}
	if ( rest_is_field_included( 'content.raw', $fields ) ) {
		$data['content']['raw'] = $template->content;
	}

	if ( rest_is_field_included( 'content.block_version', $fields ) ) {
		$data['content']['block_version'] = block_version( $template->content );
	}

	if ( rest_is_field_included( 'slug', $fields ) ) {
		$data['slug'] = $template->slug;
	}

	if ( rest_is_field_included( 'source', $fields ) ) {
		$data['source'] = $template->source;
	}

	if ( rest_is_field_included( 'origin', $fields ) ) {
		$data['origin'] = $template->origin;
	}

	if ( rest_is_field_included( 'type', $fields ) ) {
		$data['type'] = $template->type;
	}

	if ( rest_is_field_included( 'description', $fields ) ) {
		$data['description'] = $template->description;
	}

	if ( rest_is_field_included( 'title', $fields ) ) {
		$data['title'] = array();
	}

	if ( rest_is_field_included( 'title.raw', $fields ) ) {
		$data['title']['raw'] = $template->title;
	}

	if ( rest_is_field_included( 'title.rendered', $fields ) ) {
		if ( $template->wp_id ) {
			/** This filter is documented in wp-includes/post-template.php */
			$data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id );
		} else {
			$data['title']['rendered'] = $template->title;
		}
	}

	if ( rest_is_field_included( 'status', $fields ) ) {
		$data['status'] = $template->status;
	}

	if ( rest_is_field_included( 'wp_id', $fields ) ) {
		$data['wp_id'] = (int) $template->wp_id;
	}

	if ( rest_is_field_included( 'has_theme_file', $fields ) ) {
		$data['has_theme_file'] = (bool) $template->has_theme_file;
	}

	if ( rest_is_field_included( 'is_custom', $fields ) && 'wp_template' === $template->type ) {
		$data['is_custom'] = $template->is_custom;
	}

	if ( rest_is_field_included( 'author', $fields ) ) {
		$data['author'] = (int) $template->author;
	}

	if ( rest_is_field_included( 'area', $fields ) && 'wp_template_part' === $template->type ) {
		$data['area'] = $template->area;
	}

	if ( rest_is_field_included( 'modified', $fields ) ) {
		$data['modified'] = mysql_to_rfc3339( $template->modified );
	}

	if ( rest_is_field_included( 'author_text', $fields ) ) {
		$data['author_text'] = self::get_wp_templates_author_text_field( $template );
	}

	if ( rest_is_field_included( 'original_source', $fields ) ) {
		$data['original_source'] = self::get_wp_templates_original_source_field( $template );
	}

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

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

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$links = $this->prepare_links( $template->id );
		$response->add_links( $links );
		if ( ! empty( $links['self']['href'] ) ) {
			$actions = $this->get_available_actions();
			$self    = $links['self']['href'];
			foreach ( $actions as $rel ) {
				$response->add_link( $rel, $self );
			}
		}
	}

	return $response;
}

Hooks

apply_filters( ‘the_title’, string $post_title, int $post_id )

Filters the post title.

Changelog

VersionDescription
6.3.0Added modified property to the response.
5.9.0Renamed $template to $item to match parent class for PHP 8 named parameter support.
5.8.0Introduced.

User Contributed Notes

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