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/functions.php

    r38884 r38928  
    34903490 *
    34913491 * @since 3.0.0
     3492 * @since 4.7.0 Uses WP_List_Util class.
    34923493 *
    34933494 * @param array       $list     An array of objects to filter
     
    35033504 */
    35043505function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
    3505         if ( ! is_array( $list ) )
     3506        if ( ! is_array( $list ) ) {
    35063507                return array();
    3507 
    3508         $list = wp_list_filter( $list, $args, $operator );
    3509 
    3510         if ( $field )
    3511                 $list = wp_list_pluck( $list, $field );
    3512 
    3513         return $list;
     3508        }
     3509
     3510        $util = new WP_List_Util( $list );
     3511
     3512        $util->filter( $args, $operator );
     3513
     3514        if ( $field ) {
     3515                $util->pluck( $field );
     3516        }
     3517
     3518        return $util->get_output();
    35143519}
    35153520
     
    35183523 *
    35193524 * @since 3.1.0
     3525 * @since 4.7.0 Uses WP_List_Util class.
    35203526 *
    35213527 * @param array  $list     An array of objects to filter.
     
    35293535 */
    35303536function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
    3531         if ( ! is_array( $list ) )
     3537        if ( ! is_array( $list ) ) {
    35323538                return array();
    3533 
    3534         if ( empty( $args ) )
    3535                 return $list;
    3536 
    3537         $operator = strtoupper( $operator );
    3538         $count = count( $args );
    3539         $filtered = array();
    3540 
    3541         foreach ( $list as $key => $obj ) {
    3542                 $to_match = (array) $obj;
    3543 
    3544                 $matched = 0;
    3545                 foreach ( $args as $m_key => $m_value ) {
    3546                         if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
    3547                                 $matched++;
    3548                 }
    3549 
    3550                 if ( ( 'AND' == $operator && $matched == $count )
    3551                   || ( 'OR' == $operator && $matched > 0 )
    3552                   || ( 'NOT' == $operator && 0 == $matched ) ) {
    3553                         $filtered[$key] = $obj;
    3554                 }
    3555         }
    3556 
    3557         return $filtered;
     3539        }
     3540
     3541        $util = new WP_List_Util( $list );
     3542        return $util->filter( $args, $operator );
    35583543}
    35593544
     
    35663551 * @since 3.1.0
    35673552 * @since 4.0.0 $index_key parameter added.
     3553 * @since 4.7.0 Uses WP_List_Util class.
    35683554 *
    35693555 * @param array      $list      List of objects or arrays
     
    35763562 */
    35773563function wp_list_pluck( $list, $field, $index_key = null ) {
    3578         if ( ! $index_key ) {
    3579                 /*
    3580                  * This is simple. Could at some point wrap array_column()
    3581                  * if we knew we had an array of arrays.
    3582                  */
    3583                 foreach ( $list as $key => $value ) {
    3584                         if ( is_object( $value ) ) {
    3585                                 $list[ $key ] = $value->$field;
    3586                         } else {
    3587                                 $list[ $key ] = $value[ $field ];
    3588                         }
    3589                 }
    3590                 return $list;
    3591         }
    3592 
    3593         /*
    3594          * When index_key is not set for a particular item, push the value
    3595          * to the end of the stack. This is how array_column() behaves.
    3596          */
    3597         $newlist = array();
    3598         foreach ( $list as $value ) {
    3599                 if ( is_object( $value ) ) {
    3600                         if ( isset( $value->$index_key ) ) {
    3601                                 $newlist[ $value->$index_key ] = $value->$field;
    3602                         } else {
    3603                                 $newlist[] = $value->$field;
    3604                         }
    3605                 } else {
    3606                         if ( isset( $value[ $index_key ] ) ) {
    3607                                 $newlist[ $value[ $index_key ] ] = $value[ $field ];
    3608                         } else {
    3609                                 $newlist[] = $value[ $field ];
    3610                         }
    3611                 }
    3612         }
    3613 
    3614         return $newlist;
     3564        $util = new WP_List_Util( $list );
     3565        return $util->pluck( $field, $index_key );
     3566}
     3567
     3568/**
     3569 * Sorts a list of objects, based on one or more orderby arguments.
     3570 *
     3571 * @since 4.7.0
     3572 *
     3573 * @param array        $list          An array of objects to filter.
     3574 * @param string|array $orderby       Optional. Either the field name to order by or an array
     3575 *                                    of multiple orderby fields as $orderby => $order.
     3576 * @param string       $order         Optional. Either 'ASC' or 'DESC'. Only used if $orderby
     3577 *                                    is a string.
     3578 * @param bool         $preserve_keys Optional. Whether to preserve keys. Default false.
     3579 * @return array The sorted array.
     3580 */
     3581function wp_list_sort( $list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
     3582        if ( ! is_array( $list ) ) {
     3583                return array();
     3584        }
     3585
     3586        $util = new WP_List_Util( $list );
     3587        return $util->sort( $orderby, $order, $preserve_keys );
    36153588}
    36163589
Note: See TracChangeset for help on using the changeset viewer.