From 9b19a3395291da8a28da16aaf84623dec16cb777 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 31 May 2016 12:53:27 +0000 Subject: [PATCH] Ensure that `get_terms()` can accept querystring-style arguments. Prior to [37572], arguments passed to `get_terms()` were passed immediately through `wp_parse_args()`, which made it possible to pass arguments as a querystring (`hide_empty=0`) rather than an array (`array( 'hide_empty' => false )`). [37572] moved default argument parsing into `WP_Term_Query`, while assuming that arguments passed to `get_terms()` would be formatted as an array. To provide compatibility, we now parse all args passed to `get_terms()` into an array before processing. See #35381. git-svn-id: https://develop.svn.wordpress.org/trunk@37599 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 12 ++++++++---- tests/phpunit/tests/term/getTerms.php | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index e910e33cc5..82d16ae1e2 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1183,15 +1183,19 @@ function get_terms( $args = array(), $deprecated = '' ) { * (a) a second non-empty parameter is passed, or * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies) */ - $key_intersect = array_intersect_key( $term_query->query_var_defaults, (array) $args ); + $_args = wp_parse_args( $args ); + $key_intersect = array_intersect_key( $term_query->query_var_defaults, (array) $_args ); $do_legacy_args = $deprecated || empty( $key_intersect ); if ( $do_legacy_args ) { $taxonomies = (array) $args; - $args = $deprecated; + $args = wp_parse_args( $deprecated ); $args['taxonomy'] = $taxonomies; - } elseif ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) { - $args['taxonomy'] = (array) $args['taxonomy']; + } else { + $args = wp_parse_args( $args ); + if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) { + $args['taxonomy'] = (array) $args['taxonomy']; + } } $empty_array = array(); diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 47e199be48..891c5365eb 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -28,6 +28,32 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEqualSets( array( $term ), $found ); } + /** + * @ticket 35495 + * @ticket 35381 + */ + public function test_legacy_params_as_query_string_should_be_properly_parsed() { + register_taxonomy( 'wptests_tax', 'post' ); + $term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + + $found = get_terms( 'wptests_tax', 'hide_empty=0&fields=ids&update_term_meta_cache=0' ); + + $this->assertEqualSets( array( $term ), $found ); + } + + /** + * @ticket 35495 + * @ticket 35381 + */ + public function test_new_params_as_query_string_should_be_properly_parsed() { + register_taxonomy( 'wptests_tax', 'post' ); + $term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + + $found = get_terms( 'taxonomy=wptests_tax&hide_empty=0&fields=ids&update_term_meta_cache=0' ); + + $this->assertEqualSets( array( $term ), $found ); + } + /** * @ticket 35495 */