3 * The WordPress Toolbar
11 private $nodes = array();
12 private $bound = false;
17 * @return string|array|void
19 public function __get( $name ) {
22 return is_ssl() ? 'https://' : 'http://';
25 _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.' );
26 return array(); // Sorry, folks.
33 public function initialize() {
34 $this->user = new stdClass;
36 if ( is_user_logged_in() ) {
37 /* Populate settings we need for the menu based on the current user. */
38 $this->user->blogs = get_blogs_of_user( get_current_user_id() );
39 if ( is_multisite() ) {
40 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
41 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
42 $this->user->account_domain = $this->user->domain;
44 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
45 $this->user->domain = trailingslashit( home_url() );
46 $this->user->account_domain = $this->user->domain;
50 add_action( 'wp_head', 'wp_admin_bar_header' );
52 add_action( 'admin_head', 'wp_admin_bar_header' );
54 if ( current_theme_supports( 'admin-bar' ) ) {
56 * To remove the default padding styles from WordPress for the Toolbar, use the following code:
57 * add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
59 $admin_bar_args = get_theme_support( 'admin-bar' );
60 $header_callback = $admin_bar_args[0]['callback'];
63 if ( empty($header_callback) )
64 $header_callback = '_admin_bar_bump_cb';
66 add_action('wp_head', $header_callback);
68 wp_enqueue_script( 'admin-bar' );
69 wp_enqueue_style( 'admin-bar' );
72 * Fires after WP_Admin_Bar is initialized.
76 do_action( 'admin_bar_init' );
82 public function add_menu( $node ) {
83 $this->add_node( $node );
89 public function remove_menu( $id ) {
90 $this->remove_node( $id );
94 * Add a node to the menu.
96 * @param array $args {
97 * Arguments for adding a node.
99 * @type string $id ID of the item.
100 * @type string $title Title of the node.
101 * @type string $parent Optional. ID of the parent node.
102 * @type string $href Optional. Link for the item.
103 * @type bool $group Optional. Whether or not the node is a group. Default false.
104 * @type array $meta Meta data including the following keys: 'html', 'class', 'rel',
105 * 'onclick', 'target', 'title', 'tabindex'. Default empty.
108 public function add_node( $args ) {
109 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
110 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
111 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
113 if ( is_object( $args ) )
114 $args = get_object_vars( $args );
116 // Ensure we have a valid title.
117 if ( empty( $args['id'] ) ) {
118 if ( empty( $args['title'] ) )
121 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
122 // Deprecated: Generate an ID from the title.
123 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
135 // If the node already exists, keep any data that isn't provided.
136 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
137 $defaults = get_object_vars( $maybe_defaults );
139 // Do the same for 'meta' items.
140 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
141 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
143 $args = wp_parse_args( $args, $defaults );
145 $back_compat_parents = array(
146 'my-account-with-avatar' => array( 'my-account', '3.3' ),
147 'my-blogs' => array( 'my-sites', '3.3' ),
150 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
151 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
152 _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'] ) );
153 $args['parent'] = $new_parent;
156 $this->_set_node( $args );
162 final protected function _set_node( $args ) {
163 $this->nodes[ $args['id'] ] = (object) $args;
170 * @return object Node.
172 final public function get_node( $id ) {
173 if ( $node = $this->_get_node( $id ) )
179 * @return object|void
181 final protected function _get_node( $id ) {
188 if ( isset( $this->nodes[ $id ] ) )
189 return $this->nodes[ $id ];
195 final public function get_nodes() {
196 if ( ! $nodes = $this->_get_nodes() )
199 foreach ( $nodes as &$node ) {
208 final protected function _get_nodes() {
216 * Add a group to a menu node.
220 * @param array $args {
221 * Array of arguments for adding a group.
223 * @type string $id ID of the item.
224 * @type string $parent Optional. ID of the parent node. Default 'root'.
225 * @type array $meta Meta data for the group including the following keys:
226 * 'class', 'onclick', 'target', and 'title'.
229 final public function add_group( $args ) {
230 $args['group'] = true;
232 $this->add_node( $args );
238 * @param string $id The ID of the item.
240 public function remove_node( $id ) {
241 $this->_unset_node( $id );
247 final protected function _unset_node( $id ) {
248 unset( $this->nodes[ $id ] );
254 public function render() {
255 $root = $this->_bind();
257 $this->_render( $root );
261 * @return object|void
263 final protected function _bind() {
267 // Add the root node.
268 // Clear it first, just in case. Don't mess with The Root.
269 $this->remove_node( 'root' );
270 $this->add_node( array(
275 // Normalize nodes: define internal 'children' and 'type' properties.
276 foreach ( $this->_get_nodes() as $node ) {
277 $node->children = array();
278 $node->type = ( $node->group ) ? 'group' : 'item';
279 unset( $node->group );
281 // The Root wants your orphans. No lonely items allowed.
282 if ( ! $node->parent )
283 $node->parent = 'root';
286 foreach ( $this->_get_nodes() as $node ) {
287 if ( 'root' == $node->id )
290 // Fetch the parent node. If it isn't registered, ignore the node.
291 if ( ! $parent = $this->_get_node( $node->parent ) ) {
295 // Generate the group class (we distinguish between top level and other level groups).
296 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
298 if ( $node->type == 'group' ) {
299 if ( empty( $node->meta['class'] ) )
300 $node->meta['class'] = $group_class;
302 $node->meta['class'] .= ' ' . $group_class;
305 // Items in items aren't allowed. Wrap nested items in 'default' groups.
306 if ( $parent->type == 'item' && $node->type == 'item' ) {
307 $default_id = $parent->id . '-default';
308 $default = $this->_get_node( $default_id );
310 // The default group is added here to allow groups that are
311 // added before standard menu items to render first.
313 // Use _set_node because add_node can be overloaded.
314 // Make sure to specify default settings for all properties.
315 $this->_set_node( array(
317 'parent' => $parent->id,
319 'children' => array(),
321 'class' => $group_class,
326 $default = $this->_get_node( $default_id );
327 $parent->children[] = $default;
331 // Groups in groups aren't allowed. Add a special 'container' node.
332 // The container will invisibly wrap both groups.
333 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
334 $container_id = $parent->id . '-container';
335 $container = $this->_get_node( $container_id );
337 // We need to create a container for this group, life is sad.
338 if ( ! $container ) {
339 // Use _set_node because add_node can be overloaded.
340 // Make sure to specify default settings for all properties.
341 $this->_set_node( array(
342 'id' => $container_id,
343 'type' => 'container',
344 'children' => array( $parent ),
351 $container = $this->_get_node( $container_id );
353 // Link the container node if a grandparent node exists.
354 $grandparent = $this->_get_node( $parent->parent );
356 if ( $grandparent ) {
357 $container->parent = $grandparent->id;
359 $index = array_search( $parent, $grandparent->children, true );
360 if ( $index === false )
361 $grandparent->children[] = $container;
363 array_splice( $grandparent->children, $index, 1, array( $container ) );
366 $parent->parent = $container->id;
369 $parent = $container;
372 // Update the parent ID (it might have changed).
373 $node->parent = $parent->id;
375 // Add the node to the tree.
376 $parent->children[] = $node;
379 $root = $this->_get_node( 'root' );
386 * @global bool $is_IE
387 * @param object $root
389 final protected function _render( $root ) {
392 // Add browser classes.
393 // We have to do this here since admin bar shows on the front end.
394 $class = 'nojq nojs';
396 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
398 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
400 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
402 } elseif ( wp_is_mobile() ) {
407 <div id="wpadminbar" class="<?php echo $class; ?>">
408 <?php if ( ! is_admin() ) { ?>
409 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a>
411 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>" tabindex="0">
412 <?php foreach ( $root->children as $group ) {
413 $this->_render_group( $group );
416 <?php if ( is_user_logged_in() ) : ?>
417 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
425 * @param object $node
427 final protected function _render_container( $node ) {
428 if ( $node->type != 'container' || empty( $node->children ) )
431 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
432 foreach ( $node->children as $group ) {
433 $this->_render_group( $group );
439 * @param object $node
441 final protected function _render_group( $node ) {
442 if ( $node->type == 'container' ) {
443 $this->_render_container( $node );
446 if ( $node->type != 'group' || empty( $node->children ) )
449 if ( ! empty( $node->meta['class'] ) )
450 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
454 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
455 foreach ( $node->children as $item ) {
456 $this->_render_item( $item );
462 * @param object $node
464 final protected function _render_item( $node ) {
465 if ( $node->type != 'item' )
468 $is_parent = ! empty( $node->children );
469 $has_link = ! empty( $node->href );
471 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
472 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
477 $menuclass = 'menupop ';
478 $aria_attributes .= ' aria-haspopup="true"';
481 if ( ! empty( $node->meta['class'] ) )
482 $menuclass .= $node->meta['class'];
485 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
489 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
491 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
492 if ( ! empty( $node->meta['onclick'] ) ) :
493 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
495 if ( ! empty( $node->meta['target'] ) ) :
496 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
498 if ( ! empty( $node->meta['title'] ) ) :
499 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
501 if ( ! empty( $node->meta['rel'] ) ) :
502 ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php
506 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
507 if ( ! empty( $node->meta['title'] ) ) :
508 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
522 ?><div class="ab-sub-wrapper"><?php
523 foreach ( $node->children as $group ) {
524 $this->_render_group( $group );
529 if ( ! empty( $node->meta['html'] ) )
530 echo $node->meta['html'];
537 * @param string $id Unused.
538 * @param object $node
540 public function recursive_render( $id, $node ) {
541 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
542 $this->_render_item( $node );
548 public function add_menus() {
549 // User related, aligned right.
550 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
551 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
552 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
555 add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
556 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
557 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
558 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
559 add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
560 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
563 if ( ! is_network_admin() && ! is_user_admin() ) {
564 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
565 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
567 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
569 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
572 * Fires after menus are added to the menu bar.
576 do_action( 'add_admin_bar_menus' );