Add support to get_terms() to allow 'include' & 'exclude' args to be arrays(). Fixes #11076 props scribu.

git-svn-id: http://svn.automattic.com/wordpress/trunk@12658 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2010-01-08 08:44:45 +00:00
parent f05ee1844f
commit 4d0c9da793
2 changed files with 41 additions and 29 deletions

View File

@ -2749,6 +2749,21 @@ function wp_parse_args( $args, $defaults = '' ) {
return $r;
}
/**
* Clean up an array, comma- or space-separated list of IDs
*
* @since 3.0.0
*
* @param array|string $list
* @return array Sanitized array of IDs
*/
function wp_parse_id_list($list) {
if ( !is_array($list) )
$list = preg_split('/[\s,]+/', $list);
return array_unique(array_map('absint', $list));
}
/**
* Determines if default embed handlers should be loaded.
*

View File

@ -571,18 +571,19 @@ function get_term_to_edit( $id, $taxonomy ) {
* hide_empty - Default is true. Will not return empty terms, which means
* terms whose count is 0 according to the given taxonomy.
*
* exclude - Default is an empty string. A comma- or space-delimited string
* exclude - Default is an empty array. An array, comma- or space-delimited string
* of term ids to exclude from the return array. If 'include' is non-empty,
* 'exclude' is ignored.
*
* exclude_tree - A comma- or space-delimited string of term ids to exclude
* from the return array, along with all of their descendant terms according to
* the primary taxonomy. If 'include' is non-empty, 'exclude_tree' is ignored.
* exclude_tree - Default is an empty array. An array, comma- or space-delimited
* string of term ids to exclude from the return array, along with all of their
* descendant terms according to the primary taxonomy. If 'include' is non-empty,
* 'exclude_tree' is ignored.
*
* include - Default is an empty string. A comma- or space-delimited string
* include - Default is an empty array. An array, comma- or space-delimited string
* of term ids to include in the return array.
*
* number - The maximum number of terms to return. Default is empty.
* number - The maximum number of terms to return. Default is to return them all.
*
* offset - The number by which to offset the terms query.
*
@ -651,7 +652,7 @@ function &get_terms($taxonomies, $args = '') {
$in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => '', 'exclude_tree' => '', 'include' => '',
'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
'pad_counts' => false, 'offset' => '', 'search' => '');
@ -719,47 +720,43 @@ function &get_terms($taxonomies, $args = '') {
if ( !empty($include) ) {
$exclude = '';
$exclude_tree = '';
$interms = preg_split('/[\s,]+/',$include);
if ( count($interms) ) {
foreach ( (array) $interms as $interm ) {
if (empty($inclusions))
$interms = wp_parse_id_list($include);
foreach ( $interms as $interm ) {
if ( empty($inclusions) )
$inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
else
$inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
}
}
}
if ( !empty($inclusions) )
$inclusions .= ')';
$where .= $inclusions;
$exclusions = '';
if ( ! empty( $exclude_tree ) ) {
$excluded_trunks = preg_split('/[\s,]+/',$exclude_tree);
foreach( (array) $excluded_trunks as $extrunk ) {
if ( !empty( $exclude_tree ) ) {
$excluded_trunks = wp_parse_id_list($exclude_tree);
foreach ( $excluded_trunks as $extrunk ) {
$excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids'));
$excluded_children[] = $extrunk;
foreach( (array) $excluded_children as $exterm ) {
foreach( $excluded_children as $exterm ) {
if ( empty($exclusions) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
}
}
}
}
}
if ( !empty($exclude) ) {
$exterms = preg_split('/[\s,]+/',$exclude);
if ( count($exterms) ) {
foreach ( (array) $exterms as $exterm ) {
$exterms = wp_parse_id_list($exclude);
foreach ( $exterms as $exterm ) {
if ( empty($exclusions) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
}
}
if ( !empty($exclusions) )
$exclusions .= ')';
@ -834,7 +831,7 @@ function &get_terms($taxonomies, $args = '') {
foreach ( $terms as $k => $term ) {
if ( ! $term->count ) {
$children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
if( is_array($children) )
if ( is_array($children) )
foreach ( $children as $child )
if ( $child->count )
continue 2;