Changeset 37041
- Timestamp:
- 03/22/2016 12:15:49 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/class-wp-rest-server.php
r37031 r37041 422 422 public function response_to_data( $response, $embed ) { 423 423 $data = $response->get_data(); 424 $links = $this->get_ response_links( $response );424 $links = $this->get_compact_response_links( $response ); 425 425 426 426 if ( ! empty( $links ) ) { … … 455 455 public static function get_response_links( $response ) { 456 456 $links = $response->get_links(); 457 458 457 if ( empty( $links ) ) { 459 458 return array(); … … 462 461 // Convert links to part of the data. 463 462 $data = array(); 463 foreach ( $links as $rel => $items ) { 464 $data[ $rel ] = array(); 465 466 foreach ( $items as $item ) { 467 $attributes = $item['attributes']; 468 $attributes['href'] = $item['href']; 469 $data[ $rel ][] = $attributes; 470 } 471 } 472 473 return $data; 474 } 475 476 /** 477 * Retrieves the CURIEs (compact URIs) used for relations. 478 * 479 * Extracts the links from a response into a structured hash, suitable for 480 * direct output. 481 * 482 * @since 4.5.0 483 * @access public 484 * @static 485 * 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 464 496 $curies = $response->get_curies(); 465 497 $used_curies = array(); … … 473 505 continue; 474 506 } 475 $used_curies[ $curie['name'] ] = $curie;476 507 477 508 // Relation now changes from '$uri' to '$curie:$relation' 478 $rel_regex = str_replace( '\{rel\}', '( [\w]+)', preg_quote( $curie['href'], '!' ) );509 $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); 479 510 preg_match( '!' . $rel_regex . '!', $rel, $matches ); 480 511 if ( $matches ) { 481 $rel = $curie['name'] . ':' . $matches[1]; 482 } 483 break; 484 } 485 486 $data[ $rel ] = array(); 487 488 foreach ( $items as $item ) { 489 $attributes = $item['attributes']; 490 $attributes['href'] = $item['href']; 491 $data[ $rel ][] = $attributes; 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 } 492 518 } 493 519 } … … 495 521 // Push the curies onto the start of the links array. 496 522 if ( $used_curies ) { 497 $ data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data);498 } 499 500 return $ data;523 $links['curies'] = array_values( $used_curies ); 524 } 525 526 return $links; 501 527 } 502 528
Note: See TracChangeset
for help on using the changeset viewer.