retrieve_widgets

Definition:
function retrieve_widgets() {}

Source code

function retrieve_widgets() {

	global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;



	$_sidebars_widgets = array();

	$sidebars = array_keys($wp_registered_sidebars);



	unset( $sidebars_widgets['array_version'] );



	$old = array_keys($sidebars_widgets);

	sort($old);

	sort($sidebars);



	if ( $old == $sidebars )

		return;



	// Move the known-good ones first

	foreach ( $sidebars as $id ) {

		if ( array_key_exists( $id, $sidebars_widgets ) ) {

			$_sidebars_widgets[$id] = $sidebars_widgets[$id];

			unset($sidebars_widgets[$id], $sidebars[$id]);

		}

	}



	// if new theme has less sidebars than the old theme

	if ( !empty($sidebars_widgets) ) {

		foreach ( $sidebars_widgets as $lost => $val ) {

			if ( is_array($val) )

				$_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );

		}

	}



	// discard invalid, theme-specific widgets from sidebars

	$shown_widgets = array();

	foreach ( $_sidebars_widgets as $sidebar => $widgets ) {

		if ( !is_array($widgets) )

			continue;



		$_widgets = array();

		foreach ( $widgets as $widget ) {

			if ( isset($wp_registered_widgets[$widget]) )

				$_widgets[] = $widget;

		}

		$_sidebars_widgets[$sidebar] = $_widgets;

		$shown_widgets = array_merge($shown_widgets, $_widgets);

	}



	$sidebars_widgets = $_sidebars_widgets;

	unset($_sidebars_widgets, $_widgets);



	// find hidden/lost multi-widget instances

	$lost_widgets = array();

	foreach ( $wp_registered_widgets as $key => $val ) {

		if ( in_array($key, $shown_widgets, true) )

			continue;



		$number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);



		if ( 2 > (int) $number )

			continue;



		$lost_widgets[] = $key;

	}



	$sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);

	wp_set_sidebars_widgets($sidebars_widgets);

}

2745

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: