From 2399173712011faba4987009b79019f6075e9f5b Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 22 Mar 2016 00:15:49 +0000 Subject: [PATCH] REST API: Provide better method for generating CURIEs In [36533] CURIEs were added to the API responses for the link relation URIs, this makes it a lot easier for clients to look up links by relation. That patch was functional, but broke on edge cases such as embedded responses and collection items with links in the items. This patch instead takes a less obtrusive approach by creating a new `get_compact_response_links` to compliment `get_response_links` making both old and new functionality available. Also the regex for curie relations has been relaxed to `.+` as rel names can have any uri-valid charector in it. Fixes #34729. git-svn-id: https://develop.svn.wordpress.org/trunk@37041 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/class-wp-rest-server.php | 58 ++++++++++++++----- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index f795eb1086..25556962b4 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -421,7 +421,7 @@ class WP_REST_Server { */ public function response_to_data( $response, $embed ) { $data = $response->get_data(); - $links = $this->get_response_links( $response ); + $links = $this->get_compact_response_links( $response ); if ( ! empty( $links ) ) { // Convert links to part of the data. @@ -454,13 +454,45 @@ class WP_REST_Server { */ public static function get_response_links( $response ) { $links = $response->get_links(); - if ( empty( $links ) ) { return array(); } // Convert links to part of the data. $data = array(); + foreach ( $links as $rel => $items ) { + $data[ $rel ] = array(); + + foreach ( $items as $item ) { + $attributes = $item['attributes']; + $attributes['href'] = $item['href']; + $data[ $rel ][] = $attributes; + } + } + + return $data; + } + + /** + * Retrieves the CURIEs (compact URIs) used for relations. + * + * Extracts the links from a response into a structured hash, suitable for + * direct output. + * + * @since 4.5.0 + * @access public + * @static + * + * @param WP_REST_Response $response Response to extract links from. + * @return array Map of link relation to list of link hashes. + */ + 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(); @@ -472,32 +504,26 @@ class WP_REST_Server { 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'], '!' ) ); + $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); preg_match( '!' . $rel_regex . '!', $rel, $matches ); if ( $matches ) { - $rel = $curie['name'] . ':' . $matches[1]; + $new_rel = $curie['name'] . ':' . $matches[1]; + $used_curies[ $curie['name'] ] = $curie; + $links[ $new_rel ] = $items; + unset( $links[ $rel ] ); + break; } - break; - } - - $data[ $rel ] = array(); - - foreach ( $items as $item ) { - $attributes = $item['attributes']; - $attributes['href'] = $item['href']; - $data[ $rel ][] = $attributes; } } // Push the curies onto the start of the links array. if ( $used_curies ) { - $data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data ); + $links['curies'] = array_values( $used_curies ); } - return $data; + return $links; } /**