Skip to content

Commit

Permalink
prepare_item_for_response
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Jan 26, 2024
1 parent d0d8a75 commit 7372ddc
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,29 @@ public function prepare_item_for_response( $collection, $request ) {
}
}

return rest_ensure_response( $item );
$response = rest_ensure_response( $item );

if ( rest_is_field_included( '_links', $fields ) ) {
$links = $this->prepare_links( $collection );
$response->add_links( $links );
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$response->data = $this->add_additional_fields_to_object( $response->data, $request );
$response->data = $this->filter_response_by_context( $response->data, $context );

/**
* Filters a font collection returned from the REST API.
*
* Allows modification of the font collection right before it is returned.
*
* @since 6.5.0
*
* @param WP_REST_Response $response The response object.
* @param WP_Font_Collection $collection The Font Collection object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'rest_prepare_font_collection', $response, $collection, $request );
}

/**
Expand Down Expand Up @@ -246,6 +268,26 @@ public function get_item_schema() {
return $this->add_additional_fields_schema( $this->schema );
}

/**
* Prepares links for the request.
*
* @since 6.5.0
*
* @param WP_Font_Collection $collection Font collection data
* @return array Links for the given font collection.
*/
protected function prepare_links( $collection ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $collection->slug ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);
return $links;
}

/**
* Retrieves the search params for the font collections.
*
Expand Down

0 comments on commit 7372ddc

Please sign in to comment.