Make WordPress Core

Changeset 25162


Ignore:
Timestamp:
08/29/2013 04:23:30 PM (13 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r25161 r25162  
    13021302        $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
    13031303        $inclusions = '';
    1304         if ( !empty($include) ) {
     1304        if ( ! empty( $include ) ) {
    13051305                $exclude = '';
    13061306                $exclude_tree = '';
    1307                 $interms = wp_parse_id_list($include);
    1308                 foreach ( $interms as $interm ) {
    1309                         if ( empty($inclusions) )
    1310                                 $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
    1311                         else
    1312                                 $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
    1313                 }
    1314         }
    1315 
    1316         if ( !empty($inclusions) )
    1317                 $inclusions .= ')';
     1307                $inclusions = implode( ',', array_map( 'intval', wp_parse_id_list( $include ) ) );
     1308        }
     1309
     1310        if ( ! empty( $inclusions ) )
     1311                $inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )';
    13181312        $where .= $inclusions;
    13191313
    13201314        $exclusions = '';
    13211315        if ( ! empty( $exclude_tree ) ) {
    1322                 $excluded_trunks = wp_parse_id_list( $exclude_tree );
    1323                 foreach ( $excluded_trunks as $extrunk ) {
    1324                         $excluded_children = (array) get_terms( reset( $taxonomies ), array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) );
    1325                         $excluded_children[] = $extrunk;
    1326                         foreach( $excluded_children as $exterm ) {
    1327                                 if ( empty( $exclusions ) )
    1328                                         $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
    1329                                 else
    1330                                         $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    1331                         }
    1332                 }
    1333         }
    1334 
    1335         if ( !empty($exclude) ) {
    1336                 $exterms = wp_parse_id_list($exclude);
    1337                 foreach ( $exterms as $exterm ) {
    1338                         if ( empty($exclusions) )
    1339                                 $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
    1340                         else
    1341                                 $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    1342                 }
    1343         }
    1344 
    1345         if ( !empty($exclusions) )
    1346                 $exclusions .= ')';
    1347         $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
     1316                $exclude_tree = wp_parse_id_list( $exclude_tree );
     1317                $excluded_children = array();
     1318                foreach ( $exclude_tree as $extrunk ) {
     1319                        $excluded_children = array_merge(
     1320                                $excluded_children,
     1321                                (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) )
     1322                        );
     1323                }
     1324                $exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
     1325        }
     1326
     1327        if ( ! empty( $exclude ) ) {
     1328                $exterms = array_map( 'intval', wp_parse_id_list( $exclude ) );
     1329                if ( empty( $exclusions ) )
     1330                        $exclusions = implode( ',', $exterms );
     1331                else
     1332                        $exclusions .= ', ' . implode( ',', $exterms );
     1333        }
     1334
     1335        if ( ! empty( $exclusions ) )
     1336                $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
     1337
     1338        $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args );
    13481339        $where .= $exclusions;
    13491340
  • trunk/tests/tests/term/getTerms.php

    r25161 r25162  
    120120                ), $terms_id_slug );
    121121        }
     122
     123        /**
     124         * @ti
     125         * cket 11823
     126         */
     127        function test_get_terms_include_exclude() {
     128                $term_id1 = $this->factory->tag->create();
     129                $term_id2 = $this->factory->tag->create();
     130                $inc_terms = get_terms( 'post_tag', array(
     131                        'include' => array( $term_id1, $term_id2 ),
     132                        'hide_empty' => false
     133                ) );
     134                $this->assertEquals( array( $term_id1, $term_id2 ), wp_list_pluck( $inc_terms, 'term_id' ) );
     135
     136                $exc_terms = get_terms( 'post_tag', array(
     137                        'exclude' => array( $term_id1, $term_id2 ),
     138                        'hide_empty' => false
     139                ) );
     140                $this->assertEquals( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
     141        }
    122142}
Note: See TracChangeset for help on using the changeset viewer.