Skip to content

Post: Fix wp_list_pages() to correctly honor exclude with depth parameter #9045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/wp-includes/class-wp-walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,28 @@ public function walk( $elements, $max_depth, ...$args ) {
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
}

$has_excluded_parents = false;

if ( $max_depth > 0 && isset( $args[0] ) ) {
if ( is_array( $args[0] ) && ! empty( $args[0]['exclude'] ) ) {
$has_excluded_parents = true;
} elseif ( is_object( $args[0] ) && ! empty( $args[0]->exclude ) ) {
$has_excluded_parents = true;
}
}

/*
* If we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless.
*/
if ( ( 0 === $max_depth ) && count( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphans ) {
foreach ( $orphans as $op ) {
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
* If we are displaying all levels (depth = 0),
* OR if specific parents were excluded with limited depth,
* we may have got orphans, which should be displayed regardless.
*/
if ( 0 === $max_depth || $has_excluded_parents ) {
if ( ! empty( $children_elements ) ) {
$empty_array = array();
foreach ( $children_elements as $orphans ) {
foreach ( $orphans as $op ) {
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
}
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/post/wpListPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,36 @@ public function test_wp_list_pages_classes_with_hierarchical_cpt() {
'The output should contain exactly one "current_page_item" class.'
);
}

/**
* @ticket 27326
*/
public function test_wp_list_page_combo_exclude_depth() {
$args = array(
'echo' => false,
'exclude' => self::$parent_3,
'depth' => 3,
);

$expected = '<li class="pagenav">Pages<ul><li class="page_item page-item-' . self::$parent_1 . ' page_item_has_children"><a href="' . get_permalink( self::$parent_1 ) . '">Parent 1</a>
<ul class=\'children\'>
<li class="page_item page-item-' . self::$children[ self::$parent_1 ][0] . '"><a href="' . get_permalink( self::$children[ self::$parent_1 ][0] ) . '">Child 1</a></li>
<li class="page_item page-item-' . self::$children[ self::$parent_1 ][1] . '"><a href="' . get_permalink( self::$children[ self::$parent_1 ][1] ) . '">Child 2</a></li>
<li class="page_item page-item-' . self::$children[ self::$parent_1 ][2] . '"><a href="' . get_permalink( self::$children[ self::$parent_1 ][2] ) . '">Child 3</a></li>
</ul>
</li>
<li class="page_item page-item-' . self::$parent_2 . ' page_item_has_children"><a href="' . get_permalink( self::$parent_2 ) . '">Parent 2</a>
<ul class=\'children\'>
<li class="page_item page-item-' . self::$children[ self::$parent_2 ][0] . '"><a href="' . get_permalink( self::$children[ self::$parent_2 ][0] ) . '">Child 1</a></li>
<li class="page_item page-item-' . self::$children[ self::$parent_2 ][1] . '"><a href="' . get_permalink( self::$children[ self::$parent_2 ][1] ) . '">Child 2</a></li>
<li class="page_item page-item-' . self::$children[ self::$parent_2 ][2] . '"><a href="' . get_permalink( self::$children[ self::$parent_2 ][2] ) . '">Child 3</a></li>
</ul>
</li>
<li class="page_item page-item-' . self::$children[ self::$parent_3 ][0] . '"><a href="' . get_permalink( self::$children[ self::$parent_3 ][0] ) . '">Child 1</a></li>
<li class="page_item page-item-' . self::$children[ self::$parent_3 ][1] . '"><a href="' . get_permalink( self::$children[ self::$parent_3 ][1] ) . '">Child 2</a></li>
<li class="page_item page-item-' . self::$children[ self::$parent_3 ][2] . '"><a href="' . get_permalink( self::$children[ self::$parent_3 ][2] ) . '">Child 3</a></li>
</ul></li>';

$this->assertSameIgnoreEOL( $expected, wp_list_pages( $args ) );
}
}
Loading