Make WordPress Core

Changeset 30329


Ignore:
Timestamp:
11/13/2014 12:18:01 PM (12 years ago)
Author:
ocean90
Message:

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.

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/customize-controls.js

    r30309 r30329  
    13491349                                }
    13501350
     1351                                /*
     1352                                 * Walk over all panels, sections, and controls and set their
     1353                                 * respective active states to true if the preview explicitly
     1354                                 * indicates as such.
     1355                                 */
    13511356                                var constructs = {
    13521357                                        panel: data.activePanels,
    … …  
    13541359                                        control: data.activeControls
    13551360                                };
    1356 
    1357                                 $.each( constructs, function ( type, activeConstructs ) {
    1358                                         if ( activeConstructs ) {
    1359                                                 $.each( activeConstructs, function ( id, active ) {
    1360                                                         var construct = api[ type ]( id );
    1361                                                         if ( construct ) {
    1362                                                                 construct.active( active );
    1363                                                         }
    1364                                                 } );
    1365                                         }
     1361                                _( constructs ).each( function ( activeConstructs, type ) {
     1362                                        api[ type ].each( function ( construct, id ) {
     1363                                                var active = !! ( activeConstructs && activeConstructs[ id ] );
     1364                                                construct.active( active );
     1365                                        } );
    13661366                                } );
    1367 
    13681367                        } );
    13691368
  • trunk/src/wp-admin/js/customize-widgets.js

    r30308 r30329  
    13691369
    13701370        /**
     1371         * wp.customize.Widgets.SidebarSection
     1372         *
     1373         * Customizer section representing a widget area widget
     1374         *
     1375         * @since 4.1.0
     1376         */
     1377        api.Widgets.SidebarSection = api.Section.extend({
     1378
     1379                /**
     1380                 * Sync the section's active state back to the Backbone model's is_rendered attribute
     1381                 */
     1382                ready: function () {
     1383                        var section = this, registeredSidebar;
     1384                        api.Section.prototype.ready.call( this );
     1385                        registeredSidebar = api.Widgets.registeredSidebars.get( section.params.sidebarId );
     1386                        section.active.bind( function ( active ) {
     1387                                registeredSidebar.set( 'is_rendered', active );
     1388                        });
     1389                        registeredSidebar.set( 'is_rendered', section.active() );
     1390                },
     1391
     1392                /**
     1393                 * Override Section.isContextuallyActive() to skip considering
     1394                 * SidebarControl  as opposed to a WidgetControl.
     1395                 *
     1396                 * @returns {boolean}
     1397                 */
     1398                isContextuallyActive: function () {
     1399                        var section, activeCount;
     1400                        section = this;
     1401                        activeCount = 0;
     1402                        _( section.controls() ).each( function ( control ) {
     1403                                if ( control.active() && ! control.extended( api.Widgets.SidebarControl ) ) {
     1404                                        activeCount += 1;
     1405                                }
     1406                        });
     1407                        return ( activeCount !== 0 );
     1408                }
     1409        });
     1410
     1411        /**
    13711412         * wp.customize.Widgets.SidebarControl
    13721413         *
    … …  
    13741415         * Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type
    13751416         *
     1417         * @since 3.9.0
     1418         *
    13761419         * @constructor
    13771420         * @augments wp.customize.Control
    … …  
    13961439                 */
    13971440                _setupModel: function() {
    1398                         var self = this,
    1399                                 registeredSidebar = api.Widgets.registeredSidebars.get( this.params.sidebar_id );
     1441                        var self = this;
    14001442
    14011443                        this.setting.bind( function( newWidgetIds, oldWidgetIds ) {
    … …  
    15001542                                } );
    15011543                        } );
    1502 
    1503                         // Update the model with whether or not the sidebar is rendered
    1504                         self.active.bind( function ( active ) {
    1505                                 registeredSidebar.set( 'is_rendered', active );
    1506                                 api.section( self.section.get() ).active( active );
    1507                         } );
    1508                         api.section( self.section.get() ).active( self.active() );
    15091544                },
    15101545
    … …  
    18171852        } );
    18181853
    1819         /**
    1820          * Extends wp.customizer.controlConstructor with control constructor for
    1821          * widget_form and sidebar_widgets.
    1822          */
     1854        // Register models for custom section and control types
     1855        $.extend( api.sectionConstructor, {
     1856                sidebar: api.Widgets.SidebarSection
     1857        });
    18231858        $.extend( api.controlConstructor, {
    18241859                widget_form: api.Widgets.WidgetControl,
  • trunk/src/wp-includes/class-wp-customize-control.php

    r30326 r30329  
    11161116        }
    11171117
    1118         /**
    1119          * Whether the current sidebar is rendered on the page.
    1120          *
    1121          * @since 4.0.0
    1122          * @access public
    1123          *
    1124          * @return bool Whether sidebar is rendered.
    1125          */
    1126         public function active_callback() {
    1127                 return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
    1128         }
    11291118}
    11301119
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r30309 r30329  
    516516                foreach ( $this->panels as $id => $panel ) {
    517517                        $settings['activePanels'][ $id ] = $panel->active();
     518                        foreach ( $panel->sections as $id => $section ) {
     519                                $settings['activeSections'][ $id ] = $section->active();
     520                        }
    518521                }
    519522                foreach ( $this->sections as $id => $section ) {
  • trunk/src/wp-includes/class-wp-customize-section.php

    r30214 r30329  
    311311        }
    312312}
     313
     314/**
     315 * Customizer section representing widget area (sidebar).
     316 *
     317 * @package WordPress
     318 * @subpackage Customize
     319 * @since 4.1.0
     320 */
     321class WP_Customize_Sidebar_Section extends WP_Customize_Section {
     322
     323        /**
     324         * @since 4.1.0
     325         * @access public
     326         * @var string
     327         */
     328        public $type = 'sidebar';
     329
     330        /**
     331         * Unique identifier.
     332         *
     333         * @since 4.1.0
     334         * @access public
     335         * @var string
     336         */
     337        public $sidebar_id;
     338
     339        /**
     340         * Gather the parameters passed to client JavaScript via JSON.
     341         *
     342         * @since 4.1.0
     343         *
     344         * @return array The array to be exported to the client as JSON
     345         */
     346        public function json() {
     347                $json = parent::json();
     348                $json['sidebarId'] = $this->sidebar_id;
     349                return $json;
     350        }
     351
     352        /**
     353         * Whether the current sidebar is rendered on the page.
     354         *
     355         * @since 4.1.0
     356         * @access public
     357         *
     358         * @return bool Whether sidebar is rendered.
     359         */
     360        public function active_callback() {
     361                return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
     362        }
     363}
  • trunk/src/wp-includes/class-wp-customize-widgets.php

    r30055 r30329  
    469469                                                'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ) ),
    470470                                                'panel' => 'widgets',
     471                                                'sidebar_id' => $sidebar_id,
    471472                                        );
    472473
    … …  
    482483                                        $section_args = apply_filters( 'customizer_widgets_section_args', $section_args, $section_id, $sidebar_id );
    483484
    484                                         $this->manager->add_section( $section_id, $section_args );
     485                                        $section = new WP_Customize_Sidebar_Section( $this->manager, $section_id, $section_args );
     486                                        $this->manager->add_section( $section );
    485487
    486488                                        $control = new WP_Widget_Area_Customize_Control( $this->manager, $setting_id, array(
Note: See TracChangeset for help on using the changeset viewer.