WordPress.org

Make WordPress Core

Changeset 12658


Ignore:
Timestamp:
01/08/2010 08:44:45 AM (8 years ago)
Author:
westi
Message:

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

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r12656 r12658  
    27512751
    27522752/**
     2753 * Clean up an array, comma- or space-separated list of IDs
     2754 *
     2755 * @since 3.0.0
     2756 *
     2757 * @param array|string $list
     2758 * @return array Sanitized array of IDs
     2759 */
     2760function wp_parse_id_list($list) {
     2761    if ( !is_array($list) )
     2762        $list = preg_split('/[\s,]+/', $list);
     2763
     2764    return array_unique(array_map('absint', $list));
     2765}
     2766
     2767/**
    27532768 * Determines if default embed handlers should be loaded.
    27542769 *
  • trunk/wp-includes/taxonomy.php

    r12598 r12658  
    572572 * terms whose count is 0 according to the given taxonomy.
    573573 *
    574  * exclude - Default is an empty string.  A comma- or space-delimited string
     574 * exclude - Default is an empty array.  An array, comma- or space-delimited string
    575575 * of term ids to exclude from the return array.  If 'include' is non-empty,
    576576 * 'exclude' is ignored.
    577577 *
    578  * exclude_tree - A comma- or space-delimited string of term ids to exclude
    579  * from the return array, along with all of their descendant terms according to
    580  * the primary taxonomy.  If 'include' is non-empty, 'exclude_tree' is ignored.
    581  *
    582  * include - Default is an empty string.  A comma- or space-delimited string
     578 * exclude_tree - Default is an empty array.  An array, comma- or space-delimited
     579 * string of term ids to exclude from the return array, along with all of their
     580 * descendant terms according to the primary taxonomy.  If 'include' is non-empty,
     581 * 'exclude_tree' is ignored.
     582 *
     583 * include - Default is an empty array.  An array, comma- or space-delimited string
    583584 * of term ids to include in the return array.
    584585 *
    585  * number - The maximum number of terms to return.  Default is empty.
     586 * number - The maximum number of terms to return.  Default is to return them all.
    586587 *
    587588 * offset - The number by which to offset the terms query.
     
    652653
    653654    $defaults = array('orderby' => 'name', 'order' => 'ASC',
    654         'hide_empty' => true, 'exclude' => '', 'exclude_tree' => '', 'include' => '',
     655        'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
    655656        'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
    656657        'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
     
    720721        $exclude = '';
    721722        $exclude_tree = '';
    722         $interms = preg_split('/[\s,]+/',$include);
    723         if ( count($interms) ) {
    724             foreach ( (array) $interms as $interm ) {
    725                 if (empty($inclusions))
    726                     $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
    727                 else
    728                     $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
    729             }
     723        $interms = wp_parse_id_list($include);
     724        foreach ( $interms as $interm ) {
     725            if ( empty($inclusions) )
     726                $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
     727            else
     728                $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
    730729        }
    731730    }
     
    736735
    737736    $exclusions = '';
    738     if ( ! empty( $exclude_tree ) ) {
    739         $excluded_trunks = preg_split('/[\s,]+/',$exclude_tree);
    740         foreach( (array) $excluded_trunks as $extrunk ) {
     737    if ( !empty( $exclude_tree ) ) {
     738        $excluded_trunks = wp_parse_id_list($exclude_tree);
     739        foreach ( $excluded_trunks as $extrunk ) {
    741740            $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids'));
    742741            $excluded_children[] = $extrunk;
    743             foreach( (array) $excluded_children as $exterm ) {
    744                 if ( empty($exclusions) )
    745                     $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
    746                 else
    747                     $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    748 
    749             }
    750         }
    751     }
    752     if ( !empty($exclude) ) {
    753         $exterms = preg_split('/[\s,]+/',$exclude);
    754         if ( count($exterms) ) {
    755             foreach ( (array) $exterms as $exterm ) {
     742            foreach( $excluded_children as $exterm ) {
    756743                if ( empty($exclusions) )
    757744                    $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
     
    759746                    $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    760747            }
     748        }
     749    }
     750
     751    if ( !empty($exclude) ) {
     752        $exterms = wp_parse_id_list($exclude);
     753        foreach ( $exterms as $exterm ) {
     754            if ( empty($exclusions) )
     755                $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
     756            else
     757                $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    761758        }
    762759    }
     
    835832            if ( ! $term->count ) {
    836833                $children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
    837                 if( is_array($children) )
     834                if ( is_array($children) )
    838835                    foreach ( $children as $child )
    839836                        if ( $child->count )
Note: See TracChangeset for help on using the changeset viewer.