Make WordPress Core

Changeset 34696


Ignore:
Timestamp:
09/29/2015 07:11:12 PM (9 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r34625 r34696  
    499499 *     @type int          $child_of              Term ID to retrieve child terms of. See {@link get_terms()}. Default 0.
    500500 *     @type array|string $exclude               Array or comma/space-separated string of term IDs to exclude.
    501  *                                               See {@link get_terms()}. Default empty string.
     501 *                                               If `$hierarchical` is true, descendants of `$exclude` terms will also
     502 *                                               be excluded; see `$exclude_tree`. See {@link get_terms()}.
     503 *                                               Default empty string.
    502504 *     @type array|string $exclude_tree          Array or comma/space-separated string of term IDs to exclude, along
    503505 *                                               with their descendants. See {@link get_terms()}. Default empty string.
     
    537539        $r['pad_counts'] = true;
    538540
     541    // Descendants of exclusions should be excluded too.
    539542    if ( true == $r['hierarchical'] ) {
    540         $r['exclude_tree'] = $r['exclude'];
     543        $exclude_tree = array();
     544
     545        if ( $r['exclude_tree'] ) {
     546            $exclude_tree = array_merge( $exclude_tree, (array) $r['exclude_tree'] );
     547        }
     548
     549        if ( $r['exclude'] ) {
     550            $exclude_tree = array_merge( $exclude_tree, (array) $r['exclude'] );
     551        }
     552
     553        $r['exclude_tree'] = $exclude_tree;
    541554        $r['exclude'] = '';
    542555    }
  • trunk/tests/phpunit/tests/category/wpListCategories.php

    r33804 r34696  
    265265        $this->assertContains( '<li class="categories">Categories', $found );
    266266    }
     267
     268    /**
     269     * @ticket 12981
     270     */
     271    public function test_exclude_tree_should_be_respected() {
     272        $c = $this->factory->category->create();
     273        $parent = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
     274        $child = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
     275
     276        $args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
     277
     278        $actual = wp_list_categories( $args );
     279
     280        $this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
     281
     282        $this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
     283    }
     284
     285    /**
     286     * @ticket 12981
     287     */
     288    public function test_exclude_tree_should_be_merged_with_exclude() {
     289        $c = $this->factory->category->create();
     290        $parent = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
     291        $child = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
     292        $parent2 = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
     293        $child2 = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
     294
     295        $args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
     296
     297        $actual = wp_list_categories( array(
     298            'echo' => 0,
     299            'hide_empty' => 0,
     300            'exclude' => $parent,
     301            'exclude_tree' => $parent2,
     302        ) );
     303
     304        $this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
     305        $this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
     306        $this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
     307
     308        $this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
     309    }
    267310}
Note: See TracChangeset for help on using the changeset viewer.