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 {
85 * Arguments for adding a node.
87 * @type string $id ID of the item.
88 * @type string $title Title of the node.
89 * @type string $parent Optional. ID of the parent node.
90 * @type string $href Optional. Link for the item.
91 * @type bool $group Optional. Whether or not the node is a group. Default false.
92 * @type array $meta Meta data including the following keys: 'html', 'class', 'rel',
93 * 'onclick', 'target', 'title', 'tabindex'. Default empty.
96 public function add_node( $args ) {
97 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
98 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
99 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
101 if ( is_object( $args ) )
102 $args = get_object_vars( $args );
104 // Ensure we have a valid title.
105 if ( empty( $args['id'] ) ) {
106 if ( empty( $args['title'] ) )
109 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
110 // Deprecated: Generate an ID from the title.
111 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
123 // If the node already exists, keep any data that isn't provided.
124 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
125 $defaults = get_object_vars( $maybe_defaults );
127 // Do the same for 'meta' items.
128 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
129 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
131 $args = wp_parse_args( $args, $defaults );
133 $back_compat_parents = array(
134 'my-account-with-avatar' => array( 'my-account', '3.3' ),
135 'my-blogs' => array( 'my-sites', '3.3' ),
138 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
139 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
140 _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'] ) );
141 $args['parent'] = $new_parent;
144 $this->_set_node( $args );
147 final protected function _set_node( $args ) {
148 $this->nodes[ $args['id'] ] = (object) $args;
154 * @return object Node.
156 final public function get_node( $id ) {
157 if ( $node = $this->_get_node( $id ) )
161 final protected function _get_node( $id ) {
168 if ( isset( $this->nodes[ $id ] ) )
169 return $this->nodes[ $id ];
172 final public function get_nodes() {
173 if ( ! $nodes = $this->_get_nodes() )
176 foreach ( $nodes as &$node ) {
182 final protected function _get_nodes() {
190 * Add a group to a menu node.
194 * @param array $args {
195 * Array of arguments for adding a group.
197 * @type string $id ID of the item.
198 * @type string $parent Optional. ID of the parent node. Default 'root'.
199 * @type array $meta Meta data for the group including the following keys:
200 * 'class', 'onclick', 'target', and 'title'.
203 final public function add_group( $args ) {
204 $args['group'] = true;
206 $this->add_node( $args );
212 * @param string The ID of the item.
214 public function remove_node( $id ) {
215 $this->_unset_node( $id );
218 final protected function _unset_node( $id ) {
219 unset( $this->nodes[ $id ] );
222 public function render() {
223 $root = $this->_bind();
225 $this->_render( $root );
228 final protected function _bind() {
232 // Add the root node.
233 // Clear it first, just in case. Don't mess with The Root.
234 $this->remove_node( 'root' );
235 $this->add_node( array(
240 // Normalize nodes: define internal 'children' and 'type' properties.
241 foreach ( $this->_get_nodes() as $node ) {
242 $node->children = array();
243 $node->type = ( $node->group ) ? 'group' : 'item';
244 unset( $node->group );
246 // The Root wants your orphans. No lonely items allowed.
247 if ( ! $node->parent )
248 $node->parent = 'root';
251 foreach ( $this->_get_nodes() as $node ) {
252 if ( 'root' == $node->id )
255 // Fetch the parent node. If it isn't registered, ignore the node.
256 if ( ! $parent = $this->_get_node( $node->parent ) ) {
260 // Generate the group class (we distinguish between top level and other level groups).
261 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
263 if ( $node->type == 'group' ) {
264 if ( empty( $node->meta['class'] ) )
265 $node->meta['class'] = $group_class;
267 $node->meta['class'] .= ' ' . $group_class;
270 // Items in items aren't allowed. Wrap nested items in 'default' groups.
271 if ( $parent->type == 'item' && $node->type == 'item' ) {
272 $default_id = $parent->id . '-default';
273 $default = $this->_get_node( $default_id );
275 // The default group is added here to allow groups that are
276 // added before standard menu items to render first.
278 // Use _set_node because add_node can be overloaded.
279 // Make sure to specify default settings for all properties.
280 $this->_set_node( array(
282 'parent' => $parent->id,
284 'children' => array(),
286 'class' => $group_class,
291 $default = $this->_get_node( $default_id );
292 $parent->children[] = $default;
296 // Groups in groups aren't allowed. Add a special 'container' node.
297 // The container will invisibly wrap both groups.
298 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
299 $container_id = $parent->id . '-container';
300 $container = $this->_get_node( $container_id );
302 // We need to create a container for this group, life is sad.
303 if ( ! $container ) {
304 // Use _set_node because add_node can be overloaded.
305 // Make sure to specify default settings for all properties.
306 $this->_set_node( array(
307 'id' => $container_id,
308 'type' => 'container',
309 'children' => array( $parent ),
316 $container = $this->_get_node( $container_id );
318 // Link the container node if a grandparent node exists.
319 $grandparent = $this->_get_node( $parent->parent );
321 if ( $grandparent ) {
322 $container->parent = $grandparent->id;
324 $index = array_search( $parent, $grandparent->children, true );
325 if ( $index === false )
326 $grandparent->children[] = $container;
328 array_splice( $grandparent->children, $index, 1, array( $container ) );
331 $parent->parent = $container->id;
334 $parent = $container;
337 // Update the parent ID (it might have changed).
338 $node->parent = $parent->id;
340 // Add the node to the tree.
341 $parent->children[] = $node;
344 $root = $this->_get_node( 'root' );
349 final protected function _render( $root ) {
352 // Add browser classes.
353 // We have to do this here since admin bar shows on the front end.
354 $class = 'nojq nojs';
356 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
358 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
360 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
362 } elseif ( wp_is_mobile() ) {
367 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
368 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
369 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
370 <?php foreach ( $root->children as $group ) {
371 $this->_render_group( $group );
374 <?php if ( is_user_logged_in() ) : ?>
375 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
382 final protected function _render_container( $node ) {
383 if ( $node->type != 'container' || empty( $node->children ) )
386 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
387 foreach ( $node->children as $group ) {
388 $this->_render_group( $group );
393 final protected function _render_group( $node ) {
394 if ( $node->type == 'container' )
395 return $this->_render_container( $node );
397 if ( $node->type != 'group' || empty( $node->children ) )
400 if ( ! empty( $node->meta['class'] ) )
401 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
405 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
406 foreach ( $node->children as $item ) {
407 $this->_render_item( $item );
412 final protected function _render_item( $node ) {
413 if ( $node->type != 'item' )
416 $is_parent = ! empty( $node->children );
417 $has_link = ! empty( $node->href );
419 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
420 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
425 $menuclass = 'menupop ';
426 $aria_attributes .= ' aria-haspopup="true"';
429 if ( ! empty( $node->meta['class'] ) )
430 $menuclass .= $node->meta['class'];
433 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
437 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
439 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
440 if ( ! empty( $node->meta['onclick'] ) ) :
441 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
443 if ( ! empty( $node->meta['target'] ) ) :
444 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
446 if ( ! empty( $node->meta['title'] ) ) :
447 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
449 if ( ! empty( $node->meta['rel'] ) ) :
450 ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php
454 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
455 if ( ! empty( $node->meta['title'] ) ) :
456 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
470 ?><div class="ab-sub-wrapper"><?php
471 foreach ( $node->children as $group ) {
472 $this->_render_group( $group );
477 if ( ! empty( $node->meta['html'] ) )
478 echo $node->meta['html'];
484 public function recursive_render( $id, $node ) {
485 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
486 $this->_render_item( $node );
489 public function add_menus() {
490 // User related, aligned right.
491 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
492 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
493 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
496 add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
497 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
498 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
499 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
500 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
503 if ( ! is_network_admin() && ! is_user_admin() ) {
504 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
505 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
507 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
509 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
512 * Fires after menus are added to the menu bar.
516 do_action( 'add_admin_bar_menus' );