<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[19558] trunk/wp-includes/class-wp-admin-bar.php: Admin bar API improvements.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/19558">19558</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2011-12-06 03:42:11 +0000 (Tue, 06 Dec 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>Admin bar API improvements. Props koopersmith. fixes <a href="http://core.trac.wordpress.org/ticket/19416">#19416</a> <a href="http://core.trac.wordpress.org/ticket/19371">#19371</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclasswpadminbarphp">trunk/wp-includes/class-wp-admin-bar.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclasswpadminbarphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-admin-bar.php (19557 => 19558)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-admin-bar.php        2011-12-06 02:39:53 UTC (rev 19557)
+++ trunk/wp-includes/class-wp-admin-bar.php        2011-12-06 03:42:11 UTC (rev 19558)
</span><span class="lines">@@ -1,6 +1,7 @@
</span><span class="cx"> <?php
</span><span class="cx"> class WP_Admin_Bar {
</span><span class="cx">         private $nodes = array();
</span><ins>+        private $bound = false;
</ins><span class="cx">         public $user;
</span><span class="cx">
</span><span class="cx">         public function __get( $name ) {
</span><span class="lines">@@ -18,11 +19,6 @@
</span><span class="cx">         public function initialize() {
</span><span class="cx">                 $this->user = new stdClass;
</span><span class="cx">
</span><del>-                $this->add_node( array(
-                        'id' => 'root',
-                        'group' => false,
-                ) );
-
</del><span class="cx">                 if ( is_user_logged_in() ) {
</span><span class="cx">                         /* Populate settings we need for the menu based on the current user. */
</span><span class="cx">                         $this->user->blogs = get_blogs_of_user( get_current_user_id() );
</span><span class="lines">@@ -104,8 +100,8 @@
</span><span class="cx">                 );
</span><span class="cx">
</span><span class="cx">                 // If the node already exists, keep any data that isn't provided.
</span><del>-                if ( $this->get_node( $args['id'] ) )
-                        $defaults = get_object_vars( $this->get_node( $args['id'] ) );
</del><ins>+                if ( $maybe_defaults = $this->get_node( $args['id'] ) )
+                        $defaults = get_object_vars( $maybe_defaults );
</ins><span class="cx">
</span><span class="cx">                 // Do the same for 'meta' items.
</span><span class="cx">                 if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
</span><span class="lines">@@ -114,8 +110,8 @@
</span><span class="cx">                 $args = wp_parse_args( $args, $defaults );
</span><span class="cx">
</span><span class="cx">                 $back_compat_parents = array(
</span><del>-                        'my-account-with-avatar' => array( 'my-account', '3.3' ),
-                        'my-blogs' => array( 'my-sites', '3.3' ),
</del><ins>+                        'my-account-with-avatar' => array( 'my-account', '3.3' ),
+                        'my-blogs' => array( 'my-sites', '3.3' ),
</ins><span class="cx">                 );
</span><span class="cx">
</span><span class="cx">                 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
</span><span class="lines">@@ -137,11 +133,35 @@
</span><span class="cx">          * @return object Node.
</span><span class="cx">          */
</span><span class="cx">         final public function get_node( $id ) {
</span><ins>+                if ( $node = $this->_get_node( $id ) )
+                        return clone $node;
+        }
+
+        final protected function _get_node( $id ) {
+                if ( $this->bound )
+                        return;
+
+                if ( empty( $id ) )
+                        $id = 'root';
+
</ins><span class="cx">                 if ( isset( $this->nodes[ $id ] ) )
</span><span class="cx">                         return $this->nodes[ $id ];
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        final public function get_nodes() {
+         if ( ! $nodes = $this->_get_nodes() )
+         return;
+
+         foreach ( $nodes as &$node ) {
+         $node = clone $node;
+         }
+         return $nodes;
+        }
+
</ins><span class="cx">         final protected function _get_nodes() {
</span><ins>+                if ( $this->bound )
+                        return;
+
</ins><span class="cx">                 return $this->nodes;
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -175,50 +195,131 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         public function render() {
</span><del>-                $this->_bind();
-                $this->_render();
</del><ins>+                $root = $this->_bind();
+                $this->_render( $root );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         final protected function _bind() {
</span><ins>+                if ( $this->bound )
+                        return;
+
+                // Add the root node.
+                // Clear it first, just in case. Don't mess with The Root.
+                $this->remove_node( 'root' );
+                $this->add_node( array(
+                        'id' => 'root',
+                        'group' => false,
+                ) );
+
+                // Normalize nodes: define internal 'children' and 'type' properties.
</ins><span class="cx">                 foreach ( $this->_get_nodes() as $node ) {
</span><ins>+                        $node->children = array();
+                        $node->type = ( $node->group ) ? 'group' : 'item';
+                        unset( $node->group );
+
+                        // The Root wants your orphans. No lonely items allowed.
+                        if ( ! $node->parent )
+                                $node->parent = 'root';
+                }
+
+                foreach ( $this->_get_nodes() as $node ) {
</ins><span class="cx">                         if ( 'root' == $node->id )
</span><span class="cx">                                 continue;
</span><span class="cx">
</span><del>-                        // Handle root menu items
-                        if ( empty( $node->parent ) ) {
-                                $parent = $this->get_node( 'root' );
-                        } elseif ( ! $parent = $this->get_node( $node->parent ) ) {
-                                // If the parent node isn't registered, ignore the node.
</del><ins>+                        // Fetch the parent node. If it isn't registered, ignore the node.
+                        if ( ! $parent = $this->_get_node( $node->parent ) ) {
</ins><span class="cx">                                 continue;
</span><span class="cx">                         }
</span><span class="cx">
</span><del>-                        // Ensure that our tree is of the form "item -> group -> item -> group -> ..."
-                        if ( ! $parent->group && ! $node->group ) { // Both are items.
</del><ins>+                        // Generate the group class (we distinguish between top level and other level groups).
+                        $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
+
+                        if ( $node->type == 'group' ) {
+                                if ( empty( $node->meta['class'] ) )
+                                        $node->meta['class'] = '';
+                                $node->meta['class'] .= ' ' . $group_class;
+                        }
+
+                        // Items in items aren't allowed. Wrap nested items in 'default' groups.
+                        if ( $parent->type == 'item' && $node->type == 'item' ) {
+                                $default_id = $parent->id . '-default';
+                                $default = $this->_get_node( $default_id );
+
</ins><span class="cx">                                 // The default group is added here to allow groups that are
</span><span class="cx">                                 // added before standard menu items to render first.
</span><del>-                                if ( ! isset( $parent->children['default'] ) ) {
-                                        $parent->children['default'] = (object) array(
-                                                'id' => "{$parent->id}-default",
-                                                'parent' => $parent->id,
-                                                'group' => true,
-                                                'children' => array(),
-                                        );
</del><ins>+                                if ( ! $default ) {
+                                        // Use _set_node because add_node can be overloaded.
+                                        // Make sure to specify default settings for all properties.
+                                        $this->_set_node( array(
+                                                'id' => $default_id,
+                                                'parent' => $parent->id,
+                                                'type' => 'group',
+                                                'children' => array(),
+                                                'meta' => array(
+                                                        'class' => $group_class,
+                                                ),
+                                                'title' => false,
+                                                'href' => false,
+                                        ) );
+                                        $default = $this->_get_node( $default_id );
+                                        $parent->children[] = $default;
</ins><span class="cx">                                 }
</span><del>-                                $parent = $parent->children['default'];
</del><ins>+                                $parent = $default;
+
+                        // Groups in groups aren't allowed. Add a special 'container' node.
+                        // The container will invisibly wrap both groups.
+                        } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
+                                $container_id = $parent->id . '-container';
+                                $container = $this->_get_node( $container_id );
+
+                                // We need to create a container for this group, life is sad.
+                                if ( ! $container ) {
+                                        // Use _set_node because add_node can be overloaded.
+                                        // Make sure to specify default settings for all properties.
+                                        $this->_set_node( array(
+                                                'id' => $container_id,
+                                                'type' => 'container',
+                                                'children' => array( $parent ),
+                                                'parent' => false,
+                                                'title' => false,
+                                                'href' => false,
+                                                'meta' => array(),
+                                        ) );
+
+                                        $container = $this->_get_node( $container_id );
+
+                                        // Link the container node if a grandparent node exists.
+                                        $grandparent = $this->_get_node( $parent->parent );
+
+                                        if ( $grandparent ) {
+                                                $container->parent = $grandparent->id;
+
+                                                $index = array_search( $parent, $grandparent->children, true );
+                                                if ( $index === false )
+                                                        $grandparent->children[] = $container;
+                                                else
+                                                        array_splice( $grandparent->children, $index, 1, array( $container ) );
+                                        }
+
+                                        $parent->parent = $container->id;
+                                }
+
+                                $parent = $container;
</ins><span class="cx">                         }
</span><span class="cx">
</span><span class="cx">                         // Update the parent ID (it might have changed).
</span><span class="cx">                         $node->parent = $parent->id;
</span><span class="cx">
</span><del>-                        if ( ! isset( $parent->children ) )
-                                $parent->children = array();
-
</del><span class="cx">                         // Add the node to the tree.
</span><span class="cx">                         $parent->children[] = $node;
</span><span class="cx">                 }
</span><ins>+
+                $root = $this->_get_node( 'root' );
+                $this->bound = true;
+                return $root;
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        final protected function _render() {
</del><ins>+        final protected function _render( $root ) {
</ins><span class="cx">                 global $is_IE, $is_iphone;
</span><span class="cx">
</span><span class="cx">                 // Add browser classes.
</span><span class="lines">@@ -238,8 +339,8 @@
</span><span class="cx">                 ?>
</span><span class="cx">                 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
</span><span class="cx">                         <div class="quicklinks">
</span><del>-                                <?php foreach ( $this->get_node( 'root' )->children as $group ) {
-                                        $this->_render_group( $group, 'ab-top-menu' );
</del><ins>+                                <?php foreach ( $root->children as $group ) {
+                                        $this->_render_group( $group );
</ins><span class="cx">                                 } ?>
</span><span class="cx">                         </div>
</span><span class="cx">                 </div>
</span><span class="lines">@@ -247,51 +348,35 @@
</span><span class="cx">                 <?php
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        final protected function _render_group( $node, $class = '' ) {
-                if ( ! $node->group )
</del><ins>+        final protected function _render_container( $node ) {
+                if ( $node->type != 'container' || empty( $node->children ) )
</ins><span class="cx">                         return;
</span><span class="cx">
</span><del>-                // Check for groups within groups.
-                $groups = array();
-                foreach ( $node->children as $child ) {
-                        if ( $child->group ) {
-                                $groups[] = $child;
-                        } else {
-                                if ( ! isset( $default ) ) {
-                                        // Create a default proxy item to be used in the case of nested groups.
-                                        $default = (object) wp_parse_args( array( 'children' => array() ), (array) $node );
-                                        $groups[] = $default;
-                                }
-                                $default->children[] = $child;
</del><ins>+                ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
+                        foreach ( $node->children as $group ) {
+                                $this->_render_group( $group );
</ins><span class="cx">                         }
</span><del>-                }
</del><ins>+                ?></div><?php
+        }
</ins><span class="cx">
</span><del>-                $is_single_group = count( $groups ) === 1;
</del><ins>+        final protected function _render_group( $node ) {
+                if ( $node->type == 'container' )
+                        return $this->_render_container( $node );
</ins><span class="cx">
</span><del>-                // If we don't have any subgroups, render the group.
-                if ( $is_single_group && ! empty( $node->children ) ) :
</del><ins>+                if ( $node->type != 'group' || empty( $node->children ) )
+                        return;
</ins><span class="cx">
</span><del>-                        if ( ! empty( $node->meta['class'] ) )
-                                $class .= ' ' . $node->meta['class'];
</del><ins>+                $class = empty( $node->meta['class'] ) ? '' : $node->meta['class'];
</ins><span class="cx">
</span><del>-                        ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
-                                foreach ( $node->children as $item ) {
-                                        $this->_render_item( $item );
-                                }
-                        ?></ul><?php
-
-                // Wrap the subgroups in a div and render each individual subgroup.
-                elseif ( ! $is_single_group ) :
-                        ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container"><?php
-                                foreach ( $groups as $group ) {
-                                        $this->_render_group( $group, $class );
-                                }
-                        ?></div><?php
-                endif;
</del><ins>+                ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
+                        foreach ( $node->children as $item ) {
+                                $this->_render_item( $item );
+                        }
+                ?></ul><?php
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         final protected function _render_item( $node ) {
</span><del>-                if ( $node->group )
</del><ins>+                if ( $node->type != 'item' )
</ins><span class="cx">                         return;
</span><span class="cx">
</span><span class="cx">                 $is_parent = ! empty( $node->children );
</span><span class="lines">@@ -312,7 +397,7 @@
</span><span class="cx">
</span><span class="cx">                 ?>
</span><span class="cx">
</span><del>-                <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
</del><ins>+                <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
</ins><span class="cx">                         if ( $has_link ):
</span><span class="cx">                                 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
</span><span class="cx">                                         if ( ! empty( $node->meta['onclick'] ) ) :
</span><span class="lines">@@ -344,7 +429,7 @@
</span><span class="cx">                         if ( $is_parent ) :
</span><span class="cx">                                 ?><div class="ab-sub-wrapper"><?php
</span><span class="cx">                                         foreach ( $node->children as $group ) {
</span><del>-                                                $this->_render_group( $group, 'ab-submenu' );
</del><ins>+                                                $this->_render_group( $group );
</ins><span class="cx">                                         }
</span><span class="cx">                                 ?></div><?php
</span><span class="cx">                         endif;
</span></span></pre>
</div>
</div>
</body>
</html>