WordPress.org

Make WordPress Core

Changeset 27767


Ignore:
Timestamp:
03/27/2014 01:17:41 AM (6 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r27726 r27767  
    37433743        'exclude' => array(), 'include' => array(),
    37443744        'meta_key' => '', 'meta_value' => '',
    3745         'authors' => '', 'parent' => -1, 'exclude_tree' => '',
     3745        'authors' => '', 'parent' => -1, 'exclude_tree' => array(),
    37463746        'number' => '', 'offset' => 0,
    37473747        'post_type' => 'page', 'post_status' => 'publish',
     
    39253925        $pages = get_page_children($child_of, $pages);
    39263926
    3927     if ( !empty($exclude_tree) ) {
    3928         $exclude = (int) $exclude_tree;
    3929         $children = get_page_children($exclude, $pages);
    3930         $excludes = array();
    3931         foreach ( $children as $child )
    3932             $excludes[] = $child->ID;
    3933         $excludes[] = $exclude;
    3934         $num_pages = count($pages);
     3927    if ( ! empty( $exclude_tree ) ) {
     3928        $exclude = wp_parse_id_list( $exclude_tree );
     3929        foreach( $exclude as $id ) {
     3930            $children = get_page_children( $id, $pages );
     3931            foreach ( $children as $child ) {
     3932                $exclude[] = $child->ID;
     3933            }
     3934        }
     3935
     3936        $num_pages = count( $pages );
    39353937        for ( $i = 0; $i < $num_pages; $i++ ) {
    3936             if ( in_array($pages[$i]->ID, $excludes) )
    3937                 unset($pages[$i]);
     3938            if ( in_array( $pages[$i]->ID, $exclude ) ) {
     3939                unset( $pages[$i] );
     3940            }
    39383941        }
    39393942    }
  • trunk/tests/phpunit/tests/post/getPages.php

    r27755 r27767  
    237237        _unregister_post_type( $type );
    238238    }
     239
     240    function test_exclude_tree() {
     241        $post_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
     242        $post_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id1 ) );
     243        $post_id3 = $this->factory->post->create( array( 'post_type' => 'page' ) );
     244        $post_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id3 ) );
     245
     246        $all = get_pages();
     247
     248        $this->assertCount( 4, $all );
     249
     250        $exclude1 = get_pages( "exclude_tree=$post_id1" );
     251        $this->assertCount( 2, $exclude1 );
     252
     253        $exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) );
     254        $this->assertCount( 2, $exclude2 );
     255
     256        $exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) );
     257        $this->assertCount( 2, $exclude3 );
     258
     259        $exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) );
     260        $this->assertCount( 2, $exclude4 );
     261
     262        $exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
     263        $this->assertCount( 0, $exclude5 );
     264
     265        $post_id5 = $this->factory->post->create( array( 'post_type' => 'page' ) );
     266        $post_id6 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id5 ) );
     267
     268        $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
     269        $this->assertCount( 2, $exclude6 );
     270    }
    239271}
Note: See TracChangeset for help on using the changeset viewer.