Customizer: Properly handle widget settings when activating a previewed theme.

props westonruter, ocean90, gcorne.
fixes #27767.

Built from https://develop.svn.wordpress.org/trunk@28124


git-svn-id: http://core.svn.wordpress.org/trunk@27955 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin
2014-04-14 22:46:16 +00:00
parent f911d99902
commit 6b579e0d59
6 changed files with 137 additions and 13 deletions

View File

@@ -752,10 +752,18 @@ function preview_theme_ob_filter_callback( $matches ) {
* @param string $stylesheet Stylesheet name
*/
function switch_theme( $stylesheet ) {
global $wp_theme_directories, $sidebars_widgets;
global $wp_theme_directories, $wp_customize, $sidebars_widgets;
if ( is_array( $sidebars_widgets ) )
set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
$_sidebars_widgets = null;
if ( 'wp_ajax_customize_save' === current_action() ) {
$_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
} elseif ( is_array( $sidebars_widgets ) ) {
$_sidebars_widgets = $sidebars_widgets;
}
if ( is_array( $_sidebars_widgets ) ) {
set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
}
$old_theme = wp_get_theme();
$new_theme = wp_get_theme( $stylesheet );
@@ -782,9 +790,19 @@ function switch_theme( $stylesheet ) {
update_option( 'current_theme', $new_name );
// Migrate from the old mods_{name} option to theme_mods_{slug}.
if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
$default_theme_mods = (array) get_option( 'mods_' . $new_name );
add_option( "theme_mods_$stylesheet", $default_theme_mods );
} else {
/*
* Since retrieve_widgets() is called when initializing the customizer theme,
* we need to to remove the theme mods to avoid overwriting changes made via
* the widget customizer when accessing wp-admin/widgets.php.
*/
if ( 'wp_ajax_customize_save' === current_action() ) {
remove_theme_mod( 'sidebars_widgets' );
}
}
update_option( 'theme_switched', $old_theme->get_stylesheet() );
@@ -1791,6 +1809,12 @@ function check_theme_switched() {
if ( $stylesheet = get_option( 'theme_switched' ) ) {
$old_theme = wp_get_theme( $stylesheet );
// Prevent retrieve_widgets() from running since Customizer already called it up front
if ( get_option( 'theme_switched_via_customizer' ) ) {
remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
update_option( 'theme_switched_via_customizer', false );
}
if ( $old_theme->exists() ) {
/**
* Fires on the first WP load after a theme switch if the old theme still exists.
@@ -1918,4 +1942,4 @@ function wp_customize_support_script() {
}());
</script>
<?php
}
}