Taxonomy: Ensure that invalid term objects are discarded in WP_Term_Query.

The `get_term()` mapping may result in term objects that are `null` or
`WP_Error` when plugins use `get_term` or a related filter. Since `null`
and error objects are not valid results for a term query, we discard
them.

Props GM_Alex.
See #42691.

git-svn-id: https://develop.svn.wordpress.org/trunk@43049 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2018-04-30 21:07:16 +00:00
parent 360d8701aa
commit 4c36079299
2 changed files with 93 additions and 2 deletions

View File

@@ -614,4 +614,68 @@ class Tests_Term_Query extends WP_UnitTestCase {
);
$this->assertSame( array(), $q->terms );
}
/**
* @ticket 42691
*/
public function test_null_term_object_should_be_discarded() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = self::factory()->term->create_many( 3, array(
'taxonomy' => 'wptests_tax',
) );
$this->term_id = $terms[1];
add_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
$found = get_terms( array(
'taxonomy' => 'wptests_tax',
'hide_empty' => false,
) );
remove_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
$expected = array( $terms[0], $terms[2] );
$this->assertEqualSets( $expected, wp_list_pluck( $found, 'term_id' ) );
}
public function filter_term_to_null( $term ) {
if ( $this->term_id === $term->term_id ) {
return null;
}
return $term;
}
/**
* @ticket 42691
*/
public function test_error_term_object_should_be_discarded() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = self::factory()->term->create_many( 3, array(
'taxonomy' => 'wptests_tax',
) );
$this->term_id = $terms[1];
add_filter( 'get_term', array( $this, 'filter_term_to_wp_error' ) );
$found = get_terms( array(
'taxonomy' => 'wptests_tax',
'hide_empty' => false,
) );
remove_filter( 'get_term', array( $this, 'filter_term_to_wp_error' ) );
$expected = array( $terms[0], $terms[2] );
$this->assertEqualSets( $expected, wp_list_pluck( $found, 'term_id' ) );
}
public function filter_term_to_wp_error( $term ) {
if ( $this->term_id === $term->term_id ) {
return new WP_Error( 'foo' );
}
return $term;
}
}