Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Sep 24, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion 12 lib/endpoints/class-wp-rest-post-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function get_items( $request ) {
if ( is_wp_error( $post_type ) ) {
continue;
}
$data[ $obj->name ] = $post_type;
$data[ $obj->name ] = $this->prepare_response_for_collection( $post_type );
}
return $data;
}
Expand Down Expand Up @@ -84,6 +84,16 @@ public function prepare_item_for_response( $post_type, $request ) {
$data = $this->filter_response_by_context( $data, $context );
$data = $this->add_additional_fields_to_object( $data, $request );

// Wrap the data in a response object.
$data = rest_ensure_response( $data );

$base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
$data->add_links( array(
'collection' => array(
'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
),
) );

return $data;
}

Expand Down
7 changes: 5 additions & 2 deletions 7 lib/endpoints/class-wp-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,10 +1172,13 @@ protected function prepare_links( $post ) {
// Entity meta
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $post->ID ),
'href' => rest_url( trailingslashit( $base ) . $post->ID ),
),
'collection' => array(
'href' => rest_url( $base ),
'href' => rest_url( $base ),
),
'about' => array(
'href' => rest_url( '/wp/v2/types/' . $this->post_type ),
),
);

Expand Down
11 changes: 6 additions & 5 deletions 11 tests/test-rest-post-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_get_items() {
// Check each key in $data against those in $post_types
foreach ( $data as $key => $obj ) {
$this->assertEquals( $post_types[ $obj['slug'] ]->name, $key );
$this->check_post_type_obj( $post_types[ $obj['slug'] ], $obj );
$this->check_post_type_obj( $post_types[ $obj['slug'] ], $obj, $obj['_links'] );
}
}

Expand Down Expand Up @@ -49,8 +49,8 @@ public function test_delete_item() {
public function test_prepare_item() {
$obj = get_post_type_object( 'post' );
$endpoint = new WP_REST_Post_Types_Controller;
$data = $endpoint->prepare_item_for_response( $obj, new WP_REST_Request );
$this->check_post_type_obj( $obj, $data );
$response = $endpoint->prepare_item_for_response( $obj, new WP_REST_Request );
$this->check_post_type_obj( $obj, $response->get_data(), $response->get_links() );
}

public function test_get_item_schema() {
Expand Down Expand Up @@ -102,11 +102,12 @@ public function additional_field_get_callback( $object ) {
return 123;
}

protected function check_post_type_obj( $post_type_obj, $data ) {
protected function check_post_type_obj( $post_type_obj, $data, $links ) {
$this->assertEquals( $post_type_obj->label, $data['name'] );
$this->assertEquals( $post_type_obj->name, $data['slug'] );
$this->assertEquals( $post_type_obj->description, $data['description'] );
$this->assertEquals( $post_type_obj->hierarchical, $data['hierarchical'] );
$this->assertArrayHasKey( 'collection', $links );
}

protected function check_post_type_object_response( $response ) {
Expand All @@ -115,7 +116,7 @@ protected function check_post_type_object_response( $response ) {
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$obj = get_post_type_object( 'post' );
$this->check_post_type_obj( $obj, $data );
$this->check_post_type_obj( $obj, $data, $response->get_links() );
}

}
2 changes: 2 additions & 0 deletions 2 tests/test-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ public function test_get_item_links() {
$this->assertEquals( rest_url( '/wp/v2/posts/' . $this->post_id ), $links['self'][0]['href'] );
$this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );

$this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( $this->post_id ) ), $links['about'][0]['href'] );

$replies_url = rest_url( '/wp/v2/comments' );
$replies_url = add_query_arg( 'post', $this->post_id, $replies_url );
$this->assertEquals( $replies_url, $links['replies'][0]['href'] );
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.