<!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>[28124] trunk/src: Customizer: Properly handle widget settings when activating a previewed theme.</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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/28124">28124</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-04-14 22:45:40 +0000 (Mon, 14 Apr 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Customizer: Properly handle widget settings when activating a previewed theme.
props westonruter, ocean90, gcorne.
fixes <a href="http://core.trac.wordpress.org/ticket/27767">#27767</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizewidgetsjs">trunk/src/wp-admin/js/customize-widgets.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizewidgetsphp">trunk/src/wp-includes/class-wp-customize-widgets.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
<li><a href="#trunksrcwpincludeswidgetsphp">trunk/src/wp-includes/widgets.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizewidgetsjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/customize-widgets.js (28123 => 28124)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/customize-widgets.js 2014-04-14 22:05:20 UTC (rev 28123)
+++ trunk/src/wp-admin/js/customize-widgets.js 2014-04-14 22:45:40 UTC (rev 28124)
</span><span class="lines">@@ -942,6 +942,7 @@
</span><span class="cx"> params.action = 'update-widget';
</span><span class="cx"> params.wp_customize = 'on';
</span><span class="cx"> params.nonce = api.Widgets.data.nonce;
</span><ins>+ params.theme = api.settings.theme.stylesheet;
</ins><span class="cx">
</span><span class="cx"> data = $.param( params );
</span><span class="cx"> $inputs = this._getInputs( $widgetContent );
</span><span class="lines">@@ -1614,7 +1615,7 @@
</span><span class="cx"> widgetNumber = widget.get( 'multi_number' );
</span><span class="cx"> }
</span><span class="cx">
</span><del>- controlHtml = $( '#widget-tpl-' + widget.get( 'id' ) ).html();
</del><ins>+ controlHtml = $.trim( $( '#widget-tpl-' + widget.get( 'id' ) ).html() );
</ins><span class="cx"> if ( widget.get( 'is_multi' ) ) {
</span><span class="cx"> controlHtml = controlHtml.replace( /<[^<>]+>/g, function( m ) {
</span><span class="cx"> return m.replace( /__i__|%i%/g, widgetNumber );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/class-wp-customize-manager.php (28123 => 28124)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/class-wp-customize-manager.php 2014-04-14 22:05:20 UTC (rev 28123)
+++ trunk/src/wp-includes/class-wp-customize-manager.php 2014-04-14 22:45:40 UTC (rev 28124)
</span><span class="lines">@@ -575,6 +575,7 @@
</span><span class="cx"> // to operate properly.
</span><span class="cx"> $this->stop_previewing_theme();
</span><span class="cx"> switch_theme( $this->get_stylesheet() );
</span><ins>+ update_option( 'theme_switched_via_customizer', true );
</ins><span class="cx"> $this->start_previewing_theme();
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizewidgetsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/class-wp-customize-widgets.php (28123 => 28124)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/class-wp-customize-widgets.php 2014-04-14 22:05:20 UTC (rev 28123)
+++ trunk/src/wp-includes/class-wp-customize-widgets.php 2014-04-14 22:45:40 UTC (rev 28124)
</span><span class="lines">@@ -61,6 +61,13 @@
</span><span class="cx"> protected $rendered_widgets = array();
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * @since 3.9.0
+ * @access protected
+ * @var array
+ */
+ protected $old_sidebars_widgets = array();
+
+ /**
</ins><span class="cx"> * Initial loader.
</span><span class="cx"> *
</span><span class="cx"> * @since 3.9.0
</span><span class="lines">@@ -72,6 +79,7 @@
</span><span class="cx"> $this->manager = $manager;
</span><span class="cx">
</span><span class="cx"> add_action( 'after_setup_theme', array( $this, 'setup_widget_addition_previews' ) );
</span><ins>+ add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) );
</ins><span class="cx"> add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) );
</span><span class="cx"> add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 );
</span><span class="cx"> add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
</span><span class="lines">@@ -115,7 +123,6 @@
</span><span class="cx"> * @since 3.9.0
</span><span class="cx"> *
</span><span class="cx"> * @access public
</span><del>- * @global WP_Customize_Manager $wp_customize Customizer instance.
</del><span class="cx"> */
</span><span class="cx"> public function setup_widget_addition_previews() {
</span><span class="cx"> $is_customize_preview = false;
</span><span class="lines">@@ -125,12 +132,12 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $is_ajax_widget_update = false;
</span><del>- if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'update-widget' === $this->get_post_value( 'action' ) ) {
</del><ins>+ if ( $this->manager->doing_ajax() && 'update-widget' === $this->get_post_value( 'action' ) ) {
</ins><span class="cx"> $is_ajax_widget_update = check_ajax_referer( 'update-widget', 'nonce', false );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $is_ajax_customize_save = false;
</span><del>- if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'customize_save' === $this->get_post_value( 'action' ) ) {
</del><ins>+ if ( $this->manager->doing_ajax() && 'customize_save' === $this->get_post_value( 'action' ) ) {
</ins><span class="cx"> $is_ajax_customize_save = check_ajax_referer( 'save-customize_' . $this->manager->get_stylesheet(), 'nonce', false );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -279,6 +286,72 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Override sidebars_widgets for theme switch.
+ *
+ * When switching a theme via the customizer, supply any previously-configured
+ * sidebars_widgets from the target theme as the initial sidebars_widgets
+ * setting. Also store the old theme's existing settings so that they can
+ * be passed along for storing in the sidebars_widgets theme_mod when the
+ * theme gets switched.
+ *
+ * @since 3.9.0
+ * @access public
+ */
+ public function override_sidebars_widgets_for_theme_switch() {
+ global $sidebars_widgets;
+
+ if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) {
+ return;
+ }
+
+ $this->old_sidebars_widgets = wp_get_sidebars_widgets();
+ add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
+
+ // retrieve_widgets() looks at the global $sidebars_widgets
+ $sidebars_widgets = $this->old_sidebars_widgets;
+ $sidebars_widgets = retrieve_widgets( 'customize' );
+ add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
+ }
+
+ /**
+ * Filter old_sidebars_widgets_data customizer setting.
+ *
+ * When switching themes, filter the Customizer setting
+ * old_sidebars_widgets_data to supply initial $sidebars_widgets before they
+ * were overridden by retrieve_widgets(). The value for
+ * old_sidebars_widgets_data gets set in the old theme's sidebars_widgets
+ * theme_mod.
+ *
+ * @see WP_Customize_Widgets::handle_theme_switch()
+ * @since 3.9.0
+ * @access public
+ *
+ * @param array $sidebars_widgets
+ */
+ public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) {
+ return $this->old_sidebars_widgets;
+ }
+
+ /**
+ * Filter sidebars_widgets option for theme switch.
+ *
+ * When switching themes, the retrieve_widgets() function is run when the
+ * Customizer initializes, and then the new sidebars_widgets here get
+ * supplied as the default value for the sidebars_widgets option.
+ *
+ * @see WP_Customize_Widgets::handle_theme_switch()
+ * @since 3.9.0
+ * @access public
+ *
+ * @param array $sidebars_widgets
+ */
+ public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) {
+ $sidebars_widgets = $GLOBALS['sidebars_widgets'];
+ $sidebars_widgets['array_version'] = 3;
+ return $sidebars_widgets;
+ }
+
+ /**
</ins><span class="cx"> * Make sure all widgets get loaded into the Customizer.
</span><span class="cx"> *
</span><span class="cx"> * Note: these actions are also fired in wp_ajax_update_widget().
</span><span class="lines">@@ -349,6 +422,18 @@
</span><span class="cx"> $new_setting_ids[] = $setting_id;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ /*
+ * Add a setting which will be supplied for the theme's sidebars_widgets
+ * theme_mod when the the theme is switched.
+ */
+ if ( ! $this->manager->is_theme_active() ) {
+ $setting_id = 'old_sidebars_widgets_data';
+ $setting_args = $this->get_setting_args( $setting_id, array(
+ 'type' => 'global_variable',
+ ) );
+ $this->manager->add_setting( $setting_id, $setting_args );
+ }
+
</ins><span class="cx"> foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) {
</span><span class="cx"> if ( empty( $sidebar_widget_ids ) ) {
</span><span class="cx"> $sidebar_widget_ids = array();
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/theme.php (28123 => 28124)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/theme.php 2014-04-14 22:05:20 UTC (rev 28123)
+++ trunk/src/wp-includes/theme.php 2014-04-14 22:45:40 UTC (rev 28124)
</span><span class="lines">@@ -752,11 +752,19 @@
</span><span class="cx"> * @param string $stylesheet Stylesheet name
</span><span class="cx"> */
</span><span class="cx"> function switch_theme( $stylesheet ) {
</span><del>- global $wp_theme_directories, $sidebars_widgets;
</del><ins>+ global $wp_theme_directories, $wp_customize, $sidebars_widgets;
</ins><span class="cx">
</span><del>- if ( is_array( $sidebars_widgets ) )
- set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
</del><ins>+ $_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;
+ }
</ins><span class="cx">
</span><ins>+ if ( is_array( $_sidebars_widgets ) ) {
+ set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
+ }
+
</ins><span class="cx"> $old_theme = wp_get_theme();
</span><span class="cx"> $new_theme = wp_get_theme( $stylesheet );
</span><span class="cx">
</span><span class="lines">@@ -782,9 +790,19 @@
</span><span class="cx">
</span><span class="cx"> update_option( 'current_theme', $new_name );
</span><span class="cx">
</span><ins>+ // Migrate from the old mods_{name} option to theme_mods_{slug}.
</ins><span class="cx"> if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
</span><span class="cx"> $default_theme_mods = (array) get_option( 'mods_' . $new_name );
</span><span class="cx"> add_option( "theme_mods_$stylesheet", $default_theme_mods );
</span><ins>+ } 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' );
+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> update_option( 'theme_switched', $old_theme->get_stylesheet() );
</span><span class="lines">@@ -1791,6 +1809,12 @@
</span><span class="cx"> if ( $stylesheet = get_option( 'theme_switched' ) ) {
</span><span class="cx"> $old_theme = wp_get_theme( $stylesheet );
</span><span class="cx">
</span><ins>+ // 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 );
+ }
+
</ins><span class="cx"> if ( $old_theme->exists() ) {
</span><span class="cx"> /**
</span><span class="cx"> * Fires on the first WP load after a theme switch if the old theme still exists.
</span><span class="lines">@@ -1918,4 +1942,4 @@
</span><span class="cx"> }());
</span><span class="cx"> </script>
</span><span class="cx"> <?php
</span><del>-}
</del><span class="cx">\ No newline at end of file
</span><ins>+}
</ins></span></pre></div>
<a id="trunksrcwpincludeswidgetsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/widgets.php (28123 => 28124)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/widgets.php 2014-04-14 22:05:20 UTC (rev 28123)
+++ trunk/src/wp-includes/widgets.php 2014-04-14 22:45:40 UTC (rev 28124)
</span><span class="lines">@@ -1401,8 +1401,16 @@
</span><span class="cx"> retrieve_widgets(true);
</span><span class="cx"> }
</span><span class="cx">
</span><del>-// look for "lost" widgets, this has to run at least on each theme change
-function retrieve_widgets($theme_changed = false) {
</del><ins>+/**
+ * Look for "lost" widgets, this has to run at least on each theme change.
+ *
+ * @since 2.8.0
+ *
+ * @param mixed $theme_changed Whether the theme was changed as a boolean. A value
+ * of 'customize' defers updates for the customizer.
+ * @return array
+ */
+function retrieve_widgets( $theme_changed = false ) {
</ins><span class="cx"> global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
</span><span class="cx">
</span><span class="cx"> $registered_sidebar_keys = array_keys( $wp_registered_sidebars );
</span><span class="lines">@@ -1412,8 +1420,11 @@
</span><span class="cx"> if ( is_array( $old_sidebars_widgets ) ) {
</span><span class="cx"> // time() that sidebars were stored is in $old_sidebars_widgets['time']
</span><span class="cx"> $_sidebars_widgets = $old_sidebars_widgets['data'];
</span><del>- remove_theme_mod( 'sidebars_widgets' );
</del><span class="cx">
</span><ins>+ if ( 'customize' === $theme_changed ) {
+ remove_theme_mod( 'sidebars_widgets' );
+ }
+
</ins><span class="cx"> foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
</span><span class="cx"> if ( 'wp_inactive_widgets' == $sidebar || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
</span><span class="cx"> continue;
</span><span class="lines">@@ -1495,7 +1506,9 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
</span><del>- wp_set_sidebars_widgets($sidebars_widgets);
</del><ins>+ if ( 'customize' === $theme_changed ) {
+ wp_set_sidebars_widgets( $sidebars_widgets );
+ }
</ins><span class="cx">
</span><span class="cx"> return $sidebars_widgets;
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>