Make WordPress Core

Changeset 11976


Ignore:
Timestamp:
09/26/2009 10:45:52 PM (14 years ago)
Author:
azaozz
Message:

Improve get_page_hierarchy, props hailin, fixes #10853

File:
1 edited

Legend:

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

    r11968 r11976  
    21562156 * Order the pages with children under parents in a flat list.
    21572157 *
     2158 * It uses auxiliary structure to hold parent-children relationships and
     2159 * runs in O(N) complexity
     2160 *
    21582161 * @since 2.0.0
    21592162 *
     
    21622165 * @return array A list arranged by hierarchy. Children immediately follow their parents.
    21632166 */
    2164 function get_page_hierarchy($posts, $parent = 0) {
    2165     $result = array ( );
    2166     if ($posts) { foreach ( (array) $posts as $post) {
    2167         if ($post->post_parent == $parent) {
    2168             $result[$post->ID] = $post->post_name;
    2169             $children = get_page_hierarchy($posts, $post->ID);
    2170             $result += $children; //append $children to $result
    2171         }
    2172     } }
     2167function &get_page_hierarchy( &$pages, $page_id = 0 ) {
     2168   
     2169    if ( empty( $pages ) )
     2170        return null;
     2171       
     2172    $children = array();
     2173    foreach ( (array) $pages as $p ) {
     2174       
     2175        $parent_id = intval( $p->post_parent );
     2176        $children[ $parent_id ][] = $p;
     2177     }
     2178     
     2179     $result = array();
     2180     _page_traverse_name( $page_id, $children, $result );
     2181     
    21732182    return $result;
     2183}
     2184
     2185/**
     2186 * function to traverse and return all the nested children post names of a root page.
     2187 * $children contains parent-chilren relations
     2188 *
     2189 */
     2190function _page_traverse_name( $page_id, &$children, &$result ){
     2191   
     2192    if ( isset( $children[ $page_id ] ) ){
     2193       
     2194        foreach( (array)$children[ $page_id ] as $child ) {
     2195           
     2196            $result[ $child->ID ] = $child->post_name;
     2197            _page_traverse_name( $child->ID, $children, $result );
     2198        }
     2199    }
    21742200}
    21752201
Note: See TracChangeset for help on using the changeset viewer.