WordPress.org

Make WordPress Core

Changeset 30214


Ignore:
Timestamp:
11/03/2014 09:34:44 PM (6 years ago)
Author:
ocean90
Message:

Customizer: Add stable sorting for panels, sections and controls in JS. Improve sorting in PHP.

props westonruter.
fixes #30225.

Location:
trunk/src
Files:
5 edited

Legend:

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

    r30126 r30214  
    11/* globals _wpCustomizeHeader, _wpMediaViewsL10n */
    22(function( exports, $ ){
    3     var bubbleChildValueChanges, Container, focus, isKeydownButNotEnterEvent, areElementListsEqual, api = wp.customize;
     3    var bubbleChildValueChanges, Container, focus, isKeydownButNotEnterEvent, areElementListsEqual, prioritySort, api = wp.customize;
    44
    55    // @todo Move private helper functions to wp.customize.utils so they can be unit tested
     
    7979
    8080    /**
     81     * Stable sort for Panels, Sections, and Controls.
     82     *
     83     * If a.priority() === b.priority(), then sort by their respective params.instanceNumber.
     84     *
     85     * @param {(wp.customize.Panel|wp.customize.Section|wp.customize.Control)} a
     86     * @param {(wp.customize.Panel|wp.customize.Section|wp.customize.Control)} b
     87     * @returns {Number}
     88     */
     89    prioritySort = function ( a, b ) {
     90        if ( a.priority() === b.priority() && typeof a.params.instanceNumber === 'number' && typeof b.params.instanceNumber === 'number' ) {
     91            return a.params.instanceNumber - b.params.instanceNumber;
     92        } else {
     93            return a.priority() - b.priority();
     94        }
     95    };
     96
     97    /**
    8198     * Return whether the supplied Event object is for a keydown event but not the Enter key.
    8299     *
     
    177194                }
    178195            } );
    179             children.sort( function ( a, b ) {
    180                 return a.priority() - b.priority();
    181             } );
     196            children.sort( prioritySort );
    182197            return children;
    183198        },
     
    19531968
    19541969            // Sort the root panels and sections
    1955             rootNodes.sort( function ( a, b ) {
    1956                 return a.priority() - b.priority();
    1957             } );
     1970            rootNodes.sort( prioritySort );
    19581971            rootContainers = _.pluck( rootNodes, 'container' );
    19591972            appendContainer = $( '#customize-theme-controls' ).children( 'ul' ); // @todo This should be defined elsewhere, and to be configurable
  • trunk/src/wp-includes/class-wp-customize-control.php

    r30172 r30214  
    88 */
    99class WP_Customize_Control {
     10
     11    /**
     12     * Incremented with each new class instantiation, then stored in $instance_number.
     13     *
     14     * Used when sorting two instances whose priorities are equal.
     15     *
     16     * @since 4.1.0
     17     * @access protected
     18     * @var int
     19     */
     20    protected static $instance_count = 0;
     21
     22    /**
     23     * Order in which this instance was created in relation to other instances.
     24     *
     25     * @since 4.1.0
     26     * @access public
     27     * @var int
     28     */
     29    public $instance_number;
     30
    1031    /**
    1132     * @access public
     
    128149            $this->active_callback = array( $this, 'active_callback' );
    129150        }
     151        self::$instance_count += 1;
     152        $this->instance_number = self::$instance_count;
    130153
    131154        // Process settings.
     
    219242        }
    220243
    221         $this->json['type']        = $this->type;
    222         $this->json['priority']    = $this->priority;
    223         $this->json['active']      = $this->active();
    224         $this->json['section']     = $this->section;
    225         $this->json['content']     = $this->get_content();
    226         $this->json['label']       = $this->label;
     244        $this->json['type'] = $this->type;
     245        $this->json['priority'] = $this->priority;
     246        $this->json['active'] = $this->active();
     247        $this->json['section'] = $this->section;
     248        $this->json['content'] = $this->get_content();
     249        $this->json['label'] = $this->label;
    227250        $this->json['description'] = $this->description;
     251        $this->json['instanceNumber'] = $this->instance_number;
    228252    }
    229253
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r30105 r30214  
    857857     */
    858858    public function render_control_templates() {
    859         foreach( $this->registered_control_types as $control_type ) {
     859        foreach ( $this->registered_control_types as $control_type ) {
    860860            $control = new $control_type( $this, 'temp', array() );
    861861            $control->print_template();
     
    863863    }
    864864
    865     /**
    866      * Helper function to compare two objects by priority.
    867      *
    868      * @since 3.4.0
    869      *
    870      * @param object $a Object A.
    871      * @param object $b Object B.
     865    /**
     866     * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
     867     *
     868     * @since 3.4.0
     869     *
     870     * @param {WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control} $a Object A.
     871     * @param {WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control} $b Object B.
    872872     * @return int
    873873     */
    874874    protected final function _cmp_priority( $a, $b ) {
    875         $ap = $a->priority;
    876         $bp = $b->priority;
    877 
    878         if ( $ap == $bp )
    879             return 0;
    880         return ( $ap > $bp ) ? 1 : -1;
     875        if ( $a->priority === $b->priority ) {
     876            return $a->instance_number - $a->instance_number;
     877        } else {
     878            return $a->priority - $b->priority;
     879        }
    881880    }
    882881
     
    892891    public function prepare_controls() {
    893892
    894         $this->controls = array_reverse( $this->controls );
    895893        $controls = array();
     894        uasort( $this->controls, array( $this, '_cmp_priority' ) );
    896895
    897896        foreach ( $this->controls as $id => $control ) {
     
    906905
    907906        // Prepare sections.
    908         // Reversing makes uasort sort by time added when conflicts occur.
    909         $this->sections = array_reverse( $this->sections );
    910907        uasort( $this->sections, array( $this, '_cmp_priority' ) );
    911908        $sections = array();
     
    931928
    932929        // Prepare panels.
    933         // Reversing makes uasort sort by time added when conflicts occur.
    934         $this->panels = array_reverse( $this->panels );
    935930        uasort( $this->panels, array( $this, '_cmp_priority' ) );
    936931        $panels = array();
  • trunk/src/wp-includes/class-wp-customize-panel.php

    r30102 r30214  
    1212
    1313    /**
     14     * Incremented with each new class instantiation, then stored in $instance_number.
     15     *
     16     * Used when sorting two instances whose priorities are equal.
     17     *
     18     * @since 4.1.0
     19     * @access protected
     20     * @var int
     21     */
     22    protected static $instance_count = 0;
     23
     24    /**
     25     * Order in which this instance was created in relation to other instances.
     26     *
     27     * @since 4.1.0
     28     * @access public
     29     * @var int
     30     */
     31    public $instance_number;
     32
     33    /**
    1434     * WP_Customize_Manager instance.
    1535     *
     
    129149            $this->active_callback = array( $this, 'active_callback' );
    130150        }
     151        self::$instance_count += 1;
     152        $this->instance_number = self::$instance_count;
    131153
    132154        $this->sections = array(); // Users cannot customize the $sections array.
     
    186208        $array['content'] = $this->get_content();
    187209        $array['active'] = $this->active();
     210        $array['instanceNumber'] = $this->instance_number;
    188211        return $array;
    189212    }
  • trunk/src/wp-includes/class-wp-customize-section.php

    r30102 r30214  
    1212
    1313    /**
     14     * Incremented with each new class instantiation, then stored in $instance_number.
     15     *
     16     * Used when sorting two instances whose priorities are equal.
     17     *
     18     * @since 4.1.0
     19     * @access protected
     20     * @var int
     21     */
     22    protected static $instance_count = 0;
     23
     24    /**
     25     * Order in which this instance was created in relation to other instances.
     26     *
     27     * @since 4.1.0
     28     * @access public
     29     * @var int
     30     */
     31    public $instance_number;
     32
     33    /**
    1434     * WP_Customize_Manager instance.
    1535     *
     
    138158            $this->active_callback = array( $this, 'active_callback' );
    139159        }
     160        self::$instance_count += 1;
     161        $this->instance_number = self::$instance_count;
    140162
    141163        $this->controls = array(); // Users cannot customize the $controls array.
     
    195217        $array['content'] = $this->get_content();
    196218        $array['active'] = $this->active();
     219        $array['instanceNumber'] = $this->instance_number;
    197220        return $array;
    198221    }
Note: See TracChangeset for help on using the changeset viewer.