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.


git-svn-id: https://develop.svn.wordpress.org/trunk@36533 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan McCue
2016-02-16 02:18:34 +00:00
parent 6dd1dd61a1
commit d7e7c0b81b
3 changed files with 106 additions and 0 deletions

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