Skip to content

Commit

Permalink
General: Cast $max_depth and $depth to an integer in the Walker
Browse files Browse the repository at this point in the history
… class.

This ensures that the arguments are correctly interpreted when passed as a query string, i.e. when `wp_parse_args()` is involved. For example, `wp_list_pages( 'depth=0' )` should display a list of all pages to the maximum depth.

Follow-up to [57848].

Props freibergergarcia, peterwilsoncc, ahortin.
Fixes #61749.

git-svn-id: https://develop.svn.wordpress.org/trunk@58812 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jul 26, 2024
1 parent b38541d commit ed59f77
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/wp-includes/class-wp-walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public function display_element( $element, &$children_elements, $max_depth, $dep
return;
}

$max_depth = (int) $max_depth;
$depth = (int) $depth;

$id_field = $this->db_fields['id'];
$id = $element->$id_field;

Expand Down Expand Up @@ -191,6 +194,8 @@ public function display_element( $element, &$children_elements, $max_depth, $dep
public function walk( $elements, $max_depth, ...$args ) {
$output = '';

$max_depth = (int) $max_depth;

// Invalid parameter or nothing to walk.
if ( $max_depth < -1 || empty( $elements ) ) {
return $output;
Expand Down Expand Up @@ -285,12 +290,14 @@ public function walk( $elements, $max_depth, ...$args ) {
* @return string XHTML of the specified page of elements.
*/
public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
$output = '';

$max_depth = (int) $max_depth;

if ( empty( $elements ) || $max_depth < -1 ) {
return '';
return $output;
}

$output = '';

$parent_field = $this->db_fields['parent'];

$count = -1;
Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/tests/post/wpListPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ public function test_wp_list_pages_depth() {
$this->assertSameIgnoreEOL( $expected, wp_list_pages( $args ) );
}

/**
* @ticket 61749
*/
public function test_wp_list_pages_depth_equals_zero() {
$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::$parent_3 . ' page_item_has_children"><a href="' . get_permalink( self::$parent_3 ) . '">Parent 3</a>
<ul class=\'children\'>
<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>
</ul></li>';

// Execute wp_list_pages() with a string to force calling wp_parse_args().
ob_start();
wp_list_pages( 'depth=0' );
$output = ob_get_clean();

// If depth equals 0, all levels should be displayed.
$this->assertSameIgnoreEOL( $expected, $output );
}

public function test_wp_list_pages_show_date() {
$args = array(
'echo' => false,
Expand Down

0 comments on commit ed59f77

Please sign in to comment.