Make WordPress Core

Ticket #28801: class-wp-walker.php.patch

File class-wp-walker.php.patch, 1.4 KB (added by ross_ritchey, 9 years ago)

Handle the case when there are no top elements and there is an element with a non-existant parent

  • src/wp-includes/class-wp-walker.php

     
    214214                 */
    215215                $top_level_elements = array();
    216216                $children_elements  = array();
     217               
     218                /*
     219                 * Default $root to the first element
     220                 * Used in the case that there are no top level elements
     221                 */
     222                $first = array_slice( $elements, 0, 1 );                       
     223                $root = $first[0];
     224                $element_ids = array_map(function($element){ return $element->ID; }, $elements);
     225               
    217226                foreach ( $elements as $e) {
    218                         if ( empty( $e->$parent_field ) )
     227                        // If the parent_field is not blank OR in the list of elements update to be the root element
     228                        if ( !empty( $e->parent_field) && !in_array($e->parent_field, $element_ids))
     229                                $root = $e;
     230                        elseif ( empty( $e->$parent_field ) )
    219231                                $top_level_elements[] = $e;
    220232                        else
    221233                                $children_elements[ $e->$parent_field ][] = $e;
     
    223235
    224236                /*
    225237                 * When none of the elements is top level.
    226                  * Assume the first one must be root of the sub elements.
     238                 * Set the element buckets based on the root defined above
    227239                 */
    228240                if ( empty($top_level_elements) ) {
    229 
    230                         $first = array_slice( $elements, 0, 1 );
    231                         $root = $first[0];
    232 
    233241                        $top_level_elements = array();
    234242                        $children_elements  = array();
    235243                        foreach ( $elements as $e) {