| | 545 | // If searching, ignore hierarchy and treat everything as top level, otherwise split |
|---|
| | 546 | // into top level and children |
|---|
| | 547 | if ( empty($_GET['s']) ) { |
|---|
| | 548 | foreach ( $pages as $page ) { |
|---|
| | 549 | // catch and repair bad pages |
|---|
| | 550 | if ( $page->post_parent == $page->ID ) { |
|---|
| | 551 | $page->post_parent = 0; |
|---|
| | 552 | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = '0' WHERE ID = %d", $page->ID) ); |
|---|
| | 553 | clean_page_cache( $page->ID ); |
|---|
| | 554 | } |
|---|
| | 555 | |
|---|
| | 556 | if ( 0 == $page->post_parent ) |
|---|
| | 557 | $top_level_pages[] = $page; |
|---|
| | 558 | else |
|---|
| | 559 | $children_pages[] = $page; |
|---|
| | 560 | } |
|---|
| | 561 | |
|---|
| | 562 | $pages = &$top_level_pages; |
|---|
| | 563 | } |
|---|
| | 564 | |
|---|
| | 565 | $count = 0; |
|---|
| | 566 | $start = ($pagenum - 1) * $per_page; |
|---|
| | 567 | $end = $start + $per_page; |
|---|
| 557 | | |
|---|
| 558 | | // catch and repair bad pages |
|---|
| 559 | | if ( $page->post_parent == $page->ID ) { |
|---|
| 560 | | $page->post_parent = 0; |
|---|
| 561 | | $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = '0' WHERE ID = %d", $page->ID) ); |
|---|
| 562 | | clean_page_cache( $page->ID ); |
|---|
| 563 | | } |
|---|
| 564 | | |
|---|
| 565 | | if ( 0 == $page->post_parent ) |
|---|
| 566 | | $top_level_pages[] = $page; |
|---|
| 567 | | else |
|---|
| 568 | | $children_pages[] = $page; |
|---|
| 569 | | } |
|---|
| 570 | | |
|---|
| 571 | | foreach ( $top_level_pages as $page ) |
|---|
| 572 | | display_page_row($page, $children_pages, 0); |
|---|
| 573 | | |
|---|
| 574 | | /* |
|---|
| 575 | | * display the remaining children_pages which are orphans |
|---|
| 576 | | * having orphan requires parental attention |
|---|
| 577 | | */ |
|---|
| 578 | | if ( count($children_pages) > 0 ) { |
|---|
| 579 | | $empty_array = array(); |
|---|
| 580 | | foreach ( $children_pages as $orphan_page ) { |
|---|
| 581 | | clean_page_cache( $orphan_page->ID); |
|---|
| 582 | | display_page_row( $orphan_page, $empty_array, 0 ); |
|---|
| 583 | | } |
|---|
| 584 | | } |
|---|
| | 569 | if ( $count >= $end ) |
|---|
| | 570 | break; |
|---|
| | 571 | |
|---|
| | 572 | $i++; |
|---|
| | 573 | |
|---|
| | 574 | if ( $count >= $start ) |
|---|
| | 575 | echo "\t" . display_page_row( $page, $level ); |
|---|
| | 576 | |
|---|
| | 577 | $count++; |
|---|
| | 578 | |
|---|
| | 579 | if ( isset($children_pages) ) |
|---|
| | 580 | _page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page ); |
|---|
| | 581 | } |
|---|
| | 582 | } |
|---|
| | 583 | |
|---|
| | 584 | function _page_rows( $pages, &$count, $parent, $level, $pagenum, $per_page ) { |
|---|
| | 585 | $start = ($pagenum - 1) * $per_page; |
|---|
| | 586 | $end = $start + $per_page; |
|---|
| | 587 | $i = -1; |
|---|
| | 588 | foreach ( $pages as $page ) { |
|---|
| | 589 | if ( $count >= $end ) |
|---|
| | 590 | break; |
|---|
| | 591 | |
|---|
| | 592 | $i++; |
|---|
| | 593 | |
|---|
| | 594 | if ( $page->post_parent != $parent ) |
|---|
| | 595 | continue; |
|---|
| | 596 | |
|---|
| | 597 | // If the page starts in a subtree, print the parents. |
|---|
| | 598 | if ( $count == $start && $page->post_parent > 0 ) { |
|---|
| | 599 | $my_parents = array(); |
|---|
| | 600 | $my_parent = $page->post_parent; |
|---|
| | 601 | while ( $my_parent) { |
|---|
| | 602 | $my_parent = get_post($my_parent); |
|---|
| | 603 | $my_parents[] = $my_parent; |
|---|
| | 604 | if ( !$my_parent->post_parent ) |
|---|
| | 605 | break; |
|---|
| | 606 | $my_parent = $my_parent->post_parent; |
|---|
| | 607 | } |
|---|
| | 608 | $num_parents = count($my_parents); |
|---|
| | 609 | while( $my_parent = array_pop($my_parents) ) { |
|---|
| | 610 | echo "\t" . display_page_row( $my_parent, $level - $num_parents ); |
|---|
| | 611 | $num_parents--; |
|---|
| | 612 | } |
|---|
| | 613 | } |
|---|
| | 614 | |
|---|
| | 615 | if ( $count >= $start ) |
|---|
| | 616 | echo "\t" . display_page_row( $page, $level ); |
|---|
| | 617 | |
|---|
| | 618 | unset($pages[$i]); // Prune the working set |
|---|
| | 619 | $count++; |
|---|
| | 620 | |
|---|
| | 621 | _page_rows( $pages, $count, $page->ID, $level + 1, $pagenum, $per_page ); |
|---|
| | 622 | } |
|---|