Ensure that `wp_list_categories()` supports comma-separated lists for 'exclude' and 'exclude_tree'.

[34696] introduced a regression whereby comma-separated values for 'exclude'
and 'exclude_tree' would be handled improperly when merging the two parameters,
resulting in category IDs being incorrectly dropped from the combined array.

Props gblsm, hnle.
Fixes #35156.

git-svn-id: https://develop.svn.wordpress.org/trunk@36005 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-12-18 18:11:44 +00:00
parent fdeed90e77
commit 6d9f50febd
2 changed files with 76 additions and 2 deletions

View File

@ -550,11 +550,11 @@ function wp_list_categories( $args = '' ) {
$exclude_tree = array(); $exclude_tree = array();
if ( $r['exclude_tree'] ) { if ( $r['exclude_tree'] ) {
$exclude_tree = array_merge( $exclude_tree, (array) $r['exclude_tree'] ); $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude_tree'] ) );
} }
if ( $r['exclude'] ) { if ( $r['exclude'] ) {
$exclude_tree = array_merge( $exclude_tree, (array) $r['exclude'] ); $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) );
} }
$r['exclude_tree'] = $exclude_tree; $r['exclude_tree'] = $exclude_tree;

View File

@ -307,4 +307,78 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
$this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual ); $this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
} }
/**
* @ticket 35156
*/
public function test_comma_separated_exclude_tree_should_be_merged_with_exclude() {
$c = self::factory()->category->create();
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$parent2 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
$parent3 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent3' ) );
$child3 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child3', 'parent' => $parent3 ) );
$parent4 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent4' ) );
$child4 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child4', 'parent' => $parent4 ) );
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => "$parent,$parent2",
'exclude_tree' => "$parent3,$parent4",
) );
$this->assertContains( '<li class="cat-item cat-item-' . $c . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent3 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent4 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
}
/**
* @ticket 35156
*/
public function test_array_exclude_tree_should_be_merged_with_exclude() {
$c = self::factory()->category->create();
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$parent2 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
$parent3 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent3' ) );
$child3 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child3', 'parent' => $parent3 ) );
$parent4 = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent4' ) );
$child4 = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child4', 'parent' => $parent4 ) );
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => 0,
'exclude' => array( $parent, $parent2 ),
'exclude_tree' => array( $parent3, $parent4 ),
) );
$this->assertContains( '<li class="cat-item cat-item-' . $c . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent3 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent4 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
}
} }