WP_REST_Templates_Controller::prepare_links()protectedWP 5.8.0

Prepares links for the request.

Метод класса: WP_REST_Templates_Controller{}

Хуков нет.

Возвращает

Массив. Links for the given post.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_links( $id );
$id(int) (обязательный)
ID.

Список изменений

С версии 5.8.0 Введена.

Код WP_REST_Templates_Controller::prepare_links() WP 6.5.2

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