Make WordPress Core


Ignore:
Timestamp:
10/21/2016 11:11:42 AM (8 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r38785 r38859  
    37993799    return preg_replace( '%&\s*\{[^}]*(\}\s*;?|$)%', '', $string );
    38003800}
     3801
     3802/**
     3803 * Sort categories by ID.
     3804 *
     3805 * Used by usort() as a callback, should not be used directly. Can actually be
     3806 * used to sort any term object.
     3807 *
     3808 * @since 2.3.0
     3809 * @deprecated 4.7.0 Use wp_list_sort()
     3810 * @access private
     3811 *
     3812 * @param object $a
     3813 * @param object $b
     3814 * @return int
     3815 */
     3816function _usort_terms_by_ID( $a, $b ) {
     3817    _deprecated_function( __FUNCTION__, '4.7.0', 'wp_list_sort' );
     3818
     3819    if ( $a->term_id > $b->term_id )
     3820        return 1;
     3821    elseif ( $a->term_id < $b->term_id )
     3822        return -1;
     3823    else
     3824        return 0;
     3825}
     3826
     3827/**
     3828 * Sort categories by name.
     3829 *
     3830 * Used by usort() as a callback, should not be used directly. Can actually be
     3831 * used to sort any term object.
     3832 *
     3833 * @since 2.3.0
     3834 * @deprecated 4.7.0 Use wp_list_sort()
     3835 * @access private
     3836 *
     3837 * @param object $a
     3838 * @param object $b
     3839 * @return int
     3840 */
     3841function _usort_terms_by_name( $a, $b ) {
     3842    _deprecated_function( __FUNCTION__, '4.7.0', 'wp_list_sort' );
     3843
     3844    return strcmp( $a->name, $b->name );
     3845}
     3846
     3847/**
     3848 * Sort menu items by the desired key.
     3849 *
     3850 * @since 3.0.0
     3851 * @deprecated 4.7.0 Use wp_list_sort()
     3852 * @access private
     3853 *
     3854 * @global string $_menu_item_sort_prop
     3855 *
     3856 * @param object $a The first object to compare
     3857 * @param object $b The second object to compare
     3858 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
     3859 */
     3860function _sort_nav_menu_items( $a, $b ) {
     3861    global $_menu_item_sort_prop;
     3862
     3863    _deprecated_function( __FUNCTION__, '4.7.0', 'wp_list_sort' );
     3864
     3865    if ( empty( $_menu_item_sort_prop ) )
     3866        return 0;
     3867
     3868    if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
     3869        return 0;
     3870
     3871    $_a = (int) $a->$_menu_item_sort_prop;
     3872    $_b = (int) $b->$_menu_item_sort_prop;
     3873
     3874    if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
     3875        return 0;
     3876    elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
     3877        return $_a < $_b ? -1 : 1;
     3878    else
     3879        return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
     3880}
Note: See TracChangeset for help on using the changeset viewer.