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/nav-menu.php

    r38744 r38859  
    559559
    560560/**
    561  * Sort menu items by the desired key.
    562  *
    563  * @since 3.0.0
    564  * @access private
    565  *
    566  * @global string $_menu_item_sort_prop
    567  *
    568  * @param object $a The first object to compare
    569  * @param object $b The second object to compare
    570  * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
    571  */
    572 function _sort_nav_menu_items( $a, $b ) {
    573     global $_menu_item_sort_prop;
    574 
    575     if ( empty( $_menu_item_sort_prop ) )
    576         return 0;
    577 
    578     if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
    579         return 0;
    580 
    581     $_a = (int) $a->$_menu_item_sort_prop;
    582     $_b = (int) $b->$_menu_item_sort_prop;
    583 
    584     if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
    585         return 0;
    586     elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
    587         return $_a < $_b ? -1 : 1;
    588     else
    589         return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
    590 }
    591 
    592 /**
    593561 * Return if a menu item is valid.
    594562 *
     
    683651
    684652    if ( ARRAY_A == $args['output'] ) {
    685         $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
    686         usort($items, '_sort_nav_menu_items');
     653        $items = wp_list_sort( $items, array(
     654            $args['output_key'] => 'ASC',
     655        ) );
    687656        $i = 1;
    688657        foreach ( $items as $k => $item ) {
     
    777746                $menu_item->type_label = __( 'Post Type Archive' );
    778747                $post_content = wp_trim_words( $menu_item->post_content, 200 );
    779                 $post_type_description = '' == $post_content ? $post_type_description : $post_content; 
     748                $post_type_description = '' == $post_content ? $post_type_description : $post_content;
    780749                $menu_item->url = get_post_type_archive_link( $menu_item->object );
    781750            } elseif ( 'taxonomy' == $menu_item->type ) {
Note: See TracChangeset for help on using the changeset viewer.