Taxonomy: Use WP_Term_Query when querying for object terms.

The new 'object_ids' parameter for `WP_Term_Query` allows queries for
terms that "belong to" a given object. This change makes it possible
to use `WP_Term_Query` inside of `wp_get_object_terms()`, rather than
assembling a SQL query.

The refactor has a couple of benefits:
* Less redundancy.
* Better consistency in accepted arguments between the term query functions. See #31105.
* Less redundancy.
* Object term queries are now cached. The `get_object_term_cache()` cache remains, and will be a somewhat less fragile secondary cache in front of the query cache (which is subject to frequent invalidation).
* Less redundancy.

A small breaking change: Previously, if a non-hierarchical taxonomy had
terms that had a non-zero 'parent' (perhaps because of a direct SQL
query), `wp_get_object_terms()` would respect the 'parent' argument.
This is in contrast to `WP_Term_Query` and `get_terms()`, which have
always rejected 'parent' queries for non-hierarchical taxonomies. For
consistency, the behavior of `get_terms()` is being applied across the
board: passing 'parent' for a non-hierarchical taxonomy will result in
an empty result set (since the cached taxonomy hierarchy will be empty).

Props flixos90, boonebgorges.
See #37198.

git-svn-id: https://develop.svn.wordpress.org/trunk@38667 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-09-28 03:54:36 +00:00
parent 36e22efdfd
commit 81ecd4da98
5 changed files with 254 additions and 219 deletions

View File

@@ -143,6 +143,24 @@ class Tests_Term_Query extends WP_UnitTestCase {
return $clauses;
}
/**
* @ticket 37198
*/
public function test_order_by_term_order_should_fall_back_on_term_id_when_relationship_table_is_not_being_joined() {
register_taxonomy( 'wptests_tax', 'post' );
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
sort( $terms );
$q = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax',
'orderby' => 'term_order',
'fields' => 'ids',
'hide_empty' => false,
) );
$this->assertSame( $terms, $q->get_terms() );
}
/**
* @ticket 37591
*/
@@ -185,4 +203,123 @@ class Tests_Term_Query extends WP_UnitTestCase {
$this->assertEquals( array( $t1, $t2 ), $terms );
}
/**
* @ticket 37198
*/
public function test_object_ids_single() {
register_taxonomy( 'wptests_tax_1', 'post' );
$p = self::factory()->post->create();
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
) );
$this->assertEqualSets( array( $t ), $query->terms );
}
/**
* @ticket 37198
*/
public function test_object_ids_array() {
register_taxonomy( 'wptests_tax_1', 'post' );
$p = self::factory()->post->create();
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => array( $p ),
'fields' => 'ids',
) );
$this->assertEqualSets( array( $t ), $query->terms );
}
/**
* @ticket 37198
*/
public function test_duplicates_should_be_removed_for_fields_all() {
register_taxonomy( 'wptests_tax_1', 'post' );
$posts = self::factory()->post->create_many( 2 );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
foreach ( $posts as $p ) {
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
}
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $posts,
'fields' => 'all',
) );
$this->assertSame( 1, count( $query->terms ) );
$this->assertSame( $t, reset( $query->terms )->term_id );
}
/**
* @ticket 37198
*/
public function test_duplicates_should_not_be_removed_for_fields_all_with_object_id() {
register_taxonomy( 'wptests_tax_1', 'post' );
$posts = self::factory()->post->create_many( 2 );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
foreach ( $posts as $p ) {
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
}
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $posts,
'fields' => 'all_with_object_id',
) );
$this->assertSame( 2, count( $query->terms ) );
foreach ( $query->terms as $term ) {
$this->assertSame( $t, $term->term_id );
}
}
/**
* @ticket 37198
* @group cache
*/
public function test_object_ids_cache_should_be_invalidated_by_term_relationship_change() {
register_taxonomy( 'wptests_tax_1', 'post' );
$p = self::factory()->post->create();
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
) );
$found = $query->get_terms();
$this->assertEqualSets( array( $terms[0] ), $found );
wp_set_object_terms( $p, array( $terms[1] ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
) );
$found = $query->get_terms();
$this->assertEqualSets( array( $terms[1] ), $found );
}
}

View File

@@ -23,7 +23,7 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$this->assertEquals( 3, count($tt_1) );
// make sure they're correct
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 't.term_id'));
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 'term_id'));
$this->assertEquals( $terms_1_slugs, $terms );
}
@@ -360,26 +360,30 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 15675
*/
public function test_parent() {
register_taxonomy( 'wptests_tax2', 'post', array(
'hierarchical' => true,
) );
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'taxonomy' => 'wptests_tax2',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'taxonomy' => 'wptests_tax2',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'taxonomy' => 'wptests_tax2',
'parent' => $t1,
) );
$t4 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'taxonomy' => 'wptests_tax2',
'parent' => $t2,
) );
$p = self::factory()->post->create();
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), 'wptests_tax2' );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
$found = wp_get_object_terms( $p, 'wptests_tax2', array(
'parent' => $t1,
'fields' => 'ids',
) );

View File

@@ -259,7 +259,7 @@ class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
$this->assertEquals( 3, count($tt_1) );
// make sure they're correct
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id'));
$this->assertEquals( $terms_1, $terms );
// change the terms
@@ -267,7 +267,7 @@ class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
$this->assertEquals( 2, count($tt_2) );
// make sure they're correct
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id'));
$this->assertEquals( $terms_2, $terms );
// make sure the tt id for 'bar' matches
@@ -288,7 +288,7 @@ class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
$this->assertEquals( 3, count($tt_1) );
// make sure they're correct
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id'));
$this->assertEquals( $terms_1, $terms );
// change the terms
@@ -296,7 +296,7 @@ class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
$this->assertEquals( 2, count($tt_2) );
// make sure they're correct
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id'));
$this->assertEquals( $terms_2, $terms );
// make sure the tt id for 'bar' matches