WP_REST_Plugins_Controller::prepare_item_for_response( array $item, WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.5.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php:578
Compatibility
- WordPress
- since 5.5.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
$itemarray- Unmarked up and untranslated plugin data from get_plugin_data().
$requestWP_REST_Request- Request object.
Return value
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Plugins_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
- Trivial
- Scaling
- Constant
- Instructions
- 82–88
- Plugin surface
- 1 hook
- Called by
- 5
Touches nothing outside its own arguments.
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 88.
Third-party callbacks on 'rest_prepare_plugin' run inside this call, and their cost is not bounded by anything here.
5 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
Further down the call graph this can also reach query, 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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Plugins_Controller::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!rest_is_field_included() | 82 | ->get_fields_for_response(), _get_plugin_data_markup_translate(), _get_plugin_data_markup_translate(), ->get_plugin_status(), ->add_additional_fields_to_object(), raw(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
rest_is_field_included() | 83 | ->get_fields_for_response(), _get_plugin_data_markup_translate(), _get_plugin_data_markup_translate(), ->get_plugin_status(), ->add_additional_fields_to_object(), raw(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
| always | 88 | ->get_fields_for_response(), _get_plugin_data_markup_translate(), _get_plugin_data_markup_translate(), ->get_plugin_status(), ->add_additional_fields_to_object(), raw(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 88 | 82–88 | 2 | |
| 8.5 | 88 | 82–88 | 2 | |
| 8.4 | 88 | 82–88 | 2 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 91 | 85–91 | 2 | |
| 8.2 | 91 | 85–91 | 2 | |
| 8.1 | 91 | 85–91 | 2 | |
| 7.4 | 91 | 85–91 | 2 |
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 · 1
One hook fires while WP_REST_Plugins_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_plugin )filterline 619 (+41 into the body)
Filters plugin data for a REST API response.
Uses · 8
- _get_plugin_data_markup_translate()Sanitizes plugin data, optionally adds markup, optionally translates.
- rest_is_field_included()Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_REST_Plugins_Controller::get_fields_for_response()
- WP_REST_Plugins_Controller::get_plugin_status()Get's the activation status for a plugin.
- WP_REST_Plugins_Controller::add_additional_fields_to_object()
- WP_REST_Response::__construct()
- WP_REST_Plugins_Controller::prepare_links()Prepares links for the request.
Used by · 5
- WP_REST_Plugins_Controller::create_item()Uploads a plugin and optionally activates it.
- WP_REST_Plugins_Controller::delete_item()Deletes one plugin from the site.
- WP_REST_Plugins_Controller::get_item()Retrieves one plugin from the site.
- WP_REST_Plugins_Controller::get_items()Retrieves a collection of plugins.
- WP_REST_Plugins_Controller::update_item()Updates one plugin.
Source code
public function prepare_item_for_response( $item, $request ) { $fields = $this->get_fields_for_response( $request ); $item = _get_plugin_data_markup_translate( $item['_file'], $item, false ); $marked = _get_plugin_data_markup_translate( $item['_file'], $item, true ); $data = array( 'plugin' => substr( $item['_file'], 0, - 4 ), 'status' => $this->get_plugin_status( $item['_file'] ), 'name' => $item['Name'], 'plugin_uri' => $item['PluginURI'], 'author' => $item['Author'], 'author_uri' => $item['AuthorURI'], 'description' => array( 'raw' => $item['Description'], 'rendered' => $marked['Description'], ), 'version' => $item['Version'], 'network_only' => $item['Network'], 'requires_wp' => $item['RequiresWP'], 'requires_php' => $item['RequiresPHP'], 'textdomain' => $item['TextDomain'], ); $data = $this->add_additional_fields_to_object( $data, $request ); $response = new WP_REST_Response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $response->add_links( $this->prepare_links( $item ) ); } /** * Filters plugin data for a REST API response. * * @since 5.5.0 * * @param WP_REST_Response $response The response object. * @param array $item The plugin item from {@see get_plugin_data()}. * @param WP_REST_Request $request The request object. */ return apply_filters( 'rest_prepare_plugin', $response, $item, $request ); }Changelog
Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-plugins-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.