Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -639,35 +639,46 @@ public function prepare_item_for_response( $item, $request ) {
$data['slug'] = $post->post_name;
}

if ( in_array( 'guid', $fields, true ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, if guid was included, the response always contained both guid.rendered and guid.raw.
Now, if the we requests only guid (but not guid.raw or guid.rendered), they’ll get guid: [] (empty array).

Does this break BC?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll actually wind up including both, due to how rest_is_field_included works internally. Take a look here:

foreach ( $fields as $accepted_field ) {
/*
* Check to see if $field is the parent of any item in $fields.
* A field "parent" should be accepted if "parent.child" is accepted.
*/
if ( str_starts_with( $accepted_field, "$field." ) ) {
return true;
}
/*
* Conversely, if "parent" is accepted, all "parent.child" fields
* should also be accepted.
*/
if ( str_starts_with( $field, "$accepted_field." ) ) {
return true;
}
}

It's pretty neat — if a user just provides the parent guid then the sub-properties will also be matched.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks!

$data['guid'] = array(
/** This filter is documented in wp-includes/post-template.php */
'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
'raw' => $post->guid,
);
if ( rest_is_field_included( 'guid', $fields ) ) {
$data['guid'] = array();
}

if ( in_array( 'title', $fields, true ) ) {
$data['title'] = array(
'raw' => $post->post_title,
'rendered' => get_the_title( $post->ID ),
);
if ( rest_is_field_included( 'guid.rendered', $fields ) ) {
/** This filter is documented in wp-includes/post-template.php */
$data['guid']['rendered'] = apply_filters( 'get_the_guid', $post->guid, $post->ID );
}
if ( rest_is_field_included( 'guid.raw', $fields ) ) {
$data['guid']['raw'] = $post->guid;
}

if ( in_array( 'content', $fields, true ) ) {
if ( rest_is_field_included( 'title', $fields ) ) {
$data['title'] = array();
}
if ( rest_is_field_included( 'title.raw', $fields ) ) {
$data['title']['raw'] = $post->post_title;
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
$data['title']['rendered'] = get_the_title( $post->ID );
}

$data['content'] = array(
'raw' => $post->post_content,
/** This filter is documented in wp-includes/post-template.php */
'rendered' => apply_filters( 'the_content', $post->post_content ),
);
if ( rest_is_field_included( 'content', $fields ) ) {
$data['content'] = array();
}
if ( rest_is_field_included( 'content.raw', $fields ) ) {
$data['content']['raw'] = $post->post_content;
}
if ( rest_is_field_included( 'content.rendered', $fields ) ) {
/** This filter is documented in wp-includes/post-template.php */
$data['content']['rendered'] = apply_filters( 'the_content', $post->post_content );
}

if ( in_array( 'excerpt', $fields, true ) ) {
$data['excerpt'] = array(
'raw' => $post->post_excerpt,
'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
);
if ( rest_is_field_included( 'excerpt', $fields ) ) {
$data['excerpt'] = array();
}
if ( rest_is_field_included( 'excerpt.raw', $fields ) ) {
$data['excerpt']['raw'] = $post->post_excerpt;
}
if ( rest_is_field_included( 'excerpt.rendered', $fields ) ) {
$data['excerpt']['rendered'] = $this->prepare_excerpt_response( $post->post_excerpt, $post );
}

if ( rest_is_field_included( 'meta', $fields ) ) {
Expand Down
Loading