Improve the include / exclude SQL generation in `get_terms()` by using `IN` and `NOT IN` where applicable. Adds unit tests for include / exclude.

Props sirzooro, duck_.

Fixes #11823.


Built from https://develop.svn.wordpress.org/trunk@25162


git-svn-id: http://core.svn.wordpress.org/trunk@25141 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-08-29 16:24:09 +00:00
parent 2f2f50c659
commit 4b449d38b4
1 changed files with 22 additions and 31 deletions

View File

@ -1304,46 +1304,37 @@ function get_terms($taxonomies, $args = '') {
if ( ! empty( $include ) ) {
$exclude = '';
$exclude_tree = '';
$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) . ' ';
}
$inclusions = implode( ',', array_map( 'intval', wp_parse_id_list( $include ) ) );
}
if ( ! empty( $inclusions ) )
$inclusions .= ')';
$inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )';
$where .= $inclusions;
$exclusions = '';
if ( ! empty( $exclude_tree ) ) {
$excluded_trunks = wp_parse_id_list( $exclude_tree );
foreach ( $excluded_trunks as $extrunk ) {
$excluded_children = (array) get_terms( reset( $taxonomies ), array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) );
$excluded_children[] = $extrunk;
foreach( $excluded_children as $exterm ) {
if ( empty( $exclusions ) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
$exclude_tree = wp_parse_id_list( $exclude_tree );
$excluded_children = array();
foreach ( $exclude_tree as $extrunk ) {
$excluded_children = array_merge(
$excluded_children,
(array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) )
);
}
$exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
}
if ( ! empty( $exclude ) ) {
$exterms = wp_parse_id_list($exclude);
foreach ( $exterms as $exterm ) {
$exterms = array_map( 'intval', wp_parse_id_list( $exclude ) );
if ( empty( $exclusions ) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
$exclusions = implode( ',', $exterms );
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
$exclusions .= ', ' . implode( ',', $exterms );
}
if ( ! empty( $exclusions ) )
$exclusions .= ')';
$exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args );
$where .= $exclusions;