Ticket #19371: 19371.2.diff
File 19371.2.diff, 8.1 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 6 protected $_bound = false; 7 8 final public function __get( $name ) { 9 switch ( $name ) { 10 case 'proto' : 11 return is_ssl() ? 'https://' : 'http://'; 12 break; 13 case 'menu' : 14 if ( ! $this->_bound ) 15 $this->_bind(); 16 return $this->get_nodes(); 17 break; 18 } 19 } 20 9 21 function initialize() { 10 /* Set the protocol used throughout this code */ 11 if ( is_ssl() ) 12 $this->proto = 'https://'; 22 $this->user = new stdClass; 13 23 14 $this->user = new stdClass; 15 $this->root = (object) array( 24 $this->add_node( array( 16 25 'id' => 'root', 17 26 'group' => false, 18 'children' => array(), 19 ); 27 ) ); 20 28 21 29 if ( is_user_logged_in() ) { 22 30 /* Populate settings we need for the menu based on the current user. */ … … 69 77 * - parent - string - The ID of the parent node. Optional. 70 78 * - href - string - The link for the item. Optional. 71 79 * - 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 .80 * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex. 73 81 */ 74 82 public function add_node( $args ) { 75 83 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args ) 76 84 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) ) 77 85 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) ); 78 86 87 if ( is_object( $args ) ) 88 $args = get_object_vars( $args ); 89 79 90 // Ensure we have a valid title. 80 91 if ( empty( $args['id'] ) ) { 81 92 if ( empty( $args['title'] ) ) … … 96 107 ); 97 108 98 109 // 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'] ];110 if ( $this->get_node( $args['id'] ) ) 111 $defaults = get_object_vars( $this->get_node( $args['id'] ) ); 101 112 113 // Do the same for 'meta' items. 114 if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) ) 115 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] ); 116 102 117 $args = wp_parse_args( $args, $defaults ); 103 $args['children'] = array();104 118 119 $this->_add_node( $args ); 120 } 121 122 final protected function _add_node( $args ) { 105 123 $this->nodes[ $args['id'] ] = (object) $args; 106 124 } 107 125 108 126 /** 127 * Gets a node. 128 * 129 * @return object Node. 130 */ 131 final public function get_node( $id ) { 132 if ( isset( $this->nodes[ $id ] ) ) 133 return $this->nodes[ $id ]; 134 } 135 136 final protected function get_nodes() { 137 return $this->nodes; 138 } 139 140 /** 109 141 * Add a group to a menu node. 110 142 * 143 * @since 3.3.0 144 * 111 145 * @param array $args - The arguments for each node. 112 146 * - id - string - The ID of the item. 113 147 * - parent - string - The ID of the parent node. Optional. Default root. … … 119 153 $this->add_node( $args ); 120 154 } 121 155 156 /** 157 * Remove a node. 158 * 159 * @since 3.3.0 160 * @return object The removed node. 161 */ 122 162 public function remove_node( $id ) { 163 $node = $this->get_node( $id ); 123 164 unset( $this->nodes[ $id ] ); 165 return $node; 124 166 } 125 167 126 public function render() { 127 global $is_IE, $is_iphone; 168 final public function render() { 169 $this->_bind(); 170 $this->_render(); 171 } 128 172 129 // Link nodes to parents. 130 foreach ( $this->nodes as $node ) { 173 protected function _bind() { 174 if ( $this->_bound ) 175 return; 131 176 177 $this->_bound = true; 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; ?>"> 183 236 <div class="quicklinks"> 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 private function render_group( $node, $class = '' ) {246 private function _render_group( $node, $class = '' ) { 194 247 if ( ! $node->group ) 195 248 return; 196 249 250 $this->render_group( $node, $class ); 251 } 252 253 protected function render_group( $node, $class = '' ) { 197 254 // Check for groups within groups. 198 255 $groups = array(); 199 256 foreach ( $node->children as $child ) { … … 211 268 212 269 $is_single_group = count( $groups ) === 1; 213 270 214 215 271 // If we don't have any subgroups, render the group. 216 if ( $is_single_group && ! empty( $node->children ) ) :272 if ( $is_single_group && ! empty( $node->children ) ) : 217 273 218 274 if ( ! empty( $node->meta['class'] ) ) 219 275 $class .= ' ' . $node->meta['class']; 220 276 221 277 ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php 222 278 foreach ( $node->children as $item ) { 223 $this-> render_item( $item );279 $this->_render_item( $item ); 224 280 } 225 281 ?></ul><?php 226 282 227 283 // Wrap the subgroups in a div and render each individual subgroup. 228 elseif ( ! $is_single_group ) :284 elseif ( ! $is_single_group ) : 229 285 ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container"><?php 230 286 foreach ( $groups as $group ) { 231 $this-> render_group( $group, $class );287 $this->_render_group( $group, $class ); 232 288 } 233 289 ?></div><?php 234 290 endif; 235 291 } 236 292 237 private function render_item( $node ) {293 private function _render_item( $node ) { 238 294 if ( $node->group ) 239 295 return; 240 296 241 $ is_parent = (bool) $node->children;242 $has_link = (bool) $node->href;297 $this->render_item( $node ); 298 } 243 299 300 protected function render_item( $node ) { 301 $is_parent = ! empty( $node->children ); 302 $has_link = ! empty( $node->href ); 303 244 304 $menuclass = $is_parent ? 'menupop' : ''; 245 305 if ( ! empty( $node->meta['class'] ) ) 246 306 $menuclass .= ' ' . $node->meta['class']; 247 307 248 $tabindex = ! empty($node->meta['tabindex']) ? $node->meta['tabindex'] : 10;308 $tabindex = ! empty( $node->meta['tabindex'] ) ? $node->meta['tabindex'] : 10; 249 309 ?> 250 310 251 311 <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php … … 276 336 if ( $is_parent ) : 277 337 ?><div class="ab-sub-wrapper"><?php 278 338 foreach ( $node->children as $group ) { 279 $this-> render_group( $group, 'ab-submenu' );339 $this->_render_group( $group, 'ab-submenu' ); 280 340 } 281 341 ?></div><?php 282 342 endif; … … 288 348 </li><?php 289 349 } 290 350 291 function recursive_render( $node ) { 292 $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 ); 293 354 } 294 355 295 function add_menus() {356 public function add_menus() { 296 357 // User related, aligned right. 297 358 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 ); 298 359