<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[18630] trunk: Avoid losing widgets when switching themes - take one, props aaroncampbell, see #17979</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/18630">18630</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2011-09-02 22:13:55 +0000 (Fri, 02 Sep 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Avoid losing widgets when switching themes - take one, props aaroncampbell, see <a href="http://core.trac.wordpress.org/ticket/17979">#17979</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminwidgetsphp">trunk/wp-admin/widgets.php</a></li>
<li><a href="#trunkwpincludesdefaultfiltersphp">trunk/wp-includes/default-filters.php</a></li>
<li><a href="#trunkwpincludesthemephp">trunk/wp-includes/theme.php</a></li>
<li><a href="#trunkwpincludeswidgetsphp">trunk/wp-includes/widgets.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminwidgetsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/widgets.php (18629 => 18630)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/widgets.php        2011-09-02 21:25:42 UTC (rev 18629)
+++ trunk/wp-admin/widgets.php        2011-09-02 22:13:55 UTC (rev 18630)
</span><span class="lines">@@ -44,10 +44,17 @@
</span><span class="cx"> $help .= '&lt;p&gt;' . __('&lt;a href=&quot;http://wordpress.org/support/&quot; target=&quot;_blank&quot;&gt;Support Forums&lt;/a&gt;') . '&lt;/p&gt;';
</span><span class="cx"> add_contextual_help($current_screen, $help);
</span><span class="cx"> 
</span><ins>+// These are the widgets grouped by sidebar
+$sidebars_widgets = wp_get_sidebars_widgets();
+
+if ( empty( $sidebars_widgets ) )
+        $sidebars_widgets = wp_get_widget_defaults();
+
</ins><span class="cx"> // register the inactive_widgets area as sidebar
</span><span class="cx"> register_sidebar(array(
</span><span class="cx">         'name' =&gt; __('Inactive Widgets'),
</span><span class="cx">         'id' =&gt; 'wp_inactive_widgets',
</span><ins>+        'class' =&gt; 'inactive',
</ins><span class="cx">         'description' =&gt; '',
</span><span class="cx">         'before_widget' =&gt; '',
</span><span class="cx">         'after_widget' =&gt; '',
</span><span class="lines">@@ -55,78 +62,22 @@
</span><span class="cx">         'after_title' =&gt; '',
</span><span class="cx"> ));
</span><span class="cx"> 
</span><del>-// These are the widgets grouped by sidebar
-$sidebars_widgets = wp_get_sidebars_widgets();
-if ( empty( $sidebars_widgets ) )
-        $sidebars_widgets = wp_get_widget_defaults();
-
-// look for &quot;lost&quot; widgets, this has to run at least on each theme change
-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]);
-                }
</del><ins>+foreach ( $sidebars_widgets as $sidebar_id =&gt; $widgets ) {
+        if ( empty( $wp_registered_sidebars[ $sidebar_id ] ) &amp;&amp; ! empty( $widgets ) ) {
+                // register the inactive_widgets area as sidebar
+                register_sidebar(array(
+                        'name' =&gt; __( 'Inactive Widgets (Previous Theme)' ),
+                        'id' =&gt; $sidebar_id,
+                        'class' =&gt; 'orphaned',
+                        'description' =&gt; __( 'This is a left over sidebar from an old theme and does not show anywhere on your site' ),
+                        'before_widget' =&gt; '',
+                        'after_widget' =&gt; '',
+                        'before_title' =&gt; '',
+                        'after_title' =&gt; '',
+                ));
</ins><span class="cx">         }
</span><del>-
-        // if new theme has less sidebars than the old theme
-        if ( !empty($sidebars_widgets) ) {
-                foreach ( $sidebars_widgets as $lost =&gt; $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 =&gt; $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 =&gt; $val ) {
-                if ( in_array($key, $shown_widgets, true) )
-                        continue;
-
-                $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
-
-                if ( 2 &gt; (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);
</del><span class="cx"> }
</span><ins>+
</ins><span class="cx"> retrieve_widgets();
</span><span class="cx"> 
</span><span class="cx"> if ( count($wp_registered_sidebars) == 1 ) {
</span><span class="lines">@@ -387,8 +338,15 @@
</span><span class="cx"> foreach ( $wp_registered_sidebars as $sidebar =&gt; $registered_sidebar ) {
</span><span class="cx">         if ( 'wp_inactive_widgets' == $sidebar )
</span><span class="cx">                 continue;
</span><del>-        $closed = $i ? ' closed' : ''; ?&gt;
-        &lt;div class=&quot;widgets-holder-wrap&lt;?php echo $closed; ?&gt;&quot;&gt;
</del><ins>+
+        $wrap_class = 'widgets-holder-wrap';
+        if ( !empty( $registered_sidebar['class'] ) )
+                $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
+
+        if ( $i )
+                $wrap_class .= ' closed'; ?&gt;
+
+        &lt;div class=&quot;&lt;?php esc_attr_e( $wrap_class ); ?&gt;&quot;&gt;
</ins><span class="cx">         &lt;div class=&quot;sidebar-name&quot;&gt;
</span><span class="cx">         &lt;div class=&quot;sidebar-name-arrow&quot;&gt;&lt;br /&gt;&lt;/div&gt;
</span><span class="cx">         &lt;h3&gt;&lt;?php echo esc_html( $registered_sidebar['name'] ); ?&gt;
</span></span></pre></div>
<a id="trunkwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-filters.php (18629 => 18630)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-filters.php        2011-09-02 21:25:42 UTC (rev 18629)
+++ trunk/wp-includes/default-filters.php        2011-09-02 22:13:55 UTC (rev 18630)
</span><span class="lines">@@ -218,6 +218,7 @@
</span><span class="cx"> add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
</span><span class="cx"> add_action( 'template_redirect',   'wp_shortlink_header',             11, 0 );
</span><span class="cx"> add_action( 'wp_print_footer_scripts', '_wp_footer_scripts'                 );
</span><ins>+add_action( 'init',                'check_theme_switched',            99    );
</ins><span class="cx"> 
</span><span class="cx"> if ( isset( $_GET['replytocom'] ) )
</span><span class="cx">     add_filter( 'pre_option_blog_public', '__return_zero' );
</span></span></pre></div>
<a id="trunkwpincludesthemephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/theme.php (18629 => 18630)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/theme.php        2011-09-02 21:25:42 UTC (rev 18629)
+++ trunk/wp-includes/theme.php        2011-09-02 22:13:55 UTC (rev 18630)
</span><span class="lines">@@ -1248,19 +1248,26 @@
</span><span class="cx"> function switch_theme($template, $stylesheet) {
</span><span class="cx">         global $wp_theme_directories;
</span><span class="cx"> 
</span><ins>+        $old_theme = get_current_theme();
+
</ins><span class="cx">         update_option('template', $template);
</span><span class="cx">         update_option('stylesheet', $stylesheet);
</span><ins>+
</ins><span class="cx">         if ( count($wp_theme_directories) &gt; 1 ) {
</span><span class="cx">                 update_option('template_root', get_raw_theme_root($template, true));
</span><span class="cx">                 update_option('stylesheet_root', get_raw_theme_root($stylesheet, true));
</span><span class="cx">         }
</span><ins>+
</ins><span class="cx">         delete_option('current_theme');
</span><span class="cx">         $theme = get_current_theme();
</span><ins>+
</ins><span class="cx">         if ( is_admin() &amp;&amp; false === get_option( &quot;theme_mods_$stylesheet&quot; ) ) {
</span><span class="cx">                 $default_theme_mods = (array) get_option( &quot;mods_$theme&quot; );
</span><span class="cx">                 add_option( &quot;theme_mods_$stylesheet&quot;, $default_theme_mods );
</span><span class="cx">         }
</span><del>-        do_action('switch_theme', $theme);
</del><ins>+
+        update_option( 'theme_switched', $old_theme );
+        do_action( 'switch_theme', $theme );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludeswidgetsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/widgets.php (18629 => 18630)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/widgets.php        2011-09-02 21:25:42 UTC (rev 18629)
+++ trunk/wp-includes/widgets.php        2011-09-02 22:13:55 UTC (rev 18630)
</span><span class="lines">@@ -547,6 +547,7 @@
</span><span class="cx">                 'name' =&gt; sprintf(__('Sidebar %d'), $i ),
</span><span class="cx">                 'id' =&gt; &quot;sidebar-$i&quot;,
</span><span class="cx">                 'description' =&gt; '',
</span><ins>+                'class' =&gt; '',
</ins><span class="cx">                 'before_widget' =&gt; '&lt;li id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;',
</span><span class="cx">                 'after_widget' =&gt; &quot;&lt;/li&gt;\n&quot;,
</span><span class="cx">                 'before_title' =&gt; '&lt;h2 class=&quot;widgettitle&quot;&gt;',
</span><span class="lines">@@ -1055,30 +1056,7 @@
</span><span class="cx">                                 unset($_sidebars_widgets);
</span><span class="cx"> 
</span><span class="cx">                         case 2 :
</span><del>-                                $sidebars = array_keys( $wp_registered_sidebars );
-                                if ( !empty( $sidebars ) ) {
-                                        // Move the known-good ones first
-                                        foreach ( (array) $sidebars as $id ) {
-                                                if ( array_key_exists( $id, $sidebars_widgets ) ) {
-                                                        $_sidebars_widgets[$id] = $sidebars_widgets[$id];
-                                                        unset($sidebars_widgets[$id], $sidebars[$id]);
-                                                }
-                                        }
-
-                                        // move the rest to wp_inactive_widgets
-                                        if ( !isset($_sidebars_widgets['wp_inactive_widgets']) )
-                                                $_sidebars_widgets['wp_inactive_widgets'] = array();
-
-                                        if ( !empty($sidebars_widgets) ) {
-                                                foreach ( $sidebars_widgets as $lost =&gt; $val ) {
-                                                        if ( is_array($val) )
-                                                                $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
-                                                }
-                                        }
-
-                                        $sidebars_widgets = $_sidebars_widgets;
-                                        unset($_sidebars_widgets);
-                                }
</del><ins>+                                $sidebars_widgets = retrieve_widgets();
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -1215,3 +1193,102 @@
</span><span class="cx"> function _get_widget_id_base($id) {
</span><span class="cx">         return preg_replace( '/-[0-9]+$/', '', $id );
</span><span class="cx"> }
</span><ins>+
+function check_theme_switched() {
+        if ( false !== ( $old_theme = get_option( 'theme_switched' ) ) &amp;&amp; !empty( $old_theme ) ) {
+                global $sidebars_widgets;
+
+                if ( ! is_array( $sidebars_widgets ) )
+                        $sidebars_widgets = wp_get_sidebars_widgets();
+
+                $key = md5( $old_theme );
+                // Store widgets for 1 week so we can restore if needed
+                set_transient( 'old_widgets_' . $key, $sidebars_widgets, 604800 );
+
+                retrieve_widgets();
+                update_option( 'theme_switched', false );
+        }
+}
+
+// look for &quot;lost&quot; widgets, this has to run at least on each theme change
+function retrieve_widgets() {
+        global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
+
+        $key = md5( get_current_theme() );
+        if ( false !== ( $_sidebars_widgets = get_transient( &quot;old_widgets_{$key}&quot; ) ) ) {
+                delete_transient( &quot;old_widgets_{$key}&quot; );
+        } else {
+                if ( ! is_array( $sidebars_widgets ) )
+                        $sidebars_widgets = wp_get_sidebars_widgets();
+
+                $sidebars = array_keys($wp_registered_sidebars);
+
+                unset( $sidebars_widgets['array_version'] );
+
+                $old = array_keys($sidebars_widgets);
+                sort($old);
+                sort($sidebars);
+
+                if ( $old == $sidebars )
+                        return;
+
+                $_sidebars_widgets = array(
+                        'wp_inactive_widgets' =&gt; $sidebars_widgets['wp_inactive_widgets']
+                );
+
+                unset( $sidebars_widgets['wp_inactive_widgets'] );
+
+                foreach ( $wp_registered_sidebars as $id =&gt; $settings ) {
+                        if ( ! empty( $sidebars_widgets ) )
+                                $_sidebars_widgets[$id] = array_shift( $sidebars_widgets );
+                }
+
+                if ( !empty($sidebars_widgets) ) {
+                        $orphaned = 0;
+
+                        foreach ( $sidebars_widgets as $val ) {
+                                if ( is_array($val) &amp;&amp; ! empty( $val ) )
+                                        $_sidebars_widgets['orphaned_widgets_' . ++$orphaned] = $val;
+                        }
+                }
+        }
+
+        // discard invalid, theme-specific widgets from sidebars
+        $shown_widgets = array();
+
+        foreach ( $_sidebars_widgets as $sidebar =&gt; $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 =&gt; $val ) {
+                if ( in_array($key, $shown_widgets, true) )
+                        continue;
+
+                $number = preg_replace('/.+?-([0-9]+)$/', '$1', $key);
+
+                if ( 2 &gt; (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);
+
+        return $sidebars_widgets;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>