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.

git-svn-id: https://develop.svn.wordpress.org/trunk@38859 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2016-10-21 11:11:42 +00:00
parent 9d7e8fec8c
commit e4cbb7d031
11 changed files with 763 additions and 149 deletions
@@ -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 );
}