Make WordPress Core


Ignore:
Timestamp:
05/17/2014 02:46:07 PM (13 years ago)
Author:
wonderboymusic
Message:

Eliminate the use of extract() in get_terms().

See #22400.

File:
1 edited

Legend:

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

    r28464 r28465  
    12801280 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
    12811281 */
    1282 function get_terms($taxonomies, $args = '') {
     1282function get_terms( $taxonomies, $args = '' ) {
    12831283        global $wpdb;
    12841284        $empty_array = array();
    12851285
    12861286        $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
    1287         if ( ! is_array( $taxonomies ) )
     1287        if ( ! is_array( $taxonomies ) ) {
    12881288                $taxonomies = array( $taxonomies );
     1289        }
    12891290
    12901291        foreach ( $taxonomies as $taxonomy ) {
    … …  
    13271328        $args = apply_filters( 'get_terms_args', $args, $taxonomies );
    13281329
    1329         extract($args, EXTR_SKIP);
    1330 
     1330        $child_of = $args['child_of'];
    13311331        if ( $child_of ) {
    13321332                $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
    1333                 if ( ! isset( $hierarchy[ $child_of ] ) )
     1333                if ( ! isset( $hierarchy[ $child_of ] ) ) {
    13341334                        return $empty_array;
    1335         }
    1336 
     1335                }
     1336        }
     1337
     1338        $parent = $args['parent'];
    13371339        if ( $parent ) {
    13381340                $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
    1339                 if ( ! isset( $hierarchy[ $parent ] ) )
     1341                if ( ! isset( $hierarchy[ $parent ] ) ) {
    13401342                        return $empty_array;
     1343                }
    13411344        }
    13421345
    13431346        // $args can be whatever, only use the args defined in defaults to compute the key
    13441347        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    1345         $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
     1348        $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
    13461349        $last_changed = wp_cache_get( 'last_changed', 'terms' );
    13471350        if ( ! $last_changed ) {
    … …  
    13661369        }
    13671370
    1368         $_orderby = strtolower($orderby);
    1369         if ( 'count' == $_orderby )
     1371        $_orderby = strtolower( $args['orderby'] );
     1372        if ( 'count' == $_orderby ) {
    13701373                $orderby = 'tt.count';
    1371         else if ( 'name' == $_orderby )
     1374        } else if ( 'name' == $_orderby ) {
    13721375                $orderby = 't.name';
    1373         else if ( 'slug' == $_orderby )
     1376        } else if ( 'slug' == $_orderby ) {
    13741377                $orderby = 't.slug';
    1375         else if ( 'term_group' == $_orderby )
     1378        } else if ( 'term_group' == $_orderby ) {
    13761379                $orderby = 't.term_group';
    1377         else if ( 'none' == $_orderby )
     1380        } else if ( 'none' == $_orderby ) {
    13781381                $orderby = '';
    1379         elseif ( empty($_orderby) || 'id' == $_orderby )
     1382        } elseif ( empty($_orderby) || 'id' == $_orderby ) {
    13801383                $orderby = 't.term_id';
    1381         else
     1384        } else {
    13821385                $orderby = 't.name';
    1383 
     1386        }
    13841387        /**
    13851388         * Filter the ORDERBY clause of the terms query.
    … …  
    13931396        $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
    13941397
    1395         if ( !empty($orderby) )
     1398        $order = strtoupper( $args['order'] );
     1399        if ( ! empty( $orderby ) ) {
    13961400                $orderby = "ORDER BY $orderby";
    1397         else
     1401        } else {
    13981402                $order = '';
    1399 
    1400         $order = strtoupper( $order );
    1401         if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
     1403        }
     1404
     1405        if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
    14021406                $order = 'ASC';
     1407        }
    14031408
    14041409        $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
     1410
     1411        $exclude = $args['exclude'];
     1412        $exclude_tree = $args['exclude_tree'];
     1413        $include = $args['include'];
     1414
    14051415        $inclusions = '';
    14061416        if ( ! empty( $include ) ) {
    … …  
    14151425        }
    14161426
    1417         $exclusions = '';
    14181427        if ( ! empty( $exclude_tree ) ) {
    14191428                $exclude_tree = wp_parse_id_list( $exclude_tree );
    … …  
    14261435                }
    14271436                $exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
     1437        } else {
     1438                $exclusions = '';
    14281439        }
    14291440
    14301441        if ( ! empty( $exclude ) ) {
    14311442                $exterms = wp_parse_id_list( $exclude );
    1432                 if ( empty( $exclusions ) )
     1443                if ( empty( $exclusions ) ) {
    14331444                        $exclusions = implode( ',', $exterms );
    1434                 else
     1445                } else {
    14351446                        $exclusions .= ', ' . implode( ',', $exterms );
    1436         }
    1437 
    1438         if ( ! empty( $exclusions ) )
     1447                }
     1448        }
     1449
     1450        if ( ! empty( $exclusions ) ) {
    14391451                $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
     1452        }
    14401453
    14411454        /**
    … …  
    14501463        $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
    14511464
    1452         if ( ! empty( $exclusions ) )
     1465        if ( ! empty( $exclusions ) ) {
    14531466                $where .= $exclusions;
    1454 
    1455         if ( !empty($slug) ) {
    1456                 $slug = sanitize_title($slug);
     1467        }
     1468
     1469        if ( ! empty( $args['slug'] ) ) {
     1470                $slug = sanitize_title( $args['slug'] );
    14571471                $where .= " AND t.slug = '$slug'";
    14581472        }
    14591473
    1460         if ( !empty($name__like) ) {
    1461                 $name__like = like_escape( $name__like );
     1474        if ( ! empty( $args['name__like'] ) ) {
     1475                $name__like = like_escape( $args['name__like'] );
    14621476                $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' );
    14631477        }
    14641478
    1465         if ( ! empty( $description__like ) ) {
    1466                 $description__like = like_escape( $description__like );
     1479        if ( ! empty( $args['description__like'] ) ) {
     1480                $description__like = like_escape( $args['description__like'] );
    14671481                $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' );
    14681482        }
    … …  
    14731487        }
    14741488
    1475         if ( 'count' == $fields )
     1489        $hierarchical = $args['hierarchical'];
     1490        if ( 'count' == $args['fields'] ) {
    14761491                $hierarchical = false;
    1477 
    1478         if ( $hide_empty && !$hierarchical )
     1492        }
     1493        if ( $args['hide_empty'] && !$hierarchical ) {
    14791494                $where .= ' AND tt.count > 0';
     1495        }
     1496
     1497        $number = $args['number'];
     1498        $offset = $args['offset'];
    14801499
    14811500        // don't limit the query results when we have to descend the family tree
    14821501        if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
    1483                 if ( $offset )
     1502                if ( $offset ) {
    14841503                        $limits = 'LIMIT ' . $offset . ',' . $number;
    1485                 else
     1504                } else {
    14861505                        $limits = 'LIMIT ' . $number;
     1506                }
    14871507        } else {
    14881508                $limits = '';
    14891509        }
    14901510
    1491         if ( ! empty( $search ) ) {
    1492                 $search = like_escape( $search );
     1511        if ( ! empty( $args['search'] ) ) {
     1512                $search = like_escape( $args['search'] );
    14931513                $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', '%' . $search . '%', '%' . $search . '%' );
    14941514        }
    14951515
    14961516        $selects = array();
    1497         switch ( $fields ) {
     1517        switch ( $args['fields'] ) {
    14981518                case 'all':
    14991519                        $selects = array( 't.*', 'tt.*' );
    … …  
    15191539        }
    15201540
    1521         $_fields = $fields;
     1541        $_fields = $args['fields'];
    15221542
    15231543        /**
    … …  
    15461566         */
    15471567        $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
    1548         foreach ( $pieces as $piece )
     1568        foreach ( $pieces as $piece ) {
    15491569                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    1550 
     1570        }
    15511571        $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
    15521572
    1553         $fields = $_fields;
    1554 
    1555         if ( 'count' == $fields ) {
     1573        if ( 'count' == $_fields ) {
    15561574                $term_count = $wpdb->get_var($query);
    15571575                return $term_count;
    … …  
    15591577
    15601578        $terms = $wpdb->get_results($query);
    1561         if ( 'all' == $fields ) {
     1579        if ( 'all' == $_fields ) {
    15621580                update_term_cache($terms);
    15631581        }
    … …  
    15731591        if ( $child_of ) {
    15741592                $children = _get_term_hierarchy( reset( $taxonomies ) );
    1575                 if ( ! empty( $children ) )
     1593                if ( ! empty( $children ) ) {
    15761594                        $terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) );
     1595                }
    15771596        }
    15781597
    15791598        // Update term counts to include children.
    1580         if ( $pad_counts && 'all' == $fields )
     1599        if ( $args['pad_counts'] && 'all' == $_fields ) {
    15811600                _pad_term_counts( $terms, reset( $taxonomies ) );
    1582 
     1601        }
    15831602        // Make sure we show empty categories that have children.
    1584         if ( $hierarchical && $hide_empty && is_array( $terms ) ) {
     1603        if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
    15851604                foreach ( $terms as $k => $term ) {
    15861605                        if ( ! $term->count ) {
    … …  
    16031622
    16041623        $_terms = array();
    1605         if ( 'id=>parent' == $fields ) {
    1606                 while ( $term = array_shift( $terms ) )
     1624        if ( 'id=>parent' == $_fields ) {
     1625                while ( $term = array_shift( $terms ) ) {
    16071626                        $_terms[$term->term_id] = $term->parent;
    1608         } elseif ( 'ids' == $fields ) {
    1609                 while ( $term = array_shift( $terms ) )
     1627                }
     1628        } elseif ( 'ids' == $_fields ) {
     1629                while ( $term = array_shift( $terms ) ) {
    16101630                        $_terms[] = $term->term_id;
    1611         } elseif ( 'names' == $fields ) {
    1612                 while ( $term = array_shift( $terms ) )
     1631                }
     1632        } elseif ( 'names' == $_fields ) {
     1633                while ( $term = array_shift( $terms ) ) {
    16131634                        $_terms[] = $term->name;
    1614         } elseif ( 'id=>name' == $fields ) {
    1615                 while ( $term = array_shift( $terms ) )
     1635                }
     1636        } elseif ( 'id=>name' == $_fields ) {
     1637                while ( $term = array_shift( $terms ) ) {
    16161638                        $_terms[$term->term_id] = $term->name;
    1617         } elseif ( 'id=>slug' == $fields ) {
    1618                 while ( $term = array_shift( $terms ) )
     1639                }
     1640        } elseif ( 'id=>slug' == $_fields ) {
     1641                while ( $term = array_shift( $terms ) ) {
    16191642                        $_terms[$term->term_id] = $term->slug;
    1620         }
    1621 
    1622         if ( ! empty( $_terms ) )
     1643                }
     1644        }
     1645
     1646        if ( ! empty( $_terms ) ) {
    16231647                $terms = $_terms;
    1624 
    1625         if ( $number && is_array( $terms ) && count( $terms ) > $number )
     1648        }
     1649
     1650        if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
    16261651                $terms = array_slice( $terms, $offset, $number );
     1652        }
    16271653
    16281654        wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
Note: See TracChangeset for help on using the changeset viewer.