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', '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 * Add a node to the menu.
101 * @param array $args {
102 * Arguments for adding a node.
104 * @type string $id ID of the item.
105 * @type string $title Title of the node.
106 * @type string $parent Optional. ID of the parent node.
107 * @type string $href Optional. Link for the item.
108 * @type bool $group Optional. Whether or not the node is a group. Default false.
109 * @type array $meta Meta data including the following keys: 'html', 'class', 'rel',
110 * 'onclick', 'target', 'title', 'tabindex'. Default empty.
113 public function add_node( $args ) {
114 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
115 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
116 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
118 if ( is_object( $args ) )
119 $args = get_object_vars( $args );
121 // Ensure we have a valid title.
122 if ( empty( $args['id'] ) ) {
123 if ( empty( $args['title'] ) )
126 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
127 // Deprecated: Generate an ID from the title.
128 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
140 // If the node already exists, keep any data that isn't provided.
141 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
142 $defaults = get_object_vars( $maybe_defaults );
144 // Do the same for 'meta' items.
145 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
146 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
148 $args = wp_parse_args( $args, $defaults );
150 $back_compat_parents = array(
151 'my-account-with-avatar' => array( 'my-account', '3.3' ),
152 'my-blogs' => array( 'my-sites', '3.3' ),
155 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
156 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
157 _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'] ) );
158 $args['parent'] = $new_parent;
161 $this->_set_node( $args );
167 final protected function _set_node( $args ) {
168 $this->nodes[ $args['id'] ] = (object) $args;
175 * @return object Node.
177 final public function get_node( $id ) {
178 if ( $node = $this->_get_node( $id ) )
184 * @return object|void
186 final protected function _get_node( $id ) {
193 if ( isset( $this->nodes[ $id ] ) )
194 return $this->nodes[ $id ];
200 final public function get_nodes() {
201 if ( ! $nodes = $this->_get_nodes() )
204 foreach ( $nodes as &$node ) {
213 final protected function _get_nodes() {
221 * Add a group to a menu node.
225 * @param array $args {
226 * Array of arguments for adding a group.
228 * @type string $id ID of the item.
229 * @type string $parent Optional. ID of the parent node. Default 'root'.
230 * @type array $meta Meta data for the group including the following keys:
231 * 'class', 'onclick', 'target', and 'title'.
234 final public function add_group( $args ) {
235 $args['group'] = true;
237 $this->add_node( $args );
243 * @param string $id The ID of the item.
245 public function remove_node( $id ) {
246 $this->_unset_node( $id );
252 final protected function _unset_node( $id ) {
253 unset( $this->nodes[ $id ] );
259 public function render() {
260 $root = $this->_bind();
262 $this->_render( $root );
266 * @return object|void
268 final protected function _bind() {
272 // Add the root node.
273 // Clear it first, just in case. Don't mess with The Root.
274 $this->remove_node( 'root' );
275 $this->add_node( array(
280 // Normalize nodes: define internal 'children' and 'type' properties.
281 foreach ( $this->_get_nodes() as $node ) {
282 $node->children = array();
283 $node->type = ( $node->group ) ? 'group' : 'item';
284 unset( $node->group );
286 // The Root wants your orphans. No lonely items allowed.
287 if ( ! $node->parent )
288 $node->parent = 'root';
291 foreach ( $this->_get_nodes() as $node ) {
292 if ( 'root' == $node->id )
295 // Fetch the parent node. If it isn't registered, ignore the node.
296 if ( ! $parent = $this->_get_node( $node->parent ) ) {
300 // Generate the group class (we distinguish between top level and other level groups).
301 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
303 if ( $node->type == 'group' ) {
304 if ( empty( $node->meta['class'] ) )
305 $node->meta['class'] = $group_class;
307 $node->meta['class'] .= ' ' . $group_class;
310 // Items in items aren't allowed. Wrap nested items in 'default' groups.
311 if ( $parent->type == 'item' && $node->type == 'item' ) {
312 $default_id = $parent->id . '-default';
313 $default = $this->_get_node( $default_id );
315 // The default group is added here to allow groups that are
316 // added before standard menu items to render first.
318 // Use _set_node because add_node can be overloaded.
319 // Make sure to specify default settings for all properties.
320 $this->_set_node( array(
322 'parent' => $parent->id,
324 'children' => array(),
326 'class' => $group_class,
331 $default = $this->_get_node( $default_id );
332 $parent->children[] = $default;
336 // Groups in groups aren't allowed. Add a special 'container' node.
337 // The container will invisibly wrap both groups.
338 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
339 $container_id = $parent->id . '-container';
340 $container = $this->_get_node( $container_id );
342 // We need to create a container for this group, life is sad.
343 if ( ! $container ) {
344 // Use _set_node because add_node can be overloaded.
345 // Make sure to specify default settings for all properties.
346 $this->_set_node( array(
347 'id' => $container_id,
348 'type' => 'container',
349 'children' => array( $parent ),
356 $container = $this->_get_node( $container_id );
358 // Link the container node if a grandparent node exists.
359 $grandparent = $this->_get_node( $parent->parent );
361 if ( $grandparent ) {
362 $container->parent = $grandparent->id;
364 $index = array_search( $parent, $grandparent->children, true );
365 if ( $index === false )
366 $grandparent->children[] = $container;
368 array_splice( $grandparent->children, $index, 1, array( $container ) );
371 $parent->parent = $container->id;
374 $parent = $container;
377 // Update the parent ID (it might have changed).
378 $node->parent = $parent->id;
380 // Add the node to the tree.
381 $parent->children[] = $node;
384 $root = $this->_get_node( 'root' );
391 * @global bool $is_IE
392 * @param object $root
394 final protected function _render( $root ) {
397 // Add browser classes.
398 // We have to do this here since admin bar shows on the front end.
399 $class = 'nojq nojs';
401 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
403 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
405 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
407 } elseif ( wp_is_mobile() ) {
412 <div id="wpadminbar" class="<?php echo $class; ?>">
413 <?php if ( ! is_admin() ) { ?>
414 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a>
416 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>" tabindex="0">
417 <?php foreach ( $root->children as $group ) {
418 $this->_render_group( $group );
421 <?php if ( is_user_logged_in() ) : ?>
422 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
430 * @param object $node
432 final protected function _render_container( $node ) {
433 if ( $node->type != 'container' || empty( $node->children ) )
436 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
437 foreach ( $node->children as $group ) {
438 $this->_render_group( $group );
444 * @param object $node
446 final protected function _render_group( $node ) {
447 if ( $node->type == 'container' ) {
448 $this->_render_container( $node );
451 if ( $node->type != 'group' || empty( $node->children ) )
454 if ( ! empty( $node->meta['class'] ) )
455 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
459 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
460 foreach ( $node->children as $item ) {
461 $this->_render_item( $item );
467 * @param object $node
469 final protected function _render_item( $node ) {
470 if ( $node->type != 'item' )
473 $is_parent = ! empty( $node->children );
474 $has_link = ! empty( $node->href );
476 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
477 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
482 $menuclass = 'menupop ';
483 $aria_attributes .= ' aria-haspopup="true"';
486 if ( ! empty( $node->meta['class'] ) )
487 $menuclass .= $node->meta['class'];
490 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
494 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
496 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
497 if ( ! empty( $node->meta['onclick'] ) ) :
498 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
500 if ( ! empty( $node->meta['target'] ) ) :
501 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
503 if ( ! empty( $node->meta['title'] ) ) :
504 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
506 if ( ! empty( $node->meta['rel'] ) ) :
507 ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php
511 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
512 if ( ! empty( $node->meta['title'] ) ) :
513 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
527 ?><div class="ab-sub-wrapper"><?php
528 foreach ( $node->children as $group ) {
529 $this->_render_group( $group );
534 if ( ! empty( $node->meta['html'] ) )
535 echo $node->meta['html'];
542 * @param string $id Unused.
543 * @param object $node
545 public function recursive_render( $id, $node ) {
546 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
547 $this->_render_item( $node );
553 public function add_menus() {
554 // User related, aligned right.
555 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
556 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
557 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
560 add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
561 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
562 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
563 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
564 add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
565 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
568 if ( ! is_network_admin() && ! is_user_admin() ) {
569 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
570 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
572 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
574 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
577 * Fires after menus are added to the menu bar.
581 do_action( 'add_admin_bar_menus' );