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' ) ) {
49 $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
50 $header_callback = $admin_bar_args[0]['callback'];
53 if ( empty($header_callback) )
54 $header_callback = '_admin_bar_bump_cb';
56 add_action('wp_head', $header_callback);
58 wp_enqueue_script( 'admin-bar' );
59 wp_enqueue_style( 'admin-bar' );
61 do_action( 'admin_bar_init' );
64 public function add_menu( $node ) {
65 $this->add_node( $node );
68 public function remove_menu( $id ) {
69 $this->remove_node( $id );
73 * Add a node to the menu.
75 * @param array $args - The arguments for each node.
76 * - id - string - The ID of the item.
77 * - title - string - The title of the node.
78 * - parent - string - The ID of the parent node. Optional.
79 * - href - string - The link for the item. Optional.
80 * - group - boolean - If the node is a group. Optional. Default false.
81 * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex.
83 public function add_node( $args ) {
84 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
85 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
86 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
88 if ( is_object( $args ) )
89 $args = get_object_vars( $args );
91 // Ensure we have a valid title.
92 if ( empty( $args['id'] ) ) {
93 if ( empty( $args['title'] ) )
96 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
97 // Deprecated: Generate an ID from the title.
98 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
110 // If the node already exists, keep any data that isn't provided.
111 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
112 $defaults = get_object_vars( $maybe_defaults );
114 // Do the same for 'meta' items.
115 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
116 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
118 $args = wp_parse_args( $args, $defaults );
120 $back_compat_parents = array(
121 'my-account-with-avatar' => array( 'my-account', '3.3' ),
122 'my-blogs' => array( 'my-sites', '3.3' ),
125 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
126 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
127 _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'] ) );
128 $args['parent'] = $new_parent;
131 $this->_set_node( $args );
134 final protected function _set_node( $args ) {
135 $this->nodes[ $args['id'] ] = (object) $args;
141 * @return object Node.
143 final public function get_node( $id ) {
144 if ( $node = $this->_get_node( $id ) )
148 final protected function _get_node( $id ) {
155 if ( isset( $this->nodes[ $id ] ) )
156 return $this->nodes[ $id ];
159 final public function get_nodes() {
160 if ( ! $nodes = $this->_get_nodes() )
163 foreach ( $nodes as &$node ) {
169 final protected function _get_nodes() {
177 * Add a group to a menu node.
181 * @param array $args - The arguments for each node.
182 * - id - string - The ID of the item.
183 * - parent - string - The ID of the parent node. Optional. Default root.
184 * - meta - array - Meta data including the following keys: class, onclick, target, title.
186 final public function add_group( $args ) {
187 $args['group'] = true;
189 $this->add_node( $args );
195 * @param string The ID of the item.
197 public function remove_node( $id ) {
198 $this->_unset_node( $id );
201 final protected function _unset_node( $id ) {
202 unset( $this->nodes[ $id ] );
205 public function render() {
206 $root = $this->_bind();
208 $this->_render( $root );
211 final protected function _bind() {
215 // Add the root node.
216 // Clear it first, just in case. Don't mess with The Root.
217 $this->remove_node( 'root' );
218 $this->add_node( array(
223 // Normalize nodes: define internal 'children' and 'type' properties.
224 foreach ( $this->_get_nodes() as $node ) {
225 $node->children = array();
226 $node->type = ( $node->group ) ? 'group' : 'item';
227 unset( $node->group );
229 // The Root wants your orphans. No lonely items allowed.
230 if ( ! $node->parent )
231 $node->parent = 'root';
234 foreach ( $this->_get_nodes() as $node ) {
235 if ( 'root' == $node->id )
238 // Fetch the parent node. If it isn't registered, ignore the node.
239 if ( ! $parent = $this->_get_node( $node->parent ) ) {
243 // Generate the group class (we distinguish between top level and other level groups).
244 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
246 if ( $node->type == 'group' ) {
247 if ( empty( $node->meta['class'] ) )
248 $node->meta['class'] = $group_class;
250 $node->meta['class'] .= ' ' . $group_class;
253 // Items in items aren't allowed. Wrap nested items in 'default' groups.
254 if ( $parent->type == 'item' && $node->type == 'item' ) {
255 $default_id = $parent->id . '-default';
256 $default = $this->_get_node( $default_id );
258 // The default group is added here to allow groups that are
259 // added before standard menu items to render first.
261 // Use _set_node because add_node can be overloaded.
262 // Make sure to specify default settings for all properties.
263 $this->_set_node( array(
265 'parent' => $parent->id,
267 'children' => array(),
269 'class' => $group_class,
274 $default = $this->_get_node( $default_id );
275 $parent->children[] = $default;
279 // Groups in groups aren't allowed. Add a special 'container' node.
280 // The container will invisibly wrap both groups.
281 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
282 $container_id = $parent->id . '-container';
283 $container = $this->_get_node( $container_id );
285 // We need to create a container for this group, life is sad.
286 if ( ! $container ) {
287 // Use _set_node because add_node can be overloaded.
288 // Make sure to specify default settings for all properties.
289 $this->_set_node( array(
290 'id' => $container_id,
291 'type' => 'container',
292 'children' => array( $parent ),
299 $container = $this->_get_node( $container_id );
301 // Link the container node if a grandparent node exists.
302 $grandparent = $this->_get_node( $parent->parent );
304 if ( $grandparent ) {
305 $container->parent = $grandparent->id;
307 $index = array_search( $parent, $grandparent->children, true );
308 if ( $index === false )
309 $grandparent->children[] = $container;
311 array_splice( $grandparent->children, $index, 1, array( $container ) );
314 $parent->parent = $container->id;
317 $parent = $container;
320 // Update the parent ID (it might have changed).
321 $node->parent = $parent->id;
323 // Add the node to the tree.
324 $parent->children[] = $node;
327 $root = $this->_get_node( 'root' );
332 final protected function _render( $root ) {
335 // Add browser classes.
336 // We have to do this here since admin bar shows on the front end.
337 $class = 'nojq nojs';
339 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
341 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
343 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
345 } elseif ( wp_is_mobile() ) {
350 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
351 <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
352 <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
353 <?php foreach ( $root->children as $group ) {
354 $this->_render_group( $group );
357 <?php if ( is_user_logged_in() ) : ?>
358 <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
365 final protected function _render_container( $node ) {
366 if ( $node->type != 'container' || empty( $node->children ) )
369 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
370 foreach ( $node->children as $group ) {
371 $this->_render_group( $group );
376 final protected function _render_group( $node ) {
377 if ( $node->type == 'container' )
378 return $this->_render_container( $node );
380 if ( $node->type != 'group' || empty( $node->children ) )
383 if ( ! empty( $node->meta['class'] ) )
384 $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
388 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
389 foreach ( $node->children as $item ) {
390 $this->_render_item( $item );
395 final protected function _render_item( $node ) {
396 if ( $node->type != 'item' )
399 $is_parent = ! empty( $node->children );
400 $has_link = ! empty( $node->href );
402 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
403 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
408 $menuclass = 'menupop ';
409 $aria_attributes .= ' aria-haspopup="true"';
412 if ( ! empty( $node->meta['class'] ) )
413 $menuclass .= $node->meta['class'];
416 $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
420 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
422 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
423 if ( ! empty( $node->meta['onclick'] ) ) :
424 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
426 if ( ! empty( $node->meta['target'] ) ) :
427 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
429 if ( ! empty( $node->meta['title'] ) ) :
430 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
434 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
435 if ( ! empty( $node->meta['title'] ) ) :
436 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
450 ?><div class="ab-sub-wrapper"><?php
451 foreach ( $node->children as $group ) {
452 $this->_render_group( $group );
457 if ( ! empty( $node->meta['html'] ) )
458 echo $node->meta['html'];
464 public function recursive_render( $id, $node ) {
465 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
466 $this->_render_item( $node );
469 public function add_menus() {
470 // User related, aligned right.
471 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
472 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
473 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
476 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
477 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
478 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
479 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
482 if ( ! is_network_admin() && ! is_user_admin() ) {
483 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
484 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
486 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
488 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
490 do_action( 'add_admin_bar_menus' );