WP_REST_Sidebars_Controller::prepare_item_for_response( array $item, WP_REST_Request $request ): WP_REST_Response
- Since
- 5.8.0, 5.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php:318
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
$itemarray- Sidebar instance.
$requestWP_REST_Request- Full details about the request.
Return value
WP_REST_Response- Prepared response object.
Performance profile
How much work a call to WP_REST_Sidebars_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
- Moderate
- Scaling
- Constant
- Instructions
- 4
- Plugin surface
- 1 hook
- Called by
- 3
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 186.
Third-party callbacks on 'rest_prepare_sidebar' 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 - optionoption read or write
get_option()one call below WP_REST_Sidebars_Controller::prepare_item_for_response()
Further down the call graph this can also reach cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Sidebars_Controller::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 185 | 4 | 19 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 186 | 4 | 19 | |
| 8.4 | 186 | 4 | 19 | |
| 8.3 | 186 | 4 | 19 | |
| 8.2 | 186 | 4 | 19 | |
| 8.1 | 186 | 4 | 19 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 190 | 72–147 | 19 |
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_Sidebars_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_sidebar )filterline 391 (+73 into the body)
Filters the REST API response for a sidebar.
Uses · 12
- wp_sidebar_description()Retrieve description for a sidebar.
- wp_is_block_theme()Returns whether the active theme is a block-based theme or not.
- 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.
- wp_get_sidebars_widgets()Retrieve full list of sidebars and their widget instance IDs.
- rest_validate_value_from_schema()Validate a value based on a schema.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_REST_Sidebars_Controller::get_fields_for_response()
- WP_REST_Sidebars_Controller::get_item_schema()Retrieves the block type' schema, conforming to JSON Schema.
- WP_REST_Sidebars_Controller::add_additional_fields_to_object()
- WP_REST_Sidebars_Controller::filter_response_by_context()
- WP_REST_Sidebars_Controller::prepare_links()Prepares links for the sidebar.
Used by · 3
- WP_REST_Sidebars_Controller::get_item()Retrieves one sidebar from the collection.
- WP_REST_Sidebars_Controller::get_items()Retrieves the list of sidebars (active or inactive).
- WP_REST_Sidebars_Controller::update_item()Updates a sidebar.
Source code
public function prepare_item_for_response( $item, $request ) { global $wp_registered_sidebars, $wp_registered_widgets; // Restores the more descriptive, specific name for use within this method. $raw_sidebar = $item; $id = $raw_sidebar['id']; $sidebar = array( 'id' => $id ); if ( isset( $wp_registered_sidebars[ $id ] ) ) { $registered_sidebar = $wp_registered_sidebars[ $id ]; $sidebar['status'] = 'active'; $sidebar['name'] = isset( $registered_sidebar['name'] ) ? $registered_sidebar['name'] : ''; $sidebar['description'] = isset( $registered_sidebar['description'] ) ? wp_sidebar_description( $id ) : ''; $sidebar['class'] = isset( $registered_sidebar['class'] ) ? $registered_sidebar['class'] : ''; $sidebar['before_widget'] = isset( $registered_sidebar['before_widget'] ) ? $registered_sidebar['before_widget'] : ''; $sidebar['after_widget'] = isset( $registered_sidebar['after_widget'] ) ? $registered_sidebar['after_widget'] : ''; $sidebar['before_title'] = isset( $registered_sidebar['before_title'] ) ? $registered_sidebar['before_title'] : ''; $sidebar['after_title'] = isset( $registered_sidebar['after_title'] ) ? $registered_sidebar['after_title'] : ''; } else { $sidebar['status'] = 'inactive'; $sidebar['name'] = $raw_sidebar['name']; $sidebar['description'] = ''; $sidebar['class'] = ''; } if ( wp_is_block_theme() ) { $sidebar['status'] = 'inactive'; } $fields = $this->get_fields_for_response( $request ); if ( rest_is_field_included( 'widgets', $fields ) ) { $sidebars = wp_get_sidebars_widgets(); $widgets = array_filter( isset( $sidebars[ $sidebar['id'] ] ) ? $sidebars[ $sidebar['id'] ] : array(), static function ( $widget_id ) use ( $wp_registered_widgets ) { return isset( $wp_registered_widgets[ $widget_id ] ); } ); $sidebar['widgets'] = array_values( $widgets ); } $schema = $this->get_item_schema(); $data = array(); foreach ( $schema['properties'] as $property_id => $property ) { if ( isset( $sidebar[ $property_id ] ) && true === rest_validate_value_from_schema( $sidebar[ $property_id ], $property ) ) { $data[ $property_id ] = $sidebar[ $property_id ]; } elseif ( isset( $property['default'] ) ) { $data[ $property_id ] = $property['default']; } } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context ); $response = rest_ensure_response( $data ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $response->add_links( $this->prepare_links( $sidebar ) ); } /** * Filters the REST API response for a sidebar. * * @since 5.8.0 * * @param WP_REST_Response $response The response object. * @param array $raw_sidebar The raw sidebar data. * @param WP_REST_Request $request The request object. */ return apply_filters( 'rest_prepare_sidebar', $response, $raw_sidebar, $request ); }Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$raw_sidebar to $item to match parent class for PHP 8 named parameter support.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-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.