Introduce metadata for taxonomy terms.

Adds a new table to the database schema (`wp_termmeta`), and a set of
`*_term_meta()` API functions. `get_terms()` and `wp_get_object_terms()`
now also support 'meta_query' parameters, with syntax identical to other
uses of `WP_Meta_Query`.

When fetching terms via `get_terms()` or `wp_get_object_terms()`, metadata for
matched terms is preloaded into the cache by default. Disable this behavior
by setting the new `$update_term_meta_cache` paramater to `false`.

To maximize performance, within `WP_Query` loops, the termmeta cache is *not*
primed by default. Instead, we use a lazy-loading technique: metadata for all
terms belonging to posts in the loop is loaded into the cache the first time
that `get_term_meta()` is called within the loop.

Props boonebgorges, sirzooro.
See #10142.

git-svn-id: https://develop.svn.wordpress.org/trunk@34529 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-09-25 03:58:59 +00:00
parent a98ca8b314
commit 3425177325
9 changed files with 528 additions and 8 deletions
+80 -2
View File
@@ -22,7 +22,7 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$num_queries = $wpdb->num_queries;
// last_changed and num_queries should bump
$terms = get_terms( 'post_tag' );
$terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
$this->assertEquals( 3, count( $terms ) );
$time1 = wp_cache_get( 'last_changed', 'terms' );
$this->assertNotEmpty( $time1 );
@@ -31,7 +31,7 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$num_queries = $wpdb->num_queries;
// Again. last_changed and num_queries should remain the same.
$terms = get_terms( 'post_tag' );
$terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) );
$this->assertEquals( 3, count( $terms ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
@@ -1502,6 +1502,84 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
}
}
/**
* @ticket 10142
*/
public function test_termmeta_cache_should_be_primed_by_default() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'bar' );
$found = get_terms( 'wptests_tax', array(
'hide_empty' => false,
'include' => $terms,
) );
$num_queries = $wpdb->num_queries;
foreach ( $terms as $t ) {
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
}
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 10142
*/
public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'bar' );
$found = get_terms( 'wptests_tax', array(
'hide_empty' => false,
'include' => $terms,
'update_term_meta_cache' => false,
) );
$num_queries = $wpdb->num_queries;
foreach ( $terms as $t ) {
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
}
$this->assertSame( $num_queries + 3, $wpdb->num_queries );
}
/**
* @ticket 10142
*/
public function test_meta_query() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'baz' );
add_term_meta( $terms[3], 'foob', 'ar' );
$found = get_terms( 'wptests_tax', array(
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
'fields' => 'ids',
) );
$this->assertEqualSets( array( $terms[0], $terms[1] ), $found );
}
protected function create_hierarchical_terms_and_posts() {
$terms = array();
+147
View File
@@ -0,0 +1,147 @@
<?php
/**
* @group taxonomy
* @group meta
* @ticket 10142
*/
class Tests_Term_Meta extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
register_taxonomy( 'wptests_tax', 'post' );
}
public function test_add() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$this->assertNotEmpty( add_term_meta( $t, 'foo', 'bar' ) );
}
public function test_add_unique() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$this->assertNotEmpty( add_term_meta( $t, 'foo', 'bar' ) );
$this->assertFalse( add_term_meta( $t, 'foo', 'bar', true ) );
}
public function test_delete() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $t, 'foo', 'bar' );
$this->assertTrue( delete_term_meta( $t, 'foo' ) );
}
public function test_delete_with_invalid_meta_key_should_return_false() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$this->assertFalse( delete_term_meta( $t, 'foo' ) );
}
public function test_delete_should_respect_meta_value() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $t, 'foo', 'bar' );
add_term_meta( $t, 'foo', 'baz' );
$this->assertTrue( delete_term_meta( $t, 'foo', 'bar' ) );
$metas = get_term_meta( $t, 'foo', false );
$this->assertSame( array( 'baz' ), $metas );
}
public function test_get_with_no_key_should_fetch_all_keys() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $t, 'foo', 'bar' );
add_term_meta( $t, 'foo1', 'baz' );
$found = get_term_meta( $t );
$expected = array(
'foo' => array( 'bar' ),
'foo1' => array( 'baz' ),
);
$this->assertEqualSets( $expected, $found );
}
public function test_get_with_key_should_fetch_all_for_key() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $t, 'foo', 'bar' );
add_term_meta( $t, 'foo', 'baz' );
add_term_meta( $t, 'foo1', 'baz' );
$found = get_term_meta( $t, 'foo' );
$expected = array( 'bar', 'baz' );
$this->assertEqualSets( $expected, $found );
}
public function test_get_should_respect_single_true() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $t, 'foo', 'bar' );
add_term_meta( $t, 'foo', 'baz' );
$found = get_term_meta( $t, 'foo', true );
$this->assertEquals( 'bar', $found );
}
public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$actual = update_term_meta( $t, 'foo', 'bar' );
$this->assertInternalType( 'int', $actual );
$this->assertNotEmpty( $actual );
$meta = get_term_meta( $t, 'foo', true );
$this->assertSame( 'bar', $meta );
}
public function test_update_should_return_true_when_updating_existing_value_for_key() {
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $t, 'foo', 'bar' );
$actual = update_term_meta( $t, 'foo', 'baz' );
$this->assertTrue( $actual );
$meta = get_term_meta( $t, 'foo', true );
$this->assertSame( 'baz', $meta );
}
public function test_term_meta_should_be_lazy_loaded_for_all_terms_in_wp_query_loop() {
global $wpdb;
$p = $this->factory->post->create( array( 'post_status' => 'publish' ) );
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $terms, 'wptests_tax' );
foreach ( $terms as $t ) {
add_term_meta( $t, 'foo', 'bar' );
}
// Create another term, which should *not* be lazy loaded because it's unattached.
$orphan_term = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $orphan_term, 'foo', 'bar' );
$this->go_to( get_permalink( $p ) );
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// First request will hit the database.
$num_queries = $wpdb->num_queries;
$this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
// Second and third requests should be in cache.
$this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
$this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
// Querying a term not primed should result in a hit.
$this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
$this->assertSame( $num_queries + 2, $wpdb->num_queries );
}
}
}
}
@@ -418,6 +418,86 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$this->assertEqualSets( array( $t1, $t2 ), $found );
}
/**
* @ticket 10142
*/
public function test_termmeta_cache_should_be_primed_by_default() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'bar' );
$p = $this->factory->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax' );
$num_queries = $wpdb->num_queries;
foreach ( $terms as $t ) {
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
}
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 10142
*/
public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'bar' );
$p = $this->factory->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'update_term_meta_cache' => false,
) );
$num_queries = $wpdb->num_queries;
foreach ( $terms as $t ) {
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
}
$this->assertSame( $num_queries + 3, $wpdb->num_queries );
}
/**
* @ticket 10142
*/
public function test_meta_query() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
add_term_meta( $terms[0], 'foo', 'bar' );
add_term_meta( $terms[1], 'foo', 'bar' );
add_term_meta( $terms[2], 'foo', 'baz' );
add_term_meta( $terms[3], 'foob', 'ar' );
$p = $this->factory->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
) );
$this->assertEqualSets( array( $terms[0], $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
}
public function filter_get_object_terms( $terms ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
// all terms should still be objects