Revert [38859] due to an incomplete implementation.

See https://core.trac.wordpress.org/ticket/37128#comment:27.
See #37128.

git-svn-id: https://develop.svn.wordpress.org/trunk@38863 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90)
2016-10-21 17:00:29 +00:00
parent 2d7c2823b6
commit ef43d7b0fa
10 changed files with 143 additions and 735 deletions

View File

@@ -3489,7 +3489,6 @@ 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
@@ -3503,26 +3502,21 @@ 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();
}
$util = new WP_List_Util( $list );
$list = wp_list_filter( $list, $args, $operator );
$util->filter( $args, $operator );
if ( $field )
$list = wp_list_pluck( $list, $field );
if ( $field ) {
$util->pluck( $field );
}
return $util->get_output();
return $list;
}
/**
* 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
@@ -3534,12 +3528,33 @@ 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;
}
}
$util = new WP_List_Util( $list );
return $util->filter( $args, $operator );
return $filtered;
}
/**
@@ -3550,7 +3565,6 @@ 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
@@ -3561,29 +3575,43 @@ 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 ) {
$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.
* @return array The sorted array.
*/
function wp_list_sort( $list, $orderby = array(), $order = 'ASC' ) {
if ( ! is_array( $list ) ) {
return array();
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->sort( $orderby, $order );
/*
* 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;
}
/**