From f6a2067d37967eefa422f4647b1685816ecc5ecc Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 2 Jun 2016 14:37:55 +0000 Subject: [PATCH] Taxonomy: Don't pass results of 'count' query through 'get_terms' filter. Use of the 'get_terms' filter was consolidated in [37572], with the introduction of `WP_Term_Query`. At that time, the result of 'count=true' queries began being filtered by 'get_terms'. This breaks existing 'get_terms' callbacks, which often assume that the returned value will be an array or a `WP_Error` object. Props JustinSainton. Fixes #36992. git-svn-id: https://develop.svn.wordpress.org/trunk@37623 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 5 +++++ tests/phpunit/tests/term/getTerms.php | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index f71494244e..534a9de4f1 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1208,6 +1208,11 @@ function get_terms( $args = array(), $deprecated = '' ) { $terms = $term_query->query( $args ); + // Count queries are not filtered, for legacy reasons. + if ( $term_query->query_vars['count'] ) { + return $terms; + } + /** * Filters the found terms. * diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 891c5365eb..623a36cb86 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -2179,6 +2179,27 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEqualSets( array(), $found ); } + /** + * @ticket 36992 + * @ticket 35381 + */ + public function test_count_should_pass_through_main_get_terms_filter() { + add_filter( 'get_terms', array( __CLASS__, 'maybe_filter_count' ) ); + + $found = get_terms( array( + 'hide_empty' => 0, + 'count' => true, + ) ); + + remove_filter( 'get_terms', array( __CLASS__, 'maybe_filter_count' ) ); + + $this->assertNotEquals( 'foo', $found ); + } + + public static function maybe_filter_count() { + return 'foo'; + } + protected function create_hierarchical_terms_and_posts() { $terms = array();