WP_REST_Widgets_Controller::update_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.8.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php:296
Compatibility
- WordPress
- since 5.8.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
$requestWP_REST_Request- Full details about the request.
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_Widgets_Controller::update_item() 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
- 34–66
- Plugin surface
- None
- Called by
- 0
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, depending on the branch taken. The body compiles to 76.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- optionoption read or write
get_option()one call below WP_REST_Widgets_Controller::update_item() - hookthird-party callbacks
apply_filters()one call below WP_REST_Widgets_Controller::update_item()
Further down the call graph this can also reach cache, serialize, query 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 · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Widgets_Controller::update_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$sidebar_id === null | 34 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), __() |
->has_param() && is_wp_error() | 38–39 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->save_widget(), is_wp_error() |
is_wp_error() | 42–43 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->has_param(), ->save_widget(), is_wp_error() |
!->has_param() | 47–51 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->has_param(), ->has_param(), compact(), ->prepare_item_for_response() |
->has_param() && !is_wp_error() | 52–56 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->save_widget(), is_wp_error(), ->has_param(), compact(), ->prepare_item_for_response() |
$sidebar_id !== false | 56–57 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->has_param(), ->has_param(), wp_assign_widget_to_sidebar(), compact(), ->prepare_item_for_response() |
!is_wp_error() | 56–60 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->has_param(), ->save_widget(), is_wp_error(), ->has_param(), compact(), ->prepare_item_for_response() |
->has_param() && !is_wp_error() && $sidebar_id !== false | 61–62 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->save_widget(), is_wp_error(), ->has_param(), wp_assign_widget_to_sidebar(), compact(), ->prepare_item_for_response() |
!is_wp_error() && $sidebar_id !== false | 65–66 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_parse_widget_id(), ->get_widget_object(), ->has_param(), ->has_param(), ->save_widget(), is_wp_error(), ->has_param(), wp_assign_widget_to_sidebar(), compact(), ->prepare_item_for_response() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 76 instructions, 34–66 executed per call, 7 branches. The work does not change between versions.
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.
Uses · 10
- wp_get_sidebars_widgets()Retrieves the full list of sidebars and their widget instance IDs.
- wp_find_widgets_sidebar()Finds the sidebar that a given widget belongs to.
- wp_parse_widget_id()Converts a widget ID into its id_base and number components.
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_assign_widget_to_sidebar()Assigns a widget to the given sidebar.
- WP_REST_Widgets_Controller::retrieve_widgets()Looks for "lost" widgets once per request.
- WP_Error::__construct()Initializes the error.
- WP_REST_Widgets_Controller::save_widget()Saves the widget in the request object.
- WP_REST_Widgets_Controller::prepare_item_for_response()Prepares the widget for the REST response.
Source code
public function update_item( $request ) { global $wp_widget_factory; /* * retrieve_widgets() contains logic to move "hidden" or "lost" widgets to the * wp_inactive_widgets sidebar based on the contents of the $sidebars_widgets global. * * When batch requests are processed, this global is not properly updated by previous * calls, resulting in widgets incorrectly being moved to the wp_inactive_widgets * sidebar. * * See https://core.trac.wordpress.org/ticket/53657. */ wp_get_sidebars_widgets(); $this->retrieve_widgets(); $widget_id = $request['id']; $sidebar_id = wp_find_widgets_sidebar( $widget_id ); // Allow sidebar to be unset or missing when widget is not a WP_Widget. $parsed_id = wp_parse_widget_id( $widget_id ); $widget_object = $wp_widget_factory->get_widget_object( $parsed_id['id_base'] ); if ( is_null( $sidebar_id ) && $widget_object ) { return new WP_Error( 'rest_widget_not_found', __( 'No widget was found with that id.' ), array( 'status' => 404 ) ); } if ( $request->has_param( 'instance' ) || $request->has_param( 'form_data' ) ) { $maybe_error = $this->save_widget( $request, $sidebar_id ); if ( is_wp_error( $maybe_error ) ) { return $maybe_error; } } if ( $request->has_param( 'sidebar' ) ) { if ( $sidebar_id !== $request['sidebar'] ) { $sidebar_id = $request['sidebar']; wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ); } } $request['context'] = 'edit'; return $this->prepare_item_for_response( compact( 'widget_id', 'sidebar_id' ), $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.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-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.