<!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->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"> <?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->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 <code>menu</code> property.' );
+                                return array(); // Sorry, folks.
+                                break;
+                }
+        }
</ins><span class="cx">
</span><ins>+        public function initialize() {
</ins><span class="cx">                 $this->user = new stdClass;
</span><del>-                $this->root = (object) array(
</del><ins>+
+                $this->add_node( array(
</ins><span class="cx">                         'id' => 'root',
</span><span class="cx">                         'group' => false,
</span><del>-                        'children' => 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() >= 3 && is_string( func_get_arg(0) ) )
</span><span class="cx">                         $args = array_merge( array( 'parent' => 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->nodes[ $args['id'] ] ) )
-                        $defaults = (array) $this->nodes[ $args['id'] ];
</del><ins>+                if ( $this->get_node( $args['id'] ) )
+                        $defaults = get_object_vars( $this->get_node( $args['id'] ) );
</ins><span class="cx">
</span><ins>+                // Do the same for 'meta' items.
+                if ( ! empty( $defaults['meta'] ) && 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->_set_node( $args );
+        }
+
+        final protected function _set_node( $args ) {
</ins><span class="cx">                 $this->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->nodes[ $id ] ) )
+                        return $this->nodes[ $id ];
+        }
+
+        final protected function _get_nodes() {
+                return $this->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->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->_unset_node( $id );
+        }
+
+        final protected function _unset_node( $id ) {
</ins><span class="cx">                 unset( $this->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->_bind();
+                $this->_render();
+        }
</ins><span class="cx">
</span><del>-                // Link nodes to parents.
-                foreach ( $this->nodes as $node ) {
</del><ins>+        final protected function _bind() {
+                foreach ( $this->_get_nodes() as $node ) {
+                        if ( 'root' == $node->id )
+                                continue;
</ins><span class="cx">
</span><span class="cx">                         // Handle root menu items
</span><span class="cx">                         if ( empty( $node->parent ) ) {
</span><del>-                                $parent = $this->root;
-
-                        // If the parent node isn't registered, ignore the node.
-                        } elseif ( ! isset( $this->nodes[ $node->parent ] ) ) {
</del><ins>+                                $parent = $this->get_node( 'root' );
+                        } elseif ( ! $parent = $this->get_node( $node->parent ) ) {
+                                // If the parent node isn't registered, ignore the node.
</ins><span class="cx">                                 continue;
</span><del>-
-                        } else {
-                                $parent = $this->nodes[ $node->parent ];
</del><span class="cx">                         }
</span><span class="cx">
</span><del>-
</del><span class="cx">                         // Ensure that our tree is of the form "item -> group -> item -> group -> ..."
</span><span class="cx">                         if ( ! $parent->group && ! $node->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->parent = $parent->id;
</span><span class="cx">
</span><ins>+                        if ( ! isset( $parent->children ) )
+                                $parent->children = array();
+
</ins><span class="cx">                         // Add the node to the tree.
</span><span class="cx">                         $parent->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">                 ?>
</span><span class="cx">                 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
</span><span class="cx">                         <div class="quicklinks" role="menubar">
</span><del>-                                <?php foreach ( $this->root->children as $group ) {
-                                        $this->render_group( $group, 'ab-top-menu' );
</del><ins>+                                <?php foreach ( $this->get_node( 'root' )->children as $group ) {
+                                        $this->_render_group( $group, 'ab-top-menu' );
</ins><span class="cx">                                 } ?>
</span><span class="cx">                         </div>
</span><span class="cx">                 </div>
</span><span class="lines">@@ -190,7 +236,7 @@
</span><span class="cx">                 <?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->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 && ! empty( $node->children ) ):
</del><ins>+                if ( $is_single_group && ! empty( $node->children ) ) :
</ins><span class="cx">
</span><span class="cx">                         if ( ! empty( $node->meta['class'] ) )
</span><span class="cx">                                 $class .= ' ' . $node->meta['class'];
</span><span class="cx">
</span><span class="cx">                         ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php
</span><span class="cx">                                 foreach ( $node->children as $item ) {
</span><del>-                                        $this->render_item( $item );
</del><ins>+                                        $this->_render_item( $item );
</ins><span class="cx">                                 }
</span><span class="cx">                         ?></ul><?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">                         ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php
</span><span class="cx">                                 foreach ( $groups as $group ) {
</span><del>-                                        $this->render_group( $group, $class );
</del><ins>+                                        $this->_render_group( $group, $class );
</ins><span class="cx">                                 }
</span><span class="cx">                         ?></div><?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->group )
</span><span class="cx">                         return;
</span><span class="cx">
</span><del>-                $is_parent = (bool) $node->children;
-                $has_link = (bool) $node->href;
-                $tabindex = isset($node->meta['tabindex']) ? (int) $node->meta['tabindex'] : 10;
</del><ins>+                $is_parent = ! empty( $node->children );
+                $has_link = ! empty( $node->href );
</ins><span class="cx">
</span><ins>+                $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10;
+
</ins><span class="cx">                 $menuclass = '';
</span><span class="cx">                 $aria_attributes = 'tabindex="' . $tabindex . '" role="menuitem"';
</span><span class="cx">
</span><span class="lines">@@ -278,7 +324,7 @@
</span><span class="cx">
</span><span class="cx">                         echo $node->title;
</span><span class="cx">
</span><del>-                        if ( $has_link ):
</del><ins>+                        if ( $has_link ) :
</ins><span class="cx">                                 ?></a><?php
</span><span class="cx">                         else:
</span><span class="cx">                                 ?></div><?php
</span><span class="lines">@@ -287,7 +333,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, 'ab-submenu' );
</ins><span class="cx">                                         }
</span><span class="cx">                                 ?></div><?php
</span><span class="cx">                         endif;
</span><span class="lines">@@ -299,11 +345,12 @@
</span><span class="cx">                 </li><?php
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function recursive_render( $node ) {
-                $this->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->_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>