Improve get_page_hierarchy, props hailin, fixes #10853

git-svn-id: http://svn.automattic.com/wordpress/trunk@11976 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-09-26 22:45:52 +00:00
parent 0630337d8e
commit ac1deea61c

View File

@ -2155,24 +2155,50 @@ function &get_page_children($page_id, $pages) {
/** /**
* Order the pages with children under parents in a flat list. * Order the pages with children under parents in a flat list.
* *
* It uses auxiliary structure to hold parent-children relationships and
* runs in O(N) complexity
*
* @since 2.0.0 * @since 2.0.0
* *
* @param array $posts Posts array. * @param array $posts Posts array.
* @param int $parent Parent page ID. * @param int $parent Parent page ID.
* @return array A list arranged by hierarchy. Children immediately follow their parents. * @return array A list arranged by hierarchy. Children immediately follow their parents.
*/ */
function get_page_hierarchy($posts, $parent = 0) { function &get_page_hierarchy( &$pages, $page_id = 0 ) {
$result = array ( );
if ($posts) { foreach ( (array) $posts as $post) { if ( empty( $pages ) )
if ($post->post_parent == $parent) { return null;
$result[$post->ID] = $post->post_name;
$children = get_page_hierarchy($posts, $post->ID); $children = array();
$result += $children; //append $children to $result foreach ( (array) $pages as $p ) {
}
} } $parent_id = intval( $p->post_parent );
$children[ $parent_id ][] = $p;
}
$result = array();
_page_traverse_name( $page_id, $children, $result );
return $result; return $result;
} }
/**
* function to traverse and return all the nested children post names of a root page.
* $children contains parent-chilren relations
*
*/
function _page_traverse_name( $page_id, &$children, &$result ){
if ( isset( $children[ $page_id ] ) ){
foreach( (array)$children[ $page_id ] as $child ) {
$result[ $child->ID ] = $child->post_name;
_page_traverse_name( $child->ID, $children, $result );
}
}
}
/** /**
* Builds URI for a page. * Builds URI for a page.
* *