<!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"> <?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->proto = 'https://';
</span><span class="cx">
</span><span class="cx">                 $this->user = new stdClass;
</span><del>-                $this->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' => false,
-                        'href' => false,
-                        'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
-                        'id' => false, // defaults to a sanitized title value.
-                        'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
-                );
</del><ins>+        public function add_menu( $node ) {
+                $this->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->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() >= 3 && is_string( func_get_arg(0) ) )
+                        $args = array_merge( array( 'parent' => 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' => false,
+                        'title' => false,
+                        'parent' => false,
+                        'href' => false,
+                        'meta' => array(),
+                );
</ins><span class="cx">
</span><del>-                if ( ! empty( $parent ) ) {
-                        /* Add the menu to the parent item */
-                        $child = array( 'id' => $id, 'title' => $title, 'href' => $href );
</del><ins>+                // If the node already exists, keep any data that isn't provided.
+                if ( isset( $this->nodes[ $args['id'] ] ) )
+                        $defaults = (array) $this->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->add_node( $parent, $this->menu, $child );
-                } else {
-                        /* Add the menu item */
-                        $this->menu->{$id} = array( 'title' => $title, 'href' => $href );
-
-                        if ( ! empty( $meta ) )
-                                $this->menu->{$id}['meta'] = $meta;
-                }
</del><ins>+                $this->nodes[ $args['id'] ] = (object) $args;
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        function remove_menu( $id ) {
-                return $this->remove_node( $id, $this->menu );
</del><ins>+        public function remove_node( $id ) {
+                unset( $this->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->nodes as $node ) {
+
+                        // Handle root menu items
+                        if ( empty( $node->parent ) ) {
+                                $this->root[] = $node;
+                                continue;
+                        }
+
+                        // If the parent node isn't registered, ignore the node.
+                        if ( ! isset( $this->nodes[ $node->parent ] ) )
+                                continue;
+
+                        $parent = $this->nodes[ $node->parent ];
+                        if ( ! isset( $parent->children ) )
+                                $parent->children = array();
+
+                        $parent->children[] = $node;
+                }
+
</ins><span class="cx">                 ?>
</span><span class="cx">                 <div id="wpadminbar" class="nojq nojs">
</span><span class="cx">                         <div class="quicklinks">
</span><del>-                                <ul class="ab-top-menu">
-                                        <?php foreach ( (array) $this->menu as $id => $menu_item ) : ?>
-                                                <?php $this->recursive_render( $id, $menu_item ) ?>
-                                        <?php endforeach; ?>
-                                </ul>
</del><ins>+                                <ul class="ab-top-menu"><?php
+
+                                        foreach ( $this->root as $node ) {
+                                                $this->recursive_render( $node );
+                                        }
+
+                                ?></ul>
</ins><span class="cx">                         </div>
</span><span class="cx">                 </div>
</span><span class="cx">
</span><span class="cx">                 <?php
</span><del>-                /* Wipe the menu, might reduce memory usage, but probably not. */
-                $this->menu = null;
</del><span class="cx">         }
</span><span class="cx">
</span><del>-        /* Helpers */
-        function recursive_render( $id, &$menu_item ) { ?>
-                <?php
-                $is_parent = ! empty( $menu_item['children'] );
</del><ins>+        function recursive_render( $node ) {
+                $is_parent = ! empty( $node->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->meta['class'] ) )
+                        $menuclass .= ' ' . $node->meta['class'];
</ins><span class="cx">                 ?>
</span><span class="cx">
</span><del>-                <li id="<?php echo esc_attr( "wp-admin-bar-$id" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
-                        <a href="<?php echo esc_url( $menu_item['href'] ) ?>"<?php
-                                if ( ! empty( $menu_item['meta']['onclick'] ) ) :
-                                        ?> onclick="<?php echo esc_js( $menu_item['meta']['onclick'] ); ?>"<?php
</del><ins>+                <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
+                        <a href="<?php echo esc_url( $node->href ) ?>"<?php
+                                if ( ! empty( $node->meta['onclick'] ) ) :
+                                        ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
</ins><span class="cx">                                 endif;
</span><del>-                        if ( ! empty( $menu_item['meta']['target'] ) ) :
-                                ?> target="<?php echo esc_attr( $menu_item['meta']['target'] ); ?>"<?php
</del><ins>+                        if ( ! empty( $node->meta['target'] ) ) :
+                                ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
</ins><span class="cx">                         endif;
</span><del>-                        if ( ! empty( $menu_item['meta']['title'] ) ) :
-                                ?> title="<?php echo esc_attr( $menu_item['meta']['title'] ); ?>"<?php
</del><ins>+                        if ( ! empty( $node->meta['title'] ) ) :
+                                ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
</ins><span class="cx">                         endif;
</span><span class="cx">
</span><span class="cx">                         ?>><?php
</span><span class="lines">@@ -131,7 +158,7 @@
</span><span class="cx">                                 ?><span><?php
</span><span class="cx">                         endif;
</span><span class="cx">
</span><del>-                        echo $menu_item['title'];
</del><ins>+                        echo $node->title;
</ins><span class="cx">
</span><span class="cx">                         if ( $is_parent ) :
</span><span class="cx">                                 ?></span><?php
</span><span class="lines">@@ -140,36 +167,22 @@
</span><span class="cx">                         ?></a>
</span><span class="cx">
</span><span class="cx">                         <?php if ( $is_parent ) : ?>
</span><del>-                        <ul>
-                                <?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?>
-                                        <?php $this->recursive_render( $child_id, $child_menu_item ); ?>
-                                <?php endforeach; ?>
-                        </ul>
-                        <?php endif; ?>
</del><ins>+                                <ul><?php
</ins><span class="cx">
</span><del>-                        <?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?>
-                                <?php echo $menu_item['meta']['html']; ?>
-                        <?php endif; ?>
-                </li><?php
-        }
</del><ins>+                                // Render children.
+                                foreach ( $node->children as $child_node ) {
+                                        $this->recursive_render( $child_node );
+                                }
</ins><span class="cx">
</span><del>-        function add_node( $parent_id, &$menu, $child ) {
-                foreach( $menu as $id => $menu_item ) {
-                        if ( $parent_id == $id ) {
-                                if ( ! isset( $menu->{$parent_id}['children'] ) )
-                                        $menu->{$parent_id}['children'] = new stdClass;
-                                $menu->{$parent_id}['children']->{$child['id']} = $child;
-                                $child = null;
-                                return true;
-                        }
</del><ins>+                                ?></ul>
+                        <?php endif;
</ins><span class="cx">
</span><del>-                        if ( ! empty( $menu->{$id}['children'] ) )
-                                $this->add_node( $parent_id, $menu->{$id}['children'], $child );
-                }
</del><ins>+                        if ( ! empty( $node->meta['html'] ) )
+                                echo $node->meta['html'];
</ins><span class="cx">
</span><del>-                $child = null;
</del><ins>+                        ?>
+                </li><?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, &$menu ) {
-                if ( isset( $menu->$id ) ) {
-                        unset( $menu->$id );
-                        return true;
-                }
-
-                foreach( $menu as $menu_item_id => $menu_item ) {
-                        if ( ! empty( $menu->{$menu_item_id}['children'] ) )
-                                $this->remove_node( $id, $menu->{$menu_item_id}['children'] );
-                }
-
-                return false;
-        }
</del><span class="cx"> }
</span><span class="cx"> ?>
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
</body>
</html>