<!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>[19120] trunk/wp-includes/class-wp-admin-bar.php: Improve admin bar internal representation.</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/19120">19120</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2011-11-02 20:34:54 +0000 (Wed, 02 Nov 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Improve admin bar internal representation. Simpler signatures, with backwards compatibility. Add items without requiring parents to be added first. see <a href="http://core.trac.wordpress.org/ticket/18197">#18197</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 (19119 => 19120)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-admin-bar.php        2011-11-02 20:14:10 UTC (rev 19119)
+++ trunk/wp-includes/class-wp-admin-bar.php        2011-11-02 20:34:54 UTC (rev 19120)
</span><span class="lines">@@ -1,16 +1,17 @@
</span><span class="cx"> &lt;?php
</span><span class="cx"> class WP_Admin_Bar {
</span><del>-        var $menu;
-        var $proto = 'http://';
-        var $user;
</del><ins>+        private $nodes = array();
+        private $root = array();
</ins><span class="cx"> 
</span><ins>+        public $proto = 'http://';
+        public $user;
+
</ins><span class="cx">         function initialize() {
</span><span class="cx">                 /* Set the protocol used throughout this code */
</span><span class="cx">                 if ( is_ssl() )
</span><span class="cx">                         $this-&gt;proto = 'https://';
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;user = new stdClass;
</span><del>-                $this-&gt;menu = new stdClass;
</del><span class="cx"> 
</span><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="lines">@@ -46,83 +47,109 @@
</span><span class="cx">                 do_action( 'admin_bar_init' );
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function add_menu( $args = array() ) {
-                $defaults = array(
-                        'title' =&gt; false,
-                        'href' =&gt; false,
-                        'parent' =&gt; false, // false for a root menu, pass the ID value for a submenu of that menu.
-                        'id' =&gt; false, // defaults to a sanitized title value.
-                        'meta' =&gt; false // array of any of the following options: array( 'html' =&gt; '', 'class' =&gt; '', 'onclick' =&gt; '', target =&gt; '', title =&gt; '' );
-                );
</del><ins>+        public function add_menu( $node ) {
+                $this-&gt;add_node( $node );
+        }
</ins><span class="cx"> 
</span><del>-                $r = wp_parse_args( $args, $defaults );
-                extract( $r, EXTR_SKIP );
</del><ins>+        public function remove_menu( $id ) {
+                $this-&gt;remove_node( $id );
+        }
</ins><span class="cx"> 
</span><del>-                if ( empty( $title ) )
</del><ins>+        /**
+         * Add a node to the menu.
+         *
+         * @param array $args - The arguments for each node.
+         * - id       - string - The ID of the item.
+         * - title    - string - The title of the node.
+         * - parent   - string - The ID of the parent node. Optional.
+         * - href     - string - The link for the item. Optional.
+         * - meta     - array  - Meta data including the following keys: html, class, onclick, target, title.
+         */
+        public function add_node( $args ) {
+                // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
+                if ( func_num_args() &gt;= 3 &amp;&amp; is_string( func_get_arg(0) ) )
+                        $args = array_merge( array( 'parent' =&gt; func_get_arg(0) ), func_get_arg(2) );
+
+                // Ensure we have a valid ID and title.
+                if ( empty( $args['title'] ) || empty( $args['id'] ) )
</ins><span class="cx">                         return false;
</span><span class="cx"> 
</span><del>-                /* Make sure we have a valid ID */
-                if ( empty( $id ) )
-                        $id = esc_attr( sanitize_title( trim( $title ) ) );
</del><ins>+                $defaults = array(
+                        'id'       =&gt; false,
+                        'title'    =&gt; false,
+                        'parent'   =&gt; false,
+                        'href'     =&gt; false,
+                        'meta'     =&gt; array(),
+                );
</ins><span class="cx"> 
</span><del>-                if ( ! empty( $parent ) ) {
-                        /* Add the menu to the parent item */
-                        $child = array( 'id' =&gt; $id, 'title' =&gt; $title, 'href' =&gt; $href );
</del><ins>+                // If the node already exists, keep any data that isn't provided.
+                if ( isset( $this-&gt;nodes[ $args['id'] ] ) )
+                        $defaults = (array) $this-&gt;nodes[ $args['id'] ];
</ins><span class="cx"> 
</span><del>-                        if ( ! empty( $meta ) )
-                                $child['meta'] = $meta;
</del><ins>+                $args = wp_parse_args( $args, $defaults );
</ins><span class="cx"> 
</span><del>-                        $this-&gt;add_node( $parent, $this-&gt;menu, $child );
-                } else {
-                        /* Add the menu item */
-                        $this-&gt;menu-&gt;{$id} = array( 'title' =&gt; $title, 'href' =&gt; $href );
-
-                        if ( ! empty( $meta ) )
-                                $this-&gt;menu-&gt;{$id}['meta'] = $meta;
-                }
</del><ins>+                $this-&gt;nodes[ $args['id'] ] = (object) $args;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function remove_menu( $id ) {
-                return $this-&gt;remove_node( $id, $this-&gt;menu );
</del><ins>+        public function remove_node( $id ) {
+                unset( $this-&gt;nodes[ $id ] );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function render() {
</del><ins>+        public function render() {
+                // Link nodes to parents.
+                foreach ( $this-&gt;nodes as $node ) {
+
+                        // Handle root menu items
+                        if ( empty( $node-&gt;parent ) ) {
+                                $this-&gt;root[] = $node;
+                                continue;
+                        }
+
+                        // If the parent node isn't registered, ignore the node.
+                        if ( ! isset( $this-&gt;nodes[ $node-&gt;parent ] ) )
+                                continue;
+
+                        $parent = $this-&gt;nodes[ $node-&gt;parent ];
+                        if ( ! isset( $parent-&gt;children ) )
+                                $parent-&gt;children = array();
+
+                        $parent-&gt;children[] = $node;
+                }
+
</ins><span class="cx">                 ?&gt;
</span><span class="cx">                 &lt;div id=&quot;wpadminbar&quot; class=&quot;nojq nojs&quot;&gt;
</span><span class="cx">                         &lt;div class=&quot;quicklinks&quot;&gt;
</span><del>-                                &lt;ul class=&quot;ab-top-menu&quot;&gt;
-                                        &lt;?php foreach ( (array) $this-&gt;menu as $id =&gt; $menu_item ) : ?&gt;
-                                                &lt;?php $this-&gt;recursive_render( $id, $menu_item ) ?&gt;
-                                        &lt;?php endforeach; ?&gt;
-                                &lt;/ul&gt;
</del><ins>+                                &lt;ul class=&quot;ab-top-menu&quot;&gt;&lt;?php
+
+                                        foreach ( $this-&gt;root as $node ) {
+                                                $this-&gt;recursive_render( $node );
+                                        }
+
+                                ?&gt;&lt;/ul&gt;
</ins><span class="cx">                         &lt;/div&gt;
</span><span class="cx">                 &lt;/div&gt;
</span><span class="cx"> 
</span><span class="cx">                 &lt;?php
</span><del>-                /* Wipe the menu, might reduce memory usage, but probably not. */
-                $this-&gt;menu = null;
</del><span class="cx">         }
</span><span class="cx"> 
</span><del>-        /* Helpers */
-        function recursive_render( $id, &amp;$menu_item ) { ?&gt;
-                &lt;?php
-                $is_parent =  ! empty( $menu_item['children'] );
</del><ins>+        function recursive_render( $node ) {
+                $is_parent = ! empty( $node-&gt;children );
</ins><span class="cx"> 
</span><span class="cx">                 $menuclass = $is_parent ? 'menupop' : '';
</span><del>-                if ( ! empty( $menu_item['meta']['class'] ) )
-                        $menuclass .= ' ' . $menu_item['meta']['class'];
</del><ins>+                if ( ! empty( $node-&gt;meta['class'] ) )
+                        $menuclass .= ' ' . $node-&gt;meta['class'];
</ins><span class="cx">                 ?&gt;
</span><span class="cx"> 
</span><del>-                &lt;li id=&quot;&lt;?php echo esc_attr( &quot;wp-admin-bar-$id&quot; ); ?&gt;&quot; class=&quot;&lt;?php echo esc_attr( $menuclass ); ?&gt;&quot;&gt;
-                        &lt;a href=&quot;&lt;?php echo esc_url( $menu_item['href'] ) ?&gt;&quot;&lt;?php
-                                if ( ! empty( $menu_item['meta']['onclick'] ) ) :
-                                        ?&gt; onclick=&quot;&lt;?php echo esc_js( $menu_item['meta']['onclick'] ); ?&gt;&quot;&lt;?php
</del><ins>+                &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;a href=&quot;&lt;?php echo esc_url( $node-&gt;href ) ?&gt;&quot;&lt;?php
+                                if ( ! empty( $node-&gt;meta['onclick'] ) ) :
+                                        ?&gt; onclick=&quot;&lt;?php echo esc_js( $node-&gt;meta['onclick'] ); ?&gt;&quot;&lt;?php
</ins><span class="cx">                                 endif;
</span><del>-                        if ( ! empty( $menu_item['meta']['target'] ) ) :
-                                ?&gt; target=&quot;&lt;?php echo esc_attr( $menu_item['meta']['target'] ); ?&gt;&quot;&lt;?php
</del><ins>+                        if ( ! empty( $node-&gt;meta['target'] ) ) :
+                                ?&gt; target=&quot;&lt;?php echo esc_attr( $node-&gt;meta['target'] ); ?&gt;&quot;&lt;?php
</ins><span class="cx">                         endif;
</span><del>-                        if ( ! empty( $menu_item['meta']['title'] ) ) :
-                                ?&gt; title=&quot;&lt;?php echo esc_attr( $menu_item['meta']['title'] ); ?&gt;&quot;&lt;?php
</del><ins>+                        if ( ! empty( $node-&gt;meta['title'] ) ) :
+                                ?&gt; title=&quot;&lt;?php echo esc_attr( $node-&gt;meta['title'] ); ?&gt;&quot;&lt;?php
</ins><span class="cx">                         endif;
</span><span class="cx"> 
</span><span class="cx">                         ?&gt;&gt;&lt;?php
</span><span class="lines">@@ -131,7 +158,7 @@
</span><span class="cx">                                 ?&gt;&lt;span&gt;&lt;?php
</span><span class="cx">                         endif;
</span><span class="cx"> 
</span><del>-                        echo $menu_item['title'];
</del><ins>+                        echo $node-&gt;title;
</ins><span class="cx"> 
</span><span class="cx">                         if ( $is_parent ) :
</span><span class="cx">                                 ?&gt;&lt;/span&gt;&lt;?php
</span><span class="lines">@@ -140,36 +167,22 @@
</span><span class="cx">                         ?&gt;&lt;/a&gt;
</span><span class="cx"> 
</span><span class="cx">                         &lt;?php if ( $is_parent ) : ?&gt;
</span><del>-                        &lt;ul&gt;
-                                &lt;?php foreach ( $menu_item['children'] as $child_id =&gt; $child_menu_item ) : ?&gt;
-                                        &lt;?php $this-&gt;recursive_render( $child_id, $child_menu_item ); ?&gt;
-                                &lt;?php endforeach; ?&gt;
-                        &lt;/ul&gt;
-                        &lt;?php endif; ?&gt;
</del><ins>+                                &lt;ul&gt;&lt;?php
</ins><span class="cx"> 
</span><del>-                        &lt;?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?&gt;
-                                &lt;?php echo $menu_item['meta']['html']; ?&gt;
-                        &lt;?php endif; ?&gt;
-                &lt;/li&gt;&lt;?php
-        }
</del><ins>+                                // Render children.
+                                foreach ( $node-&gt;children as $child_node ) {
+                                        $this-&gt;recursive_render( $child_node );
+                                }
</ins><span class="cx"> 
</span><del>-        function add_node( $parent_id, &amp;$menu, $child ) {
-                foreach( $menu as $id =&gt; $menu_item ) {
-                        if ( $parent_id == $id ) {
-                                if ( ! isset( $menu-&gt;{$parent_id}['children'] ) )
-                                        $menu-&gt;{$parent_id}['children'] = new stdClass;
-                                $menu-&gt;{$parent_id}['children']-&gt;{$child['id']} = $child;
-                                $child = null;
-                                return true;
-                        }
</del><ins>+                                ?&gt;&lt;/ul&gt;
+                        &lt;?php endif;
</ins><span class="cx"> 
</span><del>-                        if ( ! empty( $menu-&gt;{$id}['children'] ) )
-                                $this-&gt;add_node( $parent_id, $menu-&gt;{$id}['children'], $child );
-                }
</del><ins>+                        if ( ! empty( $node-&gt;meta['html'] ) )
+                                echo $node-&gt;meta['html'];
</ins><span class="cx"> 
</span><del>-                $child = null;
</del><ins>+                        ?&gt;
+                &lt;/li&gt;&lt;?php
</ins><span class="cx"> 
</span><del>-                return false;
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         function add_menus() {
</span><span class="lines">@@ -196,19 +209,5 @@
</span><span class="cx"> 
</span><span class="cx">                 do_action( 'add_admin_bar_menus' );
</span><span class="cx">         }
</span><del>-
-        function remove_node( $id, &amp;$menu ) {
-                if ( isset( $menu-&gt;$id ) ) {
-                        unset( $menu-&gt;$id );
-                        return true;
-                }
-
-                foreach( $menu as $menu_item_id =&gt; $menu_item ) {
-                        if ( ! empty( $menu-&gt;{$menu_item_id}['children'] ) )
-                                $this-&gt;remove_node( $id, $menu-&gt;{$menu_item_id}['children'] );
-                }
-
-                return false;
-        }
</del><span class="cx"> }
</span><span class="cx"> ?&gt;
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>