diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index b7c3b42a2b..7431d1f5f0 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -678,7 +678,7 @@ class WP_Term_Query { $cache = wp_cache_get( $cache_key, 'terms' ); if ( false !== $cache ) { if ( 'all' === $_fields ) { - $cache = array_map( 'get_term', $cache ); + $cache = $this->populate_terms( $cache ); } $this->terms = $cache; @@ -810,7 +810,7 @@ class WP_Term_Query { wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) { - $terms = array_map( 'get_term', $terms ); + $terms = $this->populate_terms( $terms ); } $this->terms = $terms; @@ -972,4 +972,31 @@ class WP_Term_Query { return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like ); } + + /** + * Creates an array of term objects from an array of term IDs. + * + * Also discards invalid term objects. + * + * @since 5.0.0 + * + * @param array $term_ids Term IDs. + * @return array + */ + protected function populate_terms( $term_ids ) { + $terms = array(); + + if ( ! is_array( $term_ids ) ) { + return $terms; + } + + foreach ( $term_ids as $key => $term_id ) { + $term = get_term( $term_id ); + if ( $term instanceof WP_Term ) { + $terms[ $key ] = $term; + } + } + + return $terms; + } } diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index b517417f06..39496b16cb 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -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; + } }