Convert Walker classes to pass as reference. Props mdawaffe. fixes #6796 for trunk

git-svn-id: https://develop.svn.wordpress.org/trunk@7761 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren
2008-04-21 19:30:54 +00:00
parent 8107431372
commit 9e8d5079c6
3 changed files with 45 additions and 69 deletions
+40 -57
View File
@@ -395,26 +395,26 @@ class Walker {
var $db_fields;
//abstract callbacks
function start_lvl($output) { return $output; }
function end_lvl($output) { return $output; }
function start_el($output) { return $output; }
function end_el($output) { return $output; }
function start_lvl(&$output) {}
function end_lvl(&$output) {}
function start_el(&$output) {}
function end_el(&$output) {}
/*
* display one element if the element doesn't have any children
* otherwise, display the element and its children
*/
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, $output ) {
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
if ( !$element)
return $output;
if ( !$element )
return;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
//display this element
$cb_args = array_merge( array($output, $element, $depth), $args);
$output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
if ( $max_depth == 0 ||
($max_depth != 0 && $max_depth > $depth+1 )) { //whether to descend
@@ -427,12 +427,12 @@ class Walker {
if ( !isset($newlevel) ) {
$newlevel = true;
//start the child delimiter
$cb_args = array_merge( array($output, $depth), $args);
$output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
array_splice( $children_elements, $i, 1 );
$output = $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
$i = -1;
}
}
@@ -440,15 +440,13 @@ class Walker {
if ( isset($newlevel) && $newlevel ){
//end the child delimiter
$cb_args = array_merge( array($output, $depth), $args);
$output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge( array($output, $element, $depth), $args);
$output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
return $output;
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
}
/*
@@ -476,7 +474,7 @@ class Walker {
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$output = $this->display_element( $e, $empty_array, 1, 0, $args, $output );
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
@@ -512,7 +510,7 @@ class Walker {
}
foreach ( $top_level_elements as $e )
$output = $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
/*
* if we are displaying all levels, and remaining children_elements is not empty,
@@ -521,7 +519,7 @@ class Walker {
if ( ( $max_depth == 0 ) && sizeof( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphan_e )
$output = $this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output );
$this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output );
}
return $output;
}
@@ -531,19 +529,17 @@ class Walker_Page extends Walker {
var $tree_type = 'page';
var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
function start_lvl($output, $depth) {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul>\n";
return $output;
}
function end_lvl($output, $depth) {
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
return $output;
}
function start_el($output, $page, $depth, $current_page, $args) {
function start_el(&$output, $page, $depth, $current_page, $args) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
@@ -571,14 +567,10 @@ class Walker_Page extends Walker {
$output .= " " . mysql2date($date_format, $time);
}
return $output;
}
function end_el($output, $page, $depth) {
function end_el(&$output, $page, $depth) {
$output .= "</li>\n";
return $output;
}
}
@@ -587,18 +579,16 @@ class Walker_PageDropdown extends Walker {
var $tree_type = 'page';
var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
function start_el($output, $page, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
function start_el(&$output, $page, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$output .= "\t<option value=\"$page->ID\"";
if ( $page->ID == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$title = wp_specialchars($page->post_title);
$output .= "$pad$title";
$output .= "</option>\n";
return $output;
$output .= "\t<option value=\"$page->ID\"";
if ( $page->ID == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$title = wp_specialchars($page->post_title);
$output .= "$pad$title";
$output .= "</option>\n";
}
}
@@ -606,25 +596,23 @@ class Walker_Category extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
function start_lvl($output, $depth, $args) {
function start_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
return $output;
}
function end_lvl($output, $depth, $args) {
function end_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
return $output;
}
function start_el($output, $category, $depth, $args) {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = attribute_escape( $category->name);
@@ -687,16 +675,13 @@ class Walker_Category extends Walker {
} else {
$output .= "\t$link<br />\n";
}
return $output;
}
function end_el($output, $page, $depth, $args) {
function end_el(&$output, $page, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
return;
$output .= "</li>\n";
return $output;
}
}
@@ -705,7 +690,7 @@ class Walker_CategoryDropdown extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
function start_el($output, $category, $depth, $args) {
function start_el(&$output, $category, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
@@ -721,8 +706,6 @@ class Walker_CategoryDropdown extends Walker {
$output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
}
$output .= "</option>\n";
return $output;
}
}