Make WordPress Core


Ignore:
Timestamp:
02/28/2006 03:57:08 AM (19 years ago)
Author:
ryan
Message:

wp_dropdown_pages() and walk_page_tree()

File:
1 edited

Legend:

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

    r3570 r3573  
    744744        return $_page;
    745745    }
     746}
     747
     748function walk_page_tree($pages, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {
     749    $args = array_slice(func_get_args(), 6);
     750    $parents = array();
     751    $depth = 0;
     752    $previous_page = '';
     753    $output = '';
     754
     755    $last_page->post_parent = 0;
     756    $last_page->post_id = 0;
     757    $pages[] = $last_page;
     758
     759    foreach ( $pages as $page ) {
     760        if ( !empty($previous_page) && ($page->post_parent == $previous_page->ID) ) {
     761            // Previous page is my parent. Descend a level.
     762            array_unshift($parents, $page);
     763            $depth++;
     764            if ( !$to_depth || ($depth < $to_depth) )
     765                if ( !empty($start_level_callback) ) {
     766                    $cb_args = array_merge( array($output, $depth), $args);
     767                    $output = call_user_func_array($start_level_callback, $cb_args);
     768                }
     769        } else if ( $depth && ($page->post_parent == $parents[0]->ID) ) {
     770            // On the same level as previous page.
     771            if ( !$to_depth || ($depth < $to_depth) ) {
     772                if ( !empty($end_element_callback) ) {
     773                    $cb_args = array_merge( array($output, $previous_page, $depth), $args);
     774                    $output = call_user_func_array($end_element_callback, $cb_args);
     775                }
     776            }
     777        } else if ( $depth ) {
     778            // Ascend one or more levels.
     779            if ( !$to_depth || ($depth < $to_depth) ) {
     780                if ( !empty($end_element_callback) ) {
     781                    $cb_args = array_merge( array($output, $previous_page, $depth), $args);
     782                    $output = call_user_func_array($end_element_callback, $cb_args);
     783                }
     784            }
     785
     786            while ( $parent = array_shift($parents)) {
     787                $depth--;
     788                if ( !$to_depth || ($depth < $to_depth) ) {
     789                    if ( !empty($end_level_callback) ) {
     790                        $cb_args = array_merge( array($output, $depth), $args);
     791                        $output = call_user_func_array($end_level_callback, $cb_args);
     792                    }
     793                    if ( !empty($end_element_callback) ) {
     794                        $cb_args = array_merge( array($output, $parent, $depth), $args);
     795                        $output = call_user_func_array($end_element_callback, $cb_args);
     796                    }
     797                }
     798                if ( $page->post_parent == $parent->ID ) {
     799                    break;
     800                }
     801            }
     802        } else if ( !empty($previous_page) ) {
     803            // Close off previous page.
     804            if ( !$to_depth || ($depth < $to_depth) ) {
     805                if ( !empty($end_element_callback) ) {
     806                    $cb_args = array_merge( array($output, $previous_page, $depth), $args);
     807                    $output = call_user_func_array($end_element_callback, $cb_args);
     808                }
     809            }
     810        }
     811
     812        // Start the page.
     813        if ( !$to_depth || ($depth < $to_depth) ) {
     814            if ( !empty($start_element_callback) && ($page->ID != 0) ) {
     815                $cb_args = array_merge( array($output, $page, $depth), $args);
     816                $output = call_user_func_array($start_element_callback, $cb_args);
     817            }
     818        }
     819
     820        $previous_page = $page;
     821    }
     822
     823    return $output;
    746824}
    747825
Note: See TracChangeset for help on using the changeset viewer.