Ticket #34729: 34729.7.diff
| File 34729.7.diff, 4.5 KB (added by , 9 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..85488ca 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 * 314 * @param array $links Links to map CURIEs to. 315 * @return array Map of link relation to list of link hashes. 316 */ 317 public function get_compact_response_links( $links ) { 318 if ( empty( $links ) ) { 319 return array(); 320 } 321 322 $curies = $this->get_curies(); 323 $used_curies = array(); 324 325 foreach ( $links as $rel => $items ) { 326 327 // Convert $rel URIs to their compact versions if they exist. 328 foreach ( $curies as $curie ) { 329 $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); 330 if ( strpos( $rel, $href_prefix ) !== 0 ) { 331 continue; 332 } 333 334 // Relation now changes from '$uri' to '$curie:$relation' 335 $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); 336 preg_match( '!' . $rel_regex . '!', $rel, $matches ); 337 if ( $matches ) { 338 $new_rel = $curie['name'] . ':' . $matches[1]; 339 $used_curies[ $curie['name'] ] = $curie; 340 $links[ $new_rel ] = $items; 341 unset( $links[ $rel ] ); 342 break; 343 } 344 } 345 } 346 347 // Push the curies onto the start of the links array. 348 if ( $used_curies ) { 349 $links = array_merge( array( 'curies' => array_values( $used_curies ) ), $links ); 350 } 351 352 return $links; 353 } 305 354 } -
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..313a8d9 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 { 461 461 // Convert links to part of the data. 462 462 $data = array(); 463 463 foreach ( $links as $rel => $items ) { 464 if ( 'curies' === $rel ) { 465 $data[ $rel ] = $items; 466 continue; 467 } 468 464 469 $data[ $rel ] = array(); 465 470 466 471 foreach ( $items as $item ) { … … class WP_REST_Server { 474 479 } 475 480 476 481 /** 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 482 * Embeds the links from the data into the request. 531 483 * 532 484 * @since 4.4.0