From 05ea231402dbe059a3213cb1d9d523406c39ea31 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sat, 29 Mar 2014 06:07:17 +0000 Subject: [PATCH] Avoid infinite recursion in get_term_children() when a term is incorrectly a parent of itself. props kovshenin. fixes #27123. git-svn-id: https://develop.svn.wordpress.org/trunk@27837 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 4 ++++ tests/phpunit/tests/term/getTerms.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index c2312d7f56..df209a50e1 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1107,6 +1107,10 @@ function get_term_children( $term_id, $taxonomy ) { $children = $terms[$term_id]; foreach ( (array) $terms[$term_id] as $child ) { + if ( $term_id == $child ) { + continue; + } + if ( isset($terms[$child]) ) $children = array_merge($children, get_term_children($child, $taxonomy)); } diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 72007b05fb..bcc38a04c1 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -333,4 +333,22 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $terms = get_terms( 'category', array( 'child_of' => $parent, 'hide_empty' => false ) ); $this->assertEquals( 1, count( $terms ) ); } + + /** + * @ticket 27123 + */ + function test_get_term_children_recursion() { + // Assume there is a way to insert a term with the parent pointing to itself + // See: https://core.trac.wordpress.org/changeset/15806 + remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 ); + + $term = wp_insert_term( 'Test', 'category' ); + $term = wp_update_term( $term['term_id'], 'category', array( 'parent' => $term['term_id'] ) ); + $term = get_term( $term['term_id'], 'category' ); + + $this->assertEquals( $term->term_id, $term->parent ); + $this->assertInternalType( 'array', get_term_children( $term->term_id, 'category' ) ); + + add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 ); + } }