In `get_terms()`, don't store `WP_Term` objects in cache.

Fixes #34282.

git-svn-id: https://develop.svn.wordpress.org/trunk@35117 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-10-13 02:57:21 +00:00
parent 34cb01e2f1
commit f9094e546a
2 changed files with 39 additions and 2 deletions

View File

@ -1155,6 +1155,9 @@ function get_terms( $taxonomies, $args = '' ) {
$cache_key = "get_terms:$key:$last_changed";
$cache = wp_cache_get( $cache_key, 'terms' );
if ( false !== $cache ) {
if ( 'all' === $args['fields'] ) {
$cache = array_map( 'get_term', $cache );
}
/**
* Filter the given taxonomy's terms cache.
@ -1494,8 +1497,6 @@ function get_terms( $taxonomies, $args = '' ) {
foreach ( $terms as $term ) {
$_terms[ $term->term_id ] = $term->slug;
}
} else {
$_terms = array_map( 'get_term', $terms );
}
if ( ! empty( $_terms ) ) {
@ -1508,6 +1509,10 @@ function get_terms( $taxonomies, $args = '' ) {
wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
if ( 'all' === $_fields ) {
$terms = array_map( 'get_term', $terms );
}
/** This filter is documented in wp-includes/taxonomy */
return apply_filters( 'get_terms', $terms, $taxonomies, $args );
}

View File

@ -1591,6 +1591,38 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
}
}
/**
* @ticket 14162
* @ticket 34282
*/
public function test_should_return_wp_term_objects_when_pulling_from_the_cache() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
// Prime the cache.
get_terms( 'wptests_tax', array(
'hide_empty' => false,
'fields' => 'all',
) );
$num_queries = $wpdb->num_queries;
$found = get_terms( 'wptests_tax', array(
'hide_empty' => false,
'fields' => 'all',
) );
$this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertNotEmpty( $found );
foreach ( $found as $term ) {
$this->assertInstanceOf( 'WP_Term', $term );
}
}
/**
* @ticket 14162
*/