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/customize/class-wp-customize-nav-menu-setting.php

    r38863 r38928  
    288288                // Make sure the menu objects get re-sorted after an update/insert.
    289289                if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
    290                         $this->_current_menus_sort_orderby = $args['orderby'];
    291                         usort( $menus, array( $this, '_sort_menus_by_orderby' ) );
     290                        $menus = wp_list_sort( $menus, array(
     291                                $args['orderby'] => 'ASC',
     292                        ) );
    292293                }
    293294                // @todo add support for $args['hide_empty'] === true
     
    314315         *
    315316         * @since 4.3.0
     317         * @deprecated 4.7.0 Use wp_list_sort()
    316318         * @access protected
     319         *
    317320         * @param object $menu1
    318321         * @param object $menu2
     
    322325         */
    323326        protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
     327                _deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
     328
    324329                $key = $this->_current_menus_sort_orderby;
    325330                return strcmp( $menu1->$key, $menu2->$key );
Note: See TracChangeset for help on using the changeset viewer.