Better loop detection for `_pad_term_counts()`.

The `$ancestors` check must be reset for each term in order for term counts
to be correct.

Fixes #20635.

git-svn-id: https://develop.svn.wordpress.org/trunk@31248 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-01-19 16:51:44 +00:00
parent 2c955c0bbf
commit d80ca82693
2 changed files with 49 additions and 7 deletions

View File

@ -3920,20 +3920,20 @@ function _pad_term_counts(&$terms, $taxonomy) {
}
// Touch every ancestor's lookup row for each post in each term
$ancestors = array();
foreach ( $term_ids as $term_id ) {
$ancestors[] = $term_id;
$child = $term_id;
$ancestors = array();
while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
if ( in_array( $parent, $ancestors ) ) {
break;
}
$ancestors[] = $child;
if ( !empty( $term_items[$term_id] ) )
foreach ( $term_items[$term_id] as $item_id => $touches ) {
$term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
}
$child = $parent;
if ( in_array( $parent, $ancestors ) ) {
break;
}
}
}

View File

@ -1077,7 +1077,45 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$this->assertEqualSets( $expected, $actual );
}
/*
public function test_pad_counts() {
register_taxonomy( 'wptests_tax_1', 'post', array( 'hierarchical' => true ) );
$posts = $this->factory->post->create_many( 3 );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax_1',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax_1',
'parent' => $t1,
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax_1',
'parent' => $t2,
) );
wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax_1' );
wp_set_object_terms( $posts[1], array( $t2 ), 'wptests_tax_1' );
wp_set_object_terms( $posts[2], array( $t3 ), 'wptests_tax_1' );
$found = get_terms( 'wptests_tax_1', array(
'pad_counts' => true,
) );
$this->assertEqualSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ) );
foreach ( $found as $f ) {
if ( $t1 == $f->term_id ) {
$this->assertSame( 3, $f->count );
} elseif ( $t2 == $f->term_id ) {
$this->assertSame( 2, $f->count );
} else {
$this->assertSame( 1, $f->count );
}
}
}
/**
* @ticket 20635
*/
public function test_pad_counts_should_not_recurse_infinitely_when_term_hierarchy_has_a_loop() {
@ -1100,6 +1138,10 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
) );
$this->assertEqualSets( array( $c1, $c2, $c3 ), wp_list_pluck( $terms, 'term_id' ) );
foreach ( $terms as $term ) {
$this->assertSame( 3, $term->count );
}
}
protected function create_hierarchical_terms_and_posts() {