Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ticket - 57902 - REST API index does not respect fields #4483

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function serve_request( $path = null ) {

// Embed links inside the request.
$embed = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
$result = $this->response_to_data( $result, $embed );
$result = $this->response_to_data( $result, $embed, $request );

/**
* Filters the REST API response.
Expand Down Expand Up @@ -554,22 +554,37 @@ public function serve_request( $path = null ) {
*
* @since 4.4.0
* @since 5.4.0 The `$embed` parameter can now contain a list of link relations to include.
* @since 6.3.0 The `$request` parameter was added.
*
* @param WP_REST_Response $response Response object.
* @param bool|string[] $embed Whether to embed all links, a filtered list of link relations, or no links.
* @param WP_REST_Request $request Request object.
* @return array {
* Data with sub-requests embedded.
*
* @type array $_links Links.
* @type array $_embedded Embedded objects.
* }
*/
public function response_to_data( $response, $embed ) {
public function response_to_data( $response, $embed, $request = null ) {
$data = $response->get_data();
$links = self::get_compact_response_links( $response );
Copy link

Choose a reason for hiding this comment

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

Is it better to check that links are needed in the first place before getting them?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, it would be more efficient to skip getting the links at all if we know they are not requested. That would avoid wasted effort generating a links object we don't go on to use.

Copy link
Author

Choose a reason for hiding this comment

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

Right, it's respecting the _fields in function rest_filter_response_fields but

$links = self::get_compact_response_links( $response );
if ( ! empty( $links ) ) {
	$data['_links'] = $links;
}

This code will add the _links anyway, it's not validating the _fields. so I decide to add the $request object as parameter.


if ( ! empty( $links ) ) {
// Convert links to part of the data.
if ( null !== $request && ! empty( $request['_fields'] ) && ! empty( $links ) ) {

$is_link_requested = $request['_fields'];

if ( is_array( $is_link_requested ) ) {
$is_link_requested = implode( ',', $is_link_requested );
}

$is_link_requested = false !== strpos( $is_link_requested, '_links' );

if ( $is_link_requested ) {
$data['_links'] = $links;
}
} elseif ( ! empty( $links ) ) {

$data['_links'] = $links;
}

Expand Down