| 1 | Index: wp-includes/class-wp-admin-bar.php |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- wp-includes/class-wp-admin-bar.php (revision 19546) |
|---|
| 4 | +++ wp-includes/class-wp-admin-bar.php (working copy) |
|---|
| 5 | @@ -1,6 +1,7 @@ |
|---|
| 6 | <?php |
|---|
| 7 | class WP_Admin_Bar { |
|---|
| 8 | private $nodes = array(); |
|---|
| 9 | + private $bound = false; |
|---|
| 10 | public $user; |
|---|
| 11 | |
|---|
| 12 | public function __get( $name ) { |
|---|
| 13 | @@ -18,11 +19,6 @@ |
|---|
| 14 | public function initialize() { |
|---|
| 15 | $this->user = new stdClass; |
|---|
| 16 | |
|---|
| 17 | - $this->add_node( array( |
|---|
| 18 | - 'id' => 'root', |
|---|
| 19 | - 'group' => false, |
|---|
| 20 | - ) ); |
|---|
| 21 | - |
|---|
| 22 | if ( is_user_logged_in() ) { |
|---|
| 23 | /* Populate settings we need for the menu based on the current user. */ |
|---|
| 24 | $this->user->blogs = get_blogs_of_user( get_current_user_id() ); |
|---|
| 25 | @@ -104,8 +100,8 @@ |
|---|
| 26 | ); |
|---|
| 27 | |
|---|
| 28 | // If the node already exists, keep any data that isn't provided. |
|---|
| 29 | - if ( $this->get_node( $args['id'] ) ) |
|---|
| 30 | - $defaults = get_object_vars( $this->get_node( $args['id'] ) ); |
|---|
| 31 | + if ( $maybe_defaults = $this->get_node( $args['id'] ) ) |
|---|
| 32 | + $defaults = get_object_vars( $maybe_defaults ); |
|---|
| 33 | |
|---|
| 34 | // Do the same for 'meta' items. |
|---|
| 35 | if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) ) |
|---|
| 36 | @@ -126,11 +122,35 @@ |
|---|
| 37 | * @return object Node. |
|---|
| 38 | */ |
|---|
| 39 | final public function get_node( $id ) { |
|---|
| 40 | + if ( $node = $this->_get_node( $id ) ) |
|---|
| 41 | + return clone $node; |
|---|
| 42 | + } |
|---|
| 43 | + |
|---|
| 44 | + final protected function _get_node( $id ) { |
|---|
| 45 | + if ( $this->bound ) |
|---|
| 46 | + return; |
|---|
| 47 | + |
|---|
| 48 | + if ( empty( $id ) ) |
|---|
| 49 | + $id = 'root'; |
|---|
| 50 | + |
|---|
| 51 | if ( isset( $this->nodes[ $id ] ) ) |
|---|
| 52 | return $this->nodes[ $id ]; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | + final public function get_nodes() { |
|---|
| 56 | + if ( ! $nodes = $this->_get_nodes() ) |
|---|
| 57 | + return; |
|---|
| 58 | + |
|---|
| 59 | + foreach ( $nodes as &$node ) { |
|---|
| 60 | + $node = clone $node; |
|---|
| 61 | + } |
|---|
| 62 | + return $nodes; |
|---|
| 63 | + } |
|---|
| 64 | + |
|---|
| 65 | final protected function _get_nodes() { |
|---|
| 66 | + if ( $this->bound ) |
|---|
| 67 | + return; |
|---|
| 68 | + |
|---|
| 69 | return $this->nodes; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | @@ -164,50 +184,131 @@ |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public function render() { |
|---|
| 76 | - $this->_bind(); |
|---|
| 77 | - $this->_render(); |
|---|
| 78 | + $root = $this->_bind(); |
|---|
| 79 | + $this->_render( $root ); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | final protected function _bind() { |
|---|
| 83 | + if ( $this->bound ) |
|---|
| 84 | + return; |
|---|
| 85 | + |
|---|
| 86 | + // Add the root node. |
|---|
| 87 | + // Clear it first, just in case. Don't mess with The Root. |
|---|
| 88 | + $this->remove_node( 'root' ); |
|---|
| 89 | + $this->add_node( array( |
|---|
| 90 | + 'id' => 'root', |
|---|
| 91 | + 'group' => false, |
|---|
| 92 | + ) ); |
|---|
| 93 | + |
|---|
| 94 | + // Normalize nodes: define internal 'children' and 'type' properties. |
|---|
| 95 | foreach ( $this->_get_nodes() as $node ) { |
|---|
| 96 | + $node->children = array(); |
|---|
| 97 | + $node->type = ( $node->group ) ? 'group' : 'item'; |
|---|
| 98 | + unset( $node->group ); |
|---|
| 99 | + |
|---|
| 100 | + // The Root wants your orphans. No lonely items allowed. |
|---|
| 101 | + if ( ! $node->parent ) |
|---|
| 102 | + $node->parent = 'root'; |
|---|
| 103 | + } |
|---|
| 104 | + |
|---|
| 105 | + foreach ( $this->_get_nodes() as $node ) { |
|---|
| 106 | if ( 'root' == $node->id ) |
|---|
| 107 | continue; |
|---|
| 108 | |
|---|
| 109 | - // Handle root menu items |
|---|
| 110 | - if ( empty( $node->parent ) ) { |
|---|
| 111 | - $parent = $this->get_node( 'root' ); |
|---|
| 112 | - } elseif ( ! $parent = $this->get_node( $node->parent ) ) { |
|---|
| 113 | - // If the parent node isn't registered, ignore the node. |
|---|
| 114 | + // Fetch the parent node. If it isn't registered, ignore the node. |
|---|
| 115 | + if ( ! $parent = $this->_get_node( $node->parent ) ) { |
|---|
| 116 | continue; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | - // Ensure that our tree is of the form "item -> group -> item -> group -> ..." |
|---|
| 120 | - if ( ! $parent->group && ! $node->group ) { // Both are items. |
|---|
| 121 | + // Generate the group class (we distinguish between top level and other level groups). |
|---|
| 122 | + $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu'; |
|---|
| 123 | + |
|---|
| 124 | + if ( $node->type == 'group' ) { |
|---|
| 125 | + if ( empty( $node->meta['class'] ) ) |
|---|
| 126 | + $node->meta['class'] = ''; |
|---|
| 127 | + $node->meta['class'] .= ' ' . $group_class; |
|---|
| 128 | + } |
|---|
| 129 | + |
|---|
| 130 | + // Items in items aren't allowed. Wrap nested items in 'default' groups. |
|---|
| 131 | + if ( $parent->type == 'item' && $node->type == 'item' ) { |
|---|
| 132 | + $default_id = $parent->id . '-default'; |
|---|
| 133 | + $default = $this->_get_node( $default_id ); |
|---|
| 134 | + |
|---|
| 135 | // The default group is added here to allow groups that are |
|---|
| 136 | // added before standard menu items to render first. |
|---|
| 137 | - if ( ! isset( $parent->children['default'] ) ) { |
|---|
| 138 | - $parent->children['default'] = (object) array( |
|---|
| 139 | - 'id' => "{$parent->id}-default", |
|---|
| 140 | - 'parent' => $parent->id, |
|---|
| 141 | - 'group' => true, |
|---|
| 142 | - 'children' => array(), |
|---|
| 143 | - ); |
|---|
| 144 | + if ( ! $default ) { |
|---|
| 145 | + // Use _set_node because add_node can be overloaded. |
|---|
| 146 | + // Make sure to specify default settings for all properties. |
|---|
| 147 | + $this->_set_node( array( |
|---|
| 148 | + 'id' => $default_id, |
|---|
| 149 | + 'parent' => $parent->id, |
|---|
| 150 | + 'type' => 'group', |
|---|
| 151 | + 'children' => array(), |
|---|
| 152 | + 'meta' => array( |
|---|
| 153 | + 'class' => $group_class, |
|---|
| 154 | + ), |
|---|
| 155 | + 'title' => false, |
|---|
| 156 | + 'href' => false, |
|---|
| 157 | + ) ); |
|---|
| 158 | + $default = $this->_get_node( $default_id ); |
|---|
| 159 | + $parent->children[] = $default; |
|---|
| 160 | } |
|---|
| 161 | - $parent = $parent->children['default']; |
|---|
| 162 | + $parent = $default; |
|---|
| 163 | + |
|---|
| 164 | + // Groups in groups aren't allowed. Add a special 'container' node. |
|---|
| 165 | + // The container will invisibly wrap both groups. |
|---|
| 166 | + } elseif ( $parent->type == 'group' && $node->type == 'group' ) { |
|---|
| 167 | + $container_id = $parent->id . '-container'; |
|---|
| 168 | + $container = $this->_get_node( $container_id ); |
|---|
| 169 | + |
|---|
| 170 | + // We need to create a container for this group, life is sad. |
|---|
| 171 | + if ( ! $container ) { |
|---|
| 172 | + // Use _set_node because add_node can be overloaded. |
|---|
| 173 | + // Make sure to specify default settings for all properties. |
|---|
| 174 | + $this->_set_node( array( |
|---|
| 175 | + 'id' => $container_id, |
|---|
| 176 | + 'type' => 'container', |
|---|
| 177 | + 'children' => array( $parent ), |
|---|
| 178 | + 'parent' => false, |
|---|
| 179 | + 'title' => false, |
|---|
| 180 | + 'href' => false, |
|---|
| 181 | + 'meta' => array(), |
|---|
| 182 | + ) ); |
|---|
| 183 | + |
|---|
| 184 | + $container = $this->_get_node( $container_id ); |
|---|
| 185 | + |
|---|
| 186 | + // Link the container node if a grandparent node exists. |
|---|
| 187 | + $grandparent = $this->_get_node( $parent->parent ); |
|---|
| 188 | + |
|---|
| 189 | + if ( $grandparent ) { |
|---|
| 190 | + $container->parent = $grandparent->id; |
|---|
| 191 | + |
|---|
| 192 | + $index = array_search( $parent, $grandparent->children, true ); |
|---|
| 193 | + if ( $index === false ) |
|---|
| 194 | + $grandparent->children[] = $container; |
|---|
| 195 | + else |
|---|
| 196 | + array_splice( $grandparent->children, $index, 1, array( $container ) ); |
|---|
| 197 | + } |
|---|
| 198 | + |
|---|
| 199 | + $parent->parent = $container->id; |
|---|
| 200 | + } |
|---|
| 201 | + |
|---|
| 202 | + $parent = $container; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | // Update the parent ID (it might have changed). |
|---|
| 206 | $node->parent = $parent->id; |
|---|
| 207 | |
|---|
| 208 | - if ( ! isset( $parent->children ) ) |
|---|
| 209 | - $parent->children = array(); |
|---|
| 210 | - |
|---|
| 211 | // Add the node to the tree. |
|---|
| 212 | $parent->children[] = $node; |
|---|
| 213 | } |
|---|
| 214 | + |
|---|
| 215 | + $root = $this->_get_node( 'root' ); |
|---|
| 216 | + $this->bound = true; |
|---|
| 217 | + return $root; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | - final protected function _render() { |
|---|
| 221 | + final protected function _render( $root ) { |
|---|
| 222 | global $is_IE, $is_iphone; |
|---|
| 223 | |
|---|
| 224 | // Add browser classes. |
|---|
| 225 | @@ -227,8 +328,8 @@ |
|---|
| 226 | ?> |
|---|
| 227 | <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation"> |
|---|
| 228 | <div class="quicklinks" role="menubar"> |
|---|
| 229 | - <?php foreach ( $this->get_node( 'root' )->children as $group ) { |
|---|
| 230 | - $this->_render_group( $group, 'ab-top-menu' ); |
|---|
| 231 | + <?php foreach ( $root->children as $group ) { |
|---|
| 232 | + $this->_render_group( $group ); |
|---|
| 233 | } ?> |
|---|
| 234 | </div> |
|---|
| 235 | </div> |
|---|
| 236 | @@ -236,51 +337,35 @@ |
|---|
| 237 | <?php |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | - final protected function _render_group( $node, $class = '' ) { |
|---|
| 241 | - if ( ! $node->group ) |
|---|
| 242 | + final protected function _render_container( $node ) { |
|---|
| 243 | + if ( $node->type != 'container' || empty( $node->children ) ) |
|---|
| 244 | return; |
|---|
| 245 | |
|---|
| 246 | - // Check for groups within groups. |
|---|
| 247 | - $groups = array(); |
|---|
| 248 | - foreach ( $node->children as $child ) { |
|---|
| 249 | - if ( $child->group ) { |
|---|
| 250 | - $groups[] = $child; |
|---|
| 251 | - } else { |
|---|
| 252 | - if ( ! isset( $default ) ) { |
|---|
| 253 | - // Create a default proxy item to be used in the case of nested groups. |
|---|
| 254 | - $default = (object) wp_parse_args( array( 'children' => array() ), (array) $node ); |
|---|
| 255 | - $groups[] = $default; |
|---|
| 256 | - } |
|---|
| 257 | - $default->children[] = $child; |
|---|
| 258 | + ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container" role="menu"><?php |
|---|
| 259 | + foreach ( $node->children as $group ) { |
|---|
| 260 | + $this->_render_group( $group ); |
|---|
| 261 | } |
|---|
| 262 | - } |
|---|
| 263 | + ?></div><?php |
|---|
| 264 | + } |
|---|
| 265 | |
|---|
| 266 | - $is_single_group = count( $groups ) === 1; |
|---|
| 267 | + final protected function _render_group( $node ) { |
|---|
| 268 | + if ( $node->type == 'container' ) |
|---|
| 269 | + return $this->_render_container( $node ); |
|---|
| 270 | |
|---|
| 271 | - // If we don't have any subgroups, render the group. |
|---|
| 272 | - if ( $is_single_group && ! empty( $node->children ) ) : |
|---|
| 273 | + if ( $node->type != 'group' || empty( $node->children ) ) |
|---|
| 274 | + return; |
|---|
| 275 | |
|---|
| 276 | - if ( ! empty( $node->meta['class'] ) ) |
|---|
| 277 | - $class .= ' ' . $node->meta['class']; |
|---|
| 278 | + $class = empty( $node->meta['class'] ) ? '' : $node->meta['class']; |
|---|
| 279 | |
|---|
| 280 | - ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php |
|---|
| 281 | - foreach ( $node->children as $item ) { |
|---|
| 282 | - $this->_render_item( $item ); |
|---|
| 283 | - } |
|---|
| 284 | - ?></ul><?php |
|---|
| 285 | - |
|---|
| 286 | - // Wrap the subgroups in a div and render each individual subgroup. |
|---|
| 287 | - elseif ( ! $is_single_group ) : |
|---|
| 288 | - ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php |
|---|
| 289 | - foreach ( $groups as $group ) { |
|---|
| 290 | - $this->_render_group( $group, $class ); |
|---|
| 291 | - } |
|---|
| 292 | - ?></div><?php |
|---|
| 293 | - endif; |
|---|
| 294 | + ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php |
|---|
| 295 | + foreach ( $node->children as $item ) { |
|---|
| 296 | + $this->_render_item( $item ); |
|---|
| 297 | + } |
|---|
| 298 | + ?></ul><?php |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | final protected function _render_item( $node ) { |
|---|
| 302 | - if ( $node->group ) |
|---|
| 303 | + if ( $node->type != 'item' ) |
|---|
| 304 | return; |
|---|
| 305 | |
|---|
| 306 | $is_parent = ! empty( $node->children ); |
|---|
| 307 | @@ -301,7 +386,7 @@ |
|---|
| 308 | |
|---|
| 309 | ?> |
|---|
| 310 | |
|---|
| 311 | - <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php |
|---|
| 312 | + <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php |
|---|
| 313 | if ( $has_link ): |
|---|
| 314 | ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php |
|---|
| 315 | if ( ! empty( $node->meta['onclick'] ) ) : |
|---|
| 316 | @@ -333,7 +418,7 @@ |
|---|
| 317 | if ( $is_parent ) : |
|---|
| 318 | ?><div class="ab-sub-wrapper"><?php |
|---|
| 319 | foreach ( $node->children as $group ) { |
|---|
| 320 | - $this->_render_group( $group, 'ab-submenu' ); |
|---|
| 321 | + $this->_render_group( $group ); |
|---|
| 322 | } |
|---|
| 323 | ?></div><?php |
|---|
| 324 | endif; |
|---|