WordPress.org

Make WordPress Core

Ticket #10852: 10852_get_page_children.diff

File 10852_get_page_children.diff, 2.0 KB (added by hailin, 9 years ago)

patch

  • C:/xampp/htdocs/wordpress_trunk/wp-includes/post.php

     
    21292129
    21302130/**
    21312131 * Retrieve child pages from list of pages matching page ID.
     2132 * It returns the entire list of nested descendant pages rooted from page_id
    21322133 *
    2133  * Matches against the pages parameter against the page ID. Also matches all
    2134  * children for the same to retrieve all children of a page. Does not make any
    2135  * SQL queries to get the children.
    2136  *
    21372134 * @since 1.5.1
    21382135 *
    21392136 * @param int $page_id Page ID.
    21402137 * @param array $pages List of pages' objects.
    21412138 * @return array
    21422139 */
    2143 function &get_page_children($page_id, $pages) {
    2144         $page_list = array();
    2145         foreach ( (array) $pages as $page ) {
    2146                 if ( $page->post_parent == $page_id ) {
    2147                         $page_list[] = $page;
    2148                         if ( $children = get_page_children($page->ID, $pages) )
    2149                                 $page_list = array_merge($page_list, $children);
     2140function &get_page_children( $page_id, &$pages ) {
     2141       
     2142        /*
     2143         * use auxiliary array structure to store the parent-children relations,
     2144         * which allow O(1) per lookup, and gives O(N) overall complexity
     2145         * ex: children[10][] contains all the children pages whose parent is 10
     2146         */
     2147         $children = array();
     2148         foreach ( (array) $pages as $p ) {
     2149               
     2150                $parent_id = intval( $p->post_parent );
     2151                $children[ $parent_id ][] = $p;
     2152         }
     2153         
     2154         $page_list = array();
     2155         _page_traverse( $page_id, $children, $page_list );
     2156         
     2157        return $page_list;
     2158}
     2159
     2160/**
     2161 * function to traverse and return all the nested children of a root page.
     2162 * $children contains parent-chilren relations
     2163 *
     2164 */
     2165function _page_traverse( $page_id, &$children, &$page_list ){
     2166       
     2167        if ( isset( $children[ $page_id ] ) ){
     2168               
     2169                foreach( (array)$children[ $page_id ] as $child ) {
     2170                       
     2171                        $page_list[] = $child;
     2172                        _page_traverse( $child->ID, $children, $page_list );
    21502173                }
    21512174        }
    2152         return $page_list;
    21532175}
    21542176
    21552177/**