From ed59f778ba7e275c7f34f3f8dd5795abcb07ca91 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 26 Jul 2024 07:54:26 +0000 Subject: [PATCH] General: Cast `$max_depth` and `$depth` to an integer in the `Walker` 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 --- src/wp-includes/class-wp-walker.php | 13 +++++++-- tests/phpunit/tests/post/wpListPages.php | 36 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-walker.php b/src/wp-includes/class-wp-walker.php index ff5eac2f50040..df67921c2746c 100644 --- a/src/wp-includes/class-wp-walker.php +++ b/src/wp-includes/class-wp-walker.php @@ -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; @@ -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; @@ -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; diff --git a/tests/phpunit/tests/post/wpListPages.php b/tests/phpunit/tests/post/wpListPages.php index 38ba729cee20b..f059591219b1a 100644 --- a/tests/phpunit/tests/post/wpListPages.php +++ b/tests/phpunit/tests/post/wpListPages.php @@ -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 = ''; + + // 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,