<!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"> &lt;?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-&gt;user = new stdClass;
</span><span class="cx"> 
</span><del>-                $this-&gt;add_node( array(
-                        'id'       =&gt; 'root',
-                        'group'    =&gt; 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-&gt;user-&gt;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-&gt;get_node( $args['id'] ) )
-                        $defaults = get_object_vars( $this-&gt;get_node( $args['id'] ) );
</del><ins>+                if ( $maybe_defaults = $this-&gt;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'] ) &amp;&amp; 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' =&gt; array( 'my-account',  '3.3' ),
-                        'my-blogs'               =&gt; array( 'my-sites', '3.3' ),
</del><ins>+                        'my-account-with-avatar' =&gt; array( 'my-account', '3.3' ),
+                        'my-blogs'               =&gt; 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-&gt;_get_node( $id ) )
+                        return clone $node;
+        }
+
+        final protected function _get_node( $id ) {
+                if ( $this-&gt;bound )
+                        return;
+
+                if ( empty( $id ) )
+                        $id = 'root';
+
</ins><span class="cx">                 if ( isset( $this-&gt;nodes[ $id ] ) )
</span><span class="cx">                         return $this-&gt;nodes[ $id ];
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        final public function get_nodes() {
+           if ( ! $nodes = $this-&gt;_get_nodes() )
+              return;
+
+           foreach ( $nodes as &amp;$node ) {
+               $node = clone $node;
+           }
+           return $nodes;
+        }
+
</ins><span class="cx">         final protected function _get_nodes() {
</span><ins>+                if ( $this-&gt;bound )
+                        return;
+
</ins><span class="cx">                 return $this-&gt;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-&gt;_bind();
-                $this-&gt;_render();
</del><ins>+                $root = $this-&gt;_bind();
+                $this-&gt;_render( $root );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         final protected function _bind() {
</span><ins>+                if ( $this-&gt;bound )
+                        return;
+
+                // Add the root node.
+                // Clear it first, just in case. Don't mess with The Root.
+                $this-&gt;remove_node( 'root' );
+                $this-&gt;add_node( array(
+                        'id'    =&gt; 'root',
+                        'group' =&gt; false,
+                ) );
+
+                // Normalize nodes: define internal 'children' and 'type' properties.
</ins><span class="cx">                 foreach ( $this-&gt;_get_nodes() as $node ) {
</span><ins>+                        $node-&gt;children = array();
+                        $node-&gt;type = ( $node-&gt;group ) ? 'group' : 'item';
+                        unset( $node-&gt;group );
+
+                        // The Root wants your orphans. No lonely items allowed.
+                        if ( ! $node-&gt;parent )
+                                $node-&gt;parent = 'root';
+                }
+
+                foreach ( $this-&gt;_get_nodes() as $node ) {
</ins><span class="cx">                         if ( 'root' == $node-&gt;id )
</span><span class="cx">                                 continue;
</span><span class="cx"> 
</span><del>-                        // Handle root menu items
-                        if ( empty( $node-&gt;parent ) ) {
-                                $parent = $this-&gt;get_node( 'root' );
-                        } elseif ( ! $parent = $this-&gt;get_node( $node-&gt;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-&gt;_get_node( $node-&gt;parent ) ) {
</ins><span class="cx">                                 continue;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><del>-                        // Ensure that our tree is of the form &quot;item -&gt; group -&gt; item -&gt; group -&gt; ...&quot;
-                        if ( ! $parent-&gt;group &amp;&amp; ! $node-&gt;group ) { // Both are items.
</del><ins>+                        // Generate the group class (we distinguish between top level and other level groups).
+                        $group_class = ( $node-&gt;parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
+
+                        if ( $node-&gt;type == 'group' ) {
+                                if ( empty( $node-&gt;meta['class'] ) )
+                                        $node-&gt;meta['class'] = '';
+                                $node-&gt;meta['class'] .= ' ' . $group_class;
+                        }
+
+                        // Items in items aren't allowed. Wrap nested items in 'default' groups.
+                        if ( $parent-&gt;type == 'item' &amp;&amp; $node-&gt;type == 'item' ) {
+                                $default_id = $parent-&gt;id . '-default';
+                                $default    = $this-&gt;_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-&gt;children['default'] ) ) {
-                                        $parent-&gt;children['default'] = (object) array(
-                                                'id'       =&gt; &quot;{$parent-&gt;id}-default&quot;,
-                                                'parent'   =&gt; $parent-&gt;id,
-                                                'group'    =&gt; true,
-                                                'children' =&gt; array(),
-                                        );
</del><ins>+                                if ( ! $default ) {
+                                        // Use _set_node because add_node can be overloaded.
+                                        // Make sure to specify default settings for all properties.
+                                        $this-&gt;_set_node( array(
+                                                'id'        =&gt; $default_id,
+                                                'parent'    =&gt; $parent-&gt;id,
+                                                'type'      =&gt; 'group',
+                                                'children'  =&gt; array(),
+                                                'meta'      =&gt; array(
+                                                        'class'     =&gt; $group_class,
+                                                ),
+                                                'title'     =&gt; false,
+                                                'href'      =&gt; false,
+                                        ) );
+                                        $default = $this-&gt;_get_node( $default_id );
+                                        $parent-&gt;children[] = $default;
</ins><span class="cx">                                 }
</span><del>-                                $parent = $parent-&gt;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-&gt;type == 'group' &amp;&amp; $node-&gt;type == 'group' ) {
+                                $container_id = $parent-&gt;id . '-container';
+                                $container    = $this-&gt;_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-&gt;_set_node( array(
+                                                'id'       =&gt; $container_id,
+                                                'type'     =&gt; 'container',
+                                                'children' =&gt; array( $parent ),
+                                                'parent'   =&gt; false,
+                                                'title'    =&gt; false,
+                                                'href'     =&gt; false,
+                                                'meta'     =&gt; array(),
+                                        ) );
+
+                                        $container = $this-&gt;_get_node( $container_id );
+
+                                        // Link the container node if a grandparent node exists.
+                                        $grandparent = $this-&gt;_get_node( $parent-&gt;parent );
+
+                                        if ( $grandparent ) {
+                                                $container-&gt;parent = $grandparent-&gt;id;
+
+                                                $index = array_search( $parent, $grandparent-&gt;children, true );
+                                                if ( $index === false )
+                                                        $grandparent-&gt;children[] = $container;
+                                                else
+                                                        array_splice( $grandparent-&gt;children, $index, 1, array( $container ) );
+                                        }
+
+                                        $parent-&gt;parent = $container-&gt;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-&gt;parent = $parent-&gt;id;
</span><span class="cx"> 
</span><del>-                        if ( ! isset( $parent-&gt;children ) )
-                                $parent-&gt;children = array();
-
</del><span class="cx">                         // Add the node to the tree.
</span><span class="cx">                         $parent-&gt;children[] = $node;
</span><span class="cx">                 }
</span><ins>+
+                $root = $this-&gt;_get_node( 'root' );
+                $this-&gt;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">                 ?&gt;
</span><span class="cx">                 &lt;div id=&quot;wpadminbar&quot; class=&quot;&lt;?php echo $class; ?&gt;&quot; role=&quot;navigation&quot;&gt;
</span><span class="cx">                         &lt;div class=&quot;quicklinks&quot;&gt;
</span><del>-                                &lt;?php foreach ( $this-&gt;get_node( 'root' )-&gt;children as $group ) {
-                                        $this-&gt;_render_group( $group, 'ab-top-menu' );
</del><ins>+                                &lt;?php foreach ( $root-&gt;children as $group ) {
+                                        $this-&gt;_render_group( $group );
</ins><span class="cx">                                 } ?&gt;
</span><span class="cx">                         &lt;/div&gt;
</span><span class="cx">                 &lt;/div&gt;
</span><span class="lines">@@ -247,51 +348,35 @@
</span><span class="cx">                 &lt;?php
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        final protected function _render_group( $node, $class = '' ) {
-                if ( ! $node-&gt;group )
</del><ins>+        final protected function _render_container( $node ) {
+                if ( $node-&gt;type != 'container' || empty( $node-&gt;children ) )
</ins><span class="cx">                         return;
</span><span class="cx"> 
</span><del>-                // Check for groups within groups.
-                $groups = array();
-                foreach ( $node-&gt;children as $child ) {
-                        if ( $child-&gt;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' =&gt; array() ), (array) $node );
-                                        $groups[] = $default;
-                                }
-                                $default-&gt;children[] = $child;
</del><ins>+                ?&gt;&lt;div id=&quot;&lt;?php echo esc_attr( 'wp-admin-bar-' . $node-&gt;id ); ?&gt;&quot; class=&quot;ab-group-container&quot;&gt;&lt;?php
+                        foreach ( $node-&gt;children as $group ) {
+                                $this-&gt;_render_group( $group );
</ins><span class="cx">                         }
</span><del>-                }
</del><ins>+                ?&gt;&lt;/div&gt;&lt;?php
+        }
</ins><span class="cx"> 
</span><del>-                $is_single_group = count( $groups ) === 1;
</del><ins>+        final protected function _render_group( $node ) {
+                if ( $node-&gt;type == 'container' )
+                        return $this-&gt;_render_container( $node );
</ins><span class="cx"> 
</span><del>-                // If we don't have any subgroups, render the group.
-                if ( $is_single_group &amp;&amp; ! empty( $node-&gt;children ) ) :
</del><ins>+                if ( $node-&gt;type != 'group' || empty( $node-&gt;children ) )
+                        return;
</ins><span class="cx"> 
</span><del>-                        if ( ! empty( $node-&gt;meta['class'] ) )
-                                $class .= ' ' . $node-&gt;meta['class'];
</del><ins>+                $class = empty( $node-&gt;meta['class'] ) ? '' : $node-&gt;meta['class'];
</ins><span class="cx"> 
</span><del>-                        ?&gt;&lt;ul id=&quot;&lt;?php echo esc_attr( &quot;wp-admin-bar-{$node-&gt;id}&quot; ); ?&gt;&quot; class=&quot;&lt;?php echo esc_attr( $class ); ?&gt;&quot;&gt;&lt;?php
-                                foreach ( $node-&gt;children as $item ) {
-                                        $this-&gt;_render_item( $item );
-                                }
-                        ?&gt;&lt;/ul&gt;&lt;?php
-
-                // Wrap the subgroups in a div and render each individual subgroup.
-                elseif ( ! $is_single_group ) :
-                        ?&gt;&lt;div id=&quot;&lt;?php echo esc_attr( &quot;wp-admin-bar-{$node-&gt;id}-container&quot; ); ?&gt;&quot; class=&quot;ab-group-container&quot;&gt;&lt;?php
-                                foreach ( $groups as $group ) {
-                                        $this-&gt;_render_group( $group, $class );
-                                }
-                        ?&gt;&lt;/div&gt;&lt;?php
-                endif;
</del><ins>+                ?&gt;&lt;ul id=&quot;&lt;?php echo esc_attr( 'wp-admin-bar-' . $node-&gt;id ); ?&gt;&quot; class=&quot;&lt;?php echo esc_attr( $class ); ?&gt;&quot;&gt;&lt;?php
+                        foreach ( $node-&gt;children as $item ) {
+                                $this-&gt;_render_item( $item );
+                        }
+                ?&gt;&lt;/ul&gt;&lt;?php
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         final protected function _render_item( $node ) {
</span><del>-                if ( $node-&gt;group )
</del><ins>+                if ( $node-&gt;type != 'item' )
</ins><span class="cx">                         return;
</span><span class="cx"> 
</span><span class="cx">                 $is_parent = ! empty( $node-&gt;children );
</span><span class="lines">@@ -312,7 +397,7 @@
</span><span class="cx"> 
</span><span class="cx">                 ?&gt;
</span><span class="cx"> 
</span><del>-                &lt;li id=&quot;&lt;?php echo esc_attr( &quot;wp-admin-bar-{$node-&gt;id}&quot; ); ?&gt;&quot; class=&quot;&lt;?php echo esc_attr( $menuclass ); ?&gt;&quot;&gt;&lt;?php
</del><ins>+                &lt;li id=&quot;&lt;?php echo esc_attr( 'wp-admin-bar-' . $node-&gt;id ); ?&gt;&quot; class=&quot;&lt;?php echo esc_attr( $menuclass ); ?&gt;&quot;&gt;&lt;?php
</ins><span class="cx">                         if ( $has_link ):
</span><span class="cx">                                 ?&gt;&lt;a class=&quot;ab-item&quot; &lt;?php echo $aria_attributes; ?&gt; href=&quot;&lt;?php echo esc_url( $node-&gt;href ) ?&gt;&quot;&lt;?php
</span><span class="cx">                                         if ( ! empty( $node-&gt;meta['onclick'] ) ) :
</span><span class="lines">@@ -344,7 +429,7 @@
</span><span class="cx">                         if ( $is_parent ) :
</span><span class="cx">                                 ?&gt;&lt;div class=&quot;ab-sub-wrapper&quot;&gt;&lt;?php
</span><span class="cx">                                         foreach ( $node-&gt;children as $group ) {
</span><del>-                                                $this-&gt;_render_group( $group, 'ab-submenu' );
</del><ins>+                                                $this-&gt;_render_group( $group );
</ins><span class="cx">                                         }
</span><span class="cx">                                 ?&gt;&lt;/div&gt;&lt;?php
</span><span class="cx">                         endif;
</span></span></pre>
</div>
</div>

</body>
</html>