WP_REST_Server::get_compact_response_links() WordPress Method

The WP_REST_Server::get_compact_response_links() function is used to retrieve the links for a given response in a compact representation. This is useful for responses that are returned in a JSON or XML format, where the full representation of the links would be verbose and difficult to read.

WP_REST_Server::get_compact_response_links( WP_REST_Response $response ) #

Retrieves the CURIEs (compact URIs) used for relations.


Description

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


Top ↑

Parameters

$response

(WP_REST_Response)(Required)Response to extract links from.


Top ↑

Return

(array) Map of link relation to list of link hashes.


Top ↑

Source

File: wp-includes/rest-api/class-wp-rest-server.php

	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 ( strpos( $rel, $href_prefix ) !== 0 ) {
					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;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.