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.
26 public function initialize() {
27 $this->user = new stdClass;
29 if ( is_user_logged_in() ) {
30 /* Populate settings we need for the menu based on the current user. */
31 $this->user->blogs = get_blogs_of_user( get_current_user_id() );
32 if ( is_multisite() ) {
33 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
34 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
35 $this->user->account_domain = $this->user->domain;
37 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
38 $this->user->domain = trailingslashit( home_url() );
39 $this->user->account_domain = $this->user->domain;
43 add_action( 'wp_head', 'wp_admin_bar_header' );
45 add_action( 'admin_head', 'wp_admin_bar_header' );
47 if ( current_theme_supports( 'admin-bar' ) ) {
49 * To remove the default padding styles from WordPress for the Toolbar, use the following code:
50 * add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
52 $admin_bar_args = get_theme_support( 'admin-bar' );
53 $header_callback = $admin_bar_args[0]['callback'];
56 if ( empty($header_callback) )
57 $header_callback = '_admin_bar_bump_cb';
59 add_action('wp_head', $header_callback);
61 wp_enqueue_script( 'admin-bar' );
62 wp_enqueue_style( 'admin-bar' );
65 * Fires after WP_Admin_Bar is initialized.
69 do_action( 'admin_bar_init' );
72 public function add_menu( $node ) {
73 $this->add_node( $node );
76 public function remove_menu( $id ) {
77 $this->remove_node( $id );
81 * Add a node to the menu.
83 * @param array $args {
84 * Arguments for adding a node.
86 * @type string $id ID of the item.
87 * @type string $title Title of the node.
88 * @type string $parent Optional. ID of the parent node.
89 * @type string $href Optional. Link for the item.
90 * @type bool $group Optional. Whether or not the node is a group. Default false.
91 * @type array $meta Meta data including the following keys: 'html', 'class', 'rel',
92 * 'onclick', 'target', 'title', 'tabindex'. Default empty.
95 public function add_node( $args ) {
96 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
97 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
98 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
100 if ( is_object( $args ) )
101 $args = get_object_vars( $args );
103 // Ensure we have a valid title.
104 if ( empty( $args['id'] ) ) {
105 if ( empty( $args['title'] ) )
108 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
109 // Deprecated: Generate an ID from the title.
110 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
122 // If the node already exists, keep any data that isn't provided.
123 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
124 $defaults = get_object_vars( $maybe_defaults );
126 // Do the same for 'meta' items.
127 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
128 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
130 $args = wp_parse_args( $args, $defaults );
132 $back_compat_parents = array(
133 'my-account-with-avatar' => array( 'my-account', '3.3' ),
134 'my-blogs' => array( 'my-sites', '3.3' ),
137 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
138 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
139 _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'] ) );
140 $args['parent'] = $new_parent;
143 $this->_set_node( $args );
146 final protected function _set_node( $args ) {
147 $this->nodes[ $args['id'] ] = (object) $args;
153 * @return object Node.
155 final public function get_node( $id ) {
156 if ( $node = $this->_get_node( $id ) )
160 final protected function _get_node( $id ) {
167 if ( isset( $this->nodes[ $id ] ) )
168 return $this->nodes[ $id ];
171 final public function get_nodes() {
172 if ( ! $nodes = $this->_get_nodes() )
175 foreach ( $nodes as &$node ) {
181 final protected function _get_nodes() {
189 * Add a group to a menu node.
193 * @param array $args {
194 * Array of arguments for adding a group.
196 * @type string $id ID of the item.
197 * @type string $parent Optional. ID of the parent node. Default 'root'.
198 * @type array $meta Meta data for the group including the following keys:
199 * 'class', 'onclick', 'target', and 'title'.
202 final public function add_group( $args ) {
203 $args['group'] = true;
205 $this->add_node( $args );
211 * @param string The ID of the item.
213 public function remove_node( $id ) {
214 $this->_unset_node( $id );
217 final protected function _unset_node( $id ) {
218 unset( $this->nodes[ $id ] );
221 public function render() {
222 $root = $this->_bind();
224 $this->_render( $root );
227 final protected function _bind() {
231 // Add the root node.
232 // Clear it first, just in case. Don't mess with The Root.
233 $this->remove_node( 'root' );
234 $this->add_node( array(
239 // Normalize nodes: define internal 'children' and 'type' properties.
240 foreach ( $this->_get_nodes() as $node ) {
241 $node->children = array();
242 $node->type = ( $node->group ) ? 'group' : 'item';
243 unset( $node->group );
245 // The Root wants your orphans. No lonely items allowed.
246 if ( ! $node->parent )
247 $node->parent = 'root';
250 foreach ( $this->_get_nodes() as $node ) {
251 if ( 'root' == $node->id )
254 // Fetch the parent node. If it isn't registered, ignore the node.
255 if ( ! $parent = $this->_get_node( $node->parent ) ) {
259 // Generate the group class (we distinguish between top level and other level groups).
260 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
262 if ( $node->type == 'group' ) {
263 if ( empty( $node->meta['class'] ) )
264 $node->meta['class'] = $group_class;
266 $node->meta['class'] .= ' ' . $group_class;
269 // Items in items aren't allowed. Wrap nested items in 'default' groups.
270 if ( $parent->type == 'item' && $node->type == 'item' ) {
271 $default_id = $parent->id . '-default';
272 $default = $this->_get_node( $default_id );
274 // The default group is added here to allow groups that are
275 // added before standard menu items to render first.
277 // Use _set_node because add_node can be overloaded.
278 // Make sure to specify default settings for all properties.
279 $this->_set_node( array(
281 'parent' => $parent->id,
283 'children' => array(),
285 'class' => $group_class,
290 $default = $this->_get_node( $default_id );
291 $parent->children[] = $default;
295 // Groups in groups aren't allowed. Add a special 'container' node.
296 // The container will invisibly wrap both groups.
297 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
298 $container_id = $parent->id . '-container';
299 $container = $this->_get_node( $container_id );
301 // We need to create a container for this group, life is sad.
302 if ( ! $container ) {
303 // Use _set_node because add_node can be overloaded.
304 // Make sure to specify default settings for all properties.
305 $this->_set_node( array(
306 'id' => $container_id,
307 'type' => 'container',
308 'children' => array( $parent ),
315 $container = $this->_get_node( $container_id );
317 // Link the container node if a grandparent node exists.
318 $grandparent = $this->_get_node( $parent->parent );
320 if ( $grandparent ) {
321 $container->parent = $grandparent->id;
323 $index = array_search( $parent, $grandparent->children, true );
324 if ( $index === false )
325 $grandparent->children[] = $container;
327 array_splice( $grandparent->children, $index, 1, array( $container ) );
330 $parent->parent = $container->id;
333 $parent = $container;
336 // Update the parent ID (it might have changed).
337 $node->parent = $parent->id;
339 // Add the node to the tree.
340 $parent->children[] = $node;
343 $root = $this->_get_node( 'root' );
348 final protected function _render( $root ) {
351 // Add browser classes.
352 // We have to do this here since admin bar shows on the front end.
353 $class = 'nojq nojs';
355 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
357 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
359 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
361 } elseif ( wp_is_mobile() ) {
366 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
367 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
368 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
369 <?php foreach ( $root->children as $group ) {
370 $this->_render_group( $group );
373 <?php if ( is_user_logged_in() ) : ?>
374 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
381 final protected function _render_container( $node ) {
382 if ( $node->type != 'container' || empty( $node->children ) )
385 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
386 foreach ( $node->children as $group ) {
387 $this->_render_group( $group );
392 final protected function _render_group( $node ) {
393 if ( $node->type == 'container' )
394 return $this->_render_container( $node );
396 if ( $node->type != 'group' || empty( $node->children ) )
399 if ( ! empty( $node->meta['class'] ) )
400 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
404 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
405 foreach ( $node->children as $item ) {
406 $this->_render_item( $item );
411 final protected function _render_item( $node ) {
412 if ( $node->type != 'item' )
415 $is_parent = ! empty( $node->children );
416 $has_link = ! empty( $node->href );
418 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
419 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
424 $menuclass = 'menupop ';
425 $aria_attributes .= ' aria-haspopup="true"';
428 if ( ! empty( $node->meta['class'] ) )
429 $menuclass .= $node->meta['class'];
432 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
436 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
438 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
439 if ( ! empty( $node->meta['onclick'] ) ) :
440 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
442 if ( ! empty( $node->meta['target'] ) ) :
443 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
445 if ( ! empty( $node->meta['title'] ) ) :
446 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
448 if ( ! empty( $node->meta['rel'] ) ) :
449 ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php
453 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
454 if ( ! empty( $node->meta['title'] ) ) :
455 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
469 ?><div class="ab-sub-wrapper"><?php
470 foreach ( $node->children as $group ) {
471 $this->_render_group( $group );
476 if ( ! empty( $node->meta['html'] ) )
477 echo $node->meta['html'];
483 public function recursive_render( $id, $node ) {
484 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
485 $this->_render_item( $node );
488 public function add_menus() {
489 // User related, aligned right.
490 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
491 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
492 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
495 add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
496 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
497 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
498 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
499 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
502 if ( ! is_network_admin() && ! is_user_admin() ) {
503 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
504 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
506 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
508 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
511 * Fires after menus are added to the menu bar.
515 do_action( 'add_admin_bar_menus' );