WordPress.org

Make WordPress Core

Ticket #11076: get_terms.4.diff

File get_terms.4.diff, 5.0 KB (added by scribu, 8 years ago)

Rename helper function to wp_parse_id_list()

  • wp-includes/taxonomy.php

     
    571571 * hide_empty - Default is true. Will not return empty terms, which means
    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.
     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.
    581582 *
    582  * include - Default is an empty string.  A comma- or space-delimited string
     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 empty string.
    586587 *
    587588 * offset - The number by which to offset the terms query.
    588589 *
     
    651652        $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
    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' => '',
    657658                'pad_counts' => false, 'offset' => '', 'search' => '');
     
    719720        if ( !empty($include) ) {
    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        }
    732731
     
    735734        $where .= $inclusions;
    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 ) {
     742                        foreach( $excluded_children as $exterm ) {
    744743                                if ( empty($exclusions) )
    745744                                        $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
    746745                                else
    747746                                        $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    748 
    749747                        }
    750748                }
    751749        }
     750
    752751        if ( !empty($exclude) ) {
    753                 $exterms = preg_split('/[\s,]+/',$exclude);
    754                 if ( count($exterms) ) {
    755                         foreach ( (array) $exterms as $exterm ) {
    756                                 if ( empty($exclusions) )
    757                                         $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
    758                                 else
    759                                         $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
    760                         }
     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        }
    763760
     
    834831                foreach ( $terms as $k => $term ) {
    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 )
    840837                                                        continue 2;
  • wp-includes/functions.php

     
    27872787}
    27882788
    27892789/**
     2790 * Clean up an array, comma- or space-separated list of IDs
     2791 *
     2792 * @since 3.0.0
     2793 *
     2794 * @param array|string $list
     2795 * @return array
     2796 */
     2797function wp_parse_id_list($list) {
     2798        if ( !is_array($list) )
     2799                $list = preg_split('/[\s,]+/', $list);
     2800
     2801        return array_unique(array_map('absint', $list));
     2802}
     2803
     2804/**
    27902805 * Determines if default embed handlers should be loaded.
    27912806 *
    27922807 * Checks to make sure that the embeds library hasn't already been loaded. If