From 6b579e0d593a62edb8f0afe4fbd09bc52d2446ee Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Mon, 14 Apr 2014 22:46:16 +0000 Subject: [PATCH] Customizer: Properly handle widget settings when activating a previewed theme. props westonruter, ocean90, gcorne. fixes #27767. Built from https://develop.svn.wordpress.org/trunk@28124 git-svn-id: http://core.svn.wordpress.org/trunk@27955 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/js/customize-widgets.js | 3 +- wp-admin/js/customize-widgets.min.js | 2 +- wp-includes/class-wp-customize-manager.php | 1 + wp-includes/class-wp-customize-widgets.php | 91 +++++++++++++++++++++- wp-includes/theme.php | 32 +++++++- wp-includes/widgets.php | 21 ++++- 6 files changed, 137 insertions(+), 13 deletions(-) diff --git a/wp-admin/js/customize-widgets.js b/wp-admin/js/customize-widgets.js index 48ba335138..f2782b98e4 100644 --- a/wp-admin/js/customize-widgets.js +++ b/wp-admin/js/customize-widgets.js @@ -942,6 +942,7 @@ params.action = 'update-widget'; params.wp_customize = 'on'; params.nonce = api.Widgets.data.nonce; + params.theme = api.settings.theme.stylesheet; data = $.param( params ); $inputs = this._getInputs( $widgetContent ); @@ -1614,7 +1615,7 @@ widgetNumber = widget.get( 'multi_number' ); } - controlHtml = $( '#widget-tpl-' + widget.get( 'id' ) ).html(); + controlHtml = $.trim( $( '#widget-tpl-' + widget.get( 'id' ) ).html() ); if ( widget.get( 'is_multi' ) ) { controlHtml = controlHtml.replace( /<[^<>]+>/g, function( m ) { return m.replace( /__i__|%i%/g, widgetNumber ); diff --git a/wp-admin/js/customize-widgets.min.js b/wp-admin/js/customize-widgets.min.js index 89e4100141..07298730e6 100644 --- a/wp-admin/js/customize-widgets.min.js +++ b/wp-admin/js/customize-widgets.min.js @@ -1 +1 @@ -!function(a,b){function c(a){var b,c={number:null,id_base:null};return b=a.match(/^(.+)-(\d+)$/),b?(c.id_base=b[1],c.number=parseInt(b[2],10)):c.id_base=a,c}function d(a){var b,d=c(a);return b="widget_"+d.id_base,d.number&&(b+="["+d.number+"]"),b}if(a&&a.customize){var e,f,g=a.customize;g.Widgets=g.Widgets||{},g.Widgets.data=_wpCustomizeWidgetsSettings||{},e=g.Widgets.data.l10n,delete g.Widgets.data.l10n,g.Widgets.WidgetModel=Backbone.Model.extend({id:null,temp_id:null,classname:null,control_tpl:null,description:null,is_disabled:null,is_multi:null,multi_number:null,name:null,id_base:null,transport:"refresh",params:[],width:null,height:null,search_matched:!0}),g.Widgets.WidgetCollection=Backbone.Collection.extend({model:g.Widgets.WidgetModel,doSearch:function(a){this.terms!==a&&(this.terms=a,this.terms.length>0&&this.search(this.terms),""===this.terms&&this.each(function(a){a.set("search_matched",!0)}))},search:function(a){var b,c;a=a.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"),a=a.replace(/ /g,")(?=.*"),b=new RegExp("^(?=.*"+a+").+","i"),this.each(function(a){c=[a.get("name"),a.get("id"),a.get("description")].join(" "),a.set("search_matched",b.test(c))})}}),g.Widgets.availableWidgets=new g.Widgets.WidgetCollection(g.Widgets.data.availableWidgets),g.Widgets.SidebarModel=Backbone.Model.extend({after_title:null,after_widget:null,before_title:null,before_widget:null,"class":null,description:null,id:null,name:null,is_rendered:!1}),g.Widgets.SidebarCollection=Backbone.Collection.extend({model:g.Widgets.SidebarModel}),g.Widgets.registeredSidebars=new g.Widgets.SidebarCollection(g.Widgets.data.registeredSidebars),g.Widgets.AvailableWidgetsPanelView=a.Backbone.View.extend({el:"#available-widgets",events:{"input #widgets-search":"search","keyup #widgets-search":"search","change #widgets-search":"search","search #widgets-search":"search","focus .widget-tpl":"focus","click .widget-tpl":"_submit","keypress .widget-tpl":"_submit",keydown:"keyboardAccessible"},selected:null,currentSidebarControl:null,$search:null,initialize:function(){var a=this;this.$search=b("#widgets-search"),_.bindAll(this,"close"),this.listenTo(this.collection,"change",this.updateList),this.updateList(),b("#customize-controls").on("click keydown",function(c){var d=b(c.target).is(".add-new-widget, .add-new-widget *");b("body").hasClass("adding-widget")&&!d&&a.close()}),g.Widgets.Previewer.bind("url",this.close)},search:function(a){var b;this.collection.doSearch(a.target.value),this.selected&&!this.selected.is(":visible")&&(this.selected.removeClass("selected"),this.selected=null),this.selected&&!a.target.value&&(this.selected.removeClass("selected"),this.selected=null),!this.selected&&a.target.value&&(b=this.$el.find("> .widget-tpl:visible:first"),b.length&&this.select(b))},updateList:function(){this.collection.each(function(a){var c=b("#widget-tpl-"+a.id);c.toggle(a.get("search_matched")&&!a.get("is_disabled")),a.get("is_disabled")&&c.is(this.selected)&&(this.selected=null)})},select:function(a){this.selected=b(a),this.selected.siblings(".widget-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(a){this.select(b(a.currentTarget))},_submit:function(a){("keypress"!==a.type||13===a.which||32===a.which)&&this.submit(b(a.currentTarget))},submit:function(a){var c,d;a||(a=this.selected),a&&this.currentSidebarControl&&(this.select(a),c=b(this.selected).data("widget-id"),d=this.collection.findWhere({id:c}),d&&(this.currentSidebarControl.addWidget(d.get("id_base")),this.close()))},open:function(a){this.currentSidebarControl=a,_(this.currentSidebarControl.getWidgetFormControls()).each(function(a){a.params.is_wide&&a.collapseForm()}),b("body").addClass("adding-widget"),this.$el.find(".selected").removeClass("selected"),this.collection.doSearch(""),this.$search.focus()},close:function(a){a=a||{},a.returnFocus&&this.currentSidebarControl&&this.currentSidebarControl.container.find(".add-new-widget").focus(),this.currentSidebarControl=null,this.selected=null,b("body").removeClass("adding-widget"),this.$search.val("")},keyboardAccessible:function(a){var c=13===a.which,d=27===a.which,e=40===a.which,f=38===a.which,g=null,h=this.$el.find("> .widget-tpl:visible:first"),i=this.$el.find("> .widget-tpl:visible:last"),j=b(a.target).is(this.$search);return e||f?(e?j?g=h:this.selected&&0!==this.selected.nextAll(".widget-tpl:visible").length&&(g=this.selected.nextAll(".widget-tpl:visible:first")):f&&(j?g=i:this.selected&&0!==this.selected.prevAll(".widget-tpl:visible").length&&(g=this.selected.prevAll(".widget-tpl:visible:first"))),this.select(g),void(g?g.focus():this.$search.focus())):void((!c||this.$search.val())&&(c?this.submit():d&&this.close({returnFocus:!0})))}}),g.Widgets.formSyncHandlers={rss:function(a,c,d){var e=c.find(".widget-error:first"),f=b("
"+d+"
").find(".widget-error:first");e.length&&f.length?e.replaceWith(f):e.length?e.remove():f.length&&c.find(".widget-content:first").prepend(f)}},g.Widgets.WidgetControl=g.Control.extend({ready:function(){this._setupModel(),this._setupWideWidget(),this._setupControlToggle(),this._setupWidgetTitle(),this._setupReorderUI(),this._setupHighlightEffects(),this._setupUpdateUI(),this._setupRemoveUI()},_setupModel:function(){var a,b=this;g.Widgets.savedWidgetIds=g.Widgets.savedWidgetIds||[],a=function(){g.Widgets.savedWidgetIds[b.params.widget_id]=!0},g.bind("ready",a),g.bind("saved",a),this._updateCount=0,this.isWidgetUpdating=!1,this.liveUpdateMode=!0,this.setting.bind(function(a,c){_(c).isEqual(a)||b.isWidgetUpdating||b.updateWidget({instance:a})})},_setupWideWidget:function(){var a,c,d,e,f,h=this;this.params.is_wide&&(a=this.container.find(".widget-inside"),c=a.find("> .form"),d=b(".wp-full-overlay-sidebar-content:first"),this.container.addClass("wide-widget-control"),this.container.find(".widget-content:first").css({"max-width":this.params.width,"min-height":this.params.height}),f=function(){var d,e=h.container.offset().top,f=b(window).height(),g=c.outerHeight();a.css("max-height",f),d=Math.max(0,Math.min(Math.max(e,0),f-g)),a.css("top",d)},e=b("#customize-theme-controls"),this.container.on("expand",function(){f(),d.on("scroll",f),b(window).on("resize",f),e.on("expanded collapsed",f)}),this.container.on("collapsed",function(){d.off("scroll",f),b(window).off("resize",f),e.off("expanded collapsed",f)}),g.each(function(a){0===a.id.indexOf("sidebars_widgets[")&&a.bind(function(){h.container.hasClass("expanded")&&f()})}))},_setupControlToggle:function(){var a,b=this;this.container.find(".widget-top").on("click",function(a){a.preventDefault();var c=b.getSidebarWidgetsControl();c.isReordering||b.toggleForm()}),a=this.container.find(".widget-control-close"),a.on("click",function(a){a.preventDefault(),b.collapseForm(),b.container.find(".widget-top .widget-action:first").focus()})},_setupWidgetTitle:function(){var a,b=this;a=function(){var a=b.setting().title,c=b.container.find(".in-widget-title");c.text(a?": "+a:"")},this.setting.bind(a),a()},_setupReorderUI:function(){var a,c,d,e,f=this;a=function(a){a.siblings(".selected").removeClass("selected"),a.addClass("selected");var b=a.data("id")===f.params.sidebar_id;f.container.find(".move-widget-btn").prop("disabled",b)},this.container.find(".widget-title-action").after(b(g.Widgets.data.tpl.widgetReorderNav)),c=b(_.template(g.Widgets.data.tpl.moveWidgetArea,{sidebars:_(g.Widgets.registeredSidebars.toArray()).pluck("attributes")})),this.container.find(".widget-top").after(c),e=function(){var d,e=c.find("li");d=e.filter(function(){return b(this).data("id")===f.params.sidebar_id}),e.each(function(){var c,e,f=b(this);c=f.data("id"),e=g.Widgets.registeredSidebars.get(c),f.toggle(e.get("is_rendered")),f.hasClass("selected")&&!e.get("is_rendered")&&a(d)})},e(),g.Widgets.registeredSidebars.on("change:is_rendered",e),d=this.container.find(".widget-reorder-nav"),d.find(".move-widget, .move-widget-down, .move-widget-up").on("click keypress",function(a){if("keypress"!==a.type||13===a.which||32===a.which)if(b(this).focus(),b(this).is(".move-widget"))f.toggleWidgetMoveArea();else{var c=b(this).is(".move-widget-down"),d=b(this).is(".move-widget-up"),e=f.getWidgetSidebarPosition();if(d&&0===e||c&&e===f.getSidebarWidgetsControl().setting().length-1)return;d?f.moveUp():f.moveDown(),b(this).focus()}}),this.container.find(".widget-area-select").on("click keypress","li",function(c){("keypress"!==event.type||13===event.which||32===event.which)&&(c.preventDefault(),a(b(this)))}),this.container.find(".move-widget-btn").click(function(){f.getSidebarWidgetsControl().toggleReordering(!1);var a,b,c,d,e,h=f.params.sidebar_id,i=f.container.find(".widget-area-select li.selected").data("id");a=g("sidebars_widgets["+h+"]"),b=g("sidebars_widgets["+i+"]"),c=Array.prototype.slice.call(a()),d=Array.prototype.slice.call(b()),e=f.getWidgetSidebarPosition(),c.splice(e,1),d.push(f.params.widget_id),a(c),b(d),f.focus()})},_setupHighlightEffects:function(){var a=this;this.container.on("mouseenter click",function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),this.setting.bind(function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)})},_setupUpdateUI:function(){var a,c,d,f,h,i=this;a=this.container.find(".widget:first"),c=a.find(".widget-content:first"),d=this.container.find(".widget-control-save"),d.val(e.saveBtnLabel),d.attr("title",e.saveBtnTooltip),d.removeClass("button-primary").addClass("button-secondary"),d.on("click",function(a){a.preventDefault(),i.updateWidget({disable_form:!0})}),f=_.debounce(function(){i.updateWidget()},250),this.container.find(".widget-content").on("keydown","input",function(a){13===a.which&&(a.preventDefault(),i.updateWidget({ignoreActiveElement:!0}))}),c.on("change input propertychange",":input",function(a){i.liveUpdateMode&&("change"===a.type?i.updateWidget():this.checkValidity&&this.checkValidity()&&f())}),this.setting.previewer.channel.bind("synced",function(){i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("widget-updated",function(a){a===i.params.widget_id&&i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("rendered-widgets",function(a){var b=!!a[i.params.widget_id];i.container.toggleClass("widget-rendered",b)}),h=g.Widgets.formSyncHandlers[this.params.widget_id_base],h&&b(document).on("widget-synced",function(b,c){a.is(c)&&h.apply(document,arguments)})},_setupRemoveUI:function(){var a,b,c=this;a=this.container.find("a.widget-control-remove"),a.on("click",function(a){a.preventDefault();var b;b=c.container.next().is(".customize-control-widget_form")?c.container.next().find(".widget-action:first"):c.container.prev().is(".customize-control-widget_form")?c.container.prev().find(".widget-action:first"):c.container.next(".customize-control-sidebar_widgets").find(".add-new-widget:first"),c.container.slideUp(function(){var a,d,e=g.Widgets.getSidebarWidgetControlContainingWidget(c.params.widget_id);e&&(a=e.setting().slice(),d=_.indexOf(a,c.params.widget_id),-1!==d&&(a.splice(d,1),e.setting(a),b.focus()))})}),b=function(){a.text(e.removeBtnLabel),a.attr("title",e.removeBtnTooltip)},this.params.is_new?g.bind("saved",b):b()},_getInputs:function(a){return b(a).find(":input[name]")},_getInputsSignature:function(a){var c=_(a).map(function(a){var c,d=b(a);return c=d.is(":checkbox, :radio")?[d.attr("id"),d.attr("name"),d.prop("value")]:[d.attr("id"),d.attr("name")],c.join(",")});return c.join(";")},_getInputStatePropertyName:function(a){var c=b(a);return c.is(":radio, :checkbox")?"checked":"value"},getSidebarWidgetsControl:function(){var a,b;return a="sidebars_widgets["+this.params.sidebar_id+"]",(b=g.control(a))?b:void 0},updateWidget:function(c){var d,f,h,i,j,k,l,m,n,o,p,q=this;c=b.extend({instance:null,complete:null,ignoreActiveElement:!1},c),d=c.instance,f=c.complete,this._updateCount+=1,j=this._updateCount,h=this.container.find(".widget:first"),i=h.find(".widget-content:first"),i.find(".widget-error").remove(),this.container.addClass("widget-form-loading"),this.container.addClass("previewer-loading"),n=g.state("processing"),n(n()+1),this.liveUpdateMode||this.container.addClass("widget-form-disabled"),k={},k.action="update-widget",k.wp_customize="on",k.nonce=g.Widgets.data.nonce,l=b.param(k),m=this._getInputs(i),m.each(function(){var a=b(this),c=q._getInputStatePropertyName(this);a.data("state"+j,a.prop(c))}),l+=d?"&"+b.param({sanitized_widget_setting:JSON.stringify(d)}):"&"+m.serialize(),l+="&"+i.find("~ :input").serialize(),o=b.post(a.ajax.settings.url,l),o.done(function(a){var d,k,l,n,o=!1;return"0"===a?(g.Widgets.Previewer.preview.iframe.hide(),void g.Widgets.Previewer.login().done(function(){q.updateWidget(c),g.Widgets.Previewer.preview.iframe.show()})):"-1"===a?void g.Widgets.Previewer.cheatin():void(a.success?(k=b("
"+a.data.form+"
"),l=q._getInputs(k),n=q._getInputsSignature(m)===q._getInputsSignature(l),n&&!q.liveUpdateMode&&(q.liveUpdateMode=!0,q.container.removeClass("widget-form-disabled"),q.container.find('input[name="savewidget"]').hide()),n&&q.liveUpdateMode?(m.each(function(a){var d,e,f,g=b(this),h=b(l[a]),i=q._getInputStatePropertyName(this);d=g.data("state"+j),e=h.prop(i),g.data("sanitized",e),f=d!==e&&(c.ignoreActiveElement||!g.is(document.activeElement)),f&&g.prop(i,e)}),b(document).trigger("widget-synced",[h,a.data.form])):q.liveUpdateMode?(q.liveUpdateMode=!1,q.container.find('input[name="savewidget"]').show(),o=!0):(i.html(a.data.form),q.container.removeClass("widget-form-disabled"),b(document).trigger("widget-updated",[h])),p=!o&&!_(q.setting()).isEqual(a.data.instance),p?(q.isWidgetUpdating=!0,q.setting(a.data.instance),q.isWidgetUpdating=!1):q.container.removeClass("previewer-loading"),f&&f.call(q,null,{noChange:!p,ajaxFinished:!0})):(d=e.error,a.data&&a.data.message&&(d=a.data.message),f?f.call(q,d):i.prepend('

