Customizer: Improve ability to filter active state for widget area Customizer sections.

* Mark panels, sections, controls as active if preview explicitly indicates.
* Introduce `WP_Customize_Sidebar_Section` PHP class, and `SidebarSection` JS class.
* Move logic for determining whether a sidebar section is active from the `SidebarControl` to `SidebarSection`.

props westonruter.
fixes #30235.
Built from https://develop.svn.wordpress.org/trunk@30329


git-svn-id: http://core.svn.wordpress.org/trunk@30328 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2014-11-13 12:19:23 +00:00
parent 92e3890daa
commit 41197a7c31
9 changed files with 118 additions and 39 deletions

View File

@ -1348,23 +1348,22 @@
return; 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 = { var constructs = {
panel: data.activePanels, panel: data.activePanels,
section: data.activeSections, section: data.activeSections,
control: data.activeControls control: data.activeControls
}; };
_( constructs ).each( function ( activeConstructs, type ) {
$.each( constructs, function ( type, activeConstructs ) { api[ type ].each( function ( construct, id ) {
if ( activeConstructs ) { var active = !! ( activeConstructs && activeConstructs[ id ] );
$.each( activeConstructs, function ( id, active ) { construct.active( active );
var construct = api[ type ]( id ); } );
if ( construct ) {
construct.active( active );
}
} );
}
} ); } );
} ); } );
this.request = $.ajax( this.previewUrl(), { this.request = $.ajax( this.previewUrl(), {

File diff suppressed because one or more lines are too long

View File

@ -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 * wp.customize.Widgets.SidebarControl
* *
* Customizer control for widgets. * Customizer control for widgets.
* Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type * Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type
* *
* @since 3.9.0
*
* @constructor * @constructor
* @augments wp.customize.Control * @augments wp.customize.Control
*/ */
@ -1395,8 +1438,7 @@
* Update ordering of widget control forms when the setting is updated * Update ordering of widget control forms when the setting is updated
*/ */
_setupModel: function() { _setupModel: function() {
var self = this, var self = this;
registeredSidebar = api.Widgets.registeredSidebars.get( this.params.sidebar_id );
this.setting.bind( function( newWidgetIds, oldWidgetIds ) { this.setting.bind( function( newWidgetIds, oldWidgetIds ) {
var widgetFormControls, removedWidgetIds, priority; 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 @@
} }
} ); } );
/** // Register models for custom section and control types
* Extends wp.customizer.controlConstructor with control constructor for $.extend( api.sectionConstructor, {
* widget_form and sidebar_widgets. sidebar: api.Widgets.SidebarSection
*/ });
$.extend( api.controlConstructor, { $.extend( api.controlConstructor, {
widget_form: api.Widgets.WidgetControl, widget_form: api.Widgets.WidgetControl,
sidebar_widgets: api.Widgets.SidebarControl sidebar_widgets: api.Widgets.SidebarControl

File diff suppressed because one or more lines are too long

View File

@ -1115,17 +1115,6 @@ class WP_Widget_Area_Customize_Control extends WP_Customize_Control {
<?php <?php
} }
/**
* Whether the current sidebar is rendered on the page.
*
* @since 4.0.0
* @access public
*
* @return bool Whether sidebar is rendered.
*/
public function active_callback() {
return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
}
} }
/** /**

View File

@ -515,6 +515,9 @@ final class WP_Customize_Manager {
} }
foreach ( $this->panels as $id => $panel ) { foreach ( $this->panels as $id => $panel ) {
$settings['activePanels'][ $id ] = $panel->active(); $settings['activePanels'][ $id ] = $panel->active();
foreach ( $panel->sections as $id => $section ) {
$settings['activeSections'][ $id ] = $section->active();
}
} }
foreach ( $this->sections as $id => $section ) { foreach ( $this->sections as $id => $section ) {
$settings['activeSections'][ $id ] = $section->active(); $settings['activeSections'][ $id ] = $section->active();

View File

@ -310,3 +310,54 @@ class WP_Customize_Section {
<?php <?php
} }
} }
/**
* Customizer section representing widget area (sidebar).
*
* @package WordPress
* @subpackage Customize
* @since 4.1.0
*/
class WP_Customize_Sidebar_Section extends WP_Customize_Section {
/**
* @since 4.1.0
* @access public
* @var string
*/
public $type = 'sidebar';
/**
* Unique identifier.
*
* @since 4.1.0
* @access public
* @var string
*/
public $sidebar_id;
/**
* Gather the parameters passed to client JavaScript via JSON.
*
* @since 4.1.0
*
* @return array The array to be exported to the client as JSON
*/
public function json() {
$json = parent::json();
$json['sidebarId'] = $this->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 );
}
}

View File

@ -468,6 +468,7 @@ final class WP_Customize_Widgets {
'description' => $GLOBALS['wp_registered_sidebars'][ $sidebar_id ]['description'], 'description' => $GLOBALS['wp_registered_sidebars'][ $sidebar_id ]['description'],
'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ) ), 'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ) ),
'panel' => 'widgets', '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 ); $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( $control = new WP_Widget_Area_Customize_Control( $this->manager, $setting_id, array(
'section' => $section_id, 'section' => $section_id,

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.1-alpha-30328'; $wp_version = '4.1-alpha-30329';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.