WordPress.org

Make WordPress Core

Changeset 38464


Ignore:
Timestamp:
08/31/2016 06:20:33 AM (3 years ago)
Author:
westonruter
Message:

Customize: Improve handling of active state for dynamically-created controls/sections/panels.

When a customizer construct (panel, section, control) is not added in PHP, the JS has interpreted this to mean that a given construct should be deactivated (because it is gone). This is problematic for dynamically-created constructs in JS, as it has meant that the construct would also have to be created in PHP to ensure the active callback is called, or else a hack would be required to add a construct.active.validate = function() { return true }; to forcibly prevent the construct from getting deactivated.

These workarounds can be eliminated by treating constructs differently when they are created dynamically in JS (after page load) as opposed to being created statically in PHP (on the server). Namely, if a construct is dynamically-created then its absence in a preview refresh should not signal that the construct should be deactivated. Rather, a dynamic construct should only have its activation state toggled if it has a corresponding construct created in PHP when the preview refreshes to explicitly indicate its active state. Otherwise, the management of the active state for a construct created in JS should also be the responsibility of client-side code.

Props westonruter, sayedwp.
Fixes #37270.

File:
1 edited

Legend:

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

    r38396 r38464  
    29672967                _( constructs ).each( function ( activeConstructs, type ) {
    29682968                    api[ type ].each( function ( construct, id ) {
    2969                         var active = !! ( activeConstructs && activeConstructs[ id ] );
    2970                         if ( active ) {
    2971                             construct.activate();
    2972                         } else {
    2973                             construct.deactivate();
     2969                        var isDynamicallyCreated = _.isUndefined( api.settings[ type + 's' ][ id ] );
     2970
     2971                        /*
     2972                         * If the construct was created statically in PHP (not dynamically in JS)
     2973                         * then consider a missing (undefined) value in the activeConstructs to
     2974                         * mean it should be deactivated (since it is gone). But if it is
     2975                         * dynamically created then only toggle activation if the value is defined,
     2976                         * as this means that the construct was also then correspondingly
     2977                         * created statically in PHP and the active callback is available.
     2978                         * Otherwise, dynamically-created constructs should normally have
     2979                         * their active states toggled in JS rather than from PHP.
     2980                         */
     2981                        if ( ! isDynamicallyCreated || ! _.isUndefined( activeConstructs[ id ] ) ) {
     2982                            if ( activeConstructs[ id ] ) {
     2983                                construct.activate();
     2984                            } else {
     2985                                construct.deactivate();
     2986                            }
    29742987                        }
    29752988                    } );
Note: See TracChangeset for help on using the changeset viewer.