From 576f78ac495d5c23b4007285316eeed4930f02c3 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 20 Nov 2017 22:45:29 +0000 Subject: [PATCH] Improve data types returned from empty hierarchical term queries. When querying for 'count', ensure that 0 is returned. Otherwise, ensure that it's an array. Props xParham, birgire. Fixes #42327. git-svn-id: https://develop.svn.wordpress.org/trunk@42209 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term-query.php | 7 ++- tests/phpunit/tests/term/query.php | 67 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index d704f5e161..e27b68676b 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -379,7 +379,12 @@ class WP_Term_Query { } if ( ! $in_hierarchy ) { - return array(); + if ( 'count' == $args['fields'] ) { + return 0; + } else { + $this->terms = array(); + return $this->terms; + } } } diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 117befadd3..a3c6c0e817 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -471,4 +471,71 @@ class Tests_Term_Query extends WP_UnitTestCase { $this->assertSame( $expected, $found2 ); $this->assertSame( $expected, $found3 ); } + + /** + * The query method should return zero for field as count and parent set. + * + * @ticket 42327 + */ + public function test_query_should_return_zero_for_field_count_and_parent_set() { + $post_id = self::factory()->post->create(); + register_taxonomy( 'wptests_tax', 'post' ); + + $term_id = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + ) ); + wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); + + $q = new WP_Term_Query(); + $args = array( + 'taxonomy' => 'wptests_tax', + 'parent' => $term_id, + 'fields' => 'count', + ); + $this->assertSame( 0, $q->query( $args ) ); + } + + /** + * The query method should return zero for field as count and child_of set. + * + * @ticket 42327 + */ + public function test_query_should_return_zero_for_field_as_count_and_child_of_set() { + $post_id = self::factory()->post->create(); + register_taxonomy( 'wptests_tax', 'post' ); + + $term_id = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + ) ); + wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); + + $q = new WP_Term_Query(); + $args = array( + 'taxonomy' => 'wptests_tax', + 'child_of' => $term_id, + 'fields' => 'count', + ); + $this->assertSame( 0, $q->query( $args ) ); + } + + /** + * The terms property should be an empty array for fields not as count and parent set. + * + * @ticket 42327 + */ + public function test_terms_property_should_be_empty_array_for_field_not_as_count_and_parent_set() { + $post_id = self::factory()->post->create(); + register_taxonomy( 'wptests_tax', 'post' ); + + $term_id = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + ) ); + wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' ); + + $q = new WP_Term_Query( array( + 'taxonomy' => 'wptests_tax', + 'parent' => $term_id, + ) ); + $this->assertSame( array(), $q->terms ); + } }