WP_Customize_Widgets::sanitize_widget_js_instance( array $value, string $id_base = null ): array
- Since
- 3.9.0, 5.8.0
- Source
wp-includes/class-wp-customize-widgets.php:1518
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
$valuearray- Widget instance to convert to JSON.
$id_basestringoptional- Base of the ID of the widget being sanitized. Default null.Default:
null
Return value
array- JSON-converted widget instance.
Performance profile
How much work a call to WP_Customize_Widgets::sanitize_widget_js_instance() 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
- Trivial
- Scaling
- Constant
- Instructions
- 6–39
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
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 41.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- serializeserialisation
serialize()called directly - hookthird-party callbacks
apply_filters()one call below WP_Customize_Widgets::sanitize_widget_js_instance()
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_Customize_Widgets::sanitize_widget_js_instance() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($value) | 6 | none |
empty($value) | 25–26 | serialize(), base64_encode(), ->get_instance_hash_key() |
empty($value) && !wp_use_widgets_block_editor() | 28–29 | serialize(), base64_encode(), ->get_instance_hash_key(), wp_use_widgets_block_editor() |
empty($value) && wp_use_widgets_block_editor() | 35–39 | serialize(), base64_encode(), ->get_instance_hash_key(), wp_use_widgets_block_editor(), ->get_widget_object() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 41 | 6–39 | 5 | |
| 8.5 | 41 | 6–39 | 5 | |
| 8.4 | 41 | 6–39 | 5 | |
| 8.3 | 41 | 6–39 | 5 | |
| 8.2 | 41 | 6–39 | 5 | |
| 8.1 | 41 | 6–39 | 5 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 42 | 6–39 | 5 |
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 · 2
- wp_use_widgets_block_editor()Determines whether or not to use the block editor to manage widgets.
- WP_Customize_Widgets::get_instance_hash_key()Retrieves MAC for a serialized widget instance string.
Used by · 3
- WP_Customize_Widgets::call_widget_update()Finds and invokes the widget update and control callbacks.
- WP_Customize_Widgets::get_setting_args()Retrieves common arguments to supply when constructing a Customizer setting.
- WP_Customize_Widgets::wp_ajax_update_widget()Updates widget settings asynchronously.
Source code
public function sanitize_widget_js_instance( $value, $id_base = null ) { global $wp_widget_factory; if ( empty( $value['is_widget_customizer_js_value'] ) ) { $serialized = serialize( $value ); $js_value = array( 'encoded_serialized_instance' => base64_encode( $serialized ), 'title' => empty( $value['title'] ) ? '' : $value['title'], 'is_widget_customizer_js_value' => true, 'instance_hash_key' => $this->get_instance_hash_key( $serialized ), ); if ( $id_base && wp_use_widgets_block_editor() ) { $widget_object = $wp_widget_factory->get_widget_object( $id_base ); if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { $js_value['raw_instance'] = (object) $value; } } return $js_value; } return $value; }Changelog
Introduced in 3.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$id_base parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/class-wp-customize-widgets.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.