mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-12 00:24:34 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@38928 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -3489,6 +3489,7 @@ function wp_is_numeric_array( $data ) {
|
||||
* Filters a list of objects, based on a set of key => value arguments.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 4.7.0 Uses WP_List_Util class.
|
||||
*
|
||||
* @param array $list An array of objects to filter
|
||||
* @param array $args Optional. An array of key => value arguments to match
|
||||
@@ -3502,21 +3503,26 @@ function wp_is_numeric_array( $data ) {
|
||||
* @return array A list of objects or object fields.
|
||||
*/
|
||||
function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
|
||||
if ( ! is_array( $list ) )
|
||||
if ( ! is_array( $list ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$list = wp_list_filter( $list, $args, $operator );
|
||||
$util = new WP_List_Util( $list );
|
||||
|
||||
if ( $field )
|
||||
$list = wp_list_pluck( $list, $field );
|
||||
$util->filter( $args, $operator );
|
||||
|
||||
return $list;
|
||||
if ( $field ) {
|
||||
$util->pluck( $field );
|
||||
}
|
||||
|
||||
return $util->get_output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters a list of objects, based on a set of key => value arguments.
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @since 4.7.0 Uses WP_List_Util class.
|
||||
*
|
||||
* @param array $list An array of objects to filter.
|
||||
* @param array $args Optional. An array of key => value arguments to match
|
||||
@@ -3528,33 +3534,12 @@ function wp_filter_object_list( $list, $args = array(), $operator = 'and', $fiel
|
||||
* @return array Array of found values.
|
||||
*/
|
||||
function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
|
||||
if ( ! is_array( $list ) )
|
||||
if ( ! is_array( $list ) ) {
|
||||
return array();
|
||||
|
||||
if ( empty( $args ) )
|
||||
return $list;
|
||||
|
||||
$operator = strtoupper( $operator );
|
||||
$count = count( $args );
|
||||
$filtered = array();
|
||||
|
||||
foreach ( $list as $key => $obj ) {
|
||||
$to_match = (array) $obj;
|
||||
|
||||
$matched = 0;
|
||||
foreach ( $args as $m_key => $m_value ) {
|
||||
if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
|
||||
$matched++;
|
||||
}
|
||||
|
||||
if ( ( 'AND' == $operator && $matched == $count )
|
||||
|| ( 'OR' == $operator && $matched > 0 )
|
||||
|| ( 'NOT' == $operator && 0 == $matched ) ) {
|
||||
$filtered[$key] = $obj;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
$util = new WP_List_Util( $list );
|
||||
return $util->filter( $args, $operator );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3565,6 +3550,7 @@ function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @since 4.0.0 $index_key parameter added.
|
||||
* @since 4.7.0 Uses WP_List_Util class.
|
||||
*
|
||||
* @param array $list List of objects or arrays
|
||||
* @param int|string $field Field from the object to place instead of the entire object
|
||||
@@ -3575,43 +3561,30 @@ function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {
|
||||
* `$list` will be preserved in the results.
|
||||
*/
|
||||
function wp_list_pluck( $list, $field, $index_key = null ) {
|
||||
if ( ! $index_key ) {
|
||||
/*
|
||||
* This is simple. Could at some point wrap array_column()
|
||||
* if we knew we had an array of arrays.
|
||||
*/
|
||||
foreach ( $list as $key => $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
$list[ $key ] = $value->$field;
|
||||
} else {
|
||||
$list[ $key ] = $value[ $field ];
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
$util = new WP_List_Util( $list );
|
||||
return $util->pluck( $field, $index_key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts a list of objects, based on one or more orderby arguments.
|
||||
*
|
||||
* @since 4.7.0
|
||||
*
|
||||
* @param array $list An array of objects to filter.
|
||||
* @param string|array $orderby Optional. Either the field name to order by or an array
|
||||
* of multiple orderby fields as $orderby => $order.
|
||||
* @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby
|
||||
* is a string.
|
||||
* @param bool $preserve_keys Optional. Whether to preserve keys. Default false.
|
||||
* @return array The sorted array.
|
||||
*/
|
||||
function wp_list_sort( $list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
|
||||
if ( ! is_array( $list ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/*
|
||||
* When index_key is not set for a particular item, push the value
|
||||
* to the end of the stack. This is how array_column() behaves.
|
||||
*/
|
||||
$newlist = array();
|
||||
foreach ( $list as $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
if ( isset( $value->$index_key ) ) {
|
||||
$newlist[ $value->$index_key ] = $value->$field;
|
||||
} else {
|
||||
$newlist[] = $value->$field;
|
||||
}
|
||||
} else {
|
||||
if ( isset( $value[ $index_key ] ) ) {
|
||||
$newlist[ $value[ $index_key ] ] = $value[ $field ];
|
||||
} else {
|
||||
$newlist[] = $value[ $field ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $newlist;
|
||||
$util = new WP_List_Util( $list );
|
||||
return $util->sort( $orderby, $order, $preserve_keys );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5572,4 +5545,4 @@ function wp_cache_get_last_changed( $group ) {
|
||||
}
|
||||
|
||||
return $last_changed;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user