WP_REST_Widgets_Controller::save_widget( WP_REST_Request $request, string $sidebar_id ): string|WP_Error
- Since
- 5.8.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php:526
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.
$sidebar_idstring- ID of the sidebar the widget belongs to.
Return value
string|WP_Error- The saved widget ID.
Performance profile
How much work a call to WP_REST_Widgets_Controller::save_widget() 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
- Light
- Scaling
- Scales with input
- Instructions
- 20–118
- Plugin surface
- 1 hook
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 217.
Third-party callbacks on 'rest_after_save_widget' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - serializeserialisation
unserialize()called directly
Further down the call graph this can also reach option, cache, 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 · 31 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Widgets_Controller::save_widget() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($request) && !$request | 20 | __() |
!isset($request) && $request | 36–50 | ->get_widget_object(), __() |
isset($request) | 37–50 | wp_parse_widget_id(), ->get_widget_object(), __() |
!isset($request) && $request | 40–54 | ->get_widget_object(), next_widget_id_number(), __() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && !is_callable() | 61–78 | ->get_widget_object(), is_callable(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && !is_callable() | 62–78 | wp_parse_widget_id(), ->get_widget_object(), is_callable(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && !hash_equals() | 64–66 | ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), __() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && !is_callable() | 65–82 | ->get_widget_object(), next_widget_id_number(), is_callable(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && !hash_equals() | 65–66 | wp_parse_widget_id(), ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), __() |
$request && isset($wp_registered_widget_updates[$id_base]) && !hash_equals() | 68–70 | ->get_widget_object(), next_widget_id_number(), base64_decode(), wp_hash(), hash_equals(), __() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && !is_callable() | 69–86 | ->get_widget_object(), is_callable(), ->_set(), ->_register_one(), do_action() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && is_callable() | 69–86 | ->get_widget_object(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), do_action() |
19 further outcomes, up to 118 instructions
isset($request) && isset($wp_registered_widget_updates[$id_base]) && !is_callable() | 70–86 | wp_parse_widget_id(), ->get_widget_object(), is_callable(), ->_set(), ->_register_one(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && is_callable() | 70–86 | wp_parse_widget_id(), ->get_widget_object(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), do_action() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && !is_callable() | 73–90 | ->get_widget_object(), next_widget_id_number(), is_callable(), ->_set(), ->_register_one(), do_action() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && is_callable() | 73–90 | ->get_widget_object(), next_widget_id_number(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), do_action() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && is_callable() | 77–94 | ->get_widget_object(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->_set(), ->_register_one(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && is_callable() | 78–94 | wp_parse_widget_id(), ->get_widget_object(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->_set(), ->_register_one(), do_action() |
!isset($request) && $request && isset($wp_registered_widget_updates[$id_base]) && is_callable() | 81–98 | ->get_widget_object(), next_widget_id_number(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->_set(), ->_register_one(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && !is_callable() | 95–98 | ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && !is_callable() | 96–98 | wp_parse_widget_id(), ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && !is_callable() | 99–102 | ->get_widget_object(), next_widget_id_number(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && !is_callable() | 103–106 | ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ->_set(), ->_register_one(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && is_callable() | 103–106 | ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && !is_callable() | 104–106 | wp_parse_widget_id(), ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ->_set(), ->_register_one(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && is_callable() | 104–106 | wp_parse_widget_id(), ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && !is_callable() | 107–110 | ->get_widget_object(), next_widget_id_number(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ->_set(), ->_register_one(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && is_callable() | 107–110 | ->get_widget_object(), next_widget_id_number(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && is_callable() | 111–114 | ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->_set(), ->_register_one(), do_action() |
isset($request) && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && is_callable() | 112–114 | wp_parse_widget_id(), ->get_widget_object(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->_set(), ->_register_one(), do_action() |
$request && isset($wp_registered_widget_updates[$id_base]) && hash_equals() && is_callable() | 115–118 | ->get_widget_object(), next_widget_id_number(), base64_decode(), wp_hash(), hash_equals(), unserialize(), is_callable(), ob_start(), call_user_func_array(), ob_end_clean(), ->_set(), ->_register_one(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 216 | 20–117 | 18 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 217 | 20–118 | 18 | |
| 8.4 | 217 | 20–118 | 18 | |
| 8.3 | 217 | 20–118 | 18 | |
| 8.2 | 217 | 20–118 | 18 | |
| 8.1 | 217 | 20–118 | 18 | |
| 7.4 | 217 | 20–118 | 18 |
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_Widgets_Controller::save_widget() runs, in this order:
- do_action( rest_after_save_widget )actionline 654 (+128 into the body)
Fires after a widget is created or updated via the REST API.
Uses · 7
- wp_parse_widget_id()Converts a widget ID into its id_base and number components.
- next_widget_id_number()
- __()Retrieves the translation of $text.
- wp_hash()Gets the hash of the given string.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
Used by · 2
- WP_REST_Widgets_Controller::create_item()Creates a widget.
- WP_REST_Widgets_Controller::update_item()Updates an existing widget.
Source code
protected function save_widget( $request, $sidebar_id ) { global $wp_widget_factory, $wp_registered_widget_updates; require_once ABSPATH . 'wp-admin/includes/widgets.php'; // For next_widget_id_number(). if ( isset( $request['id'] ) ) { // Saving an existing widget. $id = $request['id']; $parsed_id = wp_parse_widget_id( $id ); $id_base = $parsed_id['id_base']; $number = $parsed_id['number'] ?? null; $widget_object = $wp_widget_factory->get_widget_object( $id_base ); $creating = false; } elseif ( $request['id_base'] ) { // Saving a new widget. $id_base = $request['id_base']; $widget_object = $wp_widget_factory->get_widget_object( $id_base ); $number = $widget_object ? next_widget_id_number( $id_base ) : null; $id = $widget_object ? $id_base . '-' . $number : $id_base; $creating = true; } else { return new WP_Error( 'rest_invalid_widget', __( 'Widget type (id_base) is required.' ), array( 'status' => 400 ) ); } if ( ! isset( $wp_registered_widget_updates[ $id_base ] ) ) { return new WP_Error( 'rest_invalid_widget', __( 'The provided widget type (id_base) cannot be updated.' ), array( 'status' => 400 ) ); } if ( isset( $request['instance'] ) ) { if ( ! $widget_object ) { return new WP_Error( 'rest_invalid_widget', __( 'Cannot set instance on a widget that does not extend WP_Widget.' ), array( 'status' => 400 ) ); } if ( isset( $request['instance']['raw'] ) ) { if ( empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { return new WP_Error( 'rest_invalid_widget', __( 'Widget type does not support raw instances.' ), array( 'status' => 400 ) ); } $instance = $request['instance']['raw']; } elseif ( isset( $request['instance']['encoded'], $request['instance']['hash'] ) ) { $serialized_instance = base64_decode( $request['instance']['encoded'] ); if ( ! hash_equals( wp_hash( $serialized_instance ), $request['instance']['hash'] ) ) { return new WP_Error( 'rest_invalid_widget', __( 'The provided instance is malformed.' ), array( 'status' => 400 ) ); } $instance = unserialize( $serialized_instance ); } else { return new WP_Error( 'rest_invalid_widget', __( 'The provided instance is invalid. Must contain raw OR encoded and hash.' ), array( 'status' => 400 ) ); } $form_data = array( "widget-$id_base" => array( $number => $instance, ), 'sidebar' => $sidebar_id, ); } elseif ( isset( $request['form_data'] ) ) { $form_data = $request['form_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 7.0.4 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.