WP_REST_Widgets_Controller::delete_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:371
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::delete_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
- 24–103
- Plugin surface
- 2 hooks
- 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 123.
Third-party callbacks on 'delete_widget', 'rest_delete_widget' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()called directly - optionoption read or write
get_option()one call below WP_REST_Widgets_Controller::delete_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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Widgets_Controller::delete_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$sidebar_id === null | 24 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), __() |
$sidebar_id !== null && !$request | 38 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), wp_assign_widget_to_sidebar(), wp_inactive_widgets(), do_action() |
$sidebar_id !== null && $request && !is_callable() | 93–95 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), compact(), ->prepare_item_for_response(), wp_parse_widget_id(), do_action(), is_callable(), ->get_widget_object(), wp_assign_widget_to_sidebar(), ->get_data(), deleted(), do_action() |
$sidebar_id !== null && $request && is_callable() | 101–103 | wp_get_sidebars_widgets(), ->retrieve_widgets(), wp_find_widgets_sidebar(), compact(), ->prepare_item_for_response(), wp_parse_widget_id(), do_action(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->get_widget_object(), wp_assign_widget_to_sidebar(), ->get_data(), deleted(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 123 | 24–103 | 4 | |
| 8.5 | 123 | 24–103 | 4 | |
| 8.4 | 123 | 24–103 | 4 | |
| 8.3 | 123 | 24–103 | 4 | |
| 8.2 | 123 | 24–103 | 4 | |
| 8.1 | 123 | 24–103 | 4 | 1 more instruction than PHP 7.4 |
| 7.4 | 122 | 24–102 | 4 |
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_Widgets_Controller::delete_item() runs, in this order:
- do_action( delete_widget )actionline 418 (+47 into the body)
Fires immediately after a widget has been marked for deletion.
- do_action( rest_delete_widget )actionline 473 (+102 into the body)
Fires after a widget is deleted via the REST API.
Uses · 9
- 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.
- __()Retrieves the translation of $text.
- wp_parse_widget_id()Converts a widget ID into its id_base and number components.
- do_action()Calls the callback functions that have been added to an action hook.
- 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::prepare_item_for_response()Prepares the widget for the REST response.
Source code
public function delete_item( $request ) { global $wp_widget_factory, $wp_registered_widget_updates; /* * 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 ); if ( is_null( $sidebar_id ) ) { return new WP_Error( 'rest_widget_not_found', __( 'No widget was found with that id.' ), array( 'status' => 404 ) ); } $request['context'] = 'edit'; if ( $request['force'] ) { $response = $this->prepare_item_for_response( compact( 'widget_id', 'sidebar_id' ), $request ); $parsed_id = wp_parse_widget_id( $widget_id ); $id_base = $parsed_id['id_base']; $original_post = $_POST; $original_request = $_REQUEST; $_POST = array( 'sidebar' => $sidebar_id, "widget-$id_base" => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1', ); $_REQUEST = $_POST; /** This action is documented in wp-admin/widgets-form.php */ do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base ); $callback = $wp_registered_widget_updates[ $id_base ]['callback']; $params = $wp_registered_widget_updates[ $id_base ]['params']; if ( is_callable( $callback ) ) { ob_start(); call_user_func_array( $callback, $params ); ob_end_clean(); } $_POST = $original_post; $_REQUEST = $original_request; $widget_object = $wp_widget_factory->get_widget_object( $id_base ); if ( $widget_object ) { /* * WP_Widget sets `updated = true` after an update to prevent more than one widget * from being saved per request. This isn't what we want in the REST API, though, * as we support batch requests. */ $widget_object->updated = false; } wp_assign_widget_to_sidebar( $widget_id, '' ); $response->set_data( array( 'deleted' => true, 'previous' => $response->get_data(), ) );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.