Walker improvements from hailin. fixes #7353

git-svn-id: https://develop.svn.wordpress.org/trunk@8494 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-07-29 22:50:57 +00:00
parent 3437da6843
commit a9751a9ee9
1 changed files with 33 additions and 38 deletions

View File

@ -424,20 +424,17 @@ class Walker {
return; return;
$id_field = $this->db_fields['id']; $id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
//display this element //display this element
$cb_args = array_merge( array(&$output, $element, $depth), $args); $cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args); call_user_func_array(array(&$this, 'start_el'), $cb_args);
if ( $max_depth == 0 || $id = $element->$id_field;
($max_depth != 0 && $max_depth > $depth+1 )) { //whether to descend
$num_elements = sizeof( $children_elements ); // descend only the depth is right and there are chilrens for this element
for ( $i = 0; $i < $num_elements; $i++ ) { if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
$child = $children_elements[$i]; foreach( $children_elements[ $id ] as $child ){
if ( $child->$parent_field == $element->$id_field ) {
if ( !isset($newlevel) ) { if ( !isset($newlevel) ) {
$newlevel = true; $newlevel = true;
@ -445,13 +442,9 @@ class Walker {
$cb_args = array_merge( array(&$output, $depth), $args); $cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args); call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
} }
array_splice( $children_elements, $i, 1 );
$num_elements--;
$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;
}
} }
unset( $children_elements[ $id ] );
} }
if ( isset($newlevel) && $newlevel ){ if ( isset($newlevel) && $newlevel ){
@ -496,7 +489,9 @@ class Walker {
/* /*
* need to display in hierarchical order * need to display in hierarchical order
* splice elements into two buckets: those without parent and those with parent * seperate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg.
* children_elements[10][] contains all sub-elements whose parent is 10.
*/ */
$top_level_elements = array(); $top_level_elements = array();
$children_elements = array(); $children_elements = array();
@ -504,26 +499,24 @@ class Walker {
if ( 0 == $e->$parent_field ) if ( 0 == $e->$parent_field )
$top_level_elements[] = $e; $top_level_elements[] = $e;
else else
$children_elements[] = $e; $children_elements[ $e->$parent_field ][] = $e;
} }
/* /*
* none of the elements is top level * when none of the elements is top level
* the first one must be root of the sub elements * assume the first one must be root of the sub elements
*/ */
if ( !$top_level_elements ) { if ( empty($top_level_elements) ) {
$root = $children_elements[0]; $root = $elements[0];
$num_elements = sizeof($children_elements);
for ( $i = 0; $i < $num_elements; $i++ ) {
$child = $children_elements[$i]; $top_level_elements = array();
if ($root->$parent_field == $child->$parent_field ) { $children_elements = array();
$top_level_elements[] = $child; foreach ( $elements as $e) {
array_splice( $children_elements, $i, 1 ); if ( $root->$parent_field == $e->$parent_field )
$num_elements--; $top_level_elements[] = $e;
$i--; else
} $children_elements[ $e->$parent_field ][] = $e;
} }
} }
@ -536,9 +529,11 @@ class Walker {
*/ */
if ( ( $max_depth == 0 ) && sizeof( $children_elements ) > 0 ) { if ( ( $max_depth == 0 ) && sizeof( $children_elements ) > 0 ) {
$empty_array = array(); $empty_array = array();
foreach ( $children_elements as $orphan_e ) foreach ( $children_elements as $orphans )
$this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output ); foreach( $orphans as $op )
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
} }
return $output; return $output;
} }
} }