Skip to content

Commit

Permalink
Fix live previews for moving widget to another sidebar
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
westonruter committed Jan 30, 2014
1 parent 0fd6cd9 commit fba1988
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
24 changes: 10 additions & 14 deletions widget-customizer-preview.js
Expand Up @@ -179,14 +179,18 @@ var WidgetCustomizerPreview = (function ($) {
return;
}

var customized = {};
var sidebar_id = null;
var sidebar_widgets = [];
wp.customize.each( function ( setting, setting_id ) {
var matches = setting_id.match( /^sidebars_widgets\[(.+)\]/ );
if ( matches && setting().indexOf( widget_id ) !== -1 ) {
sidebar_id = matches[1];
sidebar_widgets = setting();
if ( ! matches ) {
return;
}
var other_sidebar_id = matches[1];
if ( setting().indexOf( widget_id ) !== -1 ) {
sidebar_id = other_sidebar_id;
}
customized[sidebar_id_to_setting_id( other_sidebar_id )] = setting();
} );
if ( ! sidebar_id ) {
throw new Error( 'Widget does not exist in a sidebar.' );
Expand All @@ -199,8 +203,7 @@ var WidgetCustomizerPreview = (function ($) {
setting_id: setting_id,
setting: JSON.stringify( to )
};
var customized = {};
customized[ sidebar_id_to_setting_id( sidebar_id ) ] = sidebar_widgets;

customized[setting_id] = to;
data.customized = JSON.stringify(customized);
data[self.render_widget_nonce_post_key] = self.render_widget_nonce_value;
Expand Down Expand Up @@ -286,16 +289,9 @@ var WidgetCustomizerPreview = (function ($) {
return;
}

// Remove widgets (their DOM element and their setting) when removed from sidebar
// Delete the widget from the DOM if it no longer exists in the sidebar
$.each( from, function ( i, old_widget_id ) {
if ( -1 === to.indexOf( old_widget_id ) ) {
var setting_id = widget_id_to_setting_id( old_widget_id );
if ( wp.customize.has( setting_id ) ) {
wp.customize.remove( setting_id );
delete already_bound_widgets[setting_id];
}

// Delete the widget from the DOM if it wasn't added to its new location in the other sidebar
self.getSidebarWidgetElement( sidebar_id, old_widget_id ).remove();
}
} );
Expand Down
3 changes: 1 addition & 2 deletions widget-customizer.js
Expand Up @@ -168,13 +168,12 @@ var WidgetCustomizer = (function ($) {
return widget_form_controls.container[0];
} );

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

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

Expand Down
5 changes: 1 addition & 4 deletions widget-customizer.php
Expand Up @@ -269,10 +269,7 @@ static function prepreview_added_sidebars_widgets( $sidebars_widgets ) {
foreach ( self::$_customized as $setting_id => $value ) {
if ( preg_match( '/^sidebars_widgets\[(.+?)\]$/', $setting_id, $matches ) ) {
$sidebar_id = $matches[1];
if ( ! isset( $sidebars_widgets[$sidebar_id] ) ) {
$sidebars_widgets[$sidebar_id] = array();
}
$sidebars_widgets[$sidebar_id] = array_unique( array_merge( $value, $sidebars_widgets[$sidebar_id] ) );
$sidebars_widgets[$sidebar_id] = $value;
}
}
return $sidebars_widgets;
Expand Down

0 comments on commit fba1988

Please sign in to comment.