Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit 57b225c

Browse files
Add ?parent=<id> support for attachments; update Post _link
Also stubs `get_collection_params()` abstraction for Posts controller to make it possible for attachments controller to add `?parent=<id>` support
1 parent adfb546 commit 57b225c

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

lib/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,22 @@ protected function upload_from_data( $data, $headers ) {
367367
return $sideloaded;
368368
}
369369

370+
/**
371+
* Get the query params for collections of attachments.
372+
*
373+
* @return array
374+
*/
375+
public function get_collection_params() {
376+
$params = parent::get_collection_params();
377+
$params['parent'] = array(
378+
'description' => 'Limit results to attachments from a specified parent.',
379+
'type' => 'integer',
380+
'default' => null,
381+
'sanitize_callback' => 'absint',
382+
);
383+
return $params;
384+
}
385+
370386
/**
371387
* Handle an upload via multipart/form-data ($_FILES)
372388
*

lib/endpoints/class-wp-rest-posts-controller.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@ public function register_routes() {
1515

1616
$base = $this->get_post_type_base( $this->post_type );
1717

18-
$posts_args = array(
19-
'context' => array(
20-
'default' => 'view',
21-
),
22-
'page' => array(
23-
'default' => 1,
24-
'sanitize_callback' => 'absint',
25-
),
26-
'per_page' => array(
27-
'default' => 10,
28-
'sanitize_callback' => 'absint',
29-
),
30-
'filter' => array(),
31-
);
18+
$posts_args = $this->get_collection_params();
3219

3320
register_rest_route( 'wp/v2', '/' . $base, array(
3421
array(
@@ -88,6 +75,7 @@ public function get_items( $request ) {
8875
$args = array();
8976
$args['paged'] = $request['page'];
9077
$args['posts_per_page'] = $request['per_page'];
78+
$args['post_parent'] = $request['parent'];
9179

9280
if ( is_array( $request['filter'] ) ) {
9381
$args = array_merge( $args, $request['filter'] );
@@ -1219,7 +1207,7 @@ protected function prepare_links( $post ) {
12191207
}
12201208
if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ) ) ) {
12211209
$attachments_url = rest_url( 'wp/v2/media' );
1222-
$attachments_url = add_query_arg( 'post_parent', $post->ID, $attachments_url );
1210+
$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
12231211
$links['https://api.w.org/attachment'] = array(
12241212
'href' => $attachments_url,
12251213
);
@@ -1544,4 +1532,15 @@ public function get_item_schema() {
15441532
return $this->add_additional_fields_schema( $schema );
15451533
}
15461534

1535+
/**
1536+
* Get the query params for collections of attachments.
1537+
*
1538+
* @return array
1539+
*/
1540+
public function get_collection_params() {
1541+
$params = parent::get_collection_params();
1542+
$params['filter'] = array();
1543+
return $params;
1544+
}
1545+
15471546
}

tests/test-rest-attachments-controller.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ public function test_get_items() {
4646
$this->check_get_posts_response( $response );
4747
}
4848

49+
public function test_get_items_parent() {
50+
$post_id = $this->factory->post->create( array( 'post_title' => 'Test Post' ) );
51+
$attachment_id = $this->factory->attachment->create_object( $this->test_file, $post_id, array(
52+
'post_mime_type' => 'image/jpeg',
53+
'post_excerpt' => 'A sample caption',
54+
) );
55+
$attachment_id2 = $this->factory->attachment->create_object( $this->test_file, 0, array(
56+
'post_mime_type' => 'image/jpeg',
57+
'post_excerpt' => 'A sample caption',
58+
) );
59+
// all attachments
60+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
61+
$response = $this->server->dispatch( $request );
62+
$this->assertEquals( 2, count( $response->get_data() ) );
63+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
64+
// attachments without a parent
65+
$request->set_param( 'parent', 0 );
66+
$response = $this->server->dispatch( $request );
67+
$data = $response->get_data();
68+
$this->assertEquals( 1, count( $data ) );
69+
$this->assertEquals( $attachment_id2, $data[0]['id'] );
70+
// attachments with parent=post_id
71+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
72+
$request->set_param( 'parent', $post_id );
73+
$response = $this->server->dispatch( $request );
74+
$data = $response->get_data();
75+
$this->assertEquals( 1, count( $data ) );
76+
$this->assertEquals( $attachment_id, $data[0]['id'] );
77+
// attachments with invalid parent
78+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
79+
$request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
80+
$response = $this->server->dispatch( $request );
81+
$data = $response->get_data();
82+
$this->assertEquals( 0, count( $data ) );
83+
}
84+
4985
public function test_get_item() {
5086
$attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
5187
'post_mime_type' => 'image/jpeg',

0 commit comments

Comments
 (0)