WordPress.org

Make WordPress Core

Changeset 15686


Ignore:
Timestamp:
10/02/2010 06:48:51 PM (8 years ago)
Author:
scribu
Message:

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

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r15685 r15686  
    30203020 */
    30213021function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
    3022     if ( !is_array($list) )
    3023         return array();
    3024 
    3025     if ( empty($args) )
    3026         $args = array();
    3027 
    3028     if ( empty($args) && !$field )
    3029         return $list;   // nothing to do
    3030 
    3031     $count = count($args);
     3022    $list = wp_list_filter( $list, $args, $operator );
     3023
     3024    if ( $field )
     3025        $list = wp_list_pluck( $list, $field );
     3026
     3027    return $list;
     3028}
     3029
     3030/**
     3031 * Filters a list of objects, based on a set of key => value arguments
     3032 *
     3033 * @since 3.1.0
     3034 *
     3035 * @param array $list An array of objects to filter
     3036 * @param array $args An array of key => value arguments to match against each object
     3037 * @param string $operator The logical operation to perform. 'or' means only one element
     3038 *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
     3039 * @return array
     3040 */
     3041function wp_list_filter( $list, $args = array(), $operator = 'and' ) {
     3042    if ( empty( $args ) )
     3043        return $list;
     3044
     3045    $count = count( $args );
    30323046
    30333047    $filtered = array();
    30343048
    30353049    foreach ( $list as $key => $obj ) {
    3036         $matched = count( array_intersect_assoc( (array) ($obj), $args ) );
     3050        $matched = count( array_intersect_assoc( (array) $obj, $args ) );
    30373051        if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) ) {
    3038             if ( $field )
    3039                 $filtered[] = $obj->$field;
    3040             else
    3041                 $filtered[$key] = $obj;
     3052            $filtered[$key] = $obj;
    30423053        }
    30433054    }
    30443055
    30453056    return $filtered;
     3057}
     3058
     3059/**
     3060 * Pluck a certain field out of each object in a list
     3061 *
     3062 * @since 3.1.0
     3063 *
     3064 * @param array $list A list of objects or arrays
     3065 * @param int|string $field A field from the object to place instead of the entire object
     3066 * @return array
     3067 */
     3068function wp_list_pluck( $list, $field ) {
     3069    foreach ( $list as $key => $value ) {
     3070        $value = (array) $value;
     3071        $list[ $key ] = $value[ $field ];
     3072    }
     3073
     3074    return $list;
    30463075}
    30473076
  • trunk/wp-includes/query.php

    r15685 r15686  
    19521952            // Back-compat
    19531953            if ( !empty( $ids ) ) {
    1954                 $cat_query = wp_filter_object_list( $tax_query, array( 'taxonomy' => 'category' ) );
     1954                $cat_query = wp_list_filter( $tax_query, array( 'taxonomy' => 'category' ) );
    19551955                if ( !empty( $cat_query ) ) {
    19561956                    $cat_query = reset( $cat_query );
Note: See TracChangeset for help on using the changeset viewer.