Skip to content
Closed
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 @@ -970,6 +970,33 @@ public function prepare_item_for_response( $item, $request ) {
return apply_filters( 'rest_prepare_attachment', $response, $post, $request );
}

/**
* Prepares attachment links for the request.
*
* @since 6.9.0
*
* @param WP_Post $post Post object.
* @return array Links for the given attachment.
*/
protected function prepare_links( $post ) {
$links = parent::prepare_links( $post );

if ( ! empty( $post->post_parent ) ) {
$post = get_post( $post->post_parent );

if ( ! empty( $post ) ) {
$links['post'] = array(
'href' => rest_url( rest_get_route_for_post( $post ) ),
'embeddable' => true,
'post_type' => $post->post_type,
'id' => $post->ID,
);
}
}

return $links;
}

/**
* Retrieves the attachment's schema, conforming to JSON Schema.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1848,12 +1848,54 @@ public function test_links_exist() {

$this->assertArrayHasKey( 'self', $links );
$this->assertArrayHasKey( 'author', $links );
$this->assertArrayNotHasKey( 'post', $links );

$this->assertCount( 1, $links['author'] );
$this->assertArrayHasKey( 'embeddable', $links['author'][0]['attributes'] );
$this->assertTrue( $links['author'][0]['attributes']['embeddable'] );
}

/**
* @ticket 64034
*/
public function test_links_contain_parent() {
wp_set_current_user( self::$editor_id );

$post = self::factory()->post->create(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_title' => 'Test Post',
)
);
$attachment = self::factory()->attachment->create_object(
array(
'file' => self::$test_file,
'post_author' => self::$editor_id,
'post_parent' => $post,
'post_mime_type' => 'image/jpeg',
)
);

$this->assertGreaterThan( 0, $attachment );

$request = new WP_REST_Request( 'GET', "/wp/v2/media/{$attachment}" );
$request->set_query_params( array( 'context' => 'edit' ) );

$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();

$this->assertArrayHasKey( 'self', $links );
$this->assertArrayHasKey( 'author', $links );
$this->assertArrayHasKey( 'post', $links );

$this->assertCount( 1, $links['author'] );
$this->assertSame( rest_url( '/wp/v2/posts/' . $post ), $links['post'][0]['href'] );
$this->assertSame( 'post', $links['post'][0]['attributes']['post_type'] );
$this->assertSame( $post, $links['post'][0]['attributes']['id'] );
$this->assertTrue( $links['post'][0]['attributes']['embeddable'] );
}

public function test_publish_action_ldo_not_registered() {

$response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ) );
Expand Down
Loading