Make WordPress Core


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

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

props westonruter.
fixes #30225.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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();
Note: See TracChangeset for help on using the changeset viewer.