diff --git a/wp-includes/rest-api/class-wp-rest-response.php b/wp-includes/rest-api/class-wp-rest-response.php index db80029038..e4bf422ed4 100644 --- a/wp-includes/rest-api/class-wp-rest-response.php +++ b/wp-includes/rest-api/class-wp-rest-response.php @@ -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 ); + } } diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index dad4070384..98f174e9d5 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/wp-includes/rest-api/class-wp-rest-server.php @@ -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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 9bfa8e8df0..b34e1bb246 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.