Ticket #34729: 34729.6.diff
File 34729.6.diff, 4.2 KB (added by , 8 years ago) |
---|
-
wp-includes/rest-api/class-wp-rest-response.php
diff --git wp-includes/rest-api/class-wp-rest-response.php wp-includes/rest-api/class-wp-rest-response.php index 65ae872..73871a6 100644
class WP_REST_Response extends WP_HTTP_Response { 136 136 * @return array List of links. 137 137 */ 138 138 public function get_links() { 139 return $this-> links;139 return $this->get_compact_response_links( $this->links ); 140 140 } 141 141 142 142 /** … … class WP_REST_Response extends WP_HTTP_Response { 302 302 $additional = apply_filters( 'rest_response_link_curies', array() ); 303 303 return array_merge( $curies, $additional ); 304 304 } 305 306 /** 307 * Retrieves the CURIEs (compact URIs) used for relations. 308 * 309 * Adds the CURIE link and maps the full URIs to the CURIE form. 310 * 311 * @since 4.5.0 312 * @access public 313 * @static 314 * 315 * @param array $links Links to map CURIEs to. 316 * @return array Map of link relation to list of link hashes. 317 */ 318 public function get_compact_response_links( $links ) { 319 if ( empty( $links ) ) { 320 return array(); 321 } 322 323 $curies = $this->get_curies(); 324 $used_curies = array(); 325 326 foreach ( $links as $rel => $items ) { 327 328 // Convert $rel URIs to their compact versions if they exist. 329 foreach ( $curies as $curie ) { 330 $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); 331 if ( strpos( $rel, $href_prefix ) !== 0 ) { 332 continue; 333 } 334 335 // Relation now changes from '$uri' to '$curie:$relation' 336 $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); 337 preg_match( '!' . $rel_regex . '!', $rel, $matches ); 338 if ( $matches ) { 339 $new_rel = $curie['name'] . ':' . $matches[1]; 340 $used_curies[ $curie['name'] ] = $curie; 341 $links[ $new_rel ] = $items; 342 unset( $links[ $rel ] ); 343 break; 344 } 345 } 346 } 347 348 // Push the curies onto the start of the links array. 349 if ( $used_curies ) { 350 $links['curies'] = array_values( $used_curies ); 351 } 352 353 return $links; 354 } 305 355 } -
wp-includes/rest-api/class-wp-rest-server.php
diff --git wp-includes/rest-api/class-wp-rest-server.php wp-includes/rest-api/class-wp-rest-server.php index 7a45906..c76729b 100644
class WP_REST_Server { 421 421 */ 422 422 public function response_to_data( $response, $embed ) { 423 423 $data = $response->get_data(); 424 $links = $this->get_compact_response_links( $response );424 $links = self::get_response_links( $response ); 425 425 426 426 if ( ! empty( $links ) ) { 427 427 // Convert links to part of the data. … … class WP_REST_Server { 474 474 } 475 475 476 476 /** 477 * Retrieves the CURIEs (compact URIs) used for relations.478 *479 * Extracts the links from a response into a structured hash, suitable for480 * direct output.481 *482 * @since 4.5.0483 * @access public484 * @static485 *486 * @param WP_REST_Response $response Response to extract links from.487 * @return array Map of link relation to list of link hashes.488 */489 public static function get_compact_response_links( $response ) {490 $links = self::get_response_links( $response );491 492 if ( empty( $links ) ) {493 return array();494 }495 496 $curies = $response->get_curies();497 $used_curies = array();498 499 foreach ( $links as $rel => $items ) {500 501 // Convert $rel URIs to their compact versions if they exist.502 foreach ( $curies as $curie ) {503 $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) );504 if ( strpos( $rel, $href_prefix ) !== 0 ) {505 continue;506 }507 508 // Relation now changes from '$uri' to '$curie:$relation'509 $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) );510 preg_match( '!' . $rel_regex . '!', $rel, $matches );511 if ( $matches ) {512 $new_rel = $curie['name'] . ':' . $matches[1];513 $used_curies[ $curie['name'] ] = $curie;514 $links[ $new_rel ] = $items;515 unset( $links[ $rel ] );516 break;517 }518 }519 }520 521 // Push the curies onto the start of the links array.522 if ( $used_curies ) {523 $links['curies'] = array_values( $used_curies );524 }525 526 return $links;527 }528 529 /**530 477 * Embeds the links from the data into the request. 531 478 * 532 479 * @since 4.4.0