3 * Toolbar API: WP_Admin_Bar class
11 * Core class used to implement the Toolbar API.
16 private $nodes = array();
17 private $bound = false;
22 * @return string|array|void
24 public function __get( $name ) {
27 return is_ssl() ? 'https://' : 'http://';
30 _deprecated_argument( 'WP_Admin_Bar', '3.3.0', '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.' );
31 return array(); // Sorry, folks.
38 public function initialize() {
39 $this->user = new stdClass;
41 if ( is_user_logged_in() ) {
42 /* Populate settings we need for the menu based on the current user. */
43 $this->user->blogs = get_blogs_of_user( get_current_user_id() );
44 if ( is_multisite() ) {
45 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
46 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
47 $this->user->account_domain = $this->user->domain;
49 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
50 $this->user->domain = trailingslashit( home_url() );
51 $this->user->account_domain = $this->user->domain;
55 add_action( 'wp_head', 'wp_admin_bar_header' );
57 add_action( 'admin_head', 'wp_admin_bar_header' );
59 if ( current_theme_supports( 'admin-bar' ) ) {
61 * To remove the default padding styles from WordPress for the Toolbar, use the following code:
62 * add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );
64 $admin_bar_args = get_theme_support( 'admin-bar' );
65 $header_callback = $admin_bar_args[0]['callback'];
68 if ( empty($header_callback) )
69 $header_callback = '_admin_bar_bump_cb';
71 add_action('wp_head', $header_callback);
73 wp_enqueue_script( 'admin-bar' );
74 wp_enqueue_style( 'admin-bar' );
77 * Fires after WP_Admin_Bar is initialized.
81 do_action( 'admin_bar_init' );
87 public function add_menu( $node ) {
88 $this->add_node( $node );
94 public function remove_menu( $id ) {
95 $this->remove_node( $id );
99 * Adds a node to the menu.
102 * @since 4.5.0 Added the ability to pass 'lang' and 'dir' meta data.
105 * @param array $args {
106 * Arguments for adding a node.
108 * @type string $id ID of the item.
109 * @type string $title Title of the node.
110 * @type string $parent Optional. ID of the parent node.
111 * @type string $href Optional. Link for the item.
112 * @type bool $group Optional. Whether or not the node is a group. Default false.
113 * @type array $meta Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
114 * 'onclick', 'target', 'title', 'tabindex'. Default empty.
117 public function add_node( $args ) {
118 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
119 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
120 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
122 if ( is_object( $args ) )
123 $args = get_object_vars( $args );
125 // Ensure we have a valid title.
126 if ( empty( $args['id'] ) ) {
127 if ( empty( $args['title'] ) )
130 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3.0' );
131 // Deprecated: Generate an ID from the title.
132 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
144 // If the node already exists, keep any data that isn't provided.
145 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
146 $defaults = get_object_vars( $maybe_defaults );
148 // Do the same for 'meta' items.
149 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
150 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
152 $args = wp_parse_args( $args, $defaults );
154 $back_compat_parents = array(
155 'my-account-with-avatar' => array( 'my-account', '3.3' ),
156 'my-blogs' => array( 'my-sites', '3.3' ),
159 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
160 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
161 _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'] ) );
162 $args['parent'] = $new_parent;
165 $this->_set_node( $args );
171 final protected function _set_node( $args ) {
172 $this->nodes[ $args['id'] ] = (object) $args;
179 * @return object Node.
181 final public function get_node( $id ) {
182 if ( $node = $this->_get_node( $id ) )
188 * @return object|void
190 final protected function _get_node( $id ) {
197 if ( isset( $this->nodes[ $id ] ) )
198 return $this->nodes[ $id ];
204 final public function get_nodes() {
205 if ( ! $nodes = $this->_get_nodes() )
208 foreach ( $nodes as &$node ) {
217 final protected function _get_nodes() {
225 * Add a group to a menu node.
229 * @param array $args {
230 * Array of arguments for adding a group.
232 * @type string $id ID of the item.
233 * @type string $parent Optional. ID of the parent node. Default 'root'.
234 * @type array $meta Meta data for the group including the following keys:
235 * 'class', 'onclick', 'target', and 'title'.
238 final public function add_group( $args ) {
239 $args['group'] = true;
241 $this->add_node( $args );
247 * @param string $id The ID of the item.
249 public function remove_node( $id ) {
250 $this->_unset_node( $id );
256 final protected function _unset_node( $id ) {
257 unset( $this->nodes[ $id ] );
263 public function render() {
264 $root = $this->_bind();
266 $this->_render( $root );
270 * @return object|void
272 final protected function _bind() {
276 // Add the root node.
277 // Clear it first, just in case. Don't mess with The Root.
278 $this->remove_node( 'root' );
279 $this->add_node( array(
284 // Normalize nodes: define internal 'children' and 'type' properties.
285 foreach ( $this->_get_nodes() as $node ) {
286 $node->children = array();
287 $node->type = ( $node->group ) ? 'group' : 'item';
288 unset( $node->group );
290 // The Root wants your orphans. No lonely items allowed.
291 if ( ! $node->parent )
292 $node->parent = 'root';
295 foreach ( $this->_get_nodes() as $node ) {
296 if ( 'root' == $node->id )
299 // Fetch the parent node. If it isn't registered, ignore the node.
300 if ( ! $parent = $this->_get_node( $node->parent ) ) {
304 // Generate the group class (we distinguish between top level and other level groups).
305 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
307 if ( $node->type == 'group' ) {
308 if ( empty( $node->meta['class'] ) )
309 $node->meta['class'] = $group_class;
311 $node->meta['class'] .= ' ' . $group_class;
314 // Items in items aren't allowed. Wrap nested items in 'default' groups.
315 if ( $parent->type == 'item' && $node->type == 'item' ) {
316 $default_id = $parent->id . '-default';
317 $default = $this->_get_node( $default_id );
319 // The default group is added here to allow groups that are
320 // added before standard menu items to render first.
322 // Use _set_node because add_node can be overloaded.
323 // Make sure to specify default settings for all properties.
324 $this->_set_node( array(
326 'parent' => $parent->id,
328 'children' => array(),
330 'class' => $group_class,
335 $default = $this->_get_node( $default_id );
336 $parent->children[] = $default;
340 // Groups in groups aren't allowed. Add a special 'container' node.
341 // The container will invisibly wrap both groups.
342 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
343 $container_id = $parent->id . '-container';
344 $container = $this->_get_node( $container_id );
346 // We need to create a container for this group, life is sad.
347 if ( ! $container ) {
348 // Use _set_node because add_node can be overloaded.
349 // Make sure to specify default settings for all properties.
350 $this->_set_node( array(
351 'id' => $container_id,
352 'type' => 'container',
353 'children' => array( $parent ),
360 $container = $this->_get_node( $container_id );
362 // Link the container node if a grandparent node exists.
363 $grandparent = $this->_get_node( $parent->parent );
365 if ( $grandparent ) {
366 $container->parent = $grandparent->id;
368 $index = array_search( $parent, $grandparent->children, true );
369 if ( $index === false )
370 $grandparent->children[] = $container;
372 array_splice( $grandparent->children, $index, 1, array( $container ) );
375 $parent->parent = $container->id;
378 $parent = $container;
381 // Update the parent ID (it might have changed).
382 $node->parent = $parent->id;
384 // Add the node to the tree.
385 $parent->children[] = $node;
388 $root = $this->_get_node( 'root' );
395 * @global bool $is_IE
396 * @param object $root
398 final protected function _render( $root ) {
401 // Add browser classes.
402 // We have to do this here since admin bar shows on the front end.
403 $class = 'nojq nojs';
405 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
407 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
409 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
411 } elseif ( wp_is_mobile() ) {
416 <div id="wpadminbar" class="<?php echo $class; ?>">
417 <?php if ( ! is_admin() ) { ?>
418 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a>
420 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>" tabindex="0">
421 <?php foreach ( $root->children as $group ) {
422 $this->_render_group( $group );
425 <?php if ( is_user_logged_in() ) : ?>
426 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
434 * @param object $node
436 final protected function _render_container( $node ) {
437 if ( $node->type != 'container' || empty( $node->children ) )
440 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
441 foreach ( $node->children as $group ) {
442 $this->_render_group( $group );
448 * @param object $node
450 final protected function _render_group( $node ) {
451 if ( $node->type == 'container' ) {
452 $this->_render_container( $node );
455 if ( $node->type != 'group' || empty( $node->children ) )
458 if ( ! empty( $node->meta['class'] ) )
459 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
463 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
464 foreach ( $node->children as $item ) {
465 $this->_render_item( $item );
471 * @param object $node
473 final protected function _render_item( $node ) {
474 if ( $node->type != 'item' )
477 $is_parent = ! empty( $node->children );
478 $has_link = ! empty( $node->href );
480 // Allow only numeric values, then casted to integers, and allow a tabindex value of `0` for a11y.
481 $tabindex = ( isset( $node->meta['tabindex'] ) && is_numeric( $node->meta['tabindex'] ) ) ? (int) $node->meta['tabindex'] : '';
482 $aria_attributes = ( '' !== $tabindex ) ? ' tabindex="' . $tabindex . '"' : '';
487 $menuclass = 'menupop ';
488 $aria_attributes .= ' aria-haspopup="true"';
491 if ( ! empty( $node->meta['class'] ) )
492 $menuclass .= $node->meta['class'];
495 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
499 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
501 ?><a class="ab-item"<?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
502 if ( ! empty( $node->meta['onclick'] ) ) :
503 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
505 if ( ! empty( $node->meta['target'] ) ) :
506 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
508 if ( ! empty( $node->meta['title'] ) ) :
509 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
511 if ( ! empty( $node->meta['rel'] ) ) :
512 ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php
514 if ( ! empty( $node->meta['lang'] ) ) :
515 ?> lang="<?php echo esc_attr( $node->meta['lang'] ); ?>"<?php
517 if ( ! empty( $node->meta['dir'] ) ) :
518 ?> dir="<?php echo esc_attr( $node->meta['dir'] ); ?>"<?php
522 ?><div class="ab-item ab-empty-item"<?php echo $aria_attributes;
523 if ( ! empty( $node->meta['title'] ) ) :
524 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
526 if ( ! empty( $node->meta['lang'] ) ) :
527 ?> lang="<?php echo esc_attr( $node->meta['lang'] ); ?>"<?php
529 if ( ! empty( $node->meta['dir'] ) ) :
530 ?> dir="<?php echo esc_attr( $node->meta['dir'] ); ?>"<?php
544 ?><div class="ab-sub-wrapper"><?php
545 foreach ( $node->children as $group ) {
546 $this->_render_group( $group );
551 if ( ! empty( $node->meta['html'] ) )
552 echo $node->meta['html'];
559 * @param string $id Unused.
560 * @param object $node
562 public function recursive_render( $id, $node ) {
563 _deprecated_function( __METHOD__, '3.3.0', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
564 $this->_render_item( $node );
570 public function add_menus() {
571 // User related, aligned right.
572 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
573 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
574 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
577 add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
578 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
579 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
580 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
581 add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
582 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
585 if ( ! is_network_admin() && ! is_user_admin() ) {
586 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
587 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
589 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
591 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
594 * Fires after menus are added to the menu bar.
598 do_action( 'add_admin_bar_menus' );