WordPress.org

Make WordPress Core

Changeset 8319


Ignore:
Timestamp:
07/12/2008 03:40:57 PM (13 years ago)
Author:
ryan
Message:

Performance improvements for page_rows(). Props hailin. fixes #7286

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/template.php

    r8296 r8319  
    526526
    527527/*
    528  * displays pages in hierarchical order
     528 * displays pages in hierarchical order with paging support
    529529 */
    530 
    531530function page_rows($pages, $pagenum = 1, $per_page = 20) {
    532531    $level = 0;
     
    539538    }
    540539
    541     // splice pages into two parts: those without parent and those with parent
    542     $top_level_pages = array();
    543     $children_pages  = array();
    544 
    545     // If searching, ignore hierarchy and treat everything as top level, otherwise split
    546     // into top level and children
     540    /*
     541     * arrange pages into two parts: top level pages and children_pages
     542     * children_pages is two dimensional array, eg.
     543     * children_pages[10][] contains all sub-pages whose parent is 10.
     544     * It only takes O(N) to arrange this and it takes O(1) for subsequent lookup operations
     545     * If searching, ignore hierarchy and treat everything as top level
     546     */
    547547    if ( empty($_GET['s']) )  {
     548       
     549        $top_level_pages = array();
     550        $children_pages  = array();
     551       
    548552        foreach ( $pages as $page ) {
     553           
    549554            // catch and repair bad pages
    550555            if ( $page->post_parent == $page->ID ) {
     
    557562                $top_level_pages[] = $page;
    558563            else
    559                 $children_pages[] = $page;
     564                $children_pages[ $page->post_parent ][] = $page;
    560565        }
    561566
     
    566571    $start = ($pagenum - 1) * $per_page;
    567572    $end = $start + $per_page;
     573   
    568574    foreach ( $pages as $page ) {
    569575        if ( $count >= $end )
    570576            break;
    571577
    572         $i++;
    573 
    574578        if ( $count >= $start )
    575579            echo "\t" . display_page_row( $page, $level );
     
    580584            _page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page );
    581585    }
    582 }
    583 
    584 function _page_rows( $pages, &$count, $parent, $level, $pagenum, $per_page ) {
     586   
     587    // if it is the last pagenum and there are orphaned pages, display them with paging as well
     588    if ( isset($children_pages) && $count < $end ){
     589        foreach( $children_pages as $orphans ){
     590            foreach ( $orphans as $op ) {
     591                if ( $count >= $end )
     592                    break;
     593                if ( $count >= $start )
     594                    echo "\t" . display_page_row( $op, 0 );
     595                $count++;
     596            }
     597        }
     598    }
     599}
     600
     601/*
     602 * Given a top level page ID, display the nested hierarchy of sub-pages
     603 * together with paging support
     604 */
     605function _page_rows( &$children_pages, &$count, $parent, $level, $pagenum, $per_page ) {
     606   
     607    if ( ! isset( $children_pages[$parent] ) )
     608        return;
     609       
    585610    $start = ($pagenum - 1) * $per_page;
    586611    $end = $start + $per_page;
    587     $i = -1;
    588     foreach ( $pages as $page ) {
     612   
     613    foreach ( $children_pages[$parent] as $page ) {
     614       
    589615        if ( $count >= $end )
    590616            break;
    591 
    592         $i++;
    593 
    594         if ( $page->post_parent != $parent )
    595             continue;
    596 
     617           
    597618        // If the page starts in a subtree, print the parents.
    598619        if ( $count == $start && $page->post_parent > 0 ) {
     
    615636        if ( $count >= $start )
    616637            echo "\t" . display_page_row( $page, $level );
    617 
    618         unset($pages[$i]); // Prune the working set     
     638           
    619639        $count++;
    620640
    621         _page_rows( $pages, $count, $page->ID, $level + 1, $pagenum, $per_page );
    622     }
     641        _page_rows( $children_pages, $count, $page->ID, $level + 1, $pagenum, $per_page );
     642    }
     643   
     644    unset( $children_pages[$parent] ); //required in order to keep track of orphans
    623645}
    624646
Note: See TracChangeset for help on using the changeset viewer.