WP_REST_Server::get_compact_response_links()public staticWP 4.5.0

Retrieves the CURIEs (compact URIs) used for relations.

Extracts the links from a response into a structured hash, suitable for direct output.

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

Хуков нет.

Возвращает

Массив. Map of link relation to list of link hashes.

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

$result = WP_REST_Server::get_compact_response_links( $response );
$response(WP_REST_Response) (обязательный)
Response to extract links from.

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

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

Код WP_REST_Server::get_compact_response_links() WP 6.5.2

public static function get_compact_response_links( $response ) {
	$links = self::get_response_links( $response );

	if ( empty( $links ) ) {
		return array();
	}

	$curies      = $response->get_curies();
	$used_curies = array();

	foreach ( $links as $rel => $items ) {

		// Convert $rel URIs to their compact versions if they exist.
		foreach ( $curies as $curie ) {
			$href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) );
			if ( ! str_starts_with( $rel, $href_prefix ) ) {
				continue;
			}

			// Relation now changes from '$uri' to '$curie:$relation'.
			$rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) );
			preg_match( '!' . $rel_regex . '!', $rel, $matches );
			if ( $matches ) {
				$new_rel                       = $curie['name'] . ':' . $matches[1];
				$used_curies[ $curie['name'] ] = $curie;
				$links[ $new_rel ]             = $items;
				unset( $links[ $rel ] );
				break;
			}
		}
	}

	// Push the curies onto the start of the links array.
	if ( $used_curies ) {
		$links['curies'] = array_values( $used_curies );
	}

	return $links;
}