3 * The WordPress Toolbar
11 private $nodes = array();
12 private $bound = false;
15 public function __get( $name ) {
18 return is_ssl() ? 'https://' : 'http://';
21 _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.' );
22 return array(); // Sorry, folks.
27 public function initialize() {
28 $this->user = new stdClass;
30 if ( is_user_logged_in() ) {
31 /* Populate settings we need for the menu based on the current user. */
32 $this->user->blogs = get_blogs_of_user( get_current_user_id() );
33 if ( is_multisite() ) {
34 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
35 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
36 $this->user->account_domain = $this->user->domain;
38 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
39 $this->user->domain = trailingslashit( home_url() );
40 $this->user->account_domain = $this->user->domain;
44 add_action( 'wp_head', 'wp_admin_bar_header' );
46 add_action( 'admin_head', 'wp_admin_bar_header' );
48 if ( current_theme_supports( 'admin-bar' ) ) {
50 * To remove the default padding styles from WordPress for the Toolbar, use the following code:
51 * add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
53 $admin_bar_args = get_theme_support( 'admin-bar' );
54 $header_callback = $admin_bar_args[0]['callback'];
57 if ( empty($header_callback) )
58 $header_callback = '_admin_bar_bump_cb';
60 add_action('wp_head', $header_callback);
62 wp_enqueue_script( 'admin-bar' );
63 wp_enqueue_style( 'admin-bar' );
66 * Fires after WP_Admin_Bar is initialized.
70 do_action( 'admin_bar_init' );
73 public function add_menu( $node ) {
74 $this->add_node( $node );
77 public function remove_menu( $id ) {
78 $this->remove_node( $id );
82 * Add a node to the menu.
84 * @param array $args - The arguments for each node.
85 * - id - string - The ID of the item.
86 * - title - string - The title of the node.
87 * - parent - string - The ID of the parent node. Optional.
88 * - href - string - The link for the item. Optional.
89 * - group - boolean - If the node is a group. Optional. Default false.
90 * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex.
92 public function add_node( $args ) {
93 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
94 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
95 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
97 if ( is_object( $args ) )
98 $args = get_object_vars( $args );
100 // Ensure we have a valid title.
101 if ( empty( $args['id'] ) ) {
102 if ( empty( $args['title'] ) )
105 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
106 // Deprecated: Generate an ID from the title.
107 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
119 // If the node already exists, keep any data that isn't provided.
120 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
121 $defaults = get_object_vars( $maybe_defaults );
123 // Do the same for 'meta' items.
124 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
125 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
127 $args = wp_parse_args( $args, $defaults );
129 $back_compat_parents = array(
130 'my-account-with-avatar' => array( 'my-account', '3.3' ),
131 'my-blogs' => array( 'my-sites', '3.3' ),
134 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
135 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
136 _deprecated_argument( __METHOD__, $version, sprintf( 'Use <code>%s</code> as the parent for the <code>%s</code> admin bar node instead of <code>%s</code>.', $new_parent, $args['id'], $args['parent'] ) );
137 $args['parent'] = $new_parent;
140 $this->_set_node( $args );
143 final protected function _set_node( $args ) {
144 $this->nodes[ $args['id'] ] = (object) $args;
150 * @return object Node.
152 final public function get_node( $id ) {
153 if ( $node = $this->_get_node( $id ) )
157 final protected function _get_node( $id ) {
164 if ( isset( $this->nodes[ $id ] ) )
165 return $this->nodes[ $id ];
168 final public function get_nodes() {
169 if ( ! $nodes = $this->_get_nodes() )
172 foreach ( $nodes as &$node ) {
178 final protected function _get_nodes() {
186 * Add a group to a menu node.
190 * @param array $args - The arguments for each node.
191 * - id - string - The ID of the item.
192 * - parent - string - The ID of the parent node. Optional. Default root.
193 * - meta - array - Meta data including the following keys: class, onclick, target, title.
195 final public function add_group( $args ) {
196 $args['group'] = true;
198 $this->add_node( $args );
204 * @param string The ID of the item.
206 public function remove_node( $id ) {
207 $this->_unset_node( $id );
210 final protected function _unset_node( $id ) {
211 unset( $this->nodes[ $id ] );
214 public function render() {
215 $root = $this->_bind();
217 $this->_render( $root );
220 final protected function _bind() {
224 // Add the root node.
225 // Clear it first, just in case. Don't mess with The Root.
226 $this->remove_node( 'root' );
227 $this->add_node( array(
232 // Normalize nodes: define internal 'children' and 'type' properties.
233 foreach ( $this->_get_nodes() as $node ) {
234 $node->children = array();
235 $node->type = ( $node->group ) ? 'group' : 'item';
236 unset( $node->group );
238 // The Root wants your orphans. No lonely items allowed.
239 if ( ! $node->parent )
240 $node->parent = 'root';
243 foreach ( $this->_get_nodes() as $node ) {
244 if ( 'root' == $node->id )
247 // Fetch the parent node. If it isn't registered, ignore the node.
248 if ( ! $parent = $this->_get_node( $node->parent ) ) {
252 // Generate the group class (we distinguish between top level and other level groups).
253 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
255 if ( $node->type == 'group' ) {
256 if ( empty( $node->meta['class'] ) )
257 $node->meta['class'] = $group_class;
259 $node->meta['class'] .= ' ' . $group_class;
262 // Items in items aren't allowed. Wrap nested items in 'default' groups.
263 if ( $parent->type == 'item' && $node->type == 'item' ) {
264 $default_id = $parent->id . '-default';
265 $default = $this->_get_node( $default_id );
267 // The default group is added here to allow groups that are
268 // added before standard menu items to render first.
270 // Use _set_node because add_node can be overloaded.
271 // Make sure to specify default settings for all properties.
272 $this->_set_node( array(
274 'parent' => $parent->id,
276 'children' => array(),
278 'class' => $group_class,
283 $default = $this->_get_node( $default_id );
284 $parent->children[] = $default;
288 // Groups in groups aren't allowed. Add a special 'container' node.
289 // The container will invisibly wrap both groups.
290 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
291 $container_id = $parent->id . '-container';
292 $container = $this->_get_node( $container_id );
294 // We need to create a container for this group, life is sad.
295 if ( ! $container ) {
296 // Use _set_node because add_node can be overloaded.
297 // Make sure to specify default settings for all properties.
298 $this->_set_node( array(
299 'id' => $container_id,
300 'type' => 'container',
301 'children' => array( $parent ),
308 $container = $this->_get_node( $container_id );
310 // Link the container node if a grandparent node exists.
311 $grandparent = $this->_get_node( $parent->parent );
313 if ( $grandparent ) {
314 $container->parent = $grandparent->id;
316 $index = array_search( $parent, $grandparent->children, true );
317 if ( $index === false )
318 $grandparent->children[] = $container;
320 array_splice( $grandparent->children, $index, 1, array( $container ) );
323 $parent->parent = $container->id;
326 $parent = $container;
329 // Update the parent ID (it might have changed).
330 $node->parent = $parent->id;
332 // Add the node to the tree.
333 $parent->children[] = $node;
336 $root = $this->_get_node( 'root' );
341 final protected function _render( $root ) {
344 // Add browser classes.
345 // We have to do this here since admin bar shows on the front end.
346 $class = 'nojq nojs';
348 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
350 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
352 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
354 } elseif ( wp_is_mobile() ) {
359 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
360 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
361 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
362 <?php foreach ( $root->children as $group ) {
363 $this->_render_group( $group );
366 <?php if ( is_user_logged_in() ) : ?>
367 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
374 final protected function _render_container( $node ) {
375 if ( $node->type != 'container' || empty( $node->children ) )
378 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
379 foreach ( $node->children as $group ) {
380 $this->_render_group( $group );
385 final protected function _render_group( $node ) {
386 if ( $node->type == 'container' )
387 return $this->_render_container( $node );
389 if ( $node->type != 'group' || empty( $node->children ) )
392 if ( ! empty( $node->meta['class'] ) )
393 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
397 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
398 foreach ( $node->children as $item ) {
399 $this->_render_item( $item );
404 final protected function _render_item( $node ) {
405 if ( $node->type != 'item' )
408 $is_parent = ! empty( $node->children );
409 $has_link = ! empty( $node->href );
411 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
412 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
417 $menuclass = 'menupop ';
418 $aria_attributes .= ' aria-haspopup="true"';
421 if ( ! empty( $node->meta['class'] ) )
422 $menuclass .= $node->meta['class'];
425 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
429 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
431 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
432 if ( ! empty( $node->meta['onclick'] ) ) :
433 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
435 if ( ! empty( $node->meta['target'] ) ) :
436 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
438 if ( ! empty( $node->meta['title'] ) ) :
439 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
443 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
444 if ( ! empty( $node->meta['title'] ) ) :
445 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
459 ?><div class="ab-sub-wrapper"><?php
460 foreach ( $node->children as $group ) {
461 $this->_render_group( $group );
466 if ( ! empty( $node->meta['html'] ) )
467 echo $node->meta['html'];
473 public function recursive_render( $id, $node ) {
474 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
475 $this->_render_item( $node );
478 public function add_menus() {
479 // User related, aligned right.
480 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
481 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
482 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
485 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
486 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
487 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
488 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
491 if ( ! is_network_admin() && ! is_user_admin() ) {
492 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
493 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
495 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
497 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
500 * Fires after menus are added to the menu bar.
504 do_action( 'add_admin_bar_menus' );