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. git-svn-id: https://develop.svn.wordpress.org/trunk@25162 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
69785902db
commit
a4a765c5fd
@ -1301,50 +1301,41 @@ function get_terms($taxonomies, $args = '') {
|
||||
|
||||
$where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
|
||||
$inclusions = '';
|
||||
if ( !empty($include) ) {
|
||||
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 .= ')';
|
||||
if ( ! empty( $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 ) {
|
||||
if ( empty($exclusions) )
|
||||
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
|
||||
else
|
||||
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
|
||||
}
|
||||
if ( ! empty( $exclude ) ) {
|
||||
$exterms = array_map( 'intval', wp_parse_id_list( $exclude ) );
|
||||
if ( empty( $exclusions ) )
|
||||
$exclusions = implode( ',', $exterms );
|
||||
else
|
||||
$exclusions .= ', ' . implode( ',', $exterms );
|
||||
}
|
||||
|
||||
if ( !empty($exclusions) )
|
||||
$exclusions .= ')';
|
||||
$exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
|
||||
if ( ! empty( $exclusions ) )
|
||||
$exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
|
||||
|
||||
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args );
|
||||
$where .= $exclusions;
|
||||
|
||||
if ( !empty($slug) ) {
|
||||
|
@ -119,4 +119,24 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
|
||||
$term_id2 => 'hoo'
|
||||
), $terms_id_slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ti
|
||||
* cket 11823
|
||||
*/
|
||||
function test_get_terms_include_exclude() {
|
||||
$term_id1 = $this->factory->tag->create();
|
||||
$term_id2 = $this->factory->tag->create();
|
||||
$inc_terms = get_terms( 'post_tag', array(
|
||||
'include' => array( $term_id1, $term_id2 ),
|
||||
'hide_empty' => false
|
||||
) );
|
||||
$this->assertEquals( array( $term_id1, $term_id2 ), wp_list_pluck( $inc_terms, 'term_id' ) );
|
||||
|
||||
$exc_terms = get_terms( 'post_tag', array(
|
||||
'exclude' => array( $term_id1, $term_id2 ),
|
||||
'hide_empty' => false
|
||||
) );
|
||||
$this->assertEquals( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user