WP_REST_Templates_Controller::prepare_links()protectedWP 5.8.0

Prepares links for the request.

Method of the class: WP_REST_Templates_Controller{}

No Hooks.

Return

Array. Links for the given post.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_links( $id );
$id(int) (required)
ID.

Changelog

Since 5.8.0 Introduced.

WP_REST_Templates_Controller::prepare_links() code WP 6.4.3

protected function prepare_links( $id ) {
	$links = array(
		'self'       => array(
			'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ),
		),
		'collection' => array(
			'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ),
		),
		'about'      => array(
			'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
		),
	);

	if ( post_type_supports( $this->post_type, 'revisions' ) ) {
		$template = get_block_template( $id, $this->post_type );
		if ( $template instanceof WP_Block_Template && ! empty( $template->wp_id ) ) {
			$revisions       = wp_get_latest_revision_id_and_total_count( $template->wp_id );
			$revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
			$revisions_base  = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $id );

			$links['version-history'] = array(
				'href'  => rest_url( $revisions_base ),
				'count' => $revisions_count,
			);

			if ( $revisions_count > 0 ) {
				$links['predecessor-version'] = array(
					'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ),
					'id'   => $revisions['latest_id'],
				);
			}
		}
	}

	return $links;
}