Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function get_instance( $term_id, $taxonomy = null ) {
$_term = wp_cache_get( $term_id, 'terms' );

// If there isn't a cached version, hit the database.
if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
if ( ! is_object( $_term ) || ! isset( $_term->taxonomy ) || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
// Any term found in the cache is not a match, so don't use it.
$_term = false;

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/term/wpTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,36 @@ public function test_get_instance_should_respect_taxonomy_when_term_id_is_found_
$found = WP_Term::get_instance( self::$term_id, 'wptests_tax2' );
$this->assertFalse( $found );
}

/**
* @ticket 65915
*
* @dataProvider data_get_instance_should_ignore_unusable_cached_values
*
* @param mixed $cached_value A cached value that is not a usable term object.
*/
public function test_get_instance_should_ignore_unusable_cached_values( $cached_value ) {
wp_cache_set( self::$term_id, $cached_value, 'terms' );

$found = WP_Term::get_instance( self::$term_id );

$this->assertInstanceOf( WP_Term::class, $found );
$this->assertSame( self::$term_id, $found->term_id );
$this->assertSame( 'wptests_tax', $found->taxonomy );
}

/**
* Data provider.
*
* @return array<non-falsy-string, array{ 0: mixed }>
*/
public function data_get_instance_should_ignore_unusable_cached_values(): array {
return array(
Comment thread
BogdanUngureanu marked this conversation as resolved.
'an array' => array( array( 'term_id' => 12345 ) ),
'an integer' => array( 12345 ),
'a string' => array( 'a term' ),
'an object with no taxonomy' => array( (object) array( 'term_id' => 12345 ) ),
'an object with a null taxonomy' => array( (object) array( 'taxonomy' => null ) ),
);
}
}
Loading