General: Introduce a wp_list_sort() helper function.
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. Props flixos90, DrewAPicture, jorbin. Fixes #37128. Built from https://develop.svn.wordpress.org/trunk@38859 git-svn-id: http://core.svn.wordpress.org/trunk@38802 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -532,8 +532,9 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
}
|
||||
|
||||
if ( ARRAY_A === $args['output'] ) {
|
||||
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
|
||||
usort( $items, '_sort_nav_menu_items' );
|
||||
$items = wp_list_sort( $items, array(
|
||||
$args['output_key'] => 'ASC',
|
||||
) );
|
||||
$i = 1;
|
||||
|
||||
foreach ( $items as $k => $item ) {
|
||||
|
||||
@@ -287,8 +287,9 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
||||
|
||||
// Make sure the menu objects get re-sorted after an update/insert.
|
||||
if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
|
||||
$this->_current_menus_sort_orderby = $args['orderby'];
|
||||
usort( $menus, array( $this, '_sort_menus_by_orderby' ) );
|
||||
$menus = wp_list_sort( $menus, array(
|
||||
$args['orderby'] => 'ASC',
|
||||
) );
|
||||
}
|
||||
// @todo add support for $args['hide_empty'] === true
|
||||
|
||||
@@ -313,7 +314,9 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
||||
* This is a workaround for a lack of closures.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @deprecated 4.7.0 Use wp_list_sort()
|
||||
* @access protected
|
||||
*
|
||||
* @param object $menu1
|
||||
* @param object $menu2
|
||||
* @return int
|
||||
@@ -321,6 +324,8 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
||||
* @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
|
||||
*/
|
||||
protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
|
||||
_deprecated_function( __METHOD__, '4.7.0', 'wp_list_sort' );
|
||||
|
||||
$key = $this->_current_menus_sort_orderby;
|
||||
return strcmp( $menu1->$key, $menu2->$key );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user