Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1797,17 +1797,17 @@ public function check_read_permission( $post ) {
return true;
}

// Can we read the parent if we're inheriting?
if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
// Can we read the parent if we're inheriting? A post that is its own parent has none to check.
if ( 'inherit' === $post->post_status && $post->post_parent > 0 && $post->post_parent !== $post->ID ) {
$parent = get_post( $post->post_parent );
if ( $parent ) {
return $this->check_read_permission( $parent );
}
}

/*
* If there isn't a parent, but the status is set to inherit, assume
* it's published (as per get_post_status()).
* If there isn't a parent, or the post is its own parent, but the status is
* set to inherit, assume it's published (as per get_post_status()).
*/
if ( 'inherit' === $post->post_status ) {
return true;
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,30 @@ public function test_get_item_inherit_status_with_invalid_parent() {
$this->assertSame( $attachment_id, $data['id'] );
}

public function test_get_item_inherit_status_with_self_parent() {
global $wpdb;

$attachment_id = self::factory()->attachment->create_object(
self::$test_file,
0,
array(
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
)
);

// wp_insert_post() refuses to make a post its own parent, but imports and direct writes can.
$wpdb->update( $wpdb->posts, array( 'post_parent' => $attachment_id ), array( 'ID' => $attachment_id ) );
clean_post_cache( $attachment_id );

$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 200, $response->get_status() );
$this->assertSame( $attachment_id, $data['id'] );
}

public function test_get_item_auto_status_with_invalid_parent_not_authenticated_returns_error() {
$attachment_id = self::factory()->attachment->create_object(
self::$test_file,
Expand Down
Loading