<!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>[19501] trunk/wp-includes/class-wp-admin-bar.php: Finalize the WP_Admin_Bar architecture for 3.3.</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/19501">19501</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2011-12-01 00:25:04 +0000 (Thu, 01 Dec 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Finalize the WP_Admin_Bar architecture for 3.3.
 * Introduce a get_node() method for plugins.
 * Deprecate $wp_admin_bar-&gt;menu. Plugins will need to use get_node(), remove_node(), add_node() to make modifications. This finalizes a backwards incompatible change made earlier in the cycle.
 * Allow add_node() to take a node object (which could come from get_node(), then be modified).
 * Ensure that our underlying storage (the nodes property) is private to core. Introduce _set_node, _unset_node, _get_nodes, get_nodes as the only ways to interface with this.
 * Protect and finalize _render_item, and _render_group. render() remains public and technically overridable, though I would discourage this of plugin authors.
 * Deprecate recursive_render(). Use render() or _render_item().

More about the internals:
 * Late-binds a node's 'children' array.
 * Eliminates the root property, leverages a 'root' node.
 * Splits render() into _bind() and _render(), both protected and finalized.

Fixes <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 (19500 => 19501)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-admin-bar.php        2011-12-01 00:16:17 UTC (rev 19500)
+++ trunk/wp-includes/class-wp-admin-bar.php        2011-12-01 00:25:04 UTC (rev 19501)
</span><span class="lines">@@ -1,22 +1,27 @@
</span><span class="cx"> &lt;?php
</span><span class="cx"> class WP_Admin_Bar {
</span><span class="cx">         private $nodes = array();
</span><del>-        private $root = array();
-
-        public $proto = 'http://';
</del><span class="cx">         public $user;
</span><span class="cx"> 
</span><del>-        function initialize() {
-                /* Set the protocol used throughout this code */
-                if ( is_ssl() )
-                        $this-&gt;proto = 'https://';
</del><ins>+        public function __get( $name ) {
+                switch ( $name ) {
+                        case 'proto' :
+                                return is_ssl() ? 'https://' : 'http://';
+                                break;
+                        case 'menu' :
+                                _deprecated_argument( 'WP_Admin_Bar', '3.3', 'Modify admin bar nodes with WP_Admin_Bar::get_node(), WP_Admin_Bar::add_node(), and WP_Admin_Bar::remove_node(), not the &lt;code&gt;menu&lt;/code&gt; property.' );
+                                return array(); // Sorry, folks.
+                                break;
+                }
+        }
</ins><span class="cx"> 
</span><ins>+        public function initialize() {
</ins><span class="cx">                 $this-&gt;user = new stdClass;
</span><del>-                $this-&gt;root = (object) array(
</del><ins>+
+                $this-&gt;add_node( array(
</ins><span class="cx">                         'id'       =&gt; 'root',
</span><span class="cx">                         'group'    =&gt; false,
</span><del>-                        'children' =&gt; array(),
-                );
</del><ins>+                ) );
</ins><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">@@ -69,13 +74,16 @@
</span><span class="cx">          * - parent     - string    - The ID of the parent node. Optional.
</span><span class="cx">          * - href       - string    - The link for the item. Optional.
</span><span class="cx">          * - group      - boolean   - If the node is a group. Optional. Default false.
</span><del>-         * - meta       - array     - Meta data including the following keys: html, class, onclick, target, title.
</del><ins>+         * - meta       - array     - Meta data including the following keys: html, class, onclick, target, title, tabindex.
</ins><span class="cx">          */
</span><span class="cx">         public function add_node( $args ) {
</span><span class="cx">                 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
</span><span class="cx">                 if ( func_num_args() &gt;= 3 &amp;&amp; is_string( func_get_arg(0) ) )
</span><span class="cx">                         $args = array_merge( array( 'parent' =&gt; func_get_arg(0) ), func_get_arg(2) );
</span><span class="cx"> 
</span><ins>+                if ( is_object( $args ) )
+                        $args = get_object_vars( $args );
+
</ins><span class="cx">                 // Ensure we have a valid title.
</span><span class="cx">                 if ( empty( $args['id'] ) ) {
</span><span class="cx">                         if ( empty( $args['title'] ) )
</span><span class="lines">@@ -96,52 +104,83 @@
</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 ( isset( $this-&gt;nodes[ $args['id'] ] ) )
-                        $defaults = (array) $this-&gt;nodes[ $args['id'] ];
</del><ins>+                if ( $this-&gt;get_node( $args['id'] ) )
+                        $defaults = get_object_vars( $this-&gt;get_node( $args['id'] ) );
</ins><span class="cx"> 
</span><ins>+                // Do the same for 'meta' items.
+                if ( ! empty( $defaults['meta'] ) &amp;&amp; empty( $args['meta'] ) )
+                        $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
+
</ins><span class="cx">                 $args = wp_parse_args( $args, $defaults );
</span><del>-                $args['children'] = array();
</del><span class="cx"> 
</span><ins>+                $this-&gt;_set_node( $args );
+        }
+
+        final protected function _set_node( $args ) {
</ins><span class="cx">                 $this-&gt;nodes[ $args['id'] ] = (object) $args;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><ins>+         * Gets a node.
+         *
+         * @return object Node.
+         */
+        final public function get_node( $id ) {
+                if ( isset( $this-&gt;nodes[ $id ] ) )
+                        return $this-&gt;nodes[ $id ];
+        }
+
+        final protected function _get_nodes() {
+                return $this-&gt;nodes;
+        }
+
+        /**
</ins><span class="cx">          * Add a group to a menu node.
</span><span class="cx">          *
</span><ins>+         * @since 3.3.0
+         *
</ins><span class="cx">          * @param array $args - The arguments for each node.
</span><span class="cx">          * - id         - string    - The ID of the item.
</span><span class="cx">          * - parent     - string    - The ID of the parent node. Optional. Default root.
</span><span class="cx">          * - meta       - array     - Meta data including the following keys: class, onclick, target, title.
</span><span class="cx">          */
</span><del>-        public function add_group( $args ) {
</del><ins>+        final public function add_group( $args ) {
</ins><span class="cx">                 $args['group'] = true;
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;add_node( $args );
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        /**
+         * Remove a node.
+         *
+         * @return object The removed node.
+         */
</ins><span class="cx">         public function remove_node( $id ) {
</span><ins>+                $this-&gt;_unset_node( $id );
+        }
+
+        final protected function _unset_node( $id ) {
</ins><span class="cx">                 unset( $this-&gt;nodes[ $id ] );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         public function render() {
</span><del>-                global $is_IE, $is_iphone;
</del><ins>+                $this-&gt;_bind();
+                $this-&gt;_render();
+        }
</ins><span class="cx"> 
</span><del>-                // Link nodes to parents.
-                foreach ( $this-&gt;nodes as $node ) {
</del><ins>+        final protected function _bind() {
+                foreach ( $this-&gt;_get_nodes() as $node ) {
+                        if ( 'root' == $node-&gt;id )
+                                continue;
</ins><span class="cx"> 
</span><span class="cx">                         // Handle root menu items
</span><span class="cx">                         if ( empty( $node-&gt;parent ) ) {
</span><del>-                                $parent = $this-&gt;root;
-
-                        // If the parent node isn't registered, ignore the node.
-                        } elseif ( ! isset( $this-&gt;nodes[ $node-&gt;parent ] ) ) {
</del><ins>+                                $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.
</ins><span class="cx">                                 continue;
</span><del>-
-                        } else {
-                                $parent = $this-&gt;nodes[ $node-&gt;parent ];
</del><span class="cx">                         }
</span><span class="cx"> 
</span><del>-
</del><span class="cx">                         // Ensure that our tree is of the form &quot;item -&gt; group -&gt; item -&gt; group -&gt; ...&quot;
</span><span class="cx">                         if ( ! $parent-&gt;group &amp;&amp; ! $node-&gt;group ) { // Both are items.
</span><span class="cx">                                 // The default group is added here to allow groups that are
</span><span class="lines">@@ -160,10 +199,17 @@
</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><ins>+                        if ( ! isset( $parent-&gt;children ) )
+                                $parent-&gt;children = array();
+
</ins><span class="cx">                         // Add the node to the tree.
</span><span class="cx">                         $parent-&gt;children[] = $node;
</span><span class="cx">                 }
</span><ins>+        }
</ins><span class="cx"> 
</span><ins>+        final protected function _render() {
+                global $is_IE, $is_iphone;
+
</ins><span class="cx">                 // Add browser classes.
</span><span class="cx">                 // We have to do this here since admin bar shows on the front end.
</span><span class="cx">                 $class = 'nojq nojs';
</span><span class="lines">@@ -181,8 +227,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; role=&quot;menubar&quot;&gt;
</span><del>-                                &lt;?php foreach ( $this-&gt;root-&gt;children as $group ) {
-                                        $this-&gt;render_group( $group, 'ab-top-menu' );
</del><ins>+                                &lt;?php foreach ( $this-&gt;get_node( 'root' )-&gt;children as $group ) {
+                                        $this-&gt;_render_group( $group, 'ab-top-menu' );
</ins><span class="cx">                                 } ?&gt;
</span><span class="cx">                         &lt;/div&gt;
</span><span class="cx">                 &lt;/div&gt;
</span><span class="lines">@@ -190,7 +236,7 @@
</span><span class="cx">                 &lt;?php
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        private function render_group( $node, $class = '' ) {
</del><ins>+        final protected function _render_group( $node, $class = '' ) {
</ins><span class="cx">                 if ( ! $node-&gt;group )
</span><span class="cx">                         return;
</span><span class="cx"> 
</span><span class="lines">@@ -211,37 +257,37 @@
</span><span class="cx"> 
</span><span class="cx">                 $is_single_group = count( $groups ) === 1;
</span><span class="cx"> 
</span><del>-
</del><span class="cx">                 // If we don't have any subgroups, render the group.
</span><del>-                if ( $is_single_group &amp;&amp; ! empty( $node-&gt;children ) ):
</del><ins>+                if ( $is_single_group &amp;&amp; ! empty( $node-&gt;children ) ) :
</ins><span class="cx"> 
</span><span class="cx">                         if ( ! empty( $node-&gt;meta['class'] ) )
</span><span class="cx">                                 $class .= ' ' . $node-&gt;meta['class'];
</span><span class="cx"> 
</span><span class="cx">                         ?&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; role=&quot;menu&quot;&gt;&lt;?php
</span><span class="cx">                                 foreach ( $node-&gt;children as $item ) {
</span><del>-                                        $this-&gt;render_item( $item );
</del><ins>+                                        $this-&gt;_render_item( $item );
</ins><span class="cx">                                 }
</span><span class="cx">                         ?&gt;&lt;/ul&gt;&lt;?php
</span><span class="cx"> 
</span><span class="cx">                 // Wrap the subgroups in a div and render each individual subgroup.
</span><del>-                elseif ( ! $is_single_group ):
</del><ins>+                elseif ( ! $is_single_group ) :
</ins><span class="cx">                         ?&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; role=&quot;menu&quot;&gt;&lt;?php
</span><span class="cx">                                 foreach ( $groups as $group ) {
</span><del>-                                        $this-&gt;render_group( $group, $class );
</del><ins>+                                        $this-&gt;_render_group( $group, $class );
</ins><span class="cx">                                 }
</span><span class="cx">                         ?&gt;&lt;/div&gt;&lt;?php
</span><span class="cx">                 endif;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        private function render_item( $node ) {
</del><ins>+        final protected function _render_item( $node ) {
</ins><span class="cx">                 if ( $node-&gt;group )
</span><span class="cx">                         return;
</span><span class="cx"> 
</span><del>-                $is_parent = (bool) $node-&gt;children;
-                $has_link  = (bool) $node-&gt;href;
-                $tabindex = isset($node-&gt;meta['tabindex']) ? (int) $node-&gt;meta['tabindex'] : 10;
</del><ins>+                $is_parent = ! empty( $node-&gt;children );
+                $has_link  = ! empty( $node-&gt;href );
</ins><span class="cx"> 
</span><ins>+                $tabindex = isset( $node-&gt;meta['tabindex'] ) ? (int) $node-&gt;meta['tabindex'] : 10;
+
</ins><span class="cx">                 $menuclass = '';
</span><span class="cx">                 $aria_attributes = 'tabindex=&quot;' . $tabindex . '&quot; role=&quot;menuitem&quot;';
</span><span class="cx"> 
</span><span class="lines">@@ -278,7 +324,7 @@
</span><span class="cx"> 
</span><span class="cx">                         echo $node-&gt;title;
</span><span class="cx"> 
</span><del>-                        if ( $has_link ):
</del><ins>+                        if ( $has_link ) :
</ins><span class="cx">                                 ?&gt;&lt;/a&gt;&lt;?php
</span><span class="cx">                         else:
</span><span class="cx">                                 ?&gt;&lt;/div&gt;&lt;?php
</span><span class="lines">@@ -287,7 +333,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, 'ab-submenu' );
</ins><span class="cx">                                         }
</span><span class="cx">                                 ?&gt;&lt;/div&gt;&lt;?php
</span><span class="cx">                         endif;
</span><span class="lines">@@ -299,11 +345,12 @@
</span><span class="cx">                 &lt;/li&gt;&lt;?php
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function recursive_render( $node ) {
-                $this-&gt;render_item( $node );
</del><ins>+        public function recursive_render( $id, $node ) {
+                _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
+                $this-&gt;_render_item( $node );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function add_menus() {
</del><ins>+        public function add_menus() {
</ins><span class="cx">                 // User related, aligned right.
</span><span class="cx">                 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>