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
This commit is contained in:
Boone Gorges
2017-11-20 22:45:29 +00:00
parent ff1d2b5ccb
commit 576f78ac49
2 changed files with 73 additions and 1 deletions

View File

@@ -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;
}
}
}

View File

@@ -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 );
}
}