WordPress.org

Make WordPress Core

Changeset 8939


Ignore:
Timestamp:
09/19/2008 06:59:47 AM (13 years ago)
Author:
ryan
Message:

Add paging to Walker class. Props hailin. fixes #7743

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/classes.php

    r8900 r8939  
    486486     * PHP4 Constructor - Does nothing.
    487487     *
    488      * Call main() method when ready to run setup. 
     488     * Call main() method when ready to run setup.
    489489     *
    490490     * @since 2.0.0
     
    819819        $id = $element->$id_field;
    820820
    821         // descend only the depth is right and there are chilrens for this element
     821        // descend only when the depth is right and there are childrens for this element
    822822        if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
    823823
     
    918918            $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
    919919
    920         /* 
    921          * if we are displaying all levels, and remaining children_elements is not empty, 
     920        /*
     921         * if we are displaying all levels, and remaining children_elements is not empty,
    922922         * then we got orphans, which should be displayed regardless
    923923         */
     
    930930
    931931         return $output;
     932    }
     933
     934    /**
     935     * paged_walk() - produce a page of nested elements
     936     *
     937     * Given an array of hierarchical elements, the maximum depth, a specific page number,
     938     * and number of elements per page, this function first determines all top level root elements
     939     * belonging to that page, then lists them and all of their children in hierarchical order.
     940     *
     941     * @package WordPress
     942     * @since 2.7
     943     * @param $max_depth = 0  means display all levels; $max_depth > 0  specifies the number of display levels.
     944     * @param $page_num the specific page number, beginning with 1.
     945     * @return XHTML of the specified page of elements
     946     */
     947    function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
     948
     949        /* sanity check */
     950        if ( empty($elements) || $max_depth < 0 )
     951            return '';
     952
     953        $args = array_slice( func_get_args(), 4 );
     954        $output = '';
     955
     956        $id_field = $this->db_fields['id'];
     957        $parent_field = $this->db_fields['parent'];
     958
     959        /*
     960         * seperate elements into two buckets: top level and children elements
     961         * children_elements is two dimensional array, eg.
     962         * children_elements[10][] contains all sub-elements whose parent is 10.
     963         */
     964        $top_level_elements = array();
     965        $children_elements  = array();
     966        foreach ( $elements as $e) {
     967            if ( 0 == $e->$parent_field )
     968                $top_level_elements[] = $e;
     969            else
     970                $children_elements[ $e->$parent_field ][] = $e;
     971        }
     972
     973        $count = -1;
     974        $total_top = count( $top_level_elements );
     975        if ( $page_num < 1 || $per_page < 0  ) {
     976            $start = 0;
     977            $end = $total_top;
     978        } else {
     979            $start = ( (int)$page_num - 1 ) * (int)$per_page;
     980            $end   = $start + $per_page;
     981        }
     982
     983        foreach( $top_level_elements as $e ){
     984            $count++;
     985
     986            //for the last page, need to unset earlier children in order to keep track of orphans
     987            if ( $end >= $total_top && $count < $start )
     988                    $this->unset_children( $e, $children_elements );
     989
     990            if ( $count < $start )
     991                continue;
     992
     993            if ( $count >= $end )
     994                break;
     995
     996            $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
     997        }
     998
     999        if ( $end >= $total_top && count( $children_elements ) > 0 ){
     1000            $empty_array = array();
     1001            foreach ( $children_elements as $orphans )
     1002                foreach( $orphans as $op )
     1003                    $this->display_element( $op, $empty_array, 1, 0, $args, $output );
     1004        }
     1005
     1006        return $output;
     1007    }
     1008
     1009    function get_number_of_root_elements( $elements ){
     1010
     1011        $num = 0;
     1012        $parent_field = $this->db_fields['parent'];
     1013
     1014        foreach ( $elements as $e) {
     1015            if ( 0 == $e->$parent_field )
     1016                $num++;
     1017        }
     1018        return $num;
     1019    }
     1020
     1021    // unset all the children for a given top level element
     1022    function unset_children( $e, &$children_elements ){
     1023
     1024        if ( !$e || !$children_elements )
     1025            return;
     1026
     1027        $id_field = $this->db_fields['id'];
     1028        $id = $e->$id_field;
     1029
     1030        foreach ( (array)$children_elements[$id] as $child )
     1031            $this->unset_children( $child, $children_elements );
     1032
     1033        unset( $children_elements[$id] );
     1034
    9321035    }
    9331036}
Note: See TracChangeset for help on using the changeset viewer.