From 517a106c352dfbd64d98f24dc0dca19cb51a7cd2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 20 Dec 2016 13:09:37 -0800 Subject: [PATCH 1/2] Override initial value for newly-created widget settings to be default instance value --- js/customize-widget-control-form.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/js/customize-widget-control-form.js b/js/customize-widget-control-form.js index d7a660d..97c5e01 100644 --- a/js/customize-widget-control-form.js +++ b/js/customize-widget-control-form.js @@ -24,7 +24,8 @@ wp.customize.Widgets.Form = (function( api ) { { control: null, config: { - l10n: {} + l10n: {}, + default_instance: {} } }, properties @@ -38,6 +39,15 @@ wp.customize.Widgets.Form = (function( api ) { throw new Error( 'No container' ); } + /* + * When a new widget is added to a sidebar via the wp.customize.Widgets.SidebarControl#addWidget(), + * it will supply an empty object as the initial value. The following ensures that the actual + * default instance value is used as the initial setting instead. + */ + if ( form.setting._dirty && _.isEmpty( form.setting.get() ) ) { + form.setting.set( _.clone( form.config.default_instance ) ); + } + previousValidate = form.setting.validate; form.setting.validate = function validate( value ) { var setting = this, newValue, oldValue; // eslint-disable-line consistent-this From 444f5ffb4441246de1d5c0656c3379d76b2ee31e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 20 Dec 2016 13:10:37 -0800 Subject: [PATCH 2/2] Move injection of initial default value into validate routine --- js/customize-widget-control-form.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/js/customize-widget-control-form.js b/js/customize-widget-control-form.js index 97c5e01..9ae3ccb 100644 --- a/js/customize-widget-control-form.js +++ b/js/customize-widget-control-form.js @@ -39,19 +39,10 @@ wp.customize.Widgets.Form = (function( api ) { throw new Error( 'No container' ); } - /* - * When a new widget is added to a sidebar via the wp.customize.Widgets.SidebarControl#addWidget(), - * it will supply an empty object as the initial value. The following ensures that the actual - * default instance value is used as the initial setting instead. - */ - if ( form.setting._dirty && _.isEmpty( form.setting.get() ) ) { - form.setting.set( _.clone( form.config.default_instance ) ); - } - previousValidate = form.setting.validate; form.setting.validate = function validate( value ) { var setting = this, newValue, oldValue; // eslint-disable-line consistent-this - newValue = _.extend( {}, value ); + newValue = _.extend( {}, form.config.default_instance, value ); oldValue = _.extend( {}, setting() ); newValue = previousValidate.call( setting, newValue );