diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 589e0e2ccec17..53b94bf43947b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -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. * diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index d065238b8beb7..3bc9f798e6d7e 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -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' ) );