Skip to content

Commit 2da7a59

Browse files
committed
REST API: Add an embeddable post to the media endpoint within links
Allows consumers to more easily fetch details of the post a media item is linked to, i.e. to more easily display the uploaded to / attached to status of media items. Props andrewserong, ramonopoly, mukesh27, adamsilverstein. Fixes #64034. git-svn-id: https://develop.svn.wordpress.org/trunk@60893 602fd350-edb4-49c9-b593-d223f7449a82
1 parent dc9dc24 commit 2da7a59

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,33 @@ public function prepare_item_for_response( $item, $request ) {
970970
return apply_filters( 'rest_prepare_attachment', $response, $post, $request );
971971
}
972972

973+
/**
974+
* Prepares attachment links for the request.
975+
*
976+
* @since 6.9.0
977+
*
978+
* @param WP_Post $post Post object.
979+
* @return array Links for the given attachment.
980+
*/
981+
protected function prepare_links( $post ) {
982+
$links = parent::prepare_links( $post );
983+
984+
if ( ! empty( $post->post_parent ) ) {
985+
$post = get_post( $post->post_parent );
986+
987+
if ( ! empty( $post ) ) {
988+
$links['post'] = array(
989+
'href' => rest_url( rest_get_route_for_post( $post ) ),
990+
'embeddable' => true,
991+
'post_type' => $post->post_type,
992+
'id' => $post->ID,
993+
);
994+
}
995+
}
996+
997+
return $links;
998+
}
999+
9731000
/**
9741001
* Retrieves the attachment's schema, conforming to JSON Schema.
9751002
*

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,12 +1848,54 @@ public function test_links_exist() {
18481848

18491849
$this->assertArrayHasKey( 'self', $links );
18501850
$this->assertArrayHasKey( 'author', $links );
1851+
$this->assertArrayNotHasKey( 'post', $links );
18511852

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

1858+
/**
1859+
* @ticket 64034
1860+
*/
1861+
public function test_links_contain_parent() {
1862+
wp_set_current_user( self::$editor_id );
1863+
1864+
$post = self::factory()->post->create(
1865+
array(
1866+
'post_type' => 'post',
1867+
'post_status' => 'publish',
1868+
'post_title' => 'Test Post',
1869+
)
1870+
);
1871+
$attachment = self::factory()->attachment->create_object(
1872+
array(
1873+
'file' => self::$test_file,
1874+
'post_author' => self::$editor_id,
1875+
'post_parent' => $post,
1876+
'post_mime_type' => 'image/jpeg',
1877+
)
1878+
);
1879+
1880+
$this->assertGreaterThan( 0, $attachment );
1881+
1882+
$request = new WP_REST_Request( 'GET', "/wp/v2/media/{$attachment}" );
1883+
$request->set_query_params( array( 'context' => 'edit' ) );
1884+
1885+
$response = rest_get_server()->dispatch( $request );
1886+
$links = $response->get_links();
1887+
1888+
$this->assertArrayHasKey( 'self', $links );
1889+
$this->assertArrayHasKey( 'author', $links );
1890+
$this->assertArrayHasKey( 'post', $links );
1891+
1892+
$this->assertCount( 1, $links['author'] );
1893+
$this->assertSame( rest_url( '/wp/v2/posts/' . $post ), $links['post'][0]['href'] );
1894+
$this->assertSame( 'post', $links['post'][0]['attributes']['post_type'] );
1895+
$this->assertSame( $post, $links['post'][0]['attributes']['id'] );
1896+
$this->assertTrue( $links['post'][0]['attributes']['embeddable'] );
1897+
}
1898+
18571899
public function test_publish_action_ldo_not_registered() {
18581900

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

0 commit comments

Comments
 (0)