diff --git a/src/wp-admin/js/customize-controls.js b/src/wp-admin/js/customize-controls.js index ae3818760e..7b9f7b1b31 100644 --- a/src/wp-admin/js/customize-controls.js +++ b/src/wp-admin/js/customize-controls.js @@ -1348,23 +1348,22 @@ return; } + /* + * Walk over all panels, sections, and controls and set their + * respective active states to true if the preview explicitly + * indicates as such. + */ var constructs = { panel: data.activePanels, section: data.activeSections, control: data.activeControls }; - - $.each( constructs, function ( type, activeConstructs ) { - if ( activeConstructs ) { - $.each( activeConstructs, function ( id, active ) { - var construct = api[ type ]( id ); - if ( construct ) { - construct.active( active ); - } - } ); - } + _( constructs ).each( function ( activeConstructs, type ) { + api[ type ].each( function ( construct, id ) { + var active = !! ( activeConstructs && activeConstructs[ id ] ); + construct.active( active ); + } ); } ); - } ); this.request = $.ajax( this.previewUrl(), { diff --git a/src/wp-admin/js/customize-widgets.js b/src/wp-admin/js/customize-widgets.js index 0c7c0fe985..fb28d30d51 100644 --- a/src/wp-admin/js/customize-widgets.js +++ b/src/wp-admin/js/customize-widgets.js @@ -1367,12 +1367,55 @@ } } ); + /** + * wp.customize.Widgets.SidebarSection + * + * Customizer section representing a widget area widget + * + * @since 4.1.0 + */ + api.Widgets.SidebarSection = api.Section.extend({ + + /** + * Sync the section's active state back to the Backbone model's is_rendered attribute + */ + ready: function () { + var section = this, registeredSidebar; + api.Section.prototype.ready.call( this ); + registeredSidebar = api.Widgets.registeredSidebars.get( section.params.sidebarId ); + section.active.bind( function ( active ) { + registeredSidebar.set( 'is_rendered', active ); + }); + registeredSidebar.set( 'is_rendered', section.active() ); + }, + + /** + * Override Section.isContextuallyActive() to skip considering + * SidebarControl as opposed to a WidgetControl. + * + * @returns {boolean} + */ + isContextuallyActive: function () { + var section, activeCount; + section = this; + activeCount = 0; + _( section.controls() ).each( function ( control ) { + if ( control.active() && ! control.extended( api.Widgets.SidebarControl ) ) { + activeCount += 1; + } + }); + return ( activeCount !== 0 ); + } + }); + /** * wp.customize.Widgets.SidebarControl * * Customizer control for widgets. * Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type * + * @since 3.9.0 + * * @constructor * @augments wp.customize.Control */ @@ -1395,8 +1438,7 @@ * Update ordering of widget control forms when the setting is updated */ _setupModel: function() { - var self = this, - registeredSidebar = api.Widgets.registeredSidebars.get( this.params.sidebar_id ); + var self = this; this.setting.bind( function( newWidgetIds, oldWidgetIds ) { var widgetFormControls, removedWidgetIds, priority; @@ -1499,13 +1541,6 @@ } ); } ); - - // Update the model with whether or not the sidebar is rendered - self.active.bind( function ( active ) { - registeredSidebar.set( 'is_rendered', active ); - api.section( self.section.get() ).active( active ); - } ); - api.section( self.section.get() ).active( self.active() ); }, /** @@ -1816,10 +1851,10 @@ } } ); - /** - * Extends wp.customizer.controlConstructor with control constructor for - * widget_form and sidebar_widgets. - */ + // Register models for custom section and control types + $.extend( api.sectionConstructor, { + sidebar: api.Widgets.SidebarSection + }); $.extend( api.controlConstructor, { widget_form: api.Widgets.WidgetControl, sidebar_widgets: api.Widgets.SidebarControl diff --git a/src/wp-includes/class-wp-customize-control.php b/src/wp-includes/class-wp-customize-control.php index f496f009fb..2c49cc5704 100644 --- a/src/wp-includes/class-wp-customize-control.php +++ b/src/wp-includes/class-wp-customize-control.php @@ -1115,17 +1115,6 @@ class WP_Widget_Area_Customize_Control extends WP_Customize_Control { manager->widgets->is_sidebar_rendered( $this->sidebar_id ); - } } /** diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 45ad34f936..c3a8273183 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -515,6 +515,9 @@ final class WP_Customize_Manager { } foreach ( $this->panels as $id => $panel ) { $settings['activePanels'][ $id ] = $panel->active(); + foreach ( $panel->sections as $id => $section ) { + $settings['activeSections'][ $id ] = $section->active(); + } } foreach ( $this->sections as $id => $section ) { $settings['activeSections'][ $id ] = $section->active(); diff --git a/src/wp-includes/class-wp-customize-section.php b/src/wp-includes/class-wp-customize-section.php index 86c565d56c..97e1f89272 100644 --- a/src/wp-includes/class-wp-customize-section.php +++ b/src/wp-includes/class-wp-customize-section.php @@ -310,3 +310,54 @@ class WP_Customize_Section { sidebar_id; + return $json; + } + + /** + * Whether the current sidebar is rendered on the page. + * + * @since 4.1.0 + * @access public + * + * @return bool Whether sidebar is rendered. + */ + public function active_callback() { + return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id ); + } +} diff --git a/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php index fe5afd964c..04bcce64fd 100644 --- a/src/wp-includes/class-wp-customize-widgets.php +++ b/src/wp-includes/class-wp-customize-widgets.php @@ -468,6 +468,7 @@ final class WP_Customize_Widgets { 'description' => $GLOBALS['wp_registered_sidebars'][ $sidebar_id ]['description'], 'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ) ), 'panel' => 'widgets', + 'sidebar_id' => $sidebar_id, ); /** @@ -481,7 +482,8 @@ final class WP_Customize_Widgets { */ $section_args = apply_filters( 'customizer_widgets_section_args', $section_args, $section_id, $sidebar_id ); - $this->manager->add_section( $section_id, $section_args ); + $section = new WP_Customize_Sidebar_Section( $this->manager, $section_id, $section_args ); + $this->manager->add_section( $section ); $control = new WP_Widget_Area_Customize_Control( $this->manager, $setting_id, array( 'section' => $section_id,