wppaste
WordPress

wp_map_sidebars_widgets( array $existing_sidebars_widgets ): array

Since
4.9.0, 4.9.2
Source
wp-includes/widgets.php:1393
Compares a list of sidebars with their widgets against an allowed list.

Compatibility

WordPress
since 4.9.2
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

$existing_sidebars_widgetsarray
List of sidebars and their widget instance IDs.

Return value

array
Mapped sidebars widgets.

Performance profile

How much work a call to wp_map_sidebars_widgets() 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
6–69

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_map_sidebars_widgets()

Further down the call graph this can also reach option, cache, serialize, 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 · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_map_sidebars_widgets() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–8none
is_array($existing_sidebars_widgets) && !empty($existing_sidebars_widgets)24–25key(), array_pop()
is_array($existing_sidebars_widgets) && !empty($existing_sidebars_widgets) && !is_array($old_sidebars_widgets)32–42array_keys(), get_theme_mod()
is_array($existing_sidebars_widgets) && !empty($existing_sidebars_widgets) && is_array($old_sidebars_widgets)53–69array_keys(), get_theme_mod(), array_filter(), _wp_remove_unregistered_widgets(), array_merge()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2216–6953
8.52216–6953
8.42216–69539 fewer instructions than PHP 8.3
8.32306–6953
8.22306–6953
8.12306–6953
7.42306–6953

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 · 4

Used by · 1

  • retrieve_widgets()Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar, and saves the widget settings. This has to run at least on each theme change.

Source code

function wp_map_sidebars_widgets( $existing_sidebars_widgets ) {	global $wp_registered_sidebars; 	$new_sidebars_widgets = array(		'wp_inactive_widgets' => array(),	); 	// Short-circuit if there are no sidebars to map.	if ( ! is_array( $existing_sidebars_widgets ) || empty( $existing_sidebars_widgets ) ) {		return $new_sidebars_widgets;	} 	foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {		if ( 'wp_inactive_widgets' === $sidebar || str_starts_with( $sidebar, 'orphaned_widgets' ) ) {			$new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets );			unset( $existing_sidebars_widgets[ $sidebar ] );		}	} 	// If old and new theme have just one sidebar, map it and we're done.	if ( 1 === count( $existing_sidebars_widgets ) && 1 === count( $wp_registered_sidebars ) ) {		$new_sidebars_widgets[ key( $wp_registered_sidebars ) ] = array_pop( $existing_sidebars_widgets ); 		return $new_sidebars_widgets;	} 	// Map locations with the same slug.	$existing_sidebars = array_keys( $existing_sidebars_widgets ); 	foreach ( $wp_registered_sidebars as $sidebar => $name ) {		if ( in_array( $sidebar, $existing_sidebars, true ) ) {			$new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ];			unset( $existing_sidebars_widgets[ $sidebar ] );		} elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) {			$new_sidebars_widgets[ $sidebar ] = array();		}	} 	// If there are more sidebars, try to map them.	if ( ! empty( $existing_sidebars_widgets ) ) { 		/*		 * If old and new theme both have sidebars that contain phrases		 * from within the same group, make an educated guess and map it.		 */		$common_slug_groups = array(			array( 'sidebar', 'primary', 'main', 'right' ),			array( 'second', 'left' ),			array( 'sidebar-2', 'footer', 'bottom' ),			array( 'header', 'top' ),		); 		// Go through each group...		foreach ( $common_slug_groups as $slug_group ) { 			// ...and see if any of these slugs...			foreach ( $slug_group as $slug ) { 				// ...and any of the new sidebars...				foreach ( $wp_registered_sidebars as $new_sidebar => $args ) { 					// ...actually match!					if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {						continue;					} 					// Then see if any of the existing sidebars...					foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) { 						// ...and any slug in the same group...						foreach ( $slug_group as $slug ) { 							// ... have a match as well.							if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {								continue;							} 							// Make sure this sidebar wasn't mapped and removed previously.							if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {

Changelog

Introduced in 4.9.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.

4.9.2
Always tries to restore widget assignments from previous data, not just if sidebars needed mapping.from the docblock
4.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/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.