* Move the old sidebars_widgets array upgrade code to wp-admin/includes/upgrade.php

* Avoid infinite loop with wp_get_sidebars_widgets()
* Remove two unneeded wp_get_sidebars_widgets() calls
* Remove unused $wp_registered_sidebars variable from wp_get_sidebars_widgets().
* Combine  a couple of !empty( $sidebars_widgets ) checks in retrieve_widgets()
Props SergeyBiryukov, lancewillett. fixes #17979 #18598


git-svn-id: http://svn.automattic.com/wordpress/trunk@18821 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2011-09-29 21:41:22 +00:00
parent f186494c9a
commit 2edc82cd61
3 changed files with 77 additions and 66 deletions

View File

@ -451,6 +451,9 @@ function upgrade_all() {
if ( $wp_current_db_version < 15260 ) if ( $wp_current_db_version < 15260 )
upgrade_300(); upgrade_300();
if ( $wp_current_db_version < 11548 )
upgrade_old_widgets_order_array();
maybe_disable_automattic_widgets(); maybe_disable_automattic_widgets();
update_option( 'db_version', $wp_db_version ); update_option( 'db_version', $wp_db_version );
@ -1131,6 +1134,68 @@ function upgrade_300() {
} }
/**
* Convert the old style widgets order array from 2.2.
*
* @since 3.3.0
*/
function upgrade_old_widgets_order_array() {
global $wp_registered_widgets, $sidebars_widgets;
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
$_sidebars_widgets = array();
if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) )
$sidebars_widgets['array_version'] = 3;
elseif ( !isset($sidebars_widgets['array_version']) )
$sidebars_widgets['array_version'] = 1;
switch ( $sidebars_widgets['array_version'] ) {
case 1 :
foreach ( (array) $sidebars_widgets as $index => $sidebar )
if ( is_array($sidebar) )
foreach ( (array) $sidebar as $i => $name ) {
$id = strtolower($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$id = sanitize_title($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$found = false;
foreach ( $wp_registered_widgets as $widget_id => $widget ) {
if ( strtolower($widget['name']) == strtolower($name) ) {
$_sidebars_widgets[$index][$i] = $widget['id'];
$found = true;
break;
} elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
$_sidebars_widgets[$index][$i] = $widget['id'];
$found = true;
break;
}
}
if ( $found )
continue;
unset($_sidebars_widgets[$index][$i]);
}
$_sidebars_widgets['array_version'] = 2;
$sidebars_widgets = $_sidebars_widgets;
unset($_sidebars_widgets);
case 2 :
$sidebars_widgets = retrieve_widgets();
$sidebars_widgets['array_version'] = 3;
update_option( 'sidebars_widgets', $sidebars_widgets );
}
}
/** /**
* Execute network level changes * Execute network level changes
* *

View File

@ -1248,9 +1248,8 @@ function preview_theme_ob_filter_callback( $matches ) {
function switch_theme($template, $stylesheet) { function switch_theme($template, $stylesheet) {
global $wp_theme_directories, $sidebars_widgets; global $wp_theme_directories, $sidebars_widgets;
if ( ! is_array( $sidebars_widgets ) ) if ( is_array( $sidebars_widgets ) )
$sidebars_widgets = wp_get_sidebars_widgets(); set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
$old_theme = get_current_theme(); $old_theme = get_current_theme();

View File

@ -998,7 +998,7 @@ function wp_get_sidebars_widgets($deprecated = true) {
if ( $deprecated !== true ) if ( $deprecated !== true )
_deprecated_argument( __FUNCTION__, '2.8.1' ); _deprecated_argument( __FUNCTION__, '2.8.1' );
global $wp_registered_widgets, $wp_registered_sidebars, $_wp_sidebars_widgets; global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets;
// If loading from front page, consult $_wp_sidebars_widgets rather than options // If loading from front page, consult $_wp_sidebars_widgets rather than options
// to see if wp_convert_widget_settings() has made manipulations in memory. // to see if wp_convert_widget_settings() has made manipulations in memory.
@ -1009,55 +1009,6 @@ function wp_get_sidebars_widgets($deprecated = true) {
$sidebars_widgets = $_wp_sidebars_widgets; $sidebars_widgets = $_wp_sidebars_widgets;
} else { } else {
$sidebars_widgets = get_option('sidebars_widgets', array()); $sidebars_widgets = get_option('sidebars_widgets', array());
$_sidebars_widgets = array();
if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) )
$sidebars_widgets['array_version'] = 3;
elseif ( !isset($sidebars_widgets['array_version']) )
$sidebars_widgets['array_version'] = 1;
switch ( $sidebars_widgets['array_version'] ) {
case 1 :
foreach ( (array) $sidebars_widgets as $index => $sidebar )
if ( is_array($sidebar) )
foreach ( (array) $sidebar as $i => $name ) {
$id = strtolower($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$id = sanitize_title($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$found = false;
foreach ( $wp_registered_widgets as $widget_id => $widget ) {
if ( strtolower($widget['name']) == strtolower($name) ) {
$_sidebars_widgets[$index][$i] = $widget['id'];
$found = true;
break;
} elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
$_sidebars_widgets[$index][$i] = $widget['id'];
$found = true;
break;
}
}
if ( $found )
continue;
unset($_sidebars_widgets[$index][$i]);
}
$_sidebars_widgets['array_version'] = 2;
$sidebars_widgets = $_sidebars_widgets;
unset($_sidebars_widgets);
case 2 :
$sidebars_widgets = retrieve_widgets();
}
} }
if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) ) if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) )
@ -1219,8 +1170,8 @@ function retrieve_widgets() {
$_sidebars_widgets = $old_sidebars_widgets['data']; $_sidebars_widgets = $old_sidebars_widgets['data'];
remove_theme_mod( 'sidebars_widgets' ); remove_theme_mod( 'sidebars_widgets' );
} else { } else {
if ( ! is_array( $sidebars_widgets ) ) if ( empty( $sidebars_widgets ) )
$sidebars_widgets = wp_get_sidebars_widgets(); return;
$sidebars = array_keys($wp_registered_sidebars); $sidebars = array_keys($wp_registered_sidebars);
@ -1234,23 +1185,19 @@ function retrieve_widgets() {
return; return;
$_sidebars_widgets = array( $_sidebars_widgets = array(
'wp_inactive_widgets' => $sidebars_widgets['wp_inactive_widgets'] 'wp_inactive_widgets' => !empty( $sidebars_widgets['wp_inactive_widgets'] ) ? $sidebars_widgets['wp_inactive_widgets'] : array()
); );
unset( $sidebars_widgets['wp_inactive_widgets'] ); unset( $sidebars_widgets['wp_inactive_widgets'] );
foreach ( $wp_registered_sidebars as $id => $settings ) { foreach ( $wp_registered_sidebars as $id => $settings )
if ( ! empty( $sidebars_widgets ) ) $_sidebars_widgets[$id] = array_shift( $sidebars_widgets );
$_sidebars_widgets[$id] = array_shift( $sidebars_widgets );
}
if ( !empty($sidebars_widgets) ) { $orphaned = 0;
$orphaned = 0;
foreach ( $sidebars_widgets as $val ) { foreach ( $sidebars_widgets as $val ) {
if ( is_array($val) && ! empty( $val ) ) if ( is_array($val) && ! empty( $val ) )
$_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val; $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val;
}
} }
} }