'+d+"

")))}),o.fail(function(a,b){f&&f.call(q,b)}),o.always(function(){q.container.removeClass("widget-form-loading"),m.each(function(){b(this).removeData("state"+j)}),n(n()-1)})},expandControlSection:function(){var a=this.container.closest(".accordion-section");a.hasClass("open")||a.find(".accordion-section-title:first").trigger("click")},expandForm:function(){this.toggleForm(!0)},collapseForm:function(){this.toggleForm(!1)},toggleForm:function(a){var b,c,d,e=this;b=this.container.find("div.widget:first"),c=b.find(".widget-inside:first"),"undefined"==typeof a&&(a=!c.is(":visible")),c.is(":visible")!==a&&(a?(g.control.each(function(a){e.params.type===a.params.type&&e!==a&&a.collapseForm()}),d=function(){e.container.removeClass("expanding"),e.container.addClass("expanded"),e.container.trigger("expanded")},e.params.is_wide?c.fadeIn("fast",d):c.slideDown("fast",d),e.container.trigger("expand"),e.container.addClass("expanding")):(d=function(){e.container.removeClass("collapsing"),e.container.removeClass("expanded"),e.container.trigger("collapsed")},e.container.trigger("collapse"),e.container.addClass("collapsing"),e.params.is_wide?c.fadeOut("fast",d):c.slideUp("fast",function(){b.css({width:"",margin:""}),d()})))},focus:function(){this.expandControlSection(),this.expandForm(),this.container.find(".widget-content :focusable:first").focus()},getWidgetSidebarPosition:function(){var a,b;return a=this.getSidebarWidgetsControl().setting(),b=_.indexOf(a,this.params.widget_id),-1!==b?b:void 0},moveUp:function(){this._moveWidgetByOne(-1)},moveDown:function(){this._moveWidgetByOne(1)},_moveWidgetByOne:function(a){var b,c,d,e;b=this.getWidgetSidebarPosition(),c=this.getSidebarWidgetsControl().setting,d=Array.prototype.slice.call(c()),e=d[b+a],d[b+a]=this.params.widget_id,d[b]=e,c(d)},toggleWidgetMoveArea:function(a){var c,d=this;c=this.container.find(".move-widget-area"),"undefined"==typeof a&&(a=!c.hasClass("active")),a&&(c.find(".selected").removeClass("selected"),c.find("li").filter(function(){return b(this).data("id")===d.params.sidebar_id}).addClass("selected"),this.container.find(".move-widget-btn").prop("disabled",!0)),c.toggleClass("active",a)},highlightSectionAndControl:function(){var a;a=this.container.is(":hidden")?this.container.closest(".control-section"):this.container,b(".highlighted").removeClass("highlighted"),a.addClass("highlighted"),setTimeout(function(){a.removeClass("highlighted")},500)}}),g.Widgets.SidebarControl=g.Control.extend({ready:function(){this.$controlSection=this.container.closest(".control-section"),this.$sectionContent=this.container.closest(".accordion-section-content"),this._setupModel(),this._setupSortable(),this._setupAddition(),this._applyCardinalOrderClassNames()},_setupModel:function(){var a=this,d=g.Widgets.registeredSidebars.get(this.params.sidebar_id);this.setting.bind(function(d,e){var f,h,i,j;j=_(e).difference(d),d=_(d).filter(function(a){var b=c(a);return!!g.Widgets.availableWidgets.findWhere({id_base:b.id_base})}),f=_(d).map(function(b){var c=g.Widgets.getWidgetFormControlForWidget(b);return c||(c=a.addWidget(b)),c}),f.sort(function(a,b){var c=_.indexOf(d,a.params.widget_id),e=_.indexOf(d,b.params.widget_id);return c===e?0:e>c?-1:1}),i=_(f).map(function(a){return a.container[0]}),h=a.$sectionContent.find(".customize-control-sidebar_widgets"),h.before(i),a._applyCardinalOrderClassNames(),_(f).each(function(b){b.params.sidebar_id=a.params.sidebar_id}),_(j).each(function(d){setTimeout(function(){var e,f,h,i,j,k=!1;g.each(function(b){if(b.id!==a.setting.id&&0===b.id.indexOf("sidebars_widgets[")&&"sidebars_widgets[wp_inactive_widgets]"!==b.id){var c,e=b();c=_.indexOf(e,d),-1!==c&&(k=!0)}}),k||(e=g.Widgets.getWidgetFormControlForWidget(d),f=e&&b.contains(document,e.container[0])&&!b.contains(a.$sectionContent[0],e.container[0]),e&&!f&&(g.control.remove(e.id),e.container.remove()),g.Widgets.savedWidgetIds[d]&&(h=g.value("sidebars_widgets[wp_inactive_widgets]")().slice(),h.push(d),g.value("sidebars_widgets[wp_inactive_widgets]")(_(h).unique())),i=c(d).id_base,j=g.Widgets.availableWidgets.findWhere({id_base:i}),j&&!j.get("is_multi")&&j.set("is_disabled",!1))})})}),g.Widgets.Previewer.bind("rendered-sidebars",function(b){var c=!!b[a.params.sidebar_id];d.set("is_rendered",c)}),d.on("change:is_rendered",function(){var a,c="#accordion-section-sidebar-widgets-"+this.get("id");a=b(c),this.get("is_rendered")?a.stop().slideDown(function(){b(this).css("height","auto")}):(a.hasClass("open")&&a.find(".accordion-section-title").trigger("click"),a.stop().slideUp())})},_setupSortable:function(){var a=this;this.isReordering=!1,this.$sectionContent.sortable({items:"> .customize-control-widget_form",handle:".widget-top",axis:"y",connectWith:".accordion-section-content:has(.customize-control-sidebar_widgets)",update:function(){var c,d=a.$sectionContent.sortable("toArray");c=b.map(d,function(a){return b("#"+a).find(":input[name=widget-id]").val()}),a.setting(c)}}),this.$controlSection.find(".accordion-section-title").droppable({accept:".customize-control-widget_form",over:function(){a.$controlSection.hasClass("open")||(a.$controlSection.addClass("open"),a.$sectionContent.toggle(!1).slideToggle(150,function(){a.$sectionContent.sortable("refreshPositions")}))}}),this.container.find(".reorder-toggle").on("click keydown",function(b){("keydown"!==b.type||13===b.which||32===b.which)&&a.toggleReordering(!a.isReordering)})},_setupAddition:function(){var a=this;this.container.find(".add-new-widget").on("click keydown",function(c){("keydown"!==c.type||13===c.which||32===c.which)&&(a.$sectionContent.hasClass("reordering")||(b("body").hasClass("adding-widget")?g.Widgets.availableWidgetsPanel.close():g.Widgets.availableWidgetsPanel.open(a)))})},_applyCardinalOrderClassNames:function(){this.$sectionContent.find(".customize-control-widget_form").removeClass("first-widget").removeClass("last-widget").find(".move-widget-down, .move-widget-up").prop("tabIndex",0),this.$sectionContent.find(".customize-control-widget_form:first").addClass("first-widget").find(".move-widget-up").prop("tabIndex",-1),this.$sectionContent.find(".customize-control-widget_form:last").addClass("last-widget").find(".move-widget-down").prop("tabIndex",-1)},toggleReordering:function(a){a=Boolean(a),a!==this.$sectionContent.hasClass("reordering")&&(this.isReordering=a,this.$sectionContent.toggleClass("reordering",a),a&&_(this.getWidgetFormControls()).each(function(a){a.collapseForm()}))},getWidgetFormControls:function(){var a;return a=_(this.setting()).map(function(a){var b=d(a),c=g.control(b);if(c)return c})},addWidget:function(a){var d,e,f,h,i,j,k,l,m,n=this,o="widget_form",p=c(a),q=p.number,r=p.id_base,s=g.Widgets.availableWidgets.findWhere({id_base:r});return s?q&&!s.get("is_multi")?!1:(s.get("is_multi")&&!q&&(s.set("multi_number",s.get("multi_number")+1),q=s.get("multi_number")),d=b("#widget-tpl-"+s.get("id")).html(),s.get("is_multi")?d=d.replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,q)}):s.set("is_disabled",!0),e=b(d),f=b("
  • ").addClass("customize-control").addClass("customize-control-"+o).append(e),f.find("> .widget-icon").remove(),s.get("is_multi")&&(f.find('input[name="widget_number"]').val(q),f.find('input[name="multi_number"]').val(q)),a=f.find('[name="widget-id"]').val(),f.hide(),i="widget_"+s.get("id_base"),s.get("is_multi")&&(i+="["+q+"]"),f.attr("id","customize-control-"+i.replace(/\]/g,"").replace(/\[/g,"-")),this.container.after(f),j=g.has(i),j||(m={transport:"refresh",previewer:this.setting.previewer},g.create(i,i,{},m)),h=g.controlConstructor[o],k=new h(i,{params:{settings:{"default":i},sidebar_id:n.params.sidebar_id,widget_id:a,widget_id_base:s.get("id_base"),type:o,is_new:!j,width:s.get("width"),height:s.get("height"),is_wide:s.get("is_wide")},previewer:n.setting.previewer}),g.control.add(i,k),g.each(function(b){if(b.id!==n.setting.id&&0===b.id.indexOf("sidebars_widgets[")){var c=b().slice(),d=_.indexOf(c,a);-1!==d&&(c.splice(d),b(c))}}),l=this.setting().slice(),-1===_.indexOf(l,a)&&(l.push(a),this.setting(l)),f.slideDown(function(){j?(k.expandForm(),k.updateWidget({instance:k.setting(),complete:function(a){if(a)throw a;k.focus()}})):k.focus()}),b(document).trigger("widget-added",[e]),k):!1}}),b.extend(g.controlConstructor,{widget_form:g.Widgets.WidgetControl,sidebar_widgets:g.Widgets.SidebarControl}),f=g.Previewer,g.Previewer=f.extend({initialize:function(a,b){g.Widgets.Previewer=this,f.prototype.initialize.call(this,a,b),this.bind("refresh",this.refresh)}}),g.bind("ready",function(){g.Widgets.availableWidgetsPanel=new g.Widgets.AvailableWidgetsPanelView({collection:g.Widgets.availableWidgets}),g.Widgets.Previewer.bind("highlight-widget-control",g.Widgets.highlightWidgetFormControl),g.Widgets.Previewer.bind("focus-widget-control",g.Widgets.focusWidgetFormControl)}),g.Widgets.highlightWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.highlightSectionAndControl()},g.Widgets.focusWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.focus()},g.Widgets.getSidebarWidgetControlContainingWidget=function(a){var b=null;return g.control.each(function(c){"sidebar_widgets"===c.params.type&&-1!==_.indexOf(c.setting(),a)&&(b=c)}),b},g.Widgets.getWidgetFormControlForWidget=function(a){var b=null;return g.control.each(function(c){"widget_form"===c.params.type&&c.params.widget_id===a&&(b=c)}),b}}}(window.wp,jQuery); \ No newline at end of file +!function(a,b){function c(a){var b,c={number:null,id_base:null};return b=a.match(/^(.+)-(\d+)$/),b?(c.id_base=b[1],c.number=parseInt(b[2],10)):c.id_base=a,c}function d(a){var b,d=c(a);return b="widget_"+d.id_base,d.number&&(b+="["+d.number+"]"),b}if(a&&a.customize){var e,f,g=a.customize;g.Widgets=g.Widgets||{},g.Widgets.data=_wpCustomizeWidgetsSettings||{},e=g.Widgets.data.l10n,delete g.Widgets.data.l10n,g.Widgets.WidgetModel=Backbone.Model.extend({id:null,temp_id:null,classname:null,control_tpl:null,description:null,is_disabled:null,is_multi:null,multi_number:null,name:null,id_base:null,transport:"refresh",params:[],width:null,height:null,search_matched:!0}),g.Widgets.WidgetCollection=Backbone.Collection.extend({model:g.Widgets.WidgetModel,doSearch:function(a){this.terms!==a&&(this.terms=a,this.terms.length>0&&this.search(this.terms),""===this.terms&&this.each(function(a){a.set("search_matched",!0)}))},search:function(a){var b,c;a=a.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"),a=a.replace(/ /g,")(?=.*"),b=new RegExp("^(?=.*"+a+").+","i"),this.each(function(a){c=[a.get("name"),a.get("id"),a.get("description")].join(" "),a.set("search_matched",b.test(c))})}}),g.Widgets.availableWidgets=new g.Widgets.WidgetCollection(g.Widgets.data.availableWidgets),g.Widgets.SidebarModel=Backbone.Model.extend({after_title:null,after_widget:null,before_title:null,before_widget:null,"class":null,description:null,id:null,name:null,is_rendered:!1}),g.Widgets.SidebarCollection=Backbone.Collection.extend({model:g.Widgets.SidebarModel}),g.Widgets.registeredSidebars=new g.Widgets.SidebarCollection(g.Widgets.data.registeredSidebars),g.Widgets.AvailableWidgetsPanelView=a.Backbone.View.extend({el:"#available-widgets",events:{"input #widgets-search":"search","keyup #widgets-search":"search","change #widgets-search":"search","search #widgets-search":"search","focus .widget-tpl":"focus","click .widget-tpl":"_submit","keypress .widget-tpl":"_submit",keydown:"keyboardAccessible"},selected:null,currentSidebarControl:null,$search:null,initialize:function(){var a=this;this.$search=b("#widgets-search"),_.bindAll(this,"close"),this.listenTo(this.collection,"change",this.updateList),this.updateList(),b("#customize-controls").on("click keydown",function(c){var d=b(c.target).is(".add-new-widget, .add-new-widget *");b("body").hasClass("adding-widget")&&!d&&a.close()}),g.Widgets.Previewer.bind("url",this.close)},search:function(a){var b;this.collection.doSearch(a.target.value),this.selected&&!this.selected.is(":visible")&&(this.selected.removeClass("selected"),this.selected=null),this.selected&&!a.target.value&&(this.selected.removeClass("selected"),this.selected=null),!this.selected&&a.target.value&&(b=this.$el.find("> .widget-tpl:visible:first"),b.length&&this.select(b))},updateList:function(){this.collection.each(function(a){var c=b("#widget-tpl-"+a.id);c.toggle(a.get("search_matched")&&!a.get("is_disabled")),a.get("is_disabled")&&c.is(this.selected)&&(this.selected=null)})},select:function(a){this.selected=b(a),this.selected.siblings(".widget-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(a){this.select(b(a.currentTarget))},_submit:function(a){("keypress"!==a.type||13===a.which||32===a.which)&&this.submit(b(a.currentTarget))},submit:function(a){var c,d;a||(a=this.selected),a&&this.currentSidebarControl&&(this.select(a),c=b(this.selected).data("widget-id"),d=this.collection.findWhere({id:c}),d&&(this.currentSidebarControl.addWidget(d.get("id_base")),this.close()))},open:function(a){this.currentSidebarControl=a,_(this.currentSidebarControl.getWidgetFormControls()).each(function(a){a.params.is_wide&&a.collapseForm()}),b("body").addClass("adding-widget"),this.$el.find(".selected").removeClass("selected"),this.collection.doSearch(""),this.$search.focus()},close:function(a){a=a||{},a.returnFocus&&this.currentSidebarControl&&this.currentSidebarControl.container.find(".add-new-widget").focus(),this.currentSidebarControl=null,this.selected=null,b("body").removeClass("adding-widget"),this.$search.val("")},keyboardAccessible:function(a){var c=13===a.which,d=27===a.which,e=40===a.which,f=38===a.which,g=null,h=this.$el.find("> .widget-tpl:visible:first"),i=this.$el.find("> .widget-tpl:visible:last"),j=b(a.target).is(this.$search);return e||f?(e?j?g=h:this.selected&&0!==this.selected.nextAll(".widget-tpl:visible").length&&(g=this.selected.nextAll(".widget-tpl:visible:first")):f&&(j?g=i:this.selected&&0!==this.selected.prevAll(".widget-tpl:visible").length&&(g=this.selected.prevAll(".widget-tpl:visible:first"))),this.select(g),void(g?g.focus():this.$search.focus())):void((!c||this.$search.val())&&(c?this.submit():d&&this.close({returnFocus:!0})))}}),g.Widgets.formSyncHandlers={rss:function(a,c,d){var e=c.find(".widget-error:first"),f=b("
    "+d+"
    ").find(".widget-error:first");e.length&&f.length?e.replaceWith(f):e.length?e.remove():f.length&&c.find(".widget-content:first").prepend(f)}},g.Widgets.WidgetControl=g.Control.extend({ready:function(){this._setupModel(),this._setupWideWidget(),this._setupControlToggle(),this._setupWidgetTitle(),this._setupReorderUI(),this._setupHighlightEffects(),this._setupUpdateUI(),this._setupRemoveUI()},_setupModel:function(){var a,b=this;g.Widgets.savedWidgetIds=g.Widgets.savedWidgetIds||[],a=function(){g.Widgets.savedWidgetIds[b.params.widget_id]=!0},g.bind("ready",a),g.bind("saved",a),this._updateCount=0,this.isWidgetUpdating=!1,this.liveUpdateMode=!0,this.setting.bind(function(a,c){_(c).isEqual(a)||b.isWidgetUpdating||b.updateWidget({instance:a})})},_setupWideWidget:function(){var a,c,d,e,f,h=this;this.params.is_wide&&(a=this.container.find(".widget-inside"),c=a.find("> .form"),d=b(".wp-full-overlay-sidebar-content:first"),this.container.addClass("wide-widget-control"),this.container.find(".widget-content:first").css({"max-width":this.params.width,"min-height":this.params.height}),f=function(){var d,e=h.container.offset().top,f=b(window).height(),g=c.outerHeight();a.css("max-height",f),d=Math.max(0,Math.min(Math.max(e,0),f-g)),a.css("top",d)},e=b("#customize-theme-controls"),this.container.on("expand",function(){f(),d.on("scroll",f),b(window).on("resize",f),e.on("expanded collapsed",f)}),this.container.on("collapsed",function(){d.off("scroll",f),b(window).off("resize",f),e.off("expanded collapsed",f)}),g.each(function(a){0===a.id.indexOf("sidebars_widgets[")&&a.bind(function(){h.container.hasClass("expanded")&&f()})}))},_setupControlToggle:function(){var a,b=this;this.container.find(".widget-top").on("click",function(a){a.preventDefault();var c=b.getSidebarWidgetsControl();c.isReordering||b.toggleForm()}),a=this.container.find(".widget-control-close"),a.on("click",function(a){a.preventDefault(),b.collapseForm(),b.container.find(".widget-top .widget-action:first").focus()})},_setupWidgetTitle:function(){var a,b=this;a=function(){var a=b.setting().title,c=b.container.find(".in-widget-title");c.text(a?": "+a:"")},this.setting.bind(a),a()},_setupReorderUI:function(){var a,c,d,e,f=this;a=function(a){a.siblings(".selected").removeClass("selected"),a.addClass("selected");var b=a.data("id")===f.params.sidebar_id;f.container.find(".move-widget-btn").prop("disabled",b)},this.container.find(".widget-title-action").after(b(g.Widgets.data.tpl.widgetReorderNav)),c=b(_.template(g.Widgets.data.tpl.moveWidgetArea,{sidebars:_(g.Widgets.registeredSidebars.toArray()).pluck("attributes")})),this.container.find(".widget-top").after(c),e=function(){var d,e=c.find("li");d=e.filter(function(){return b(this).data("id")===f.params.sidebar_id}),e.each(function(){var c,e,f=b(this);c=f.data("id"),e=g.Widgets.registeredSidebars.get(c),f.toggle(e.get("is_rendered")),f.hasClass("selected")&&!e.get("is_rendered")&&a(d)})},e(),g.Widgets.registeredSidebars.on("change:is_rendered",e),d=this.container.find(".widget-reorder-nav"),d.find(".move-widget, .move-widget-down, .move-widget-up").on("click keypress",function(a){if("keypress"!==a.type||13===a.which||32===a.which)if(b(this).focus(),b(this).is(".move-widget"))f.toggleWidgetMoveArea();else{var c=b(this).is(".move-widget-down"),d=b(this).is(".move-widget-up"),e=f.getWidgetSidebarPosition();if(d&&0===e||c&&e===f.getSidebarWidgetsControl().setting().length-1)return;d?f.moveUp():f.moveDown(),b(this).focus()}}),this.container.find(".widget-area-select").on("click keypress","li",function(c){("keypress"!==event.type||13===event.which||32===event.which)&&(c.preventDefault(),a(b(this)))}),this.container.find(".move-widget-btn").click(function(){f.getSidebarWidgetsControl().toggleReordering(!1);var a,b,c,d,e,h=f.params.sidebar_id,i=f.container.find(".widget-area-select li.selected").data("id");a=g("sidebars_widgets["+h+"]"),b=g("sidebars_widgets["+i+"]"),c=Array.prototype.slice.call(a()),d=Array.prototype.slice.call(b()),e=f.getWidgetSidebarPosition(),c.splice(e,1),d.push(f.params.widget_id),a(c),b(d),f.focus()})},_setupHighlightEffects:function(){var a=this;this.container.on("mouseenter click",function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)}),this.setting.bind(function(){a.setting.previewer.send("highlight-widget",a.params.widget_id)})},_setupUpdateUI:function(){var a,c,d,f,h,i=this;a=this.container.find(".widget:first"),c=a.find(".widget-content:first"),d=this.container.find(".widget-control-save"),d.val(e.saveBtnLabel),d.attr("title",e.saveBtnTooltip),d.removeClass("button-primary").addClass("button-secondary"),d.on("click",function(a){a.preventDefault(),i.updateWidget({disable_form:!0})}),f=_.debounce(function(){i.updateWidget()},250),this.container.find(".widget-content").on("keydown","input",function(a){13===a.which&&(a.preventDefault(),i.updateWidget({ignoreActiveElement:!0}))}),c.on("change input propertychange",":input",function(a){i.liveUpdateMode&&("change"===a.type?i.updateWidget():this.checkValidity&&this.checkValidity()&&f())}),this.setting.previewer.channel.bind("synced",function(){i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("widget-updated",function(a){a===i.params.widget_id&&i.container.removeClass("previewer-loading")}),g.Widgets.Previewer.bind("rendered-widgets",function(a){var b=!!a[i.params.widget_id];i.container.toggleClass("widget-rendered",b)}),h=g.Widgets.formSyncHandlers[this.params.widget_id_base],h&&b(document).on("widget-synced",function(b,c){a.is(c)&&h.apply(document,arguments)})},_setupRemoveUI:function(){var a,b,c=this;a=this.container.find("a.widget-control-remove"),a.on("click",function(a){a.preventDefault();var b;b=c.container.next().is(".customize-control-widget_form")?c.container.next().find(".widget-action:first"):c.container.prev().is(".customize-control-widget_form")?c.container.prev().find(".widget-action:first"):c.container.next(".customize-control-sidebar_widgets").find(".add-new-widget:first"),c.container.slideUp(function(){var a,d,e=g.Widgets.getSidebarWidgetControlContainingWidget(c.params.widget_id);e&&(a=e.setting().slice(),d=_.indexOf(a,c.params.widget_id),-1!==d&&(a.splice(d,1),e.setting(a),b.focus()))})}),b=function(){a.text(e.removeBtnLabel),a.attr("title",e.removeBtnTooltip)},this.params.is_new?g.bind("saved",b):b()},_getInputs:function(a){return b(a).find(":input[name]")},_getInputsSignature:function(a){var c=_(a).map(function(a){var c,d=b(a);return c=d.is(":checkbox, :radio")?[d.attr("id"),d.attr("name"),d.prop("value")]:[d.attr("id"),d.attr("name")],c.join(",")});return c.join(";")},_getInputStatePropertyName:function(a){var c=b(a);return c.is(":radio, :checkbox")?"checked":"value"},getSidebarWidgetsControl:function(){var a,b;return a="sidebars_widgets["+this.params.sidebar_id+"]",(b=g.control(a))?b:void 0},updateWidget:function(c){var d,f,h,i,j,k,l,m,n,o,p,q=this;c=b.extend({instance:null,complete:null,ignoreActiveElement:!1},c),d=c.instance,f=c.complete,this._updateCount+=1,j=this._updateCount,h=this.container.find(".widget:first"),i=h.find(".widget-content:first"),i.find(".widget-error").remove(),this.container.addClass("widget-form-loading"),this.container.addClass("previewer-loading"),n=g.state("processing"),n(n()+1),this.liveUpdateMode||this.container.addClass("widget-form-disabled"),k={},k.action="update-widget",k.wp_customize="on",k.nonce=g.Widgets.data.nonce,k.theme=g.settings.theme.stylesheet,l=b.param(k),m=this._getInputs(i),m.each(function(){var a=b(this),c=q._getInputStatePropertyName(this);a.data("state"+j,a.prop(c))}),l+=d?"&"+b.param({sanitized_widget_setting:JSON.stringify(d)}):"&"+m.serialize(),l+="&"+i.find("~ :input").serialize(),o=b.post(a.ajax.settings.url,l),o.done(function(a){var d,k,l,n,o=!1;return"0"===a?(g.Widgets.Previewer.preview.iframe.hide(),void g.Widgets.Previewer.login().done(function(){q.updateWidget(c),g.Widgets.Previewer.preview.iframe.show()})):"-1"===a?void g.Widgets.Previewer.cheatin():void(a.success?(k=b("
    "+a.data.form+"
    "),l=q._getInputs(k),n=q._getInputsSignature(m)===q._getInputsSignature(l),n&&!q.liveUpdateMode&&(q.liveUpdateMode=!0,q.container.removeClass("widget-form-disabled"),q.container.find('input[name="savewidget"]').hide()),n&&q.liveUpdateMode?(m.each(function(a){var d,e,f,g=b(this),h=b(l[a]),i=q._getInputStatePropertyName(this);d=g.data("state"+j),e=h.prop(i),g.data("sanitized",e),f=d!==e&&(c.ignoreActiveElement||!g.is(document.activeElement)),f&&g.prop(i,e)}),b(document).trigger("widget-synced",[h,a.data.form])):q.liveUpdateMode?(q.liveUpdateMode=!1,q.container.find('input[name="savewidget"]').show(),o=!0):(i.html(a.data.form),q.container.removeClass("widget-form-disabled"),b(document).trigger("widget-updated",[h])),p=!o&&!_(q.setting()).isEqual(a.data.instance),p?(q.isWidgetUpdating=!0,q.setting(a.data.instance),q.isWidgetUpdating=!1):q.container.removeClass("previewer-loading"),f&&f.call(q,null,{noChange:!p,ajaxFinished:!0})):(d=e.error,a.data&&a.data.message&&(d=a.data.message),f?f.call(q,d):i.prepend('

    '+d+"

    ")))}),o.fail(function(a,b){f&&f.call(q,b)}),o.always(function(){q.container.removeClass("widget-form-loading"),m.each(function(){b(this).removeData("state"+j)}),n(n()-1)})},expandControlSection:function(){var a=this.container.closest(".accordion-section");a.hasClass("open")||a.find(".accordion-section-title:first").trigger("click")},expandForm:function(){this.toggleForm(!0)},collapseForm:function(){this.toggleForm(!1)},toggleForm:function(a){var b,c,d,e=this;b=this.container.find("div.widget:first"),c=b.find(".widget-inside:first"),"undefined"==typeof a&&(a=!c.is(":visible")),c.is(":visible")!==a&&(a?(g.control.each(function(a){e.params.type===a.params.type&&e!==a&&a.collapseForm()}),d=function(){e.container.removeClass("expanding"),e.container.addClass("expanded"),e.container.trigger("expanded")},e.params.is_wide?c.fadeIn("fast",d):c.slideDown("fast",d),e.container.trigger("expand"),e.container.addClass("expanding")):(d=function(){e.container.removeClass("collapsing"),e.container.removeClass("expanded"),e.container.trigger("collapsed")},e.container.trigger("collapse"),e.container.addClass("collapsing"),e.params.is_wide?c.fadeOut("fast",d):c.slideUp("fast",function(){b.css({width:"",margin:""}),d()})))},focus:function(){this.expandControlSection(),this.expandForm(),this.container.find(".widget-content :focusable:first").focus()},getWidgetSidebarPosition:function(){var a,b;return a=this.getSidebarWidgetsControl().setting(),b=_.indexOf(a,this.params.widget_id),-1!==b?b:void 0},moveUp:function(){this._moveWidgetByOne(-1)},moveDown:function(){this._moveWidgetByOne(1)},_moveWidgetByOne:function(a){var b,c,d,e;b=this.getWidgetSidebarPosition(),c=this.getSidebarWidgetsControl().setting,d=Array.prototype.slice.call(c()),e=d[b+a],d[b+a]=this.params.widget_id,d[b]=e,c(d)},toggleWidgetMoveArea:function(a){var c,d=this;c=this.container.find(".move-widget-area"),"undefined"==typeof a&&(a=!c.hasClass("active")),a&&(c.find(".selected").removeClass("selected"),c.find("li").filter(function(){return b(this).data("id")===d.params.sidebar_id}).addClass("selected"),this.container.find(".move-widget-btn").prop("disabled",!0)),c.toggleClass("active",a)},highlightSectionAndControl:function(){var a;a=this.container.is(":hidden")?this.container.closest(".control-section"):this.container,b(".highlighted").removeClass("highlighted"),a.addClass("highlighted"),setTimeout(function(){a.removeClass("highlighted")},500)}}),g.Widgets.SidebarControl=g.Control.extend({ready:function(){this.$controlSection=this.container.closest(".control-section"),this.$sectionContent=this.container.closest(".accordion-section-content"),this._setupModel(),this._setupSortable(),this._setupAddition(),this._applyCardinalOrderClassNames()},_setupModel:function(){var a=this,d=g.Widgets.registeredSidebars.get(this.params.sidebar_id);this.setting.bind(function(d,e){var f,h,i,j;j=_(e).difference(d),d=_(d).filter(function(a){var b=c(a);return!!g.Widgets.availableWidgets.findWhere({id_base:b.id_base})}),f=_(d).map(function(b){var c=g.Widgets.getWidgetFormControlForWidget(b);return c||(c=a.addWidget(b)),c}),f.sort(function(a,b){var c=_.indexOf(d,a.params.widget_id),e=_.indexOf(d,b.params.widget_id);return c===e?0:e>c?-1:1}),i=_(f).map(function(a){return a.container[0]}),h=a.$sectionContent.find(".customize-control-sidebar_widgets"),h.before(i),a._applyCardinalOrderClassNames(),_(f).each(function(b){b.params.sidebar_id=a.params.sidebar_id}),_(j).each(function(d){setTimeout(function(){var e,f,h,i,j,k=!1;g.each(function(b){if(b.id!==a.setting.id&&0===b.id.indexOf("sidebars_widgets[")&&"sidebars_widgets[wp_inactive_widgets]"!==b.id){var c,e=b();c=_.indexOf(e,d),-1!==c&&(k=!0)}}),k||(e=g.Widgets.getWidgetFormControlForWidget(d),f=e&&b.contains(document,e.container[0])&&!b.contains(a.$sectionContent[0],e.container[0]),e&&!f&&(g.control.remove(e.id),e.container.remove()),g.Widgets.savedWidgetIds[d]&&(h=g.value("sidebars_widgets[wp_inactive_widgets]")().slice(),h.push(d),g.value("sidebars_widgets[wp_inactive_widgets]")(_(h).unique())),i=c(d).id_base,j=g.Widgets.availableWidgets.findWhere({id_base:i}),j&&!j.get("is_multi")&&j.set("is_disabled",!1))})})}),g.Widgets.Previewer.bind("rendered-sidebars",function(b){var c=!!b[a.params.sidebar_id];d.set("is_rendered",c)}),d.on("change:is_rendered",function(){var a,c="#accordion-section-sidebar-widgets-"+this.get("id");a=b(c),this.get("is_rendered")?a.stop().slideDown(function(){b(this).css("height","auto")}):(a.hasClass("open")&&a.find(".accordion-section-title").trigger("click"),a.stop().slideUp())})},_setupSortable:function(){var a=this;this.isReordering=!1,this.$sectionContent.sortable({items:"> .customize-control-widget_form",handle:".widget-top",axis:"y",connectWith:".accordion-section-content:has(.customize-control-sidebar_widgets)",update:function(){var c,d=a.$sectionContent.sortable("toArray");c=b.map(d,function(a){return b("#"+a).find(":input[name=widget-id]").val()}),a.setting(c)}}),this.$controlSection.find(".accordion-section-title").droppable({accept:".customize-control-widget_form",over:function(){a.$controlSection.hasClass("open")||(a.$controlSection.addClass("open"),a.$sectionContent.toggle(!1).slideToggle(150,function(){a.$sectionContent.sortable("refreshPositions")}))}}),this.container.find(".reorder-toggle").on("click keydown",function(b){("keydown"!==b.type||13===b.which||32===b.which)&&a.toggleReordering(!a.isReordering)})},_setupAddition:function(){var a=this;this.container.find(".add-new-widget").on("click keydown",function(c){("keydown"!==c.type||13===c.which||32===c.which)&&(a.$sectionContent.hasClass("reordering")||(b("body").hasClass("adding-widget")?g.Widgets.availableWidgetsPanel.close():g.Widgets.availableWidgetsPanel.open(a)))})},_applyCardinalOrderClassNames:function(){this.$sectionContent.find(".customize-control-widget_form").removeClass("first-widget").removeClass("last-widget").find(".move-widget-down, .move-widget-up").prop("tabIndex",0),this.$sectionContent.find(".customize-control-widget_form:first").addClass("first-widget").find(".move-widget-up").prop("tabIndex",-1),this.$sectionContent.find(".customize-control-widget_form:last").addClass("last-widget").find(".move-widget-down").prop("tabIndex",-1)},toggleReordering:function(a){a=Boolean(a),a!==this.$sectionContent.hasClass("reordering")&&(this.isReordering=a,this.$sectionContent.toggleClass("reordering",a),a&&_(this.getWidgetFormControls()).each(function(a){a.collapseForm()}))},getWidgetFormControls:function(){var a;return a=_(this.setting()).map(function(a){var b=d(a),c=g.control(b);if(c)return c})},addWidget:function(a){var d,e,f,h,i,j,k,l,m,n=this,o="widget_form",p=c(a),q=p.number,r=p.id_base,s=g.Widgets.availableWidgets.findWhere({id_base:r});return s?q&&!s.get("is_multi")?!1:(s.get("is_multi")&&!q&&(s.set("multi_number",s.get("multi_number")+1),q=s.get("multi_number")),d=b.trim(b("#widget-tpl-"+s.get("id")).html()),s.get("is_multi")?d=d.replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,q)}):s.set("is_disabled",!0),e=b(d),f=b("
  • ").addClass("customize-control").addClass("customize-control-"+o).append(e),f.find("> .widget-icon").remove(),s.get("is_multi")&&(f.find('input[name="widget_number"]').val(q),f.find('input[name="multi_number"]').val(q)),a=f.find('[name="widget-id"]').val(),f.hide(),i="widget_"+s.get("id_base"),s.get("is_multi")&&(i+="["+q+"]"),f.attr("id","customize-control-"+i.replace(/\]/g,"").replace(/\[/g,"-")),this.container.after(f),j=g.has(i),j||(m={transport:"refresh",previewer:this.setting.previewer},g.create(i,i,{},m)),h=g.controlConstructor[o],k=new h(i,{params:{settings:{"default":i},sidebar_id:n.params.sidebar_id,widget_id:a,widget_id_base:s.get("id_base"),type:o,is_new:!j,width:s.get("width"),height:s.get("height"),is_wide:s.get("is_wide")},previewer:n.setting.previewer}),g.control.add(i,k),g.each(function(b){if(b.id!==n.setting.id&&0===b.id.indexOf("sidebars_widgets[")){var c=b().slice(),d=_.indexOf(c,a);-1!==d&&(c.splice(d),b(c))}}),l=this.setting().slice(),-1===_.indexOf(l,a)&&(l.push(a),this.setting(l)),f.slideDown(function(){j?(k.expandForm(),k.updateWidget({instance:k.setting(),complete:function(a){if(a)throw a;k.focus()}})):k.focus()}),b(document).trigger("widget-added",[e]),k):!1}}),b.extend(g.controlConstructor,{widget_form:g.Widgets.WidgetControl,sidebar_widgets:g.Widgets.SidebarControl}),f=g.Previewer,g.Previewer=f.extend({initialize:function(a,b){g.Widgets.Previewer=this,f.prototype.initialize.call(this,a,b),this.bind("refresh",this.refresh)}}),g.bind("ready",function(){g.Widgets.availableWidgetsPanel=new g.Widgets.AvailableWidgetsPanelView({collection:g.Widgets.availableWidgets}),g.Widgets.Previewer.bind("highlight-widget-control",g.Widgets.highlightWidgetFormControl),g.Widgets.Previewer.bind("focus-widget-control",g.Widgets.focusWidgetFormControl)}),g.Widgets.highlightWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.highlightSectionAndControl()},g.Widgets.focusWidgetFormControl=function(a){var b=g.Widgets.getWidgetFormControlForWidget(a);b&&b.focus()},g.Widgets.getSidebarWidgetControlContainingWidget=function(a){var b=null;return g.control.each(function(c){"sidebar_widgets"===c.params.type&&-1!==_.indexOf(c.setting(),a)&&(b=c)}),b},g.Widgets.getWidgetFormControlForWidget=function(a){var b=null;return g.control.each(function(c){"widget_form"===c.params.type&&c.params.widget_id===a&&(b=c)}),b}}}(window.wp,jQuery); \ No newline at end of file diff --git a/wp-includes/class-wp-customize-manager.php b/wp-includes/class-wp-customize-manager.php index 18a1d761c8..8ed4e699a5 100644 --- a/wp-includes/class-wp-customize-manager.php +++ b/wp-includes/class-wp-customize-manager.php @@ -575,6 +575,7 @@ final class WP_Customize_Manager { // to operate properly. $this->stop_previewing_theme(); switch_theme( $this->get_stylesheet() ); + update_option( 'theme_switched_via_customizer', true ); $this->start_previewing_theme(); } diff --git a/wp-includes/class-wp-customize-widgets.php b/wp-includes/class-wp-customize-widgets.php index 5416590b9a..049ceddbfb 100644 --- a/wp-includes/class-wp-customize-widgets.php +++ b/wp-includes/class-wp-customize-widgets.php @@ -60,6 +60,13 @@ final class WP_Customize_Widgets { */ protected $rendered_widgets = array(); + /** + * @since 3.9.0 + * @access protected + * @var array + */ + protected $old_sidebars_widgets = array(); + /** * Initial loader. * @@ -72,6 +79,7 @@ final class WP_Customize_Widgets { $this->manager = $manager; add_action( 'after_setup_theme', array( $this, 'setup_widget_addition_previews' ) ); + add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) ); add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) ); add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 ); add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); @@ -115,7 +123,6 @@ final class WP_Customize_Widgets { * @since 3.9.0 * * @access public - * @global WP_Customize_Manager $wp_customize Customizer instance. */ public function setup_widget_addition_previews() { $is_customize_preview = false; @@ -125,12 +132,12 @@ final class WP_Customize_Widgets { } $is_ajax_widget_update = false; - if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'update-widget' === $this->get_post_value( 'action' ) ) { + if ( $this->manager->doing_ajax() && 'update-widget' === $this->get_post_value( 'action' ) ) { $is_ajax_widget_update = check_ajax_referer( 'update-widget', 'nonce', false ); } $is_ajax_customize_save = false; - if ( defined( 'DOING_AJAX' ) && DOING_AJAX && 'customize_save' === $this->get_post_value( 'action' ) ) { + if ( $this->manager->doing_ajax() && 'customize_save' === $this->get_post_value( 'action' ) ) { $is_ajax_customize_save = check_ajax_referer( 'save-customize_' . $this->manager->get_stylesheet(), 'nonce', false ); } @@ -278,6 +285,72 @@ final class WP_Customize_Widgets { $this->_prepreview_added_filters = array(); } + /** + * 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; + } + /** * Make sure all widgets get loaded into the Customizer. * @@ -349,6 +422,18 @@ final class WP_Customize_Widgets { $new_setting_ids[] = $setting_id; } + /* + * 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 ); + } + foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) { if ( empty( $sidebar_widget_ids ) ) { $sidebar_widget_ids = array(); diff --git a/wp-includes/theme.php b/wp-includes/theme.php index f66fd95215..00b3c39d1f 100644 --- a/wp-includes/theme.php +++ b/wp-includes/theme.php @@ -752,10 +752,18 @@ function preview_theme_ob_filter_callback( $matches ) { * @param string $stylesheet Stylesheet name */ function switch_theme( $stylesheet ) { - global $wp_theme_directories, $sidebars_widgets; + global $wp_theme_directories, $wp_customize, $sidebars_widgets; - if ( is_array( $sidebars_widgets ) ) - set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) ); + $_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; + } + + if ( is_array( $_sidebars_widgets ) ) { + set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) ); + } $old_theme = wp_get_theme(); $new_theme = wp_get_theme( $stylesheet ); @@ -782,9 +790,19 @@ function switch_theme( $stylesheet ) { update_option( 'current_theme', $new_name ); + // Migrate from the old mods_{name} option to theme_mods_{slug}. if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) { $default_theme_mods = (array) get_option( 'mods_' . $new_name ); add_option( "theme_mods_$stylesheet", $default_theme_mods ); + } 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' ); + } } update_option( 'theme_switched', $old_theme->get_stylesheet() ); @@ -1791,6 +1809,12 @@ function check_theme_switched() { if ( $stylesheet = get_option( 'theme_switched' ) ) { $old_theme = wp_get_theme( $stylesheet ); + // 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 ); + } + if ( $old_theme->exists() ) { /** * Fires on the first WP load after a theme switch if the old theme still exists. @@ -1918,4 +1942,4 @@ function wp_customize_support_script() { }()); $widgets ) { if ( 'wp_inactive_widgets' == $sidebar || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) @@ -1495,7 +1506,9 @@ function retrieve_widgets($theme_changed = false) { } $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']); - wp_set_sidebars_widgets($sidebars_widgets); + if ( 'customize' === $theme_changed ) { + wp_set_sidebars_widgets( $sidebars_widgets ); + } return $sidebars_widgets; }