Make WordPress Core

Ticket #14477: 14477.7.diff

File 14477.7.diff, 2.6 KB (added by eclev91, 7 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 1dbf0c9..a629623 100644
    a b function get_page_children( $page_id, $pages ) { 
    44574457}
    44584458
    44594459/**
     4460 * Retrieve all descendants of a given page ID, in a flat list.
     4461 * Respects heirarchy, then alphabetical.
     4462 * Needs a better name.
     4463 *
     4464 * @since 4.9
     4465 *
     4466 * @param int   $page_id Page ID.
     4467 * @return array List of page children.
     4468 */
     4469
     4470function get_page_descendants( $page_id ) {
     4471        $descendants = array();
     4472
     4473        $to_look = get_posts( array (
     4474                'post_parent' => ($page_id !== false ? $page_id : -1),
     4475                'post_type' => 'page',
     4476                'posts_per_page' => -1
     4477        ) );
     4478
     4479        $descendants = $to_look;
     4480
     4481        foreach($to_look as $index => $descendant) {
     4482                $next_descendants = get_page_descendants( $descendant->ID );
     4483                array_splice($descendants, $index, 0, $next_descendants);
     4484        }
     4485
     4486        $descendants = array_unique($descendants, SORT_REGULAR);
     4487
     4488        return $descendants;
     4489}
     4490/**
    44604491 * Order the pages with children under parents in a flat list.
    44614492 *
    44624493 * It uses auxiliary structure to hold parent-children relationships and
    function get_pages( $args = array() ) { 
    48044835        update_post_cache( $pages );
    48054836
    48064837        if ( $child_of || $hierarchical ) {
    4807                 $pages = get_page_children($child_of, $pages);
     4838                $descendants = get_page_descendants($child_of);
     4839                foreach($pages as $index => $page) {
     4840                        if( !in_array($page->ID, wp_list_pluck($descendants, 'ID'))) {
     4841                                unset($pages[$index]);
     4842                        }
     4843                }
    48084844        }
    48094845
    48104846        if ( ! empty( $r['exclude_tree'] ) ) {
  • tests/phpunit/tests/post/getPages.php

    diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php
    index b4bdd17..ed0588b 100644
    a b class Tests_Post_getPages extends WP_UnitTestCase { 
    509509                $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
    510510                $this->assertCount( 2, $exclude6 );
    511511        }
     512
     513        /**
     514         * @ticket 14477
     515         */
     516        // function test_get_pages_interrupted_hierarchy() {
     517        //      $page1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
     518        //      $page2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page1 ) );
     519        //      add_post_meta( $page2, 'color', 'red' );
     520        //      $page3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page2 ) );
     521        //      add_post_meta( $page3, 'color', 'blue' );
     522  //
     523        //      $pages = get_pages( array( 'child_of' => $page1, 'meta_key' => 'color', 'meta_value' => 'blue' ) );
     524  //
     525        //      $this->assertEqualSets( array( $page3 ), wp_list_pluck( $pages, 'ID' ) );
     526        // }
    512527}