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

Commit 7573f8a

Browse files
committed
Merge pull request #1973 from WP-API/1959-fix-query-media
Permit logged-in users to query for media
2 parents d0a13d4 + db71a55 commit 7573f8a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ public function get_collection_params() {
16041604
),
16051605
);
16061606
$params['status'] = array(
1607-
'default' => 'publish',
1607+
'default' => 'attachment' === $this->post_type ? 'inherit' : 'publish',
16081608
'description' => __( 'Limit result set to posts assigned a specific status.' ),
16091609
'sanitize_callback' => 'sanitize_key',
16101610
'type' => 'string',
@@ -1625,7 +1625,7 @@ public function get_collection_params() {
16251625
* @return WP_Error|bool
16261626
*/
16271627
public function validate_user_can_query_private_statuses( $value, $request, $parameter ) {
1628-
if ( 'publish' === $value ) {
1628+
if ( 'publish' === $value || ( 'attachment' === $this->post_type && 'inherit' === $value ) ) {
16291629
return true;
16301630
}
16311631
$post_type_obj = get_post_type_object( $this->post_type );

tests/test-rest-attachments-controller.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ public function test_get_items() {
6565
$this->check_get_posts_response( $response );
6666
}
6767

68+
public function test_get_items_logged_in_editor() {
69+
wp_set_current_user( $this->editor_id );
70+
$id1 = $this->factory->attachment->create_object( $this->test_file, 0, array(
71+
'post_mime_type' => 'image/jpeg',
72+
'post_excerpt' => 'A sample caption',
73+
) );
74+
$draft_post = $this->factory->post->create( array( 'post_status' => 'draft' ) );
75+
$id2 = $this->factory->attachment->create_object( $this->test_file, $draft_post, array(
76+
'post_mime_type' => 'image/jpeg',
77+
'post_excerpt' => 'A sample caption',
78+
) );
79+
$published_post = $this->factory->post->create( array( 'post_status' => 'publish' ) );
80+
$id3 = $this->factory->attachment->create_object( $this->test_file, $published_post, array(
81+
'post_mime_type' => 'image/jpeg',
82+
'post_excerpt' => 'A sample caption',
83+
) );
84+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
85+
$response = $this->server->dispatch( $request );
86+
87+
$data = $response->get_data();
88+
$this->assertCount( 3, $data );
89+
$ids = wp_list_pluck( $data, 'id' );
90+
$this->assertTrue( in_array( $id1, $ids ) );
91+
$this->assertTrue( in_array( $id2, $ids ) );
92+
$this->assertTrue( in_array( $id3, $ids ) );
93+
}
94+
6895
public function test_get_items_parent() {
6996
$post_id = $this->factory->post->create( array( 'post_title' => 'Test Post' ) );
7097
$attachment_id = $this->factory->attachment->create_object( $this->test_file, $post_id, array(

0 commit comments

Comments
 (0)