WordPress.org

Make WordPress Core

Opened 3 months ago

Last modified 2 months ago

#39805 new enhancement

Expose featured_media property on post resources in "embed" context

Reported by: kadamwhite Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: REST API Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

When you create a link relation that allows you to embed a post in a separate response, the "featured_media" property is omitted from that embedded post. This means that the only way to know the featured media item for an embedded post is to either re-request the embedded resource using the "view" or "edit" context, or else to use the _link to wp:featuredmedia (which _is_ present in the embedded resource, despite the omission of the featured_media property) to deduce the ID of the embedded post's associated media item.

It would be more consistent and straightforward for the ID of the featured media for a post to be included in that post object when the post is embedded. To clarify, this is not suggesting the media item is recursively embedded: just that featured_media: 4168 is included in that embedded post object.

Thanks to @westonruter for raising this inconsistency.

Attachments (3)

39805.1.diff (710 bytes) - added by kadamwhite 3 months ago.
A simple patch permitting featured_media to display in "embed" context
39805.2.diff (3.2 KB) - added by kadamwhite 3 months ago.
Add unit tests to previous patch
39805.3.diff (3.3 KB) - added by kadamwhite 3 months ago.
Adjust unit tests per patch feedback

Download all attachments as: .zip

Change History (12)

@kadamwhite
3 months ago

A simple patch permitting featured_media to display in "embed" context

#1 @kadamwhite
3 months ago

  • Keywords has-patch needs-unit-tests added

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


3 months ago

#3 @westonruter
3 months ago

The situation in particular that I had this problem is I created a related field that links one post to another. This related field is an integer post ID and its link is added as embeddable. When I made a request with _embed for the items having related posts, I was unable to obtain the featured_media via getFeaturedMedia() in the Backbone client.

In other words, if the post is hydrated with data that has _embedded:

post.getRelatedPost().done( function( relatedPost ){
    relatedPost.getFeaturedMedia(); // => Will always fail.
} );

@kadamwhite
3 months ago

Add unit tests to previous patch

#5 @kadamwhite
3 months ago

  • Keywords needs-unit-tests removed

There may be a more elegant way to handle testing this, but I added three test cases to assert which properties a post has in view, edit and embed contexts.

#6 @jnylen0
3 months ago

I wonder why the embed context is such a small subset of data compared to view? There are other elements which could potentially be useful.

Some feedback on the tests - I would test that there are no extra keys, rather than just no missing keys:

<?php
$expected_keys = array( ... );
$actual_keys = array_keys( $data );
sort( $actual_keys );
$this->assertEquals( $expected_keys, $actual_keys );

It may also be worth using a data provider here to cut down on boilerplate.

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


3 months ago

@kadamwhite
3 months ago

Adjust unit tests per patch feedback

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


2 months ago

#9 @jnylen0
2 months ago

  • Keywords has-unit-tests commit added

This particular change looks fine to me, though I would still like to see an answer to this larger question:

I wonder why the embed context is such a small subset of data compared to view? There are other elements which could potentially be useful.

This ticket was mentioned in Slack in #core by kadamwhite. View the logs.


2 months ago

Note: See TracTickets for help on using tickets.