WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 3 months ago

#39805 closed enhancement (fixed)

Expose featured_media property on post resources in "embed" context

Reported by: kadamwhite Owned by: rmccue
Milestone: 4.8 Priority: normal
Severity: normal Version: 4.8
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 6 months ago.
A simple patch permitting featured_media to display in "embed" context
39805.2.diff (3.2 KB) - added by kadamwhite 6 months ago.
Add unit tests to previous patch
39805.3.diff (3.3 KB) - added by kadamwhite 6 months ago.
Adjust unit tests per patch feedback

Download all attachments as: .zip

Change History (16)

@kadamwhite
6 months ago

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

#1 @kadamwhite
6 months ago

  • Keywords has-patch needs-unit-tests added

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


6 months ago

#3 @westonruter
6 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
6 months ago

Add unit tests to previous patch

#5 @kadamwhite
6 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 follow-up: @jnylen0
6 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.


6 months ago

@kadamwhite
6 months ago

Adjust unit tests per patch feedback

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


6 months ago

#9 @jnylen0
6 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.


6 months ago

#11 @joehoyle
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.

This was intended to save bandwidth I believe.

#12 in reply to: ↑ 6 @rmccue
3 months ago

  • Owner set to rmccue
  • Status changed from new to reviewing

Replying to jnylen0:

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.

The embed context was intended for data you'd want if embedding the object on a page (such as an oEmbed item, sidebar widget, search result, etc). Makes sense to include the featured media for that.

#13 @rmccue
3 months ago

  • Milestone changed from Awaiting Review to 4.8

#14 @rmccue
3 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 40602:

REST API: Include featured_media in embed responses.

Props kadamwhite, jnylen0, westonruter.
Fixes #39805.

Note: See TracTickets for help on using tickets.