Split wp_filter_object_list() into wp_list_filter() and wp_list_pluck(). Fixes #15016

git-svn-id: http://svn.automattic.com/wordpress/trunk@15686 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-10-02 18:48:51 +00:00
parent 70728369f9
commit 688f990cd0
2 changed files with 42 additions and 13 deletions

View File

@ -3019,32 +3019,61 @@ function wp_array_slice_assoc( $array, $keys ) {
* @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) )
return array();
$list = wp_list_filter( $list, $args, $operator );
if ( empty($args) )
$args = array();
if ( $field )
$list = wp_list_pluck( $list, $field );
if ( empty($args) && !$field )
return $list; // nothing to do
return $list;
}
$count = count($args);
/**
* Filters a list of objects, based on a set of key => value arguments
*
* @since 3.1.0
*
* @param array $list An array of objects to filter
* @param array $args An array of key => value arguments to match against each object
* @param string $operator The logical operation to perform. 'or' means only one element
* from the array needs to match; 'and' means all elements must match. The default is 'and'.
* @return array
*/
function wp_list_filter( $list, $args = array(), $operator = 'and' ) {
if ( empty( $args ) )
return $list;
$count = count( $args );
$filtered = array();
foreach ( $list as $key => $obj ) {
$matched = count( array_intersect_assoc( (array) ($obj), $args ) );
$matched = count( array_intersect_assoc( (array) $obj, $args ) );
if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) ) {
if ( $field )
$filtered[] = $obj->$field;
else
$filtered[$key] = $obj;
$filtered[$key] = $obj;
}
}
return $filtered;
}
/**
* Pluck a certain field out of each object in a list
*
* @since 3.1.0
*
* @param array $list A list of objects or arrays
* @param int|string $field A field from the object to place instead of the entire object
* @return array
*/
function wp_list_pluck( $list, $field ) {
foreach ( $list as $key => $value ) {
$value = (array) $value;
$list[ $key ] = $value[ $field ];
}
return $list;
}
/**
* Determines if default embed handlers should be loaded.
*

View File

@ -1951,7 +1951,7 @@ class WP_Query {
// Back-compat
if ( !empty( $ids ) ) {
$cat_query = wp_filter_object_list( $tax_query, array( 'taxonomy' => 'category' ) );
$cat_query = wp_list_filter( $tax_query, array( 'taxonomy' => 'category' ) );
if ( !empty( $cat_query ) ) {
$cat_query = reset( $cat_query );
$cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );