wppaste
WordPress

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
Saves the widget in the request object.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
20–118

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 217.

Plugin surface
1 hook

Third-party callbacks on 'rest_after_save_widget' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • serializeserialisationunserialize()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.

WhenInstructionsCalls it makes
!isset($request) && !$request20__()
!isset($request) && $request36–50->get_widget_object(), __()
isset($request)37–50wp_parse_widget_id(), ->get_widget_object(), __()
!isset($request) && $request40–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–78wp_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–66wp_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–86wp_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–86wp_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–94wp_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–98wp_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–106wp_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–106wp_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–114wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev21620–117181 fewer instruction than PHP 8.5
8.521720–11818
8.421720–11818
8.321720–11818
8.221720–11818
8.121720–11818
7.421720–11818

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:

  1. 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

Used by · 2

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.