diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 94e898ea7c..1abf9f4c10 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -918,7 +918,7 @@ function the_meta() { * @param array|string $args Optional. Override default arguments. * @return string HTML content, if not displaying. */ -function wp_dropdown_pages($args = '') { +function wp_dropdown_pages( $args = '' ) { $defaults = array( 'depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1, @@ -928,21 +928,23 @@ function wp_dropdown_pages($args = '') { ); $r = wp_parse_args( $args, $defaults ); - extract( $r, EXTR_SKIP ); - $pages = get_pages($r); + $pages = get_pages( $r ); $output = ''; // Back-compat with old system where both id and name were based on $name argument - if ( empty($id) ) - $id = $name; + if ( empty( $r['id'] ) ) { + $r['id'] = $r['name']; + } - if ( ! empty($pages) ) { - $output = "\n"; + if ( $r['show_option_no_change'] ) { + $output .= "\t\n"; + } + if ( $r['show_option_none'] ) { + $output .= "\t\n"; + } + $output .= walk_page_dropdown_tree( $pages, $r['depth'], $r ); $output .= "\n"; } @@ -951,14 +953,14 @@ function wp_dropdown_pages($args = '') { * * @since 2.1.0 * - * @param string $output HTML output for drop down list of pages. + * @param string $html HTML output for drop down list of pages. */ - $output = apply_filters( 'wp_dropdown_pages', $output ); + $html = apply_filters( 'wp_dropdown_pages', $output ); - if ( $echo ) - echo $output; - - return $output; + if ( $r['echo'] ) { + echo $html; + } + return $html; } /** diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index 72cbba6b14..eb6e4840ec 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -164,7 +164,7 @@ class Tests_Post_getPages extends WP_UnitTestCase { * @ticket 22389 */ function test_wp_dropdown_pages() { - $posts = $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) ); + $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) ); preg_match_all( '#assertEquals( $pagelink, $output ); } + + function test_wp_dropdown_pages() { + $none = wp_dropdown_pages( array( 'echo' => 0 ) ); + $this->assertEmpty( $none ); + + $bump = '   '; + $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $child_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id ) ); + $grandchild_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $child_id ) ); + + $lineage =<< + + + + + +LINEAGE; + + $output = wp_dropdown_pages( array( 'echo' => 0 ) ); + $this->assertEquals( $lineage, $output ); + + $depth =<< + + + +DEPTH; + + $output = wp_dropdown_pages( array( 'echo' => 0, 'depth' => 1 ) ); + $this->assertEquals( $depth, $output ); + + $option_none =<< + + + + +NONE; + + $output = wp_dropdown_pages( array( 'echo' => 0, 'depth' => 1, + 'show_option_none' => 'Hoo', 'option_none_value' => 'Woo' + ) ); + $this->assertEquals( $option_none, $output ); + + $option_no_change =<< + + + + + +NO; + $output = wp_dropdown_pages( array( 'echo' => 0, 'depth' => 1, + 'show_option_none' => 'Hoo', 'option_none_value' => 'Woo', + 'show_option_no_change' => 'Burrito' + ) ); + $this->assertEquals( $option_no_change, $output ); + } } \ No newline at end of file