From 4cead42b2601912e2e961c6c73807227f5e962ab Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 2 Jan 2015 21:33:33 +0000 Subject: [PATCH] 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 --- src/wp-includes/category-template.php | 7 +++++- src/wp-includes/taxonomy.php | 14 +++++++++++- tests/phpunit/tests/term/getTerms.php | 33 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index f8c6a56aa1..7f2c539676 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -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; diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 63d53a7762..1e3e2a6565 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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'] ); diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 865357a715..8e0abce271 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -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';