Skip to content

Commit fba1988

Browse files
committed
Fix live previews for moving widget to another sidebar
* Send all sidebars_widgets settings, not just the setting for the widget being edited * Let incoming customized POST data completely override sidebars_widgets settings; stop merging * Stop deleting widget instance settings from preview customizer because unnecessary and because of possible race condition when moving widgets across sidebars
1 parent 0fd6cd9 commit fba1988

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

widget-customizer-preview.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,18 @@ var WidgetCustomizerPreview = (function ($) {
179179
return;
180180
}
181181

182+
var customized = {};
182183
var sidebar_id = null;
183-
var sidebar_widgets = [];
184184
wp.customize.each( function ( setting, setting_id ) {
185185
var matches = setting_id.match( /^sidebars_widgets\[(.+)\]/ );
186-
if ( matches && setting().indexOf( widget_id ) !== -1 ) {
187-
sidebar_id = matches[1];
188-
sidebar_widgets = setting();
186+
if ( ! matches ) {
187+
return;
188+
}
189+
var other_sidebar_id = matches[1];
190+
if ( setting().indexOf( widget_id ) !== -1 ) {
191+
sidebar_id = other_sidebar_id;
189192
}
193+
customized[sidebar_id_to_setting_id( other_sidebar_id )] = setting();
190194
} );
191195
if ( ! sidebar_id ) {
192196
throw new Error( 'Widget does not exist in a sidebar.' );
@@ -199,8 +203,7 @@ var WidgetCustomizerPreview = (function ($) {
199203
setting_id: setting_id,
200204
setting: JSON.stringify( to )
201205
};
202-
var customized = {};
203-
customized[ sidebar_id_to_setting_id( sidebar_id ) ] = sidebar_widgets;
206+
204207
customized[setting_id] = to;
205208
data.customized = JSON.stringify(customized);
206209
data[self.render_widget_nonce_post_key] = self.render_widget_nonce_value;
@@ -286,16 +289,9 @@ var WidgetCustomizerPreview = (function ($) {
286289
return;
287290
}
288291

289-
// Remove widgets (their DOM element and their setting) when removed from sidebar
292+
// Delete the widget from the DOM if it no longer exists in the sidebar
290293
$.each( from, function ( i, old_widget_id ) {
291294
if ( -1 === to.indexOf( old_widget_id ) ) {
292-
var setting_id = widget_id_to_setting_id( old_widget_id );
293-
if ( wp.customize.has( setting_id ) ) {
294-
wp.customize.remove( setting_id );
295-
delete already_bound_widgets[setting_id];
296-
}
297-
298-
// Delete the widget from the DOM if it wasn't added to its new location in the other sidebar
299295
self.getSidebarWidgetElement( sidebar_id, old_widget_id ).remove();
300296
}
301297
} );

widget-customizer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,12 @@ var WidgetCustomizer = (function ($) {
168168
return widget_form_controls.container[0];
169169
} );
170170

171-
// Re-sort widget form controls
171+
// Re-sort widget form controls (including widgets form other sidebars newly moved here)
172172
sidebar_widgets_add_control.before( final_control_containers );
173173
control.applyCardinalOrderClassNames();
174174

175175
// If the widget was dragged into the sidebar, make sure the sidebar_id param is updated
176176
_( widget_form_controls ).each( function ( widget_form_control ) {
177-
// @todo We need to delete the widget from the old sidebar, and re-fetch via Ajax
178177
widget_form_control.params.sidebar_id = control.params.sidebar_id;
179178
} );
180179

widget-customizer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,7 @@ static function prepreview_added_sidebars_widgets( $sidebars_widgets ) {
269269
foreach ( self::$_customized as $setting_id => $value ) {
270270
if ( preg_match( '/^sidebars_widgets\[(.+?)\]$/', $setting_id, $matches ) ) {
271271
$sidebar_id = $matches[1];
272-
if ( ! isset( $sidebars_widgets[$sidebar_id] ) ) {
273-
$sidebars_widgets[$sidebar_id] = array();
274-
}
275-
$sidebars_widgets[$sidebar_id] = array_unique( array_merge( $value, $sidebars_widgets[$sidebar_id] ) );
272+
$sidebars_widgets[$sidebar_id] = $value;
276273
}
277274
}
278275
return $sidebars_widgets;

0 commit comments

Comments
 (0)