Introduce WP_Term.

`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.

The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.

The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.

Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.

git-svn-id: https://develop.svn.wordpress.org/trunk@34997 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-10-10 01:58:37 +00:00
parent ecb75a16be
commit 134a7af0fc
7 changed files with 321 additions and 90 deletions

View File

@@ -103,10 +103,10 @@ class Tests_Term_Cache extends WP_UnitTestCase {
) );
$term_object = get_term( $term, 'wptests_tax' );
wp_cache_delete( $term, 'wptests_tax' );
wp_cache_delete( $term, 'terms' );
// Affirm that the cache is empty.
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
$num_queries = $wpdb->num_queries;
@@ -128,16 +128,16 @@ class Tests_Term_Cache extends WP_UnitTestCase {
'taxonomy' => 'wptests_tax',
) );
wp_cache_delete( $term, 'wptests_tax' );
wp_cache_delete( $term, 'terms' );
// Affirm that the cache is empty.
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
$num_queries = $wpdb->num_queries;
// Prime cache.
$term_object = get_term( $term, 'wptests_tax' );
$this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
$this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$term_object_2 = get_term( $term, 'wptests_tax' );
@@ -155,16 +155,16 @@ class Tests_Term_Cache extends WP_UnitTestCase {
'taxonomy' => 'wptests_tax',
) );
wp_cache_delete( $term, 'wptests_tax' );
wp_cache_delete( $term, 'terms' );
// Affirm that the cache is empty.
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
$this->assertEmpty( wp_cache_get( $term, 'terms' ) );
$num_queries = $wpdb->num_queries;
// Prime cache.
$term_object = get_term_by( 'id', $term, 'wptests_tax' );
$this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
$this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
$term_object_2 = get_term( $term, 'wptests_tax' );

View File

@@ -35,19 +35,6 @@ class Tests_Term_GetTerm extends WP_UnitTestCase {
$this->assertSame( $num_queries, $wpdb->num_queries );
}
public function test_passing_term_object_should_not_skip_database_query_when_filter_property_is_set() {
global $wpdb;
$term = $this->factory->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) );
clean_term_cache( $term->term_id, 'wptests_tax' );
$num_queries = $wpdb->num_queries;
$term_a = get_term( $term, 'wptests_tax' );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
}
public function test_passing_term_string_that_casts_to_int_0_should_return_null() {
$this->assertSame( null, get_term( 'abc', 'wptests_tax' ) );
}
@@ -98,4 +85,25 @@ class Tests_Term_GetTerm extends WP_UnitTestCase {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$this->assertInternalType( 'object', get_term( $t, 'wptests_tax', 'foo' ) );
}
/**
* @ticket 14162
*/
public function test_numeric_properties_should_be_cast_to_ints() {
global $wpdb;
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
// Get raw data from the database.
$term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $t ) );
$found = get_term( $term_data );
$this->assertTrue( $found instanceof WP_Term );
$this->assertInternalType( 'int', $found->term_id );
$this->assertInternalType( 'int', $found->term_taxonomy_id );
$this->assertInternalType( 'int', $found->parent );
$this->assertInternalType( 'int', $found->count );
$this->assertInternalType( 'int', $found->term_group );
}
}

View File

@@ -59,4 +59,32 @@ class Tests_Term_GetTermBy extends WP_UnitTestCase {
$found = get_term_by( 'term_taxonomy_id', $new_ttid, 'foo' );
$this->assertSame( $t, $found->term_id );
}
/**
* @ticket 14162
*/
public function test_should_prime_term_cache() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$t = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
'slug' => 'foo',
) );
clean_term_cache( $t, 'wptests_tax' );
$num_queries = $wpdb->num_queries;
$found = get_term_by( 'slug', 'foo', 'wptests_tax' );
$num_queries++;
$this->assertTrue( $found instanceof WP_Term );
$this->assertSame( $t, $found->term_id );
$this->assertSame( $num_queries, $wpdb->num_queries );
// Calls to `get_term()` should now hit cache.
$found2 = get_term( $t );
$this->assertSame( $t, $found->term_id );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
}