#37270 closed defect (bug) (fixed)
Improve handling of active state for dynamically-created controls/sections/panels
Reported by: | westonruter | Owned by: | westonruter |
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | 4.0 |
Component: | Customize | Keywords: | has-patch |
Focuses: | javascript | Cc: |
Description (last modified by )
When a control, section, or panel is dynamically created in JS and doesn't have a corresponding construct that gets created in PHP, then when the preview refreshes the construct won't be included among the activeControls
, activeSections
, or activePanels
and so it will get its active
state set to false
. A workaround as used for nav menu items is to define control.active.validate
to override whatever is being sent from the preview:
control.active.validate = function() { var value, section = api.section( control.section() ); if ( section ) { value = section.active(); } else { value = false; } return value; };
The problematic code in Core is:
var active = !! ( activeConstructs && activeConstructs[ id ] );
This is not ideal, and it means that a plugin cannot programmatically do control.active.set( false )
.
As suggested in an issue on Customize Posts, we could improve the situation by discontinuing from considering constructs that are absent in the preview when determining whether to change the active
state, which would specifically be for any constructs that are dynamically created:
var active, isDynamicallyCreated; if ( ! _.isUndefined( activeConstructs[ id ] ) { active = _.isUndefined( activeConstructs[ id ]; } else { isDynamicallyCreated = ! _.isUndefined( wp.customize.settings[ type + 's' ][ id ] ); active = isDynamicallyCreated ? null : false; } if ( _.isBoolean( active ) ) { ...
Contextual controls were introduced in #27993 with contextual sections/panels in #29758.
Attachments (2)
Change History (7)
#2
@
8 years ago
@westonruter Can we do it more simply like this
var isDynamicallyCreated = _.isUndefined( wp.customize.settings[ type + 's' ][ id ] ), active = ! _.isUndefined( activeConstructs[ id ] ); if ( ! isDynamicallyCreated ) { if ( active ) { construct.activate(); } else { construct.deactivate(); } }
#3
@
8 years ago
- Keywords has-patch added; needs-patch removed
- Milestone changed from Future Release to 4.7
- Owner set to westonruter
- Status changed from new to accepted
@sayedwp that does look good!
#5
@
8 years ago
Example code which can be eliminated once this is released: https://github.com/xwp/wp-customize-posts/pull/235/files
Prevents dynamically created constructs from getting deactivated.