Ticket #34729: 34729.3.diff
| File 34729.3.diff, 3.6 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/rest-api/class-wp-rest-response.php
diff --git a/src/wp-includes/rest-api/class-wp-rest-response.php b/src/wp-includes/rest-api/class-wp-rest-response.php index db80029..3bbbd8c 100644
a b class WP_REST_Response extends WP_HTTP_Response { 256 256 257 257 return $error; 258 258 } 259 260 /** 261 * Get the CURIEs (compact URIs) used for relations. 262 * 263 * @return array 264 */ 265 public function get_curies() { 266 $curies = array( 267 array( 268 'name' => 'wp', 269 'href' => 'https://api.w.org/{rel}', 270 'templated' => true, 271 ), 272 ); 273 274 return array_merge( $curies, apply_filters( 'rest_response_link_curies', array() ) ); 275 } 259 276 } -
src/wp-includes/rest-api/class-wp-rest-server.php
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 dad4070..98f174e 100644
a b class WP_REST_Server { 460 460 461 461 // Convert links to part of the data. 462 462 $data = array(); 463 $curies = $response->get_curies(); 464 $used_curies = array(); 465 463 466 foreach ( $links as $rel => $items ) { 467 468 // Convert $rel URIs to their compact versions if they exist. 469 foreach ( $curies as $curie ) { 470 $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); 471 if ( strpos( $rel, $href_prefix ) !== 0 ) { 472 continue; 473 } 474 $used_curies[ $curie['name'] ] = $curie; 475 476 // Relation now changes from '$uri' to '$curie:$relation' 477 $rel_regex = str_replace( '\{rel\}', '([\w]+)', preg_quote( $curie['href'], '!' ) ); 478 preg_match( '!' . $rel_regex . '!', $rel, $matches ); 479 if ( $matches ) { 480 $rel = $curie['name'] . ':' . $matches[1]; 481 } 482 break; 483 } 484 464 485 $data[ $rel ] = array(); 465 486 466 487 foreach ( $items as $item ) { … … class WP_REST_Server { 470 491 } 471 492 } 472 493 494 // Push the curies onto the start of the links array. 495 if ( $used_curies ) { 496 $data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data ); 497 } 498 473 499 return $data; 474 500 } 475 501 -
tests/phpunit/tests/rest-api/rest-server.php
diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 33e3f24..4763edf 100644
a b class Tests_REST_Server extends WP_Test_REST_TestCase { 393 393 $this->assertEquals( 'embed', $alternate[1]['parameters']['context'] ); 394 394 } 395 395 396 public function test_link_curies() { 397 $response = new WP_REST_Response(); 398 $response->add_link( 'https://api.w.org/term', 'http://example.com/' ); 399 400 $data = $this->server->response_to_data( $response, false ); 401 $links = $data['_links']; 402 403 $this->assertArrayHasKey( 'wp:term', $links ); 404 $this->assertArrayHasKey( 'curies', $links ); 405 } 406 407 public function test_custom_curie_link() { 408 $response = new WP_REST_Response(); 409 $response->add_link( 'http://mysite.com/contact.html', 'http://example.com/' ); 410 411 add_filter( 'rest_response_link_curies', array( $this, 'add_custom_curie' ) ); 412 413 $data = $this->server->response_to_data( $response, false ); 414 $links = $data['_links']; 415 416 $this->assertArrayHasKey( 'my_site:contact', $links ); 417 $this->assertArrayHasKey( 'curies', $links ); 418 } 419 420 /** 421 * Helper callback to add a new custom curie via a filter. 422 * 423 * @param array $curies 424 * @return array 425 */ 426 public function add_custom_curie( $curies ) { 427 $curies[] = array( 428 'name' => 'my_site', 429 'href' => 'http://mysite.com/{rel}.html', 430 'templated' => true, 431 ); 432 return $curies; 433 } 434 396 435 /** 397 436 * @depends test_link_embedding 398 437 */