Fix 'exclude_tree' in wp_list_categories()
.
The 'exclude_tree' parameter must be compatible with 'hierarchical'; previously, 'hierarchical' canceled it out. This changeset also makes it so that 'exclude_tree' is compatible with 'exclude'. When both are passed, and 'hierarchical' is true, the descendant trees of terms in both parameters will be excluded from matched terms. Props tott, webord, MikeHansenMe. Fixes #12981. git-svn-id: https://develop.svn.wordpress.org/trunk@34696 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c81fe78b9c
commit
3b7187eade
@ -498,7 +498,9 @@ function wp_dropdown_categories( $args = '' ) {
|
|||||||
* @type string $feed_image URL of an image to use for the feed link. Default empty string.
|
* @type string $feed_image URL of an image to use for the feed link. Default empty string.
|
||||||
* @type int $child_of Term ID to retrieve child terms of. See {@link get_terms()}. Default 0.
|
* @type int $child_of Term ID to retrieve child terms of. See {@link get_terms()}. Default 0.
|
||||||
* @type array|string $exclude Array or comma/space-separated string of term IDs to exclude.
|
* @type array|string $exclude Array or comma/space-separated string of term IDs to exclude.
|
||||||
* See {@link get_terms()}. Default empty string.
|
* If `$hierarchical` is true, descendants of `$exclude` terms will also
|
||||||
|
* be excluded; see `$exclude_tree`. See {@link get_terms()}.
|
||||||
|
* Default empty string.
|
||||||
* @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along
|
* @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along
|
||||||
* with their descendants. See {@link get_terms()}. Default empty string.
|
* with their descendants. See {@link get_terms()}. Default empty string.
|
||||||
* @type bool|int $echo True to echo markup, false to return it. Default 1.
|
* @type bool|int $echo True to echo markup, false to return it. Default 1.
|
||||||
@ -536,8 +538,19 @@ function wp_list_categories( $args = '' ) {
|
|||||||
if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
|
if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
|
||||||
$r['pad_counts'] = true;
|
$r['pad_counts'] = true;
|
||||||
|
|
||||||
|
// Descendants of exclusions should be excluded too.
|
||||||
if ( true == $r['hierarchical'] ) {
|
if ( true == $r['hierarchical'] ) {
|
||||||
$r['exclude_tree'] = $r['exclude'];
|
$exclude_tree = array();
|
||||||
|
|
||||||
|
if ( $r['exclude_tree'] ) {
|
||||||
|
$exclude_tree = array_merge( $exclude_tree, (array) $r['exclude_tree'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $r['exclude'] ) {
|
||||||
|
$exclude_tree = array_merge( $exclude_tree, (array) $r['exclude'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$r['exclude_tree'] = $exclude_tree;
|
||||||
$r['exclude'] = '';
|
$r['exclude'] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,4 +264,47 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$this->assertContains( '<li class="categories">Categories', $found );
|
$this->assertContains( '<li class="categories">Categories', $found );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 12981
|
||||||
|
*/
|
||||||
|
public function test_exclude_tree_should_be_respected() {
|
||||||
|
$c = $this->factory->category->create();
|
||||||
|
$parent = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
|
||||||
|
$child = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
|
||||||
|
|
||||||
|
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
|
||||||
|
|
||||||
|
$actual = wp_list_categories( $args );
|
||||||
|
|
||||||
|
$this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
|
||||||
|
|
||||||
|
$this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 12981
|
||||||
|
*/
|
||||||
|
public function test_exclude_tree_should_be_merged_with_exclude() {
|
||||||
|
$c = $this->factory->category->create();
|
||||||
|
$parent = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
|
||||||
|
$child = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
|
||||||
|
$parent2 = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
|
||||||
|
$child2 = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
|
||||||
|
|
||||||
|
$args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
|
||||||
|
|
||||||
|
$actual = wp_list_categories( array(
|
||||||
|
'echo' => 0,
|
||||||
|
'hide_empty' => 0,
|
||||||
|
'exclude' => $parent,
|
||||||
|
'exclude_tree' => $parent2,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user