REST API: Add support for CURIEs.

CURIEs are Compact URIs, which provide a more usable way to use
custom relations in the API. The `wp` CURIE is registered by default
for `https://api.w.org/` URI relations.

Fixes #34729.
Props joehoyle.

Built from https://develop.svn.wordpress.org/trunk@36533


git-svn-id: http://core.svn.wordpress.org/trunk@36500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan McCue 2016-02-16 02:19:27 +00:00
parent b5e18056e5
commit 47bee5157b
3 changed files with 68 additions and 1 deletions

View File

@ -256,4 +256,45 @@ class WP_REST_Response extends WP_HTTP_Response {
return $error;
}
/**
* Get the CURIEs (compact URIs) used for relations.
*
* @return array
*/
public function get_curies() {
$curies = array(
array(
'name' => 'wp',
'href' => 'https://api.w.org/{rel}',
'templated' => true,
),
);
/**
* Filter extra CURIEs available on API responses.
*
* CURIEs allow a shortened version of URI relations. This allows a more
* usable form for custom relations than using the full URI. These work
* similarly to how XML namespaces work.
*
* Registered CURIES need to specify a name and URI template. This will
* automatically transform URI relations into their shortened version.
* The shortened relation follows the format `{name}:{rel}`. `{rel}` in
* the URI template will be replaced with the `{rel}` part of the
* shortened relation.
*
* For example, a CURIE with name `example` and URI template
* `http://w.org/{rel}` would transform a `http://w.org/term` relation
* into `example:term`.
*
* Well-behaved clients should expand and normalise these back to their
* full URI relation, however some naive clients may not resolve these
* correctly, so adding new CURIEs may break backwards compatibility.
*
* @param array $additional Additional CURIEs to register with the API.
*/
$additional = apply_filters( 'rest_response_link_curies', array() );
return array_merge( $curies, $additional );
}
}

View File

@ -460,7 +460,28 @@ class WP_REST_Server {
// Convert links to part of the data.
$data = 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;
}
$used_curies[ $curie['name'] ] = $curie;
// Relation now changes from '$uri' to '$curie:$relation'
$rel_regex = str_replace( '\{rel\}', '([\w]+)', preg_quote( $curie['href'], '!' ) );
preg_match( '!' . $rel_regex . '!', $rel, $matches );
if ( $matches ) {
$rel = $curie['name'] . ':' . $matches[1];
}
break;
}
$data[ $rel ] = array();
foreach ( $items as $item ) {
@ -470,6 +491,11 @@ class WP_REST_Server {
}
}
// Push the curies onto the start of the links array.
if ( $used_curies ) {
$data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data );
}
return $data;
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.5-alpha-36532';
$wp_version = '4.5-alpha-36533';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.