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.

git-svn-id: http://svn.automattic.com/wordpress/trunk@14108 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin
2010-04-16 14:08:58 +00:00
parent 340acf8263
commit 14916e2449
4 changed files with 57 additions and 67 deletions

View File

@@ -2901,13 +2901,48 @@ function wp_parse_args( $args, $defaults = '' ) {
* @param array|string $list
* @return array Sanitized array of IDs
*/
function wp_parse_id_list($list) {
function wp_parse_id_list( $list ) {
if ( !is_array($list) )
$list = preg_split('/[\s,]+/', $list);
return array_unique(array_map('absint', $list));
}
/**
* Filters a list of objects, based on a set of key => value arguments
*
* @since 3.0.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'.
* @param bool|string $field A field from the object to place instead of the entire object
* @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();
if ( empty($args) )
$args = array();
if ( empty($args) && !$field )
return $list; // nothing to do
$count = count($args);
$filtered = array();
foreach ( $list as $key => $obj ) {
$matched = count(array_intersect_assoc(get_object_vars($obj), $args));
if ( ('and' == $operator && $matched == $count) || ('or' == $operator && $matched <= $count) )
$filtered[$key] = $field ? $obj->$field : $obj;
}
return $filtered;
}
/**
* Determines if default embed handlers should be loaded.
*