Ticket #19371: 19371.4.diff
File 19371.4.diff, 8.2 KB (added by , 13 years ago) |
---|
-
wp-includes/class-wp-admin-bar.php
1 1 <?php 2 2 class WP_Admin_Bar { 3 3 private $nodes = array(); 4 private $root = array();5 6 public $proto = 'http://';7 4 public $user; 8 5 9 function initialize() { 10 /* Set the protocol used throughout this code */ 11 if ( is_ssl() ) 12 $this->proto = 'https://'; 6 public function __get( $name ) { 7 switch ( $name ) { 8 case 'proto' : 9 return is_ssl() ? 'https://' : 'http://'; 10 break; 11 case 'menu' : 12 $this->_bind(); 13 return $this->get_nodes(); 14 break; 15 } 16 } 13 17 18 public function initialize() { 14 19 $this->user = new stdClass; 15 $this->root = (object) array( 20 21 $this->add_node( array( 16 22 'id' => 'root', 17 23 'group' => false, 18 'children' => array(), 19 ); 24 ) ); 20 25 21 26 if ( is_user_logged_in() ) { 22 27 /* Populate settings we need for the menu based on the current user. */ … … 69 74 * - parent - string - The ID of the parent node. Optional. 70 75 * - href - string - The link for the item. Optional. 71 76 * - group - boolean - If the node is a group. Optional. Default false. 72 * - meta - array - Meta data including the following keys: html, class, onclick, target, title .77 * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex. 73 78 */ 74 79 public function add_node( $args ) { 75 80 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args ) 76 81 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) ) 77 82 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) ); 78 83 84 if ( is_object( $args ) ) 85 $args = get_object_vars( $args ); 86 79 87 // Ensure we have a valid title. 80 88 if ( empty( $args['id'] ) ) { 81 89 if ( empty( $args['title'] ) ) … … 96 104 ); 97 105 98 106 // If the node already exists, keep any data that isn't provided. 99 if ( isset( $this->nodes[ $args['id']] ) )100 $defaults = (array) $this->nodes[ $args['id'] ];107 if ( $this->get_node( $args['id'] ) ) 108 $defaults = get_object_vars( $this->get_node( $args['id'] ) ); 101 109 110 // Do the same for 'meta' items. 111 if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) ) 112 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] ); 113 102 114 $args = wp_parse_args( $args, $defaults ); 103 $args['children'] = array();104 115 116 $this->_set_node( $args ); 117 } 118 119 final protected function _set_node( $args ) { 105 120 $this->nodes[ $args['id'] ] = (object) $args; 106 121 } 107 122 108 123 /** 124 * Gets a node. 125 * 126 * @return object Node. 127 */ 128 final public function get_node( $id ) { 129 if ( isset( $this->nodes[ $id ] ) ) 130 return $this->nodes[ $id ]; 131 } 132 133 final protected function get_nodes() { 134 return $this->nodes; 135 } 136 137 /** 109 138 * Add a group to a menu node. 110 139 * 140 * @since 3.3.0 141 * 111 142 * @param array $args - The arguments for each node. 112 143 * - id - string - The ID of the item. 113 144 * - parent - string - The ID of the parent node. Optional. Default root. 114 145 * - meta - array - Meta data including the following keys: class, onclick, target, title. 115 146 */ 116 public function add_group( $args ) {147 final public function add_group( $args ) { 117 148 $args['group'] = true; 118 149 119 150 $this->add_node( $args ); 120 151 } 121 152 153 /** 154 * Remove a node. 155 * 156 * @return object The removed node. 157 */ 122 158 public function remove_node( $id ) { 159 $node = $this->get_node( $id ); 160 $this->_unset_node( $id ); 161 return $node; 162 } 163 164 final protected function _unset_node( $id ) { 123 165 unset( $this->nodes[ $id ] ); 124 166 } 125 167 126 168 public function render() { 127 global $is_IE, $is_iphone; 169 $this->_bind(); 170 $this->_render(); 171 } 128 172 129 // Link nodes to parents. 130 foreach ( $this->nodes as $node ) { 173 final protected function _bind() { 174 static $bound = false; 175 if ( $bound ) 176 return; 177 $bound = true; 131 178 179 foreach ( $this->get_nodes() as $node ) { 180 if ( 'root' == $node->id ) 181 continue; 182 132 183 // Handle root menu items 133 184 if ( empty( $node->parent ) ) { 134 $parent = $this->root; 135 136 // If the parent node isn't registered, ignore the node. 137 } elseif ( ! isset( $this->nodes[ $node->parent ] ) ) { 185 $parent = $this->get_node( 'root' ); 186 } elseif ( ! $parent = $this->get_node( $node->parent ) ) { 187 // If the parent node isn't registered, ignore the node. 138 188 continue; 139 140 } else {141 $parent = $this->nodes[ $node->parent ];142 189 } 143 190 144 145 191 // Ensure that our tree is of the form "item -> group -> item -> group -> ..." 146 192 if ( ! $parent->group && ! $node->group ) { // Both are items. 147 193 // The default group is added here to allow groups that are … … 160 206 // Update the parent ID (it might have changed). 161 207 $node->parent = $parent->id; 162 208 209 if ( ! isset( $parent->children ) ) 210 $parent->children = array(); 211 163 212 // Add the node to the tree. 164 213 $parent->children[] = $node; 165 214 } 215 } 166 216 217 protected function _render() { 218 global $is_IE, $is_iphone; 219 167 220 // Add browser classes. 168 221 // We have to do this here since admin bar shows on the front end. 169 222 $class = 'nojq nojs'; … … 181 234 ?> 182 235 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation"> 183 236 <div class="quicklinks" role="menubar"> 184 <?php foreach ( $this-> root->children as $group ) {185 $this-> render_group( $group, 'ab-top-menu' );237 <?php foreach ( $this->get_node( 'root' )->children as $group ) { 238 $this->_render_group( $group, 'ab-top-menu' ); 186 239 } ?> 187 240 </div> 188 241 </div> … … 190 243 <?php 191 244 } 192 245 193 pr ivate functionrender_group( $node, $class = '' ) {246 protected function _render_group( $node, $class = '' ) { 194 247 if ( ! $node->group ) 195 248 return; 196 249 … … 211 264 212 265 $is_single_group = count( $groups ) === 1; 213 266 214 215 267 // If we don't have any subgroups, render the group. 216 if ( $is_single_group && ! empty( $node->children ) ) :268 if ( $is_single_group && ! empty( $node->children ) ) : 217 269 218 270 if ( ! empty( $node->meta['class'] ) ) 219 271 $class .= ' ' . $node->meta['class']; 220 272 221 273 ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php 222 274 foreach ( $node->children as $item ) { 223 $this-> render_item( $item );275 $this->_render_item( $item ); 224 276 } 225 277 ?></ul><?php 226 278 227 279 // Wrap the subgroups in a div and render each individual subgroup. 228 elseif ( ! $is_single_group ) :280 elseif ( ! $is_single_group ) : 229 281 ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php 230 282 foreach ( $groups as $group ) { 231 $this-> render_group( $group, $class );283 $this->_render_group( $group, $class ); 232 284 } 233 285 ?></div><?php 234 286 endif; 235 287 } 236 288 237 pr ivate functionrender_item( $node ) {289 protected function _render_item( $node ) { 238 290 if ( $node->group ) 239 291 return; 240 292 241 $is_parent = (bool) $node->children; 242 $has_link = (bool) $node->href; 243 $tabindex = isset($node->meta['tabindex']) ? (int) $node->meta['tabindex'] : 10; 293 $is_parent = ! empty( $node->children ); 294 $has_link = ! empty( $node->href ); 244 295 296 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10; 297 245 298 $menuclass = ''; 246 299 $aria_attributes = 'tabindex="' . $tabindex . '" role="menuitem"'; 247 300 … … 269 322 endif; 270 323 ?>><?php 271 324 else: 272 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; ?> ><?php325 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes; ?>"><?php 273 326 endif; 274 327 275 328 echo $node->title; … … 283 336 if ( $is_parent ) : 284 337 ?><div class="ab-sub-wrapper"><?php 285 338 foreach ( $node->children as $group ) { 286 $this-> render_group( $group, 'ab-submenu' );339 $this->_render_group( $group, 'ab-submenu' ); 287 340 } 288 341 ?></div><?php 289 342 endif; … … 295 348 </li><?php 296 349 } 297 350 298 function recursive_render( $node ) { 299 $this->render_item( $node ); 351 public function recursive_render( $id, $node ) { 352 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_Bar::_render_item()' ); 353 $this->_render_item( $node ); 300 354 } 301 355 302 function add_menus() {356 public function add_menus() { 303 357 // User related, aligned right. 304 358 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 ); 305 359