WP_REST_Autosaves_Controller::prepare_item_for_response( WP_Post $item, WP_REST_Request $request ): WP_REST_Response
- Since
- 5.0.0, 5.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php:463
Compatibility
- WordPress
- since 5.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$itemWP_Post- Post revision object.
$requestWP_REST_Request- Request object.
Return value
WP_REST_Response- Response object.
Performance profile
How much work a call to WP_REST_Autosaves_Controller::prepare_item_for_response() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 17–75
- Plugin surface
- 1 hook
- Called by
- 3
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.
Third-party callbacks on 'rest_prepare_autosave' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()one call below WP_REST_Autosaves_Controller::prepare_item_for_response()
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Autosaves_Controller::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
->is_method() | 17 | ->is_method(), apply_filters() |
!->is_method() && !$fields | 47–48 | ->is_method(), ->prepare_item_for_response(), ->get_fields_for_response(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters() |
!->is_method() && $fields && $parent_id === false | 65–67 | ->is_method(), ->prepare_item_for_response(), ->get_fields_for_response(), wp_is_post_autosave(), get_preview_post_link(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters() |
!->is_method() && $fields && $parent_id !== false | 73–75 | ->is_method(), ->prepare_item_for_response(), ->get_fields_for_response(), wp_is_post_autosave(), wp_create_nonce(), get_preview_post_link(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 17–75 | 5 | |
| 8.5 | 87 | 17–75 | 5 | |
| 8.4 | 87 | 17–75 | 5 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 90 | 17–78 | 5 | |
| 8.2 | 90 | 17–78 | 5 | |
| 8.1 | 90 | 17–78 | 5 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 92 | 17–80 | 5 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while WP_REST_Autosaves_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_autosave )filterline 470 (+7 into the body)
Filters a revision returned from the REST API.
- apply_filters( rest_prepare_autosave )filterline 503 (+40 into the body)
Filters a revision returned from the REST API.
Uses · 8
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_is_post_autosave()Determines if the specified post is an autosave.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
- get_preview_post_link()Retrieves the URL used for the post preview.
- WP_REST_Response::__construct()
- WP_REST_Autosaves_Controller::get_fields_for_response()
- WP_REST_Autosaves_Controller::add_additional_fields_to_object()
- WP_REST_Autosaves_Controller::filter_response_by_context()
Used by · 3
- WP_REST_Autosaves_Controller::create_item()Creates, updates or deletes an autosave revision.
- WP_REST_Autosaves_Controller::get_item()Get the autosave, if the ID is valid.
- WP_REST_Autosaves_Controller::get_items()Gets a collection of autosaves using wp_get_post_autosave.
Source code
public function prepare_item_for_response( $item, $request ) { // Restores the more descriptive, specific name for use within this method. $post = $item; // Don't prepare the response body for HEAD requests. if ( $request->is_method( 'HEAD' ) ) { /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php */ return apply_filters( 'rest_prepare_autosave', new WP_REST_Response( array() ), $post, $request ); } $response = $this->revisions_controller->prepare_item_for_response( $post, $request ); $fields = $this->get_fields_for_response( $request ); if ( in_array( 'preview_link', $fields, true ) ) { $parent_id = wp_is_post_autosave( $post ); $preview_post_id = false === $parent_id ? $post->ID : $parent_id; $preview_query_args = array(); if ( false !== $parent_id ) { $preview_query_args['preview_id'] = $parent_id; $preview_query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $parent_id ); } $response->data['preview_link'] = get_preview_post_link( $preview_post_id, $preview_query_args ); } $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 revision returned from the REST API. * * Allows modification of the revision right before it is returned. * * @since 5.0.0 * * @param WP_REST_Response $response The response object. * @param WP_Post $post The original revision object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters( 'rest_prepare_autosave', $response, $post, $request ); }Changelog
Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$post to $item to match parent class for PHP 8 named parameter support.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.