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