Make WordPress Core

Changeset 8494


Ignore:
Timestamp:
07/29/2008 10:50:57 PM (18 years ago)
Author:
ryan
Message:

Walker improvements from hailin. fixes #7353

File:
1 edited

Legend:

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

    r8263 r8494  
    425425
    426426                $id_field = $this->db_fields['id'];
    427                 $parent_field = $this->db_fields['parent'];
    428427
    429428                //display this element
     
    431430                call_user_func_array(array(&$this, 'start_el'), $cb_args);
    432431
    433                 if ( $max_depth == 0 ||
    434                      ($max_depth != 0 &&  $max_depth > $depth+1 )) { //whether to descend
    435 
    436                         $num_elements = sizeof( $children_elements );
    437                         for ( $i = 0; $i < $num_elements; $i++ ) {
    438 
    439                                 $child = $children_elements[$i];
    440                                 if ( $child->$parent_field == $element->$id_field ) {
    441 
    442                                         if ( !isset($newlevel) ) {
    443                                                 $newlevel = true;
    444                                                 //start the child delimiter
    445                                                 $cb_args = array_merge( array(&$output, $depth), $args);
    446                                                 call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
    447                                         }
    448 
    449                                         array_splice( $children_elements, $i, 1 );
    450                                         $num_elements--;
    451                                         $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
    452                                         $i = -1;
     432                $id = $element->$id_field;
     433               
     434                // descend only the depth is right and there are chilrens for this element
     435                if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
     436
     437                        foreach( $children_elements[ $id ] as $child ){
     438                               
     439                                if ( !isset($newlevel) ) {
     440                                        $newlevel = true;
     441                                        //start the child delimiter
     442                                        $cb_args = array_merge( array(&$output, $depth), $args);
     443                                        call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
    453444                                }
    454                         }
     445                                $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
     446                        }
     447                        unset( $children_elements[ $id ] );
    455448                }
    456449
     
    497490                /*
    498491                 * need to display in hierarchical order
    499                  * splice elements into two buckets: those without parent and those with parent
     492                 * seperate elements into two buckets: top level and children elements
     493                 * children_elements is two dimensional array, eg.
     494                 * children_elements[10][] contains all sub-elements whose parent is 10.
    500495                 */
    501496                $top_level_elements = array();
     
    505500                                $top_level_elements[] = $e;
    506501                        else
    507                                 $children_elements[] = $e;
     502                                $children_elements[ $e->$parent_field ][] = $e;
    508503                }
    509504
    510505                /*
    511                  * none of the elements is top level
    512                  * the first one must be root of the sub elements
     506                 * when none of the elements is top level
     507                 * assume the first one must be root of the sub elements
    513508                 */
    514                 if ( !$top_level_elements ) {
    515 
    516                         $root = $children_elements[0];
    517                         $num_elements = sizeof($children_elements);
    518                         for ( $i = 0; $i < $num_elements; $i++ ) {
    519 
    520                                 $child = $children_elements[$i];
    521                                 if ($root->$parent_field == $child->$parent_field ) {
    522                                         $top_level_elements[] = $child;
    523                                         array_splice( $children_elements, $i, 1 );
    524                                         $num_elements--;
    525                                         $i--;
    526                                 }
     509                if ( empty($top_level_elements) ) {
     510
     511                        $root = $elements[0];
     512                       
     513                        $top_level_elements = array();
     514                        $children_elements  = array();
     515                        foreach ( $elements as $e) {
     516                                if ( $root->$parent_field == $e->$parent_field )
     517                                        $top_level_elements[] = $e;
     518                                else
     519                                        $children_elements[ $e->$parent_field ][] = $e;
    527520                        }
    528521                }
     
    537530                if ( ( $max_depth == 0 ) && sizeof( $children_elements ) > 0 ) {
    538531                        $empty_array = array();
    539                         foreach ( $children_elements as $orphan_e )
    540                                 $this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output );
     532                        foreach ( $children_elements as $orphans )
     533                                foreach( $orphans as $op )
     534                                        $this->display_element( $op, $empty_array, 1, 0, $args, $output );
    541535                 }
     536                 
    542537                 return $output;
    543538        }
Note: See TracChangeset for help on using the changeset viewer.