Make WordPress Core

Ticket #19416: 19416.2.diff

File 19416.2.diff, 10.3 KB (added by koopersmith, 13 years ago)
  • wp-includes/class-wp-admin-bar.php

     
    11<?php
    22class WP_Admin_Bar {
    33        private $nodes = array();
     4        private $bound = false;
    45        public $user;
    56
    67        public function __get( $name ) {
     
    1819        public function initialize() {
    1920                $this->user = new stdClass;
    2021
    21                 $this->add_node( array(
    22                         'id'       => 'root',
    23                         'group'    => false,
    24                 ) );
    25 
    2622                if ( is_user_logged_in() ) {
    2723                        /* Populate settings we need for the menu based on the current user. */
    2824                        $this->user->blogs = get_blogs_of_user( get_current_user_id() );
     
    104100                );
    105101
    106102                // If the node already exists, keep any data that isn't provided.
    107                 if ( $this->get_node( $args['id'] ) )
    108                         $defaults = get_object_vars( $this->get_node( $args['id'] ) );
     103                if ( $maybe_defaults = $this->get_node( $args['id'] ) )
     104                        $defaults = get_object_vars( $maybe_defaults );
    109105
    110106                // Do the same for 'meta' items.
    111107                if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
     
    114110                $args = wp_parse_args( $args, $defaults );
    115111
    116112                $back_compat_parents = array(
    117                         'my-account-with-avatar' => array( 'my-account',  '3.3' ),
    118                         'my-blogs'               => array( 'my-sites', '3.3' ),
     113                        'my-account-with-avatar' => array( 'my-account', '3.3' ),
     114                        'my-blogs'               => array( 'my-sites',   '3.3' ),
    119115                );
    120116
    121117                if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
     
    137133         * @return object Node.
    138134         */
    139135        final public function get_node( $id ) {
     136                if ( $node = $this->_get_node( $id ) )
     137                        return clone $node;
     138        }
     139
     140        final protected function _get_node( $id ) {
     141                if ( $this->bound )
     142                        return;
     143
     144                if ( empty( $id ) )
     145                        $id = 'root';
     146
    140147                if ( isset( $this->nodes[ $id ] ) )
    141148                        return $this->nodes[ $id ];
    142149        }
    143150
     151        final public function get_nodes() {
     152           if ( ! $nodes = $this->_get_nodes() )
     153              return;
     154
     155           foreach ( $nodes as &$node ) {
     156               $node = clone $node;
     157           }
     158           return $nodes;
     159        }
     160
    144161        final protected function _get_nodes() {
     162                if ( $this->bound )
     163                        return;
     164
    145165                return $this->nodes;
    146166        }
    147167
     
    175195        }
    176196
    177197        public function render() {
    178                 $this->_bind();
    179                 $this->_render();
     198                $root = $this->_bind();
     199                $this->_render( $root );
    180200        }
    181201
    182202        final protected function _bind() {
     203                if ( $this->bound )
     204                        return;
     205
     206                // Add the root node.
     207                // Clear it first, just in case. Don't mess with The Root.
     208                $this->remove_node( 'root' );
     209                $this->add_node( array(
     210                        'id'    => 'root',
     211                        'group' => false,
     212                ) );
     213
     214                // Normalize nodes: define internal 'children' and 'type' properties.
    183215                foreach ( $this->_get_nodes() as $node ) {
     216                        $node->children = array();
     217                        $node->type = ( $node->group ) ? 'group' : 'item';
     218                        unset( $node->group );
     219
     220                        // The Root wants your orphans. No lonely items allowed.
     221                        if ( ! $node->parent )
     222                                $node->parent = 'root';
     223                }
     224
     225                foreach ( $this->_get_nodes() as $node ) {
    184226                        if ( 'root' == $node->id )
    185227                                continue;
    186228
    187                         // Handle root menu items
    188                         if ( empty( $node->parent ) ) {
    189                                 $parent = $this->get_node( 'root' );
    190                         } elseif ( ! $parent = $this->get_node( $node->parent ) ) {
    191                                 // If the parent node isn't registered, ignore the node.
     229                        // Fetch the parent node. If it isn't registered, ignore the node.
     230                        if ( ! $parent = $this->_get_node( $node->parent ) ) {
    192231                                continue;
    193232                        }
    194233
    195                         // Ensure that our tree is of the form "item -> group -> item -> group -> ..."
    196                         if ( ! $parent->group && ! $node->group ) { // Both are items.
     234                        // Generate the group class (we distinguish between top level and other level groups).
     235                        $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
     236
     237                        if ( $node->type == 'group' ) {
     238                                if ( empty( $node->meta['class'] ) )
     239                                        $node->meta['class'] = '';
     240                                $node->meta['class'] .= ' ' . $group_class;
     241                        }
     242
     243                        // Items in items aren't allowed. Wrap nested items in 'default' groups.
     244                        if ( $parent->type == 'item' && $node->type == 'item' ) {
     245                                $default_id = $parent->id . '-default';
     246                                $default    = $this->_get_node( $default_id );
     247
    197248                                // The default group is added here to allow groups that are
    198249                                // added before standard menu items to render first.
    199                                 if ( ! isset( $parent->children['default'] ) ) {
    200                                         $parent->children['default'] = (object) array(
    201                                                 'id'       => "{$parent->id}-default",
    202                                                 'parent'   => $parent->id,
    203                                                 'group'    => true,
    204                                                 'children' => array(),
    205                                         );
     250                                if ( ! $default ) {
     251                                        // Use _set_node because add_node can be overloaded.
     252                                        // Make sure to specify default settings for all properties.
     253                                        $this->_set_node( array(
     254                                                'id'        => $default_id,
     255                                                'parent'    => $parent->id,
     256                                                'type'      => 'group',
     257                                                'children'  => array(),
     258                                                'meta'      => array(
     259                                                        'class'     => $group_class,
     260                                                ),
     261                                                'title'     => false,
     262                                                'href'      => false,
     263                                        ) );
     264                                        $default = $this->_get_node( $default_id );
     265                                        $parent->children[] = $default;
    206266                                }
    207                                 $parent = $parent->children['default'];
     267                                $parent = $default;
     268
     269                        // Groups in groups aren't allowed. Add a special 'container' node.
     270                        // The container will invisibly wrap both groups.
     271                        } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
     272                                $container_id = $parent->id . '-container';
     273                                $container    = $this->_get_node( $container_id );
     274
     275                                // We need to create a container for this group, life is sad.
     276                                if ( ! $container ) {
     277                                        // Use _set_node because add_node can be overloaded.
     278                                        // Make sure to specify default settings for all properties.
     279                                        $this->_set_node( array(
     280                                                'id'       => $container_id,
     281                                                'type'     => 'container',
     282                                                'children' => array( $parent ),
     283                                                'parent'   => false,
     284                                                'title'    => false,
     285                                                'href'     => false,
     286                                                'meta'     => array(),
     287                                        ) );
     288
     289                                        $container = $this->_get_node( $container_id );
     290
     291                                        // Link the container node if a grandparent node exists.
     292                                        $grandparent = $this->_get_node( $parent->parent );
     293
     294                                        if ( $grandparent ) {
     295                                                $container->parent = $grandparent->id;
     296
     297                                                $index = array_search( $parent, $grandparent->children, true );
     298                                                if ( $index === false )
     299                                                        $grandparent->children[] = $container;
     300                                                else
     301                                                        array_splice( $grandparent->children, $index, 1, array( $container ) );
     302                                        }
     303
     304                                        $parent->parent = $container->id;
     305                                }
     306
     307                                $parent = $container;
    208308                        }
    209309
    210310                        // Update the parent ID (it might have changed).
    211311                        $node->parent = $parent->id;
    212312
    213                         if ( ! isset( $parent->children ) )
    214                                 $parent->children = array();
    215 
    216313                        // Add the node to the tree.
    217314                        $parent->children[] = $node;
    218315                }
     316
     317                $root = $this->_get_node( 'root' );
     318                $this->bound = true;
     319                return $root;
    219320        }
    220321
    221         final protected function _render() {
     322        final protected function _render( $root ) {
    222323                global $is_IE, $is_iphone;
    223324
    224325                // Add browser classes.
     
    238339                ?>
    239340                <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
    240341                        <div class="quicklinks">
    241                                 <?php foreach ( $this->get_node( 'root' )->children as $group ) {
    242                                         $this->_render_group( $group, 'ab-top-menu' );
     342                                <?php foreach ( $root->children as $group ) {
     343                                        $this->_render_group( $group );
    243344                                } ?>
    244345                        </div>
    245346                </div>
     
    247348                <?php
    248349        }
    249350
    250         final protected function _render_group( $node, $class = '' ) {
    251                 if ( ! $node->group )
     351        final protected function _render_container( $node ) {
     352                if ( $node->type != 'container' || empty( $node->children ) )
    252353                        return;
    253354
    254                 // Check for groups within groups.
    255                 $groups = array();
    256                 foreach ( $node->children as $child ) {
    257                         if ( $child->group ) {
    258                                 $groups[] = $child;
    259                         } else {
    260                                 if ( ! isset( $default ) ) {
    261                                         // Create a default proxy item to be used in the case of nested groups.
    262                                         $default  = (object) wp_parse_args( array( 'children' => array() ), (array) $node );
    263                                         $groups[] = $default;
    264                                 }
    265                                 $default->children[] = $child;
     355                ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
     356                        foreach ( $node->children as $group ) {
     357                                $this->_render_group( $group );
    266358                        }
    267                 }
     359                ?></div><?php
     360        }
    268361
    269                 $is_single_group = count( $groups ) === 1;
     362        final protected function _render_group( $node ) {
     363                if ( $node->type == 'container' )
     364                        return $this->_render_container( $node );
    270365
    271                 // If we don't have any subgroups, render the group.
    272                 if ( $is_single_group && ! empty( $node->children ) ) :
     366                if ( $node->type != 'group' || empty( $node->children ) )
     367                        return;
    273368
    274                         if ( ! empty( $node->meta['class'] ) )
    275                                 $class .= ' ' . $node->meta['class'];
     369                $class = empty( $node->meta['class'] ) ? '' : $node->meta['class'];
    276370
    277                         ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
    278                                 foreach ( $node->children as $item ) {
    279                                         $this->_render_item( $item );
    280                                 }
    281                         ?></ul><?php
    282 
    283                 // Wrap the subgroups in a div and render each individual subgroup.
    284                 elseif ( ! $is_single_group ) :
    285                         ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container"><?php
    286                                 foreach ( $groups as $group ) {
    287                                         $this->_render_group( $group, $class );
    288                                 }
    289                         ?></div><?php
    290                 endif;
     371                ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
     372                        foreach ( $node->children as $item ) {
     373                                $this->_render_item( $item );
     374                        }
     375                ?></ul><?php
    291376        }
    292377
    293378        final protected function _render_item( $node ) {
    294                 if ( $node->group )
     379                if ( $node->type != 'item' )
    295380                        return;
    296381
    297382                $is_parent = ! empty( $node->children );
     
    312397
    313398                ?>
    314399
    315                 <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
     400                <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
    316401                        if ( $has_link ):
    317402                                ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
    318403                                        if ( ! empty( $node->meta['onclick'] ) ) :
     
    344429                        if ( $is_parent ) :
    345430                                ?><div class="ab-sub-wrapper"><?php
    346431                                        foreach ( $node->children as $group ) {
    347                                                 $this->_render_group( $group, 'ab-submenu' );
     432                                                $this->_render_group( $group );
    348433                                        }
    349434                                ?></div><?php
    350435                        endif;