WordPress.org

Make WordPress Core

Ticket #15667: 15667.2.diff

File 15667.2.diff, 4.3 KB (added by wonderboymusic, 7 years ago)
  • src/wp-includes/class-wp-walker.php

     
    281281                 * When none of the elements is top level.
    282282                 * Assume the first one must be root of the sub elements.
    283283                 */
    284                 if ( empty($top_level_elements) ) {
     284                if ( empty( $top_level_elements ) && $max_depth !== 1 ) {
    285285
    286286                        $first = array_slice( $elements, 0, 1 );
    287287                        $root = $first[0];
  • tests/phpunit/tests/post/getPages.php

     
    132132                $this->assertEquals( $inc, $exc_result );
    133133        }
    134134
     135        /*
     136         * @ticket 15667
     137         */
     138        function test_get_pages_exclude_with_children() {
     139
     140                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     141                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     142                $page_2       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     143                $page_2_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     144
     145                // If hierarchical is false get_pages returns the children even though the parents are excluded
     146                $pages = get_pages( 'hierarchical=0&exclude=' . implode( ',', array( $page_1, $page_2 ) ) );
     147                $this->assertEqualSets( array( $page_1_child, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     148
     149                // If hierarchical is true get_pages doesn't return the children if the parents are excluded
     150                $pages = get_pages( 'hierarchical=1&exclude=' . implode( ',', array( $page_1, $page_2 ) ) );
     151                $this->assertEmpty( $pages );
     152
     153        }
     154
     155        /*
     156         * @ticket 15667
     157         */
     158        function test_get_pages_exclude_tree() {
     159
     160                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     161                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     162                $page_2       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     163                $page_2_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     164
     165                // exclude_tree always excludes the children regardless of what hierarchical is set to
     166                $pages = get_pages( 'hierarchical=0&exclude_tree=' . $page_1 );
     167                $this->assertEqualSets( array( $page_2, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     168                $pages = get_pages( 'hierarchical=1&exclude_tree=' . $page_1 );
     169                $this->assertEqualSets( array( $page_2, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     170
     171                // You can only pass a single ID to exclude_tree
     172                $pages = get_pages( 'exclude_tree=' . implode( ',', array( $page_1, $page_2 ) ) );
     173                $this->assertEqualSets( array( $page_2, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     174        }
     175
    135176        /**
    136177         * @ticket 9470
    137178         */
  • tests/phpunit/tests/post/wpListPages.php

     
     1<?php
     2
     3class Tests_Post_ListPages extends WP_UnitTestCase {
     4
     5        function setUp() {
     6                parent::setUp();
     7        }
     8
     9        function test_wp_list_pages_exclude_all_parents() {
     10
     11                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     12                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     13                $page_2       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     14                $page_2_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     15
     16                // excluding the parents should cause wp_list_pages to return nothing
     17                $pages = wp_list_pages( 'echo=0&exclude=' . implode( ',', array( $page_1, $page_2 ) ) );
     18
     19                $this->assertEmpty( $pages );
     20
     21        }
     22
     23        function test_wp_list_pages_exclude_tree() {
     24
     25                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     26                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     27
     28                // excluding the parent should cause wp_list_pages to return nothing
     29                $pages = wp_list_pages( 'echo=0&exclude_tree=' . $page_1 );
     30
     31                $this->assertEmpty( $pages );
     32
     33        }
     34
     35}