Make WordPress Core


Ignore:
Timestamp:
04/16/2010 02:08:58 PM (15 years ago)
Author:
nacin
Message:

Introduce the wp_filter_object_list() helper, with an $operator arg. Fixes an intersection bug in get_post_types() and get_taxonomies(). Also switches $operator default from 'or' to 'and' for get_post_stati(). props scribu, fixes #12966.

File:
1 edited

Legend:

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

    r14101 r14108  
    29022902 * @return array Sanitized array of IDs
    29032903 */
    2904 function wp_parse_id_list($list) {
     2904function wp_parse_id_list( $list ) {
    29052905    if ( !is_array($list) )
    29062906        $list = preg_split('/[\s,]+/', $list);
    29072907
    29082908    return array_unique(array_map('absint', $list));
     2909}
     2910
     2911/**
     2912 * Filters a list of objects, based on a set of key => value arguments
     2913 *
     2914 * @since 3.0.0
     2915 *
     2916 * @param array $list An array of objects to filter
     2917 * @param array $args An array of key => value arguments to match against each object
     2918 * @param string $operator The logical operation to perform. 'or' means only one element
     2919 *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
     2920 * @param bool|string $field A field from the object to place instead of the entire object
     2921 * @return array A list of objects or object fields
     2922 */
     2923function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) {
     2924    if ( !is_array($list) )
     2925        return array();
     2926
     2927    if ( empty($args) )
     2928        $args = array();
     2929
     2930    if ( empty($args) && !$field )
     2931        return $list;   // nothing to do
     2932
     2933    $count = count($args);
     2934
     2935    $filtered = array();
     2936
     2937    foreach ( $list as $key => $obj ) {
     2938        $matched = count(array_intersect_assoc(get_object_vars($obj), $args));
     2939        if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) )
     2940            $filtered[$key] = $field ? $obj->$field : $obj;
     2941    }
     2942
     2943    return $filtered;
    29092944}
    29102945
Note: See TracChangeset for help on using the changeset viewer.