Improve performance of get_page_children()
.
The new algorithm uses a hash table rather than function recursion, reducing complexity to O(N). On large numbers of pages, the performance improvement is several orders of magnitude. Props santagada, hailin, mihai. Fixes #10852. git-svn-id: https://develop.svn.wordpress.org/trunk@32355 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ba712c6789
commit
bb29a878c6
@ -4317,25 +4317,38 @@ function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' )
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve child pages from list of pages matching page ID.
|
||||
* Identify descendants of a given page ID in a list of page objects.
|
||||
*
|
||||
* Matches against the pages parameter against the page ID. Also matches all
|
||||
* children for the same to retrieve all children of a page. Does not make any
|
||||
* SQL queries to get the children.
|
||||
* Descendants are identified from the `$pages` array passed to the function. No database queries are performed.
|
||||
*
|
||||
* @since 1.5.1
|
||||
*
|
||||
* @param int $page_id Page ID.
|
||||
* @param array $pages List of pages' objects.
|
||||
* @param int $page_id Page ID.
|
||||
* @param array $pages List of page objects from which descendants should be identified.
|
||||
* @return array List of page children.
|
||||
*/
|
||||
function get_page_children( $page_id, $pages ) {
|
||||
$page_list = array();
|
||||
// Build a hash of ID -> children.
|
||||
$children = array();
|
||||
foreach ( (array) $pages as $page ) {
|
||||
if ( $page->post_parent == $page_id ) {
|
||||
$page_list[] = $page;
|
||||
if ( $children = get_page_children( $page->ID, $pages ) ) {
|
||||
$page_list = array_merge( $page_list, $children );
|
||||
$children[ intval( $page->post_parent ) ][] = $page;
|
||||
}
|
||||
|
||||
$page_list = array();
|
||||
|
||||
// Start the search by looking at immediate children.
|
||||
if ( isset( $children[ $page_id ] ) ) {
|
||||
// Always start at the end of the stack in order to preserve original `$pages` order.
|
||||
$to_look = array_reverse( $children[ $page_id ] );
|
||||
|
||||
while ( $to_look ) {
|
||||
$p = array_pop( $to_look );
|
||||
$page_list[] = $p;
|
||||
if ( isset( $children[ $p->ID ] ) ) {
|
||||
foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
|
||||
// Append to the `$to_look` stack to descend the tree.
|
||||
$to_look[] = $child;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user