Make WordPress Core

Changeset 3573


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

wp_dropdown_pages() and walk_page_tree()

Location:
trunk/wp-includes
Files:
2 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
  • trunk/wp-includes/template-functions-post.php

    r3558 r3573  
    296296function &get_pages($args = '') {
    297297    global $wpdb;
    298 
    299298    parse_str($args, $r);
    300299
     
    305304    if ( !isset($r['sort_order']) )
    306305        $r['sort_order'] = 'ASC';
     306    if ( !isset($r['hierarchical']) )
     307        $r['hierarchical'] = 1;
    307308
    308309    $exclusions = '';
     
    328329    update_page_cache($pages);
    329330
    330     if ( $r['child_of'] )
     331    if ( $r['child_of'] || $r['hierarchical'] )
    331332        $pages = & get_page_children($r['child_of'], $pages);
    332333
     
    334335}
    335336
     337function wp_dropdown_pages($args = '') {
     338    parse_str($args, $r);
     339    if ( !isset($r['depth']) )
     340        $r['depth'] = 0;
     341    if ( !isset($r['child_of']) )
     342        $r['child_of'] = 0;
     343    if ( !isset($r['echo']) )
     344        $r['echo'] = 1;
     345    if ( !isset($r['selected']) )
     346        $r['selected'] = 0;
     347
     348    extract($r);
     349
     350    $pages = get_pages($args);
     351    $output = '';
     352
     353    if ( ! empty($pages) ) {
     354        $output = "<select name='page_id'>\n";
     355        $output .= walk_page_tree($pages, $depth, '_page_dropdown_element', '', '', '', $selected);
     356        $output .= "</select>\n";
     357    }
     358
     359    $output = apply_filters('wp_dropdown_pages', $output);
     360
     361    if ( $echo )
     362        echo $output;
     363
     364    return $output;
     365}
     366
     367function _page_dropdown_element($output, $page, $depth, $selected) {
     368    $pad = str_repeat('&nbsp;', $depth * 3);
     369
     370    $output .= "\t<option value=\"$page->ID\"";
     371    if ( $page->ID == $selected )
     372        $output .= ' selected="selected"';
     373    $output .= '>';
     374    $title = wp_specialchars($page->post_title);
     375    $output .= "$pad$title";
     376    $output .= "</option>\n";
     377
     378    return $output;
     379}
    336380
    337381function wp_list_pages($args = '') {
     
    341385    if ( !isset($r['show_date']) )
    342386        $r['show_date'] = '';
     387    if ( !isset($r['date_format']) )
     388        $r['date_format'] = get_settings('date_format');
    343389    if ( !isset($r['child_of']) )
    344390        $r['child_of'] = 0;
     
    351397
    352398    // Query pages.
    353     $pages = & get_pages($args);
    354     if ( $pages ) {
    355 
     399    $pages = get_pages($args);
     400
     401    if ( !empty($pages) ) {
    356402        if ( $r['title_li'] )
    357403            $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
    358404
    359         // Now loop over all pages that were selected
    360         $page_tree = Array();
    361         foreach ( $pages as $page ) {
    362             // set the title for the current page
    363             $page_tree[$page->ID]['title'] = $page->post_title;
    364             $page_tree[$page->ID]['name'] = $page->post_name;
    365 
    366             // set the selected date for the current page
    367             // depending on the query arguments this is either
    368             // the createtion date or the modification date
    369             // as a unix timestamp. It will also always be in the
    370             // ts field.
    371             if ( !empty($r['show_date']) ) {
    372                 if ( 'modified' == $r['show_date'] )
    373                     $page_tree[$page->ID]['ts'] = $page->post_modified;
    374                 else
    375                     $page_tree[$page->ID]['ts'] = $page->post_date;
    376             }
    377 
    378             // The tricky bit!!
    379             // Using the parent ID of the current page as the
    380             // array index we set the curent page as a child of that page.
    381             // We can now start looping over the $page_tree array
    382             // with any ID which will output the page links from that ID downwards.
    383             if ( $page->post_parent != $page->ID)
    384                 $page_tree[$page->post_parent]['children'][] = $page->ID;
    385         }
    386         // Output of the pages starting with child_of as the root ID.
    387         // child_of defaults to 0 if not supplied in the query.
    388         $output .= _page_level_out($r['child_of'],$page_tree, $r, 0, false);
     405        global $wp_query;
     406        $current_page = $wp_query->get_queried_object_id();
     407        $output .= walk_page_tree($pages, $depth, '_page_list_element_start', '_page_list_element_end', '_page_list_level_start', '_page_list_level_end', $current_page, $r['show_date'], $r['date_format']);
     408
    389409        if ( $r['title_li'] )
    390410            $output .= '</ul></li>';
     
    399419}
    400420
    401 
    402 function _page_level_out($parent, $page_tree, $args, $depth = 0, $echo = true) {
    403     global $wp_query;
    404     $queried_obj = $wp_query->get_queried_object();
    405     $output = '';
    406 
     421function _page_list_level_start($output, $depth) {
     422    $indent = str_repeat("\t", $depth);
     423    $output .= "$indent<ul>\n";
     424    return $output;
     425}
     426
     427function _page_list_level_end($output, $depth) {
     428    $indent = str_repeat("\t", $depth);
     429    $output .= "$indent</ul>\n";
     430    return $output;
     431}
     432
     433function _page_list_element_start($output, $page, $depth, $current_page, $show_date, $date_format) {
    407434    if ( $depth )
    408435        $indent = str_repeat("\t", $depth);
    409         //$indent = join('', array_fill(0,$depth,"\t"));
    410 
    411     if ( !is_array($page_tree[$parent]['children']) )
    412         return false;
    413 
    414     foreach ( $page_tree[$parent]['children'] as $page_id ) {
    415         $cur_page = $page_tree[$page_id];
    416         $title = $cur_page['title'];
    417 
    418         $css_class = 'page_item';
    419         if ( $page_id == $queried_obj->ID )
    420             $css_class .= ' current_page_item';
    421 
    422         $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page_id) . '" title="' . wp_specialchars($title) . '">' . $title . '</a>';
    423 
    424         if ( isset($cur_page['ts']) ) {
    425             $format = get_settings('date_format');
    426             if ( isset($args['date_format']) )
    427                 $format = $args['date_format'];
    428             $output .= " " . mysql2date($format, $cur_page['ts']);
    429         }
    430 
    431         if ( isset($cur_page['children']) && is_array($cur_page['children']) ) {
    432             $new_depth = $depth + 1;
    433 
    434             if ( !$args['depth'] || $depth < ($args['depth']-1) ) {
    435                 $output .= "$indent<ul>\n";
    436                 $output .= _page_level_out($page_id, $page_tree, $args, $new_depth, false);
    437                 $output .= "$indent</ul>\n";
    438             }
    439         }
    440         $output .= "$indent</li>\n";
    441     }
    442     if ( $echo )
    443         echo $output;
    444     else
    445         return $output;
     436
     437    $css_class = 'page_item';
     438    if ( $page->ID == $current_page )
     439        $css_class .= ' current_page_item';
     440
     441    $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . wp_specialchars($page->post_title) . '">' . $page->post_title . '</a>';
     442
     443    if ( !empty($show_date) ) {
     444        if ( 'modified' == $show_date )
     445            $time = $page->post_modified;
     446        else
     447            $time = $page->post_date;
     448
     449        $output .= " " . mysql2date($date_format, $time);
     450    }
     451
     452    return $output;
     453}
     454
     455function _page_list_element_end($output, $page, $depth) {
     456    $output .= "</li>\n";
     457
     458    return $output;
    446459}
    447460
Note: See TracChangeset for help on using the changeset viewer.