WordPress.org

Make WordPress Core

Ticket #30225: 30225.diff

File 30225.diff, 9.2 KB (added by westonruter, 6 years ago)

https://github.com/xwpco/wordpress-develop/pull/49

  • src/wp-admin/js/customize-controls.js

    diff --git src/wp-admin/js/customize-controls.js src/wp-admin/js/customize-controls.js
    index 0ea60ce..851eaf3 100644
     
    11/* globals _wpCustomizeHeader, _wpMediaViewsL10n */
    22(function( exports, $ ){
    3         var bubbleChildValueChanges, Container, focus, isKeydownButNotEnterEvent, areElementListsEqual, api = wp.customize;
     3        var bubbleChildValueChanges, Container, focus, isKeydownButNotEnterEvent, areElementListsEqual, stableSort, api = wp.customize;
    44
    55        // @todo Move private helper functions to wp.customize.utils so they can be unit tested
    66
     
    7878        };
    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        stableSort = 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         *
    83100         * @param {jQuery.Event} event
     
    176193                                        children.push( child );
    177194                                }
    178195                        } );
    179                         children.sort( function ( a, b ) {
    180                                 return a.priority() - b.priority();
    181                         } );
     196                        children.sort( stableSort );
    182197                        return children;
    183198                },
    184199
     
    19521967                        } );
    19531968
    19541969                        // Sort the root panels and sections
    1955                         rootNodes.sort( function ( a, b ) {
    1956                                 return a.priority() - b.priority();
    1957                         } );
     1970                        rootNodes.sort( stableSort );
    19581971                        rootContainers = _.pluck( rootNodes, 'container' );
    19591972                        appendContainer = $( '#customize-theme-controls' ).children( 'ul' ); // @todo This should be defined elsewhere, and to be configurable
    19601973                        if ( ! areElementListsEqual( rootContainers, appendContainer.children() ) ) {
  • src/wp-includes/class-wp-customize-control.php

    diff --git src/wp-includes/class-wp-customize-control.php src/wp-includes/class-wp-customize-control.php
    index dcebb0f..a2df83f 100644
     
    77 * @since 3.4.0
    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
    1233         * @var WP_Customize_Manager
    class WP_Customize_Control { 
    127148                if ( empty( $this->active_callback ) ) {
    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.
    132155                if ( empty( $this->settings ) ) {
    class WP_Customize_Control { 
    218241                        $this->json['settings'][ $key ] = $setting->id;
    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; // note: camelCase for JS
    228252        }
    229253
    230254        /**
  • src/wp-includes/class-wp-customize-manager.php

    diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
    index 371ea90..987edc6 100644
    final class WP_Customize_Manager { 
    856856         * @since 4.1.0
    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();
    862862                }
    863863        }
    864864
    865     /**
    866          * Helper function to compare two objects by priority.
     865        /**
     866         * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
    867867         *
    868868         * @since 3.4.0
    869869         *
    870          * @param object $a Object A.
    871          * @param object $b Object B.
     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
    883882        /**
    final class WP_Customize_Manager { 
    891890         */
    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 ) {
    898897                        if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
    final class WP_Customize_Manager { 
    905904                $this->controls = $controls;
    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();
    912909
    final class WP_Customize_Manager { 
    930927                $this->sections = $sections;
    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();
    937932
  • src/wp-includes/class-wp-customize-panel.php

    diff --git src/wp-includes/class-wp-customize-panel.php src/wp-includes/class-wp-customize-panel.php
    index 201c4b9..af8f243 100644
     
    1111class WP_Customize_Panel {
    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         *
    1636         * @since 4.0.0
    class WP_Customize_Panel { 
    128148                if ( empty( $this->active_callback ) ) {
    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.
    133155
    class WP_Customize_Panel { 
    185207                $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'type' ) );
    186208                $array['content'] = $this->get_content();
    187209                $array['active'] = $this->active();
     210                $array['instanceNumber'] = $this->instance_number; // camelCase
    188211                return $array;
    189212        }
    190213
  • src/wp-includes/class-wp-customize-section.php

    diff --git src/wp-includes/class-wp-customize-section.php src/wp-includes/class-wp-customize-section.php
    index 3553285..a90feb5 100644
     
    1111class WP_Customize_Section {
    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         *
    1636         * @since 3.4.0
    class WP_Customize_Section { 
    137157                if ( empty( $this->active_callback ) ) {
    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.
    142164
    class WP_Customize_Section { 
    194216                $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'panel', 'type' ) );
    195217                $array['content'] = $this->get_content();
    196218                $array['active'] = $this->active();
     219                $array['instanceNumber'] = $this->instance_number; // camelCase
    197220                return $array;
    198221        }
    199222