From 115f291221cf9da4bf65490eee88cd62bc12ab6d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 27 Mar 2014 01:17:41 +0000 Subject: [PATCH] Use `wp_parse_id_list()` when parsing `exclude_tree` in `get_pages()`. Add unit tests to ensure a URL string, array with string as value, and array with array as value for `exclude_tree` can be used to specify multiple IDs. Props cgaffga, roothorick, hakre, tbrams for patches across the years. Fixes #9153. git-svn-id: https://develop.svn.wordpress.org/trunk@27767 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 25 ++++++++++++--------- tests/phpunit/tests/post/getPages.php | 32 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3f7232976f..2122a5fdea 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3742,7 +3742,7 @@ function get_pages( $args = array() ) { 'sort_column' => 'post_title', 'hierarchical' => 1, 'exclude' => array(), 'include' => array(), 'meta_key' => '', 'meta_value' => '', - 'authors' => '', 'parent' => -1, 'exclude_tree' => '', + 'authors' => '', 'parent' => -1, 'exclude_tree' => array(), 'number' => '', 'offset' => 0, 'post_type' => 'page', 'post_status' => 'publish', ); @@ -3924,17 +3924,20 @@ function get_pages( $args = array() ) { if ( $child_of || $hierarchical ) $pages = get_page_children($child_of, $pages); - if ( !empty($exclude_tree) ) { - $exclude = (int) $exclude_tree; - $children = get_page_children($exclude, $pages); - $excludes = array(); - foreach ( $children as $child ) - $excludes[] = $child->ID; - $excludes[] = $exclude; - $num_pages = count($pages); + if ( ! empty( $exclude_tree ) ) { + $exclude = wp_parse_id_list( $exclude_tree ); + foreach( $exclude as $id ) { + $children = get_page_children( $id, $pages ); + foreach ( $children as $child ) { + $exclude[] = $child->ID; + } + } + + $num_pages = count( $pages ); for ( $i = 0; $i < $num_pages; $i++ ) { - if ( in_array($pages[$i]->ID, $excludes) ) - unset($pages[$i]); + if ( in_array( $pages[$i]->ID, $exclude ) ) { + unset( $pages[$i] ); + } } } diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index 3ce65bbb69..72cbba6b14 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -236,4 +236,36 @@ class Tests_Post_getPages extends WP_UnitTestCase { _unregister_post_type( $type ); } + + function test_exclude_tree() { + $post_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $post_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id1 ) ); + $post_id3 = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $post_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id3 ) ); + + $all = get_pages(); + + $this->assertCount( 4, $all ); + + $exclude1 = get_pages( "exclude_tree=$post_id1" ); + $this->assertCount( 2, $exclude1 ); + + $exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) ); + $this->assertCount( 2, $exclude2 ); + + $exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) ); + $this->assertCount( 2, $exclude3 ); + + $exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) ); + $this->assertCount( 2, $exclude4 ); + + $exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) ); + $this->assertCount( 0, $exclude5 ); + + $post_id5 = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $post_id6 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id5 ) ); + + $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) ); + $this->assertCount( 2, $exclude6 ); + } } \ No newline at end of file