Make WordPress Core


Ignore:
Timestamp:
06/16/2008 09:08:49 PM (16 years ago)
Author:
mdawaffe
Message:

crazyhorse: merge with trunk [8072:8102]

File:
1 edited

Legend:

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

    r7956 r8103  
    55//
    66
    7 // Dandy new recursive multiple category stuff.
    8 function cat_rows( $parent = 0, $level = 0, $categories = 0 ) {
    9     if ( !$categories ) {
     7// Ugly recursive category stuff.
     8function cat_rows( $parent = 0, $level = 0, $categories = 0, $page = 1, $per_page = 20 ) {
     9    $count = 0;
     10    _cat_rows($categories, $count, $parent, $level, $page, $per_page);
     11}
     12
     13function _cat_rows( $categories, &$count, $parent = 0, $level = 0, $page = 1, $per_page = 20 ) {
     14    if ( empty($categories) ) {
    1015        $args = array('hide_empty' => 0);
    1116        if ( !empty($_GET['s']) )
     
    1419    }
    1520
     21    if ( !$categories )
     22        return false;
     23
    1624    $children = _get_term_hierarchy('category');
    1725
    18     if ( $categories ) {
    19         ob_start();
    20         foreach ( $categories as $category ) {
    21             if ( $category->parent == $parent) {
    22                 echo "\t" . _cat_row( $category, $level );
    23                 if ( isset($children[$category->term_id]) )
    24                     cat_rows( $category->term_id, $level +1, $categories );
     26    $start = ($page - 1) * $per_page;
     27    $end = $start + $per_page;
     28    $i = -1;
     29    ob_start();
     30    foreach ( $categories as $category ) {
     31        if ( $count >= $end )
     32            break;
     33
     34        $i++;
     35
     36        if ( $category->parent != $parent )
     37            continue;
     38
     39        // If the page starts in a subtree, print the parents.
     40        if ( $count == $start && $category->parent > 0 ) {
     41            $my_parents = array();
     42            $my_parent = $category->parent;
     43            while ( $my_parent) {
     44                $my_parent = get_category($my_parent);
     45                $my_parents[] = $my_parent;
     46                if ( !$my_parent->parent )
     47                    break;
     48                $my_parent = $my_parent->parent;
    2549            }
    26         }
    27         $output = ob_get_contents();
    28         ob_end_clean();
    29 
    30         $output = apply_filters('cat_rows', $output);
    31 
    32         echo $output;
    33     } else {
    34         return false;
    35     }
     50            $num_parents = count($my_parents);
     51            while( $my_parent = array_pop($my_parents) ) {
     52                echo "\t" . _cat_row( $my_parent, $level - $num_parents );
     53                $num_parents--;
     54            }
     55        }
     56
     57        if ( $count >= $start )
     58            echo "\t" . _cat_row( $category, $level );
     59
     60        unset($categories[$i]); // Prune the working set       
     61        $count++;
     62
     63        if ( isset($children[$category->term_id]) )
     64            _cat_rows( $categories, $count, $category->term_id, $level + 1, $page, $per_page );
     65
     66    }
     67
     68    $output = ob_get_contents();
     69    ob_end_clean();
     70
     71    $output = apply_filters('cat_rows', $output);
     72
     73    echo $output;
    3674}
    3775
     
    361399 * otherwise, display the row and its children in subsequent rows
    362400 */
    363 function display_page_row( $page, &$children_pages, $level = 0 ) {
     401function display_page_row( $page, $level = 0 ) {
    364402    global $post;
    365403    static $class;
     
    485523
    486524<?php
    487 
    488     if ( ! $children_pages )
    489         return true;
    490 
    491     for ( $i = 0; $i < count($children_pages); $i++ ) {
    492 
    493         $child = $children_pages[$i];
    494 
    495         if ( $child->post_parent == $id ) {
    496             array_splice($children_pages, $i, 1);
    497             display_page_row($child, $children_pages, $level+1);
    498             $i = -1; //as numeric keys in $children_pages are not preserved after splice
    499         }
    500     }
    501525}
    502526
     
    504528 * displays pages in hierarchical order
    505529 */
    506 function page_rows( $pages ) {
    507     if ( ! $pages )
     530
     531function page_rows($pages, $pagenum = 1, $per_page = 20) {
     532    $level = 0;
     533
     534    if ( ! $pages ) {
    508535        $pages = get_pages( array('sort_column' => 'menu_order') );
    509536
    510     if ( ! $pages )
    511         return false;
     537        if ( ! $pages )
     538            return false;
     539    }
    512540
    513541    // splice pages into two parts: those without parent and those with parent
    514 
    515542    $top_level_pages = array();
    516543    $children_pages  = array();
    517544
     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;
    518568    foreach ( $pages as $page ) {
    519 
    520         // catch and repair bad pages
    521         if ( $page->post_parent == $page->ID ) {
    522             $page->post_parent = 0;
    523             $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = '0' WHERE ID = %d", $page->ID) );
    524             clean_page_cache( $page->ID );
    525         }
    526 
    527         if ( 0 == $page->post_parent )
    528             $top_level_pages[] = $page;
    529         else
    530             $children_pages[] = $page;
    531     }
    532 
    533     foreach ( $top_level_pages as $page )
    534         display_page_row($page, $children_pages, 0);
    535 
    536     /*
    537      * display the remaining children_pages which are orphans
    538      * having orphan requires parental attention
    539      */
    540      if ( count($children_pages) > 0 ) {
    541         $empty_array = array();
    542         foreach ( $children_pages as $orphan_page ) {
    543             clean_page_cache( $orphan_page->ID);
    544             display_page_row( $orphan_page, $empty_array, 0 );
    545         }
    546      }
     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
     584function _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    }
    547623}
    548624
Note: See TracChangeset for help on using the changeset viewer.