Introduce 'name' parameter for `get_terms()`.

This enhancement requires a modification in the way that `wp_dropdown_categories()`
prepares its arguments for `get_terms()`, so that its unrelated 'name' param is
not mistaken for the new 'name' argument in `get_terms()`.

Props danielbachhuber.
Fixes #30611.

git-svn-id: https://develop.svn.wordpress.org/trunk@31024 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-01-02 21:33:33 +00:00
parent 95fd7b1590
commit 4cead42b26
3 changed files with 52 additions and 2 deletions

View File

@ -376,7 +376,12 @@ function wp_dropdown_categories( $args = '' ) {
if ( (int) $tab_index > 0 ) {
$tab_index_attribute = " tabindex=\"$tab_index\"";
}
$categories = get_terms( $r['taxonomy'], $r );
// Avoid clashes with the 'name' param of get_terms().
$get_terms_args = $r;
unset( $get_terms_args['name'] );
$categories = get_terms( $r['taxonomy'], $get_terms_args );
$name = esc_attr( $r['name'] );
$class = esc_attr( $r['class'] );
$id = $r['id'] ? esc_attr( $r['id'] ) : $name;

View File

@ -1548,6 +1548,7 @@ function get_term_to_edit( $id, $taxonomy ) {
* along with the $args array.
*
* @since 2.3.0
* @since 4.2.0 Introduced 'name' parameter.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
@ -1578,6 +1579,7 @@ function get_term_to_edit( $id, $taxonomy ) {
* @type string $fields Term fields to query for. Accepts 'all' (returns an array of
* term objects), 'ids' or 'names' (returns an array of integers
* or strings, respectively. Default 'all'.
* @type string|array $name Optional. Name or array of names to return term(s) for. Default empty.
* @type string|array $slug Optional. Slug or array of slugs to return term(s) for. Default empty.
* @type bool $hierarchical Whether to include terms that have non-empty descendants (even
* if $hide_empty is set to true). Default true.
@ -1618,7 +1620,7 @@ function get_terms( $taxonomies, $args = '' ) {
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
$args = wp_parse_args( $args, $defaults );
@ -1795,6 +1797,16 @@ function get_terms( $taxonomies, $args = '' ) {
$where .= $exclusions;
}
if ( ! empty( $args['name'] ) ) {
if ( is_array( $args['name'] ) ) {
$name = array_map( 'sanitize_text_field', $args['name'] );
$where .= " AND t.name IN ('" . implode( "', '", $name ) . "')";
} else {
$name = sanitize_text_field( $args['name'] );
$where .= $wpdb->prepare( " AND t.name = %s", $name );
}
}
if ( ! empty( $args['slug'] ) ) {
if ( is_array( $args['slug'] ) ) {
$slug = array_map( 'sanitize_title', $args['slug'] );

View File

@ -424,6 +424,39 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$this->assertEquals( array( $t1, $t3 ), $found );
}
/**
* @ticket 30611
*/
public function test_get_terms_by_name() {
$t1 = $this->factory->tag->create( array( 'name' => 'Foo' ) );
$t2 = $this->factory->tag->create( array( 'name' => 'Bar' ) );
$found = get_terms( 'post_tag', array(
'hide_empty' => false,
'fields' => 'ids',
'name' => 'Foo',
) );
$this->assertEquals( array( $t1 ), $found );
}
/**
* @ticket 30611
*/
public function test_get_terms_by_multiple_names() {
$t1 = $this->factory->tag->create( array( 'name' => 'Foo' ) );
$t2 = $this->factory->tag->create( array( 'name' => 'Bar' ) );
$t3 = $this->factory->tag->create( array( 'name' => 'Barry' ) );
$found = get_terms( 'post_tag', array(
'hide_empty' => false,
'fields' => 'ids',
'name' => array( 'Foo', 'Barry' )
) );
$this->assertEqualSets( array( $t3, $t1 ), $found );
}
public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() {
// Set up a clean taxonomy.
$tax = 'hierarchical_fields';