Make WordPress Core


Ignore:
Timestamp:
10/25/2016 09:25:25 PM (10 years ago)
Author:
ocean90
Message:

General: Introduce a wp_list_sort() helper function, v2.

In addition to wp_list_filter() for filtering a list of objects, and wp_list_pluck() for plucking a certain field out of each object in a list, this new function can be used for sorting a list of objects by specific fields. These functions are now all contained within the new WP_List_Util() class and wp_list_sort() is used in various parts of core for sorting lists.

This was previously committed in [38859] but got reverted in [38862] and [38863]. To fix the previous issues, wp_list_sort() supports now an additional argument to preserve array keys via uasort().

Props flixos90, DrewAPicture, jorbin.
Fixes #37128.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r38906 r38928  
    25062506         *
    25072507         * @since 3.4.0
     2508         * @deprecated 4.7.0 Use wp_list_sort()
    25082509         *
    25092510         * @param WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control $a Object A.
     
    25122513         */
    25132514        protected function _cmp_priority( $a, $b ) {
     2515                _deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
     2516
    25142517                if ( $a->priority === $b->priority ) {
    25152518                        return $a->instance_number - $b->instance_number;
     
    25312534
    25322535                $controls = array();
    2533                 uasort( $this->controls, array( $this, '_cmp_priority' ) );
     2536                $this->controls = wp_list_sort( $this->controls, array(
     2537                        'priority'        => 'ASC',
     2538                        'instance_number' => 'ASC',
     2539                ), 'ASC', true );
    25342540
    25352541                foreach ( $this->controls as $id => $control ) {
     
    25442550
    25452551                // Prepare sections.
    2546                 uasort( $this->sections, array( $this, '_cmp_priority' ) );
     2552                $this->sections = wp_list_sort( $this->sections, array(
     2553                        'priority'        => 'ASC',
     2554                        'instance_number' => 'ASC',
     2555                ), 'ASC', true );
    25472556                $sections = array();
    25482557
     
    25522561                        }
    25532562
    2554                         usort( $section->controls, array( $this, '_cmp_priority' ) );
     2563
     2564                        $section->controls = wp_list_sort( $section->controls, array(
     2565                                'priority'        => 'ASC',
     2566                                'instance_number' => 'ASC',
     2567                        ) );
    25552568
    25562569                        if ( ! $section->panel ) {
     
    25672580
    25682581                // Prepare panels.
    2569                 uasort( $this->panels, array( $this, '_cmp_priority' ) );
     2582                $this->panels = wp_list_sort( $this->panels, array(
     2583                        'priority'        => 'ASC',
     2584                        'instance_number' => 'ASC',
     2585                ), 'ASC', true );
    25702586                $panels = array();
    25712587
     
    25752591                        }
    25762592
    2577                         uasort( $panel->sections, array( $this, '_cmp_priority' ) );
     2593                        $panel->sections = wp_list_sort( $panel->sections, array(
     2594                                'priority'        => 'ASC',
     2595                                'instance_number' => 'ASC',
     2596                        ), 'ASC', true );
    25782597                        $panels[ $panel->id ] = $panel;
    25792598                }
     
    25822601                // Sort panels and top-level sections together.
    25832602                $this->containers = array_merge( $this->panels, $this->sections );
    2584                 uasort( $this->containers, array( $this, '_cmp_priority' ) );
     2603                $this->containers = wp_list_sort( $this->containers, array(
     2604                        'priority'        => 'ASC',
     2605                        'instance_number' => 'ASC',
     2606                ), 'ASC', true );
    25852607        }
    25862608
Note: See TracChangeset for help on using the changeset viewer.