Make WordPress Core

Changeset 60893


Ignore:
Timestamp:
10/03/2025 01:37:52 AM (11 months ago)
Author:
andrewserong
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r60195 r60893  
    969969                 */
    970970                return apply_filters( 'rest_prepare_attachment', $response, $post, $request );
     971        }
     972
     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;
    971998        }
    972999
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r60195 r60893  
    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'] );
     1856        }
     1857
     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'] );
    18551897        }
    18561898
Note: See TracChangeset for help on using the changeset viewer.