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:
parent
340acf8263
commit
14916e2449
|
@ -2901,13 +2901,48 @@ function wp_parse_args( $args, $defaults = '' ) {
|
||||||
* @param array|string $list
|
* @param array|string $list
|
||||||
* @return array Sanitized array of IDs
|
* @return array Sanitized array of IDs
|
||||||
*/
|
*/
|
||||||
function wp_parse_id_list($list) {
|
function wp_parse_id_list( $list ) {
|
||||||
if ( !is_array($list) )
|
if ( !is_array($list) )
|
||||||
$list = preg_split('/[\s,]+/', $list);
|
$list = preg_split('/[\s,]+/', $list);
|
||||||
|
|
||||||
return array_unique(array_map('absint', $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.
|
* Determines if default embed handlers should be loaded.
|
||||||
*
|
*
|
||||||
|
|
|
@ -631,42 +631,18 @@ function get_post_status_object( $post_status ) {
|
||||||
* @see register_post_status
|
* @see register_post_status
|
||||||
* @see get_post_status_object
|
* @see get_post_status_object
|
||||||
*
|
*
|
||||||
* @param array|string $args An array of key => value arguments to match against the post statuses.
|
* @param array|string $args An array of key => value arguments to match against the post status objects.
|
||||||
* Only post statuses having attributes that match all arguments are returned.
|
|
||||||
* @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
|
* @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
|
||||||
* @param string $operator Whether the elements in $args should be logicallly 'or'ed or 'and'ed together. 'or' means only one element from the array needs to match. 'and' means all elements must match. The default is 'or'.
|
* @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 A list of post type names or objects
|
* @return array A list of post type names or objects
|
||||||
*/
|
*/
|
||||||
function get_post_stati( $args = array(), $output = 'names', $operator = 'or' ) {
|
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
|
||||||
global $wp_post_statuses;
|
global $wp_post_statuses;
|
||||||
|
|
||||||
$do_names = false;
|
$field = ('names' == $output) ? 'name' : false;
|
||||||
if ( 'names' == $output )
|
|
||||||
$do_names = true;
|
|
||||||
|
|
||||||
if ( 'and' == $operator )
|
return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
|
||||||
$arg_count = count($args);
|
|
||||||
else
|
|
||||||
$arg_count = 0;
|
|
||||||
|
|
||||||
$post_statuses = array();
|
|
||||||
foreach ( (array) $wp_post_statuses as $post_status ) {
|
|
||||||
if ( empty($args) ) {
|
|
||||||
if ( $do_names )
|
|
||||||
$post_statuses[] = $post_status->name;
|
|
||||||
else
|
|
||||||
$post_statuses[] = $post_status;
|
|
||||||
} elseif ( $intersect = array_intersect_assoc((array) $post_status, $args) ) {
|
|
||||||
if ( $arg_count && ( $arg_count != count($intersect) ) )
|
|
||||||
continue;
|
|
||||||
if ( $do_names )
|
|
||||||
$post_statuses[] = $post_status->name;
|
|
||||||
else
|
|
||||||
$post_statuses[] = $post_status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $post_statuses;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -701,7 +677,7 @@ function is_post_type_hierarchical( $post = false ) {
|
||||||
* @param mixed $the_post Optional. Post object or post ID.
|
* @param mixed $the_post Optional. Post object or post ID.
|
||||||
* @return bool|string post type or false on failure.
|
* @return bool|string post type or false on failure.
|
||||||
*/
|
*/
|
||||||
function get_post_type($the_post = false) {
|
function get_post_type( $the_post = false ) {
|
||||||
global $post;
|
global $post;
|
||||||
|
|
||||||
if ( false === $the_post )
|
if ( false === $the_post )
|
||||||
|
@ -745,36 +721,19 @@ function get_post_type_object( $post_type ) {
|
||||||
* @since 2.9.0
|
* @since 2.9.0
|
||||||
* @uses $wp_post_types
|
* @uses $wp_post_types
|
||||||
* @see register_post_type
|
* @see register_post_type
|
||||||
* @see get_post_types
|
|
||||||
*
|
*
|
||||||
* @param array|string $args An array of key => value arguments to match against the post types.
|
* @param array|string $args An array of key => value arguments to match against the post type objects.
|
||||||
* Only post types having attributes that match all arguments are returned.
|
|
||||||
* @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
|
* @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
|
||||||
|
* @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 A list of post type names or objects
|
* @return array A list of post type names or objects
|
||||||
*/
|
*/
|
||||||
function get_post_types( $args = array(), $output = 'names' ) {
|
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
|
||||||
global $wp_post_types;
|
global $wp_post_types;
|
||||||
|
|
||||||
$do_names = false;
|
$field = ('names' == $output) ? 'name' : false;
|
||||||
if ( 'names' == $output )
|
|
||||||
$do_names = true;
|
|
||||||
|
|
||||||
$post_types = array();
|
return wp_filter_object_list($wp_post_types, $args, $operator, $field);
|
||||||
foreach ( (array) $wp_post_types as $post_type ) {
|
|
||||||
if ( empty($args) ) {
|
|
||||||
if ( $do_names )
|
|
||||||
$post_types[] = $post_type->name;
|
|
||||||
else
|
|
||||||
$post_types[] = $post_type;
|
|
||||||
} elseif ( array_intersect_assoc((array) $post_type, $args) ) {
|
|
||||||
if ( $do_names )
|
|
||||||
$post_types[] = $post_type->name;
|
|
||||||
else
|
|
||||||
$post_types[] = $post_type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $post_types;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2255,7 +2255,7 @@ class WP_Query {
|
||||||
|
|
||||||
if ( is_admin() ) {
|
if ( is_admin() ) {
|
||||||
// Add protected states that should show in the admin all list.
|
// Add protected states that should show in the admin all list.
|
||||||
$admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true), 'names', 'and' );
|
$admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
|
||||||
foreach ( (array) $admin_all_states as $state )
|
foreach ( (array) $admin_all_states as $state )
|
||||||
$where .= " OR $wpdb->posts.post_status = '$state'";
|
$where .= " OR $wpdb->posts.post_status = '$state'";
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,25 +67,21 @@ add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
|
||||||
* @uses $wp_taxonomies
|
* @uses $wp_taxonomies
|
||||||
* @see register_taxonomy
|
* @see register_taxonomy
|
||||||
*
|
*
|
||||||
* @param array $args An array of key => value arguments to match against the taxonomies.
|
* @param array $args An array of key => value arguments to match against the taxonomy objects.
|
||||||
* Only taxonomies having attributes that match all arguments are returned.
|
|
||||||
* @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
|
* @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
|
||||||
|
* @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 A list of taxonomy names or objects
|
* @return array A list of taxonomy names or objects
|
||||||
*/
|
*/
|
||||||
function get_taxonomies( $args = array(), $output = 'names' ) {
|
function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
|
||||||
global $wp_taxonomies;
|
global $wp_taxonomies;
|
||||||
|
|
||||||
$taxonomies = array();
|
$field = ('names' == $output) ? 'name' : false;
|
||||||
foreach ( (array) $wp_taxonomies as $taxname => $taxobj )
|
|
||||||
if ( empty($args) || array_intersect_assoc((array) $taxobj, $args) )
|
|
||||||
$taxonomies[$taxname] = $taxobj;
|
|
||||||
|
|
||||||
if ( 'names' == $output )
|
return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
|
||||||
return array_keys($taxonomies);
|
|
||||||
|
|
||||||
return $taxonomies;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all of the taxonomy names that are of $object_type.
|
* Return all of the taxonomy names that are of $object_type.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue