Eliminate the use of `extract()` in `get_terms()`.
See #22400. Built from https://develop.svn.wordpress.org/trunk@28465 git-svn-id: http://core.svn.wordpress.org/trunk@28292 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
c1b5670a00
commit
bec9993f38
|
@ -1284,8 +1284,9 @@ function get_terms($taxonomies, $args = '') {
|
||||||
$empty_array = array();
|
$empty_array = array();
|
||||||
|
|
||||||
$single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
|
$single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
|
||||||
if ( ! is_array( $taxonomies ) )
|
if ( ! is_array( $taxonomies ) ) {
|
||||||
$taxonomies = array( $taxonomies );
|
$taxonomies = array( $taxonomies );
|
||||||
|
}
|
||||||
|
|
||||||
foreach ( $taxonomies as $taxonomy ) {
|
foreach ( $taxonomies as $taxonomy ) {
|
||||||
if ( ! taxonomy_exists($taxonomy) ) {
|
if ( ! taxonomy_exists($taxonomy) ) {
|
||||||
|
@ -1326,23 +1327,25 @@ function get_terms($taxonomies, $args = '') {
|
||||||
*/
|
*/
|
||||||
$args = apply_filters( 'get_terms_args', $args, $taxonomies );
|
$args = apply_filters( 'get_terms_args', $args, $taxonomies );
|
||||||
|
|
||||||
extract($args, EXTR_SKIP);
|
$child_of = $args['child_of'];
|
||||||
|
|
||||||
if ( $child_of ) {
|
if ( $child_of ) {
|
||||||
$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
|
$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
|
||||||
if ( ! isset( $hierarchy[ $child_of ] ) )
|
if ( ! isset( $hierarchy[ $child_of ] ) ) {
|
||||||
return $empty_array;
|
return $empty_array;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = $args['parent'];
|
||||||
if ( $parent ) {
|
if ( $parent ) {
|
||||||
$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
|
$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
|
||||||
if ( ! isset( $hierarchy[ $parent ] ) )
|
if ( ! isset( $hierarchy[ $parent ] ) ) {
|
||||||
return $empty_array;
|
return $empty_array;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// $args can be whatever, only use the args defined in defaults to compute the key
|
// $args can be whatever, only use the args defined in defaults to compute the key
|
||||||
$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
|
$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
|
||||||
$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
|
$key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
|
||||||
$last_changed = wp_cache_get( 'last_changed', 'terms' );
|
$last_changed = wp_cache_get( 'last_changed', 'terms' );
|
||||||
if ( ! $last_changed ) {
|
if ( ! $last_changed ) {
|
||||||
$last_changed = microtime();
|
$last_changed = microtime();
|
||||||
|
@ -1365,22 +1368,22 @@ function get_terms($taxonomies, $args = '') {
|
||||||
return $cache;
|
return $cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
$_orderby = strtolower($orderby);
|
$_orderby = strtolower( $args['orderby'] );
|
||||||
if ( 'count' == $_orderby )
|
if ( 'count' == $_orderby ) {
|
||||||
$orderby = 'tt.count';
|
$orderby = 'tt.count';
|
||||||
else if ( 'name' == $_orderby )
|
} else if ( 'name' == $_orderby ) {
|
||||||
$orderby = 't.name';
|
$orderby = 't.name';
|
||||||
else if ( 'slug' == $_orderby )
|
} else if ( 'slug' == $_orderby ) {
|
||||||
$orderby = 't.slug';
|
$orderby = 't.slug';
|
||||||
else if ( 'term_group' == $_orderby )
|
} else if ( 'term_group' == $_orderby ) {
|
||||||
$orderby = 't.term_group';
|
$orderby = 't.term_group';
|
||||||
else if ( 'none' == $_orderby )
|
} else if ( 'none' == $_orderby ) {
|
||||||
$orderby = '';
|
$orderby = '';
|
||||||
elseif ( empty($_orderby) || 'id' == $_orderby )
|
} elseif ( empty($_orderby) || 'id' == $_orderby ) {
|
||||||
$orderby = 't.term_id';
|
$orderby = 't.term_id';
|
||||||
else
|
} else {
|
||||||
$orderby = 't.name';
|
$orderby = 't.name';
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Filter the ORDERBY clause of the terms query.
|
* Filter the ORDERBY clause of the terms query.
|
||||||
*
|
*
|
||||||
|
@ -1392,16 +1395,23 @@ function get_terms($taxonomies, $args = '') {
|
||||||
*/
|
*/
|
||||||
$orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
|
$orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
|
||||||
|
|
||||||
if ( !empty($orderby) )
|
$order = strtoupper( $args['order'] );
|
||||||
|
if ( ! empty( $orderby ) ) {
|
||||||
$orderby = "ORDER BY $orderby";
|
$orderby = "ORDER BY $orderby";
|
||||||
else
|
} else {
|
||||||
$order = '';
|
$order = '';
|
||||||
|
}
|
||||||
|
|
||||||
$order = strtoupper( $order );
|
if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
|
||||||
if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
|
|
||||||
$order = 'ASC';
|
$order = 'ASC';
|
||||||
|
}
|
||||||
|
|
||||||
$where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
|
$where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
|
||||||
|
|
||||||
|
$exclude = $args['exclude'];
|
||||||
|
$exclude_tree = $args['exclude_tree'];
|
||||||
|
$include = $args['include'];
|
||||||
|
|
||||||
$inclusions = '';
|
$inclusions = '';
|
||||||
if ( ! empty( $include ) ) {
|
if ( ! empty( $include ) ) {
|
||||||
$exclude = '';
|
$exclude = '';
|
||||||
|
@ -1414,7 +1424,6 @@ function get_terms($taxonomies, $args = '') {
|
||||||
$where .= $inclusions;
|
$where .= $inclusions;
|
||||||
}
|
}
|
||||||
|
|
||||||
$exclusions = '';
|
|
||||||
if ( ! empty( $exclude_tree ) ) {
|
if ( ! empty( $exclude_tree ) ) {
|
||||||
$exclude_tree = wp_parse_id_list( $exclude_tree );
|
$exclude_tree = wp_parse_id_list( $exclude_tree );
|
||||||
$excluded_children = $exclude_tree;
|
$excluded_children = $exclude_tree;
|
||||||
|
@ -1425,18 +1434,22 @@ function get_terms($taxonomies, $args = '') {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
|
$exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
|
||||||
|
} else {
|
||||||
|
$exclusions = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! empty( $exclude ) ) {
|
if ( ! empty( $exclude ) ) {
|
||||||
$exterms = wp_parse_id_list( $exclude );
|
$exterms = wp_parse_id_list( $exclude );
|
||||||
if ( empty( $exclusions ) )
|
if ( empty( $exclusions ) ) {
|
||||||
$exclusions = implode( ',', $exterms );
|
$exclusions = implode( ',', $exterms );
|
||||||
else
|
} else {
|
||||||
$exclusions .= ', ' . implode( ',', $exterms );
|
$exclusions .= ', ' . implode( ',', $exterms );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! empty( $exclusions ) )
|
if ( ! empty( $exclusions ) ) {
|
||||||
$exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
|
$exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the terms to exclude from the terms query.
|
* Filter the terms to exclude from the terms query.
|
||||||
|
@ -1449,21 +1462,22 @@ function get_terms($taxonomies, $args = '') {
|
||||||
*/
|
*/
|
||||||
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
|
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
|
||||||
|
|
||||||
if ( ! empty( $exclusions ) )
|
if ( ! empty( $exclusions ) ) {
|
||||||
$where .= $exclusions;
|
$where .= $exclusions;
|
||||||
|
}
|
||||||
|
|
||||||
if ( !empty($slug) ) {
|
if ( ! empty( $args['slug'] ) ) {
|
||||||
$slug = sanitize_title($slug);
|
$slug = sanitize_title( $args['slug'] );
|
||||||
$where .= " AND t.slug = '$slug'";
|
$where .= " AND t.slug = '$slug'";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !empty($name__like) ) {
|
if ( ! empty( $args['name__like'] ) ) {
|
||||||
$name__like = like_escape( $name__like );
|
$name__like = like_escape( $args['name__like'] );
|
||||||
$where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' );
|
$where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! empty( $description__like ) ) {
|
if ( ! empty( $args['description__like'] ) ) {
|
||||||
$description__like = like_escape( $description__like );
|
$description__like = like_escape( $args['description__like'] );
|
||||||
$where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' );
|
$where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1472,29 +1486,35 @@ function get_terms($taxonomies, $args = '') {
|
||||||
$where .= " AND tt.parent = '$parent'";
|
$where .= " AND tt.parent = '$parent'";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'count' == $fields )
|
$hierarchical = $args['hierarchical'];
|
||||||
|
if ( 'count' == $args['fields'] ) {
|
||||||
$hierarchical = false;
|
$hierarchical = false;
|
||||||
|
}
|
||||||
if ( $hide_empty && !$hierarchical )
|
if ( $args['hide_empty'] && !$hierarchical ) {
|
||||||
$where .= ' AND tt.count > 0';
|
$where .= ' AND tt.count > 0';
|
||||||
|
}
|
||||||
|
|
||||||
|
$number = $args['number'];
|
||||||
|
$offset = $args['offset'];
|
||||||
|
|
||||||
// don't limit the query results when we have to descend the family tree
|
// don't limit the query results when we have to descend the family tree
|
||||||
if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
|
if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
|
||||||
if ( $offset )
|
if ( $offset ) {
|
||||||
$limits = 'LIMIT ' . $offset . ',' . $number;
|
$limits = 'LIMIT ' . $offset . ',' . $number;
|
||||||
else
|
} else {
|
||||||
$limits = 'LIMIT ' . $number;
|
$limits = 'LIMIT ' . $number;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$limits = '';
|
$limits = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! empty( $search ) ) {
|
if ( ! empty( $args['search'] ) ) {
|
||||||
$search = like_escape( $search );
|
$search = like_escape( $args['search'] );
|
||||||
$where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', '%' . $search . '%', '%' . $search . '%' );
|
$where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', '%' . $search . '%', '%' . $search . '%' );
|
||||||
}
|
}
|
||||||
|
|
||||||
$selects = array();
|
$selects = array();
|
||||||
switch ( $fields ) {
|
switch ( $args['fields'] ) {
|
||||||
case 'all':
|
case 'all':
|
||||||
$selects = array( 't.*', 'tt.*' );
|
$selects = array( 't.*', 'tt.*' );
|
||||||
break;
|
break;
|
||||||
|
@ -1518,7 +1538,7 @@ function get_terms($taxonomies, $args = '') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$_fields = $fields;
|
$_fields = $args['fields'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the fields to select in the terms query.
|
* Filter the fields to select in the terms query.
|
||||||
|
@ -1545,20 +1565,18 @@ function get_terms($taxonomies, $args = '') {
|
||||||
* @param array $args An array of terms query arguments.
|
* @param array $args An array of terms query arguments.
|
||||||
*/
|
*/
|
||||||
$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
|
$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
|
||||||
foreach ( $pieces as $piece )
|
foreach ( $pieces as $piece ) {
|
||||||
$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
|
$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
|
||||||
|
}
|
||||||
$query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
|
$query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
|
||||||
|
|
||||||
$fields = $_fields;
|
if ( 'count' == $_fields ) {
|
||||||
|
|
||||||
if ( 'count' == $fields ) {
|
|
||||||
$term_count = $wpdb->get_var($query);
|
$term_count = $wpdb->get_var($query);
|
||||||
return $term_count;
|
return $term_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
$terms = $wpdb->get_results($query);
|
$terms = $wpdb->get_results($query);
|
||||||
if ( 'all' == $fields ) {
|
if ( 'all' == $_fields ) {
|
||||||
update_term_cache($terms);
|
update_term_cache($terms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1572,16 +1590,17 @@ function get_terms($taxonomies, $args = '') {
|
||||||
|
|
||||||
if ( $child_of ) {
|
if ( $child_of ) {
|
||||||
$children = _get_term_hierarchy( reset( $taxonomies ) );
|
$children = _get_term_hierarchy( reset( $taxonomies ) );
|
||||||
if ( ! empty( $children ) )
|
if ( ! empty( $children ) ) {
|
||||||
$terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) );
|
$terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update term counts to include children.
|
// Update term counts to include children.
|
||||||
if ( $pad_counts && 'all' == $fields )
|
if ( $args['pad_counts'] && 'all' == $_fields ) {
|
||||||
_pad_term_counts( $terms, reset( $taxonomies ) );
|
_pad_term_counts( $terms, reset( $taxonomies ) );
|
||||||
|
}
|
||||||
// Make sure we show empty categories that have children.
|
// Make sure we show empty categories that have children.
|
||||||
if ( $hierarchical && $hide_empty && is_array( $terms ) ) {
|
if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
|
||||||
foreach ( $terms as $k => $term ) {
|
foreach ( $terms as $k => $term ) {
|
||||||
if ( ! $term->count ) {
|
if ( ! $term->count ) {
|
||||||
$children = get_term_children( $term->term_id, reset( $taxonomies ) );
|
$children = get_term_children( $term->term_id, reset( $taxonomies ) );
|
||||||
|
@ -1602,28 +1621,35 @@ function get_terms($taxonomies, $args = '') {
|
||||||
reset( $terms );
|
reset( $terms );
|
||||||
|
|
||||||
$_terms = array();
|
$_terms = array();
|
||||||
if ( 'id=>parent' == $fields ) {
|
if ( 'id=>parent' == $_fields ) {
|
||||||
while ( $term = array_shift( $terms ) )
|
while ( $term = array_shift( $terms ) ) {
|
||||||
$_terms[$term->term_id] = $term->parent;
|
$_terms[$term->term_id] = $term->parent;
|
||||||
} elseif ( 'ids' == $fields ) {
|
}
|
||||||
while ( $term = array_shift( $terms ) )
|
} elseif ( 'ids' == $_fields ) {
|
||||||
|
while ( $term = array_shift( $terms ) ) {
|
||||||
$_terms[] = $term->term_id;
|
$_terms[] = $term->term_id;
|
||||||
} elseif ( 'names' == $fields ) {
|
}
|
||||||
while ( $term = array_shift( $terms ) )
|
} elseif ( 'names' == $_fields ) {
|
||||||
|
while ( $term = array_shift( $terms ) ) {
|
||||||
$_terms[] = $term->name;
|
$_terms[] = $term->name;
|
||||||
} elseif ( 'id=>name' == $fields ) {
|
}
|
||||||
while ( $term = array_shift( $terms ) )
|
} elseif ( 'id=>name' == $_fields ) {
|
||||||
|
while ( $term = array_shift( $terms ) ) {
|
||||||
$_terms[$term->term_id] = $term->name;
|
$_terms[$term->term_id] = $term->name;
|
||||||
} elseif ( 'id=>slug' == $fields ) {
|
}
|
||||||
while ( $term = array_shift( $terms ) )
|
} elseif ( 'id=>slug' == $_fields ) {
|
||||||
|
while ( $term = array_shift( $terms ) ) {
|
||||||
$_terms[$term->term_id] = $term->slug;
|
$_terms[$term->term_id] = $term->slug;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! empty( $_terms ) )
|
if ( ! empty( $_terms ) ) {
|
||||||
$terms = $_terms;
|
$terms = $_terms;
|
||||||
|
}
|
||||||
|
|
||||||
if ( $number && is_array( $terms ) && count( $terms ) > $number )
|
if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
|
||||||
$terms = array_slice( $terms, $offset, $number );
|
$terms = array_slice( $terms, $offset, $number );
|
||||||
|
}
|
||||||
|
|
||||||
wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
|
wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue