Ticket #24461: 24461.patch
| File 24461.patch, 3.9 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/taxonomy.php
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php index 63d53a7..acf3c97 100644
function _get_term_hierarchy($taxonomy) { 3818 3818 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. 3819 3819 * @return array The subset of $terms that are descendants of $term_id. 3820 3820 */ 3821 function _get_term_children( $term_id, $terms, $taxonomy) {3821 function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { 3822 3822 $empty_array = array(); 3823 3823 if ( empty($terms) ) 3824 3824 return $empty_array; … … function _get_term_children($term_id, $terms, $taxonomy) { 3829 3829 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) ) 3830 3830 return $empty_array; 3831 3831 3832 // We check $ancestors to avoid recursion, so we need to be sure the term itself is listed. 3833 if ( empty( $ancestors ) ) { 3834 $ancestors[] = $term_id; 3835 } 3836 3832 3837 foreach ( (array) $terms as $term ) { 3833 3838 $use_id = false; 3834 3839 if ( !is_object($term) ) { … … function _get_term_children($term_id, $terms, $taxonomy) { 3838 3843 $use_id = true; 3839 3844 } 3840 3845 3841 if ( $term->term_id == $term_id ) { 3846 // Don't recurse if we've already identified the term as a child - this indicates a loop. 3847 if ( in_array( $term->term_id, $ancestors ) ) { 3842 3848 continue; 3843 3849 } 3844 3850 … … function _get_term_children($term_id, $terms, $taxonomy) { 3851 3857 if ( !isset($has_children[$term->term_id]) ) 3852 3858 continue; 3853 3859 3854 if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) ) 3860 if ( $use_id ) { 3861 $ancestors = array_merge( $ancestors, $term_list ); 3862 } else { 3863 $ancestors = array_merge( $ancestors, wp_list_pluck( $term_list, 'term_id' ) ); 3864 } 3865 3866 if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) ) 3855 3867 $term_list = array_merge($term_list, $children); 3856 3868 } 3857 3869 } -
tests/phpunit/tests/term/getTerms.php
diff --git tests/phpunit/tests/term/getTerms.php tests/phpunit/tests/term/getTerms.php index 865357a..8a11365 100644
class Tests_Term_getTerms extends WP_UnitTestCase { 394 394 add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 ); 395 395 } 396 396 397 /** 398 * @covers ::_get_term_children 399 * @ticket 24461 400 */ 401 public function test__get_term_children_handles_cycles() { 402 remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 ); 403 404 $c1 = $this->factory->category->create(); 405 $c2 = $this->factory->category->create( array( 'parent' => $c1 ) ); 406 $c3 = $this->factory->category->create( array( 'parent' => $c2 ) ); 407 wp_update_term( $c1, 'category', array( 'parent' => $c3 ) ); 408 409 add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 ); 410 411 $result = _get_term_children( $c1, array( $c1, $c2, $c3 ), 'category' ); 412 413 $this->assertEqualSets( array( $c2, $c3 ), $result ); 414 } 415 416 /** 417 * @covers ::_get_term_children 418 * @ticket 24461 419 */ 420 public function test__get_term_children_handles_cycles_when_terms_argument_contains_objects() { 421 remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 ); 422 423 $c1 = $this->factory->category->create_and_get(); 424 $c2 = $this->factory->category->create_and_get( array( 'parent' => $c1->term_id ) ); 425 $c3 = $this->factory->category->create_and_get( array( 'parent' => $c2->term_id ) ); 426 wp_update_term( $c1->term_id, 'category', array( 'parent' => $c3->term_id ) ); 427 428 add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 ); 429 430 $result = _get_term_children( $c1->term_id, array( $c1, $c2, $c3 ), 'category' ); 431 432 $this->assertEqualSets( array( $c2, $c3 ), $result ); 433 } 434 397 435 public function test_get_terms_by_slug() { 398 436 $t1 = $this->factory->tag->create( array( 'slug' => 'foo' ) ); 399 437 $t2 = $this->factory->tag->create( array( 'slug' => 'bar' ) );
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)