WP_REST_Templates_Controller::prepare_item_for_responsepublicWP 5.8.0

Prepare a single template output for response

Method of the class: WP_REST_Templates_Controller{}

Hooks from the method

Returns

WP_REST_Response. Response object.

Usage

$WP_REST_Templates_Controller = new WP_REST_Templates_Controller();
$WP_REST_Templates_Controller->prepare_item_for_response( $item, $request );
$item(WP_Block_Template) (required)
Template instance.
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 5.8.0 Introduced.
Since 5.9.0 Renamed $template to $item to match parent class for PHP 8 named parameter support.
Since 6.3.0 Added modified property to the response.
Since 7.1.0 Added date property to the response.
Since 7.1.0 The modified property is null for templates that have no modification date.

WP_REST_Templates_Controller::prepare_item_for_response() code WP 7.1

public function prepare_item_for_response( $item, $request ) {
	// Don't prepare the response body for HEAD requests.
	if ( $request->is_method( 'HEAD' ) ) {
		return new WP_REST_Response( array() );
	}

	/*
	 * Resolve pattern blocks so they don't need to be resolved client-side
	 * in the editor, improving performance.
	 */
	$blocks        = parse_blocks( $item->content );
	$blocks        = resolve_pattern_blocks( $blocks );
	$item->content = serialize_blocks( $blocks );

	// 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 ) ) {
		/*
		 * File-backed templates have no modification date, and `mysql_to_rfc3339()`
		 * returns `false` for an empty or malformed value, which the schema does
		 * not allow. Return `null` in that case.
		 */
		$modified         = mysql_to_rfc3339( $template->modified );
		$data['modified'] = false !== $modified ? $modified : null;
	}

	if ( rest_is_field_included( 'date', $fields ) ) {
		/*
		 * File-backed templates have no date, and `mysql_to_rfc3339()` returns
		 * `false` for an empty or malformed value, which the schema does not
		 * allow. Return `null` in that case.
		 */
		$date         = mysql_to_rfc3339( $template->date );
		$data['date'] = false !== $date ? $date : null;
	}

	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 );
	}

	if ( rest_is_field_included( 'plugin', $fields ) ) {
		$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template->slug );
		if ( $registered_template ) {
			$data['plugin'] = $registered_template->plugin;
		}
	}

	$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;
}