Changeset 19501 for trunk/wp-includes/class-wp-admin-bar.php
- Timestamp:
- 12/01/2011 12:25:04 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class-wp-admin-bar.php
r19499 r19501 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://'; 13 6 public function __get( $name ) { 7 switch ( $name ) { 8 case 'proto' : 9 return is_ssl() ? 'https://' : 'http://'; 10 break; 11 case 'menu' : 12 _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.' ); 13 return array(); // Sorry, folks. 14 break; 15 } 16 } 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() ) { … … 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 ) { … … 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) ); 83 84 if ( is_object( $args ) ) 85 $args = get_object_vars( $args ); 78 86 79 87 // Ensure we have a valid title. … … 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'] ) ); 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'] ); 101 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; 121 } 122 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; 106 135 } 107 136 108 137 /** 109 138 * Add a group to a menu node. 139 * 140 * @since 3.3.0 110 141 * 111 142 * @param array $args - The arguments for each node. … … 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 … … 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 $this->_unset_node( $id ); 160 } 161 162 final protected function _unset_node( $id ) { 123 163 unset( $this->nodes[ $id ] ); 124 164 } 125 165 126 166 public function render() { 127 global $is_IE, $is_iphone; 128 129 // Link nodes to parents. 130 foreach ( $this->nodes as $node ) { 167 $this->_bind(); 168 $this->_render(); 169 } 170 171 final protected function _bind() { 172 foreach ( $this->_get_nodes() as $node ) { 173 if ( 'root' == $node->id ) 174 continue; 131 175 132 176 // Handle root menu items 133 177 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 ] ) ) { 178 $parent = $this->get_node( 'root' ); 179 } elseif ( ! $parent = $this->get_node( $node->parent ) ) { 180 // If the parent node isn't registered, ignore the node. 138 181 continue; 139 140 } else {141 $parent = $this->nodes[ $node->parent ];142 182 } 143 144 183 145 184 // Ensure that our tree is of the form "item -> group -> item -> group -> ..." … … 161 200 $node->parent = $parent->id; 162 201 202 if ( ! isset( $parent->children ) ) 203 $parent->children = array(); 204 163 205 // Add the node to the tree. 164 206 $parent->children[] = $node; 165 207 } 208 } 209 210 final protected function _render() { 211 global $is_IE, $is_iphone; 166 212 167 213 // Add browser classes. … … 182 228 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation"> 183 229 <div class="quicklinks" role="menubar"> 184 <?php foreach ( $this-> root->children as $group ) {185 $this-> render_group( $group, 'ab-top-menu' );230 <?php foreach ( $this->get_node( 'root' )->children as $group ) { 231 $this->_render_group( $group, 'ab-top-menu' ); 186 232 } ?> 187 233 </div> … … 191 237 } 192 238 193 private functionrender_group( $node, $class = '' ) {239 final protected function _render_group( $node, $class = '' ) { 194 240 if ( ! $node->group ) 195 241 return; … … 212 258 $is_single_group = count( $groups ) === 1; 213 259 214 215 260 // If we don't have any subgroups, render the group. 216 if ( $is_single_group && ! empty( $node->children ) ) :261 if ( $is_single_group && ! empty( $node->children ) ) : 217 262 218 263 if ( ! empty( $node->meta['class'] ) ) … … 221 266 ?><ul id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $class ); ?>" role="menu"><?php 222 267 foreach ( $node->children as $item ) { 223 $this-> render_item( $item );268 $this->_render_item( $item ); 224 269 } 225 270 ?></ul><?php 226 271 227 272 // Wrap the subgroups in a div and render each individual subgroup. 228 elseif ( ! $is_single_group ) :273 elseif ( ! $is_single_group ) : 229 274 ?><div id="<?php echo esc_attr( "wp-admin-bar-{$node->id}-container" ); ?>" class="ab-group-container" role="menu"><?php 230 275 foreach ( $groups as $group ) { 231 $this-> render_group( $group, $class );276 $this->_render_group( $group, $class ); 232 277 } 233 278 ?></div><?php … … 235 280 } 236 281 237 private functionrender_item( $node ) {282 final protected function _render_item( $node ) { 238 283 if ( $node->group ) 239 284 return; 240 285 241 $is_parent = (bool) $node->children; 242 $has_link = (bool) $node->href; 243 $tabindex = isset($node->meta['tabindex']) ? (int) $node->meta['tabindex'] : 10; 286 $is_parent = ! empty( $node->children ); 287 $has_link = ! empty( $node->href ); 288 289 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10; 244 290 245 291 $menuclass = ''; … … 279 325 echo $node->title; 280 326 281 if ( $has_link ) :327 if ( $has_link ) : 282 328 ?></a><?php 283 329 else: … … 288 334 ?><div class="ab-sub-wrapper"><?php 289 335 foreach ( $node->children as $group ) { 290 $this-> render_group( $group, 'ab-submenu' );336 $this->_render_group( $group, 'ab-submenu' ); 291 337 } 292 338 ?></div><?php … … 300 346 } 301 347 302 function recursive_render( $node ) { 303 $this->render_item( $node ); 304 } 305 306 function add_menus() { 348 public function recursive_render( $id, $node ) { 349 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' ); 350 $this->_render_item( $node ); 351 } 352 353 public function add_menus() { 307 354 // User related, aligned right. 308 355 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
Note: See TracChangeset
for help on using the changeset viewer.