Changeset 19558
- Timestamp:
- 12/06/2011 03:42:11 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-wp-admin-bar.php
r19556 r19558 2 2 class WP_Admin_Bar { 3 3 private $nodes = array(); 4 private $bound = false; 4 5 public $user; 5 6 … … 18 19 public function initialize() { 19 20 $this->user = new stdClass; 20 21 $this->add_node( array(22 'id' => 'root',23 'group' => false,24 ) );25 21 26 22 if ( is_user_logged_in() ) { … … 105 101 106 102 // If the node already exists, keep any data that isn't provided. 107 if ( $ this->get_node( $args['id'] ) )108 $defaults = get_object_vars( $ this->get_node( $args['id'] ));103 if ( $maybe_defaults = $this->get_node( $args['id'] ) ) 104 $defaults = get_object_vars( $maybe_defaults ); 109 105 110 106 // Do the same for 'meta' items. … … 115 111 116 112 $back_compat_parents = array( 117 'my-account-with-avatar' => array( 'my-account', 118 'my-blogs' => array( 'my-sites', '3.3' ),113 'my-account-with-avatar' => array( 'my-account', '3.3' ), 114 'my-blogs' => array( 'my-sites', '3.3' ), 119 115 ); 120 116 … … 138 134 */ 139 135 final public function get_node( $id ) { 136 if ( $node = $this->_get_node( $id ) ) 137 return clone $node; 138 } 139 140 final protected function _get_node( $id ) { 141 if ( $this->bound ) 142 return; 143 144 if ( empty( $id ) ) 145 $id = 'root'; 146 140 147 if ( isset( $this->nodes[ $id ] ) ) 141 148 return $this->nodes[ $id ]; 142 149 } 143 150 151 final public function get_nodes() { 152 if ( ! $nodes = $this->_get_nodes() ) 153 return; 154 155 foreach ( $nodes as &$node ) { 156 $node = clone $node; 157 } 158 return $nodes; 159 } 160 144 161 final protected function _get_nodes() { 162 if ( $this->bound ) 163 return; 164 145 165 return $this->nodes; 146 166 } … … 176 196 177 197 public function render() { 178 $ this->_bind();179 $this->_render( );198 $root = $this->_bind(); 199 $this->_render( $root ); 180 200 } 181 201 182 202 final protected function _bind() { 203 if ( $this->bound ) 204 return; 205 206 // Add the root node. 207 // Clear it first, just in case. Don't mess with The Root. 208 $this->remove_node( 'root' ); 209 $this->add_node( array( 210 'id' => 'root', 211 'group' => false, 212 ) ); 213 214 // Normalize nodes: define internal 'children' and 'type' properties. 215 foreach ( $this->_get_nodes() as $node ) { 216 $node->children = array(); 217 $node->type = ( $node->group ) ? 'group' : 'item'; 218 unset( $node->group ); 219 220 // The Root wants your orphans. No lonely items allowed. 221 if ( ! $node->parent ) 222 $node->parent = 'root'; 223 } 224 183 225 foreach ( $this->_get_nodes() as $node ) { 184 226 if ( 'root' == $node->id ) 185 227 continue; 186 228 187 // Handle root menu items 188 if ( empty( $node->parent ) ) { 189 $parent = $this->get_node( 'root' ); 190 } elseif ( ! $parent = $this->get_node( $node->parent ) ) { 191 // If the parent node isn't registered, ignore the node. 229 // Fetch the parent node. If it isn't registered, ignore the node. 230 if ( ! $parent = $this->_get_node( $node->parent ) ) { 192 231 continue; 193 232 } 194 233 195 // Ensure that our tree is of the form "item -> group -> item -> group -> ..." 196 if ( ! $parent->group && ! $node->group ) { // Both are items. 234 // Generate the group class (we distinguish between top level and other level groups). 235 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu'; 236 237 if ( $node->type == 'group' ) { 238 if ( empty( $node->meta['class'] ) ) 239 $node->meta['class'] = ''; 240 $node->meta['class'] .= ' ' . $group_class; 241 } 242 243 // Items in items aren't allowed. Wrap nested items in 'default' groups. 244 if ( $parent->type == 'item' && $node->type == 'item' ) { 245 $default_id = $parent->id . '-default'; 246 $default = $this->_get_node( $default_id ); 247 197 248 // The default group is added here to allow groups that are 198 249 // added before standard menu items to render first. 199 if ( ! isset( $parent->children['default'] ) ) { 200 $parent->children['default'] = (object) array( 201 'id' => "{$parent->id}-default", 202 'parent' => $parent->id, 203 'group' => true, 204 'children' => array(), 205 ); 250 if ( ! $default ) { 251 // Use _set_node because add_node can be overloaded. 252 // Make sure to specify default settings for all properties. 253 $this->_set_node( array( 254 'id' => $default_id, 255 'parent' => $parent->id, 256 'type' => 'group', 257 'children' => array(), 258 'meta' => array( 259 'class' => $group_class, 260 ), 261 'title' => false, 262 'href' => false, 263 ) ); 264 $default = $this->_get_node( $default_id ); 265 $parent->children[] = $default; 206 266 } 207 $parent = $parent->children['default']; 267 $parent = $default; 268 269 // Groups in groups aren't allowed. Add a special 'container' node. 270 // The container will invisibly wrap both groups. 271 } elseif ( $parent->type == 'group' && $node->type == 'group' ) { 272 $container_id = $parent->id . '-container'; 273 $container = $this->_get_node( $container_id ); 274 275 // We need to create a container for this group, life is sad. 276 if ( ! $container ) { 277 // Use _set_node because add_node can be overloaded. 278 // Make sure to specify default settings for all properties. 279 $this->_set_node( array( 280 'id' => $container_id, 281 'type' => 'container', 282 'children' => array( $parent ), 283 'parent' => false, 284 'title' => false, 285 'href' => false, 286 'meta' => array(), 287 ) ); 288 289 $container = $this->_get_node( $container_id ); 290 291 // Link the container node if a grandparent node exists. 292 $grandparent = $this->_get_node( $parent->parent ); 293 294 if ( $grandparent ) { 295 $container->parent = $grandparent->id; 296 297 $index = array_search( $parent, $grandparent->children, true ); 298 if ( $index === false ) 299 $grandparent->children[] = $container; 300 else 301 array_splice( $grandparent->children, $index, 1, array( $container ) ); 302 } 303 304 $parent->parent = $container->id; 305 } 306 307 $parent = $container; 208 308 } 209 309 … … 211 311 $node->parent = $parent->id; 212 312 213 if ( ! isset( $parent->children ) )214 $parent->children = array();215 216 313 // Add the node to the tree. 217 314 $parent->children[] = $node; 218 315 } 219 } 220 221 final protected function _render() { 316 317 $root = $this->_get_node( 'root' ); 318 $this->bound = true; 319 return $root; 320 } 321 322 final protected function _render( $root ) { 222 323 global $is_IE, $is_iphone; 223 324 … … 239 340 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation"> 240 341 <div class="quicklinks"> 241 <?php foreach ( $ this->get_node( 'root' )->children as $group ) {242 $this->_render_group( $group , 'ab-top-menu');342 <?php foreach ( $root->children as $group ) { 343 $this->_render_group( $group ); 243 344 } ?> 244 345 </div> … … 248 349 } 249 350 250 final protected function _render_group( $node, $class = '' ) { 251 if ( ! $node->group ) 252 return; 253 254 // Check for groups within groups. 255 $groups = array(); 256 foreach ( $node->children as $child ) { 257 if ( $child->group ) { 258 $groups[] = $child; 259 } else { 260 if ( ! isset( $default ) ) { 261 // Create a default proxy item to be used in the case of nested groups. 262 $default = (object) wp_parse_args( array( 'children' => array() ), (array) $node ); 263 $groups[] = $default; 264 } 265 $default->children[] = $child; 266 } 267 } 268 269 $is_single_group = count( $groups ) === 1; 270 271 // If we don't have any subgroups, render the group. 272 if ( $is_single_group && ! empty( $node->children ) ) : 273 274 if ( ! empty( $node->meta['class'] ) ) 275 $class .= ' ' . $node->meta['class']; 276 277 ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php 278 foreach ( $node->children as $item ) { 279 $this->_render_item( $item ); 280 } 281 ?></ul><?php 282 283 // Wrap the subgroups in a div and render each individual subgroup. 284 elseif ( ! $is_single_group ) : 285 ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container"><?php 286 foreach ( $groups as $group ) { 287 $this->_render_group( $group, $class ); 288 } 289 ?></div><?php 290 endif; 351 final protected function _render_container( $node ) { 352 if ( $node->type != 'container' || empty( $node->children ) ) 353 return; 354 355 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php 356 foreach ( $node->children as $group ) { 357 $this->_render_group( $group ); 358 } 359 ?></div><?php 360 } 361 362 final protected function _render_group( $node ) { 363 if ( $node->type == 'container' ) 364 return $this->_render_container( $node ); 365 366 if ( $node->type != 'group' || empty( $node->children ) ) 367 return; 368 369 $class = empty( $node->meta['class'] ) ? '' : $node->meta['class']; 370 371 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php 372 foreach ( $node->children as $item ) { 373 $this->_render_item( $item ); 374 } 375 ?></ul><?php 291 376 } 292 377 293 378 final protected function _render_item( $node ) { 294 if ( $node-> group)379 if ( $node->type != 'item' ) 295 380 return; 296 381 … … 313 398 ?> 314 399 315 <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}"); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php400 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php 316 401 if ( $has_link ): 317 402 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php … … 345 430 ?><div class="ab-sub-wrapper"><?php 346 431 foreach ( $node->children as $group ) { 347 $this->_render_group( $group , 'ab-submenu');432 $this->_render_group( $group ); 348 433 } 349 434 ?></div><?php
Note: See TracChangeset
for help on using the changeset viewer.