3 private $nodes = array();
4 private $bound = false;
7 public function __get( $name ) {
10 return is_ssl() ? 'https://' : 'http://';
13 _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.' );
14 return array(); // Sorry, folks.
19 public function initialize() {
20 $this->user = new stdClass;
22 if ( is_user_logged_in() ) {
23 /* Populate settings we need for the menu based on the current user. */
24 $this->user->blogs = get_blogs_of_user( get_current_user_id() );
25 if ( is_multisite() ) {
26 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
27 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
28 $this->user->account_domain = $this->user->domain;
30 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
31 $this->user->domain = trailingslashit( home_url() );
32 $this->user->account_domain = $this->user->domain;
36 add_action( 'wp_head', 'wp_admin_bar_header' );
38 add_action( 'admin_head', 'wp_admin_bar_header' );
40 if ( current_theme_supports( 'admin-bar' ) ) {
41 $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
42 $header_callback = $admin_bar_args[0]['callback'];
45 if ( empty($header_callback) )
46 $header_callback = '_admin_bar_bump_cb';
48 add_action('wp_head', $header_callback);
50 wp_enqueue_script( 'admin-bar' );
51 wp_enqueue_style( 'admin-bar' );
53 do_action( 'admin_bar_init' );
56 public function add_menu( $node ) {
57 $this->add_node( $node );
60 public function remove_menu( $id ) {
61 $this->remove_node( $id );
65 * Add a node to the menu.
67 * @param array $args - The arguments for each node.
68 * - id - string - The ID of the item.
69 * - title - string - The title of the node.
70 * - parent - string - The ID of the parent node. Optional.
71 * - href - string - The link for the item. Optional.
72 * - group - boolean - If the node is a group. Optional. Default false.
73 * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex.
75 public function add_node( $args ) {
76 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
77 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
78 $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
80 if ( is_object( $args ) )
81 $args = get_object_vars( $args );
83 // Ensure we have a valid title.
84 if ( empty( $args['id'] ) ) {
85 if ( empty( $args['title'] ) )
88 _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
89 // Deprecated: Generate an ID from the title.
90 $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
102 // If the node already exists, keep any data that isn't provided.
103 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
104 $defaults = get_object_vars( $maybe_defaults );
106 // Do the same for 'meta' items.
107 if ( ! empty( $defaults['meta'] ) && empty( $args['meta'] ) )
108 $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
110 $args = wp_parse_args( $args, $defaults );
112 $back_compat_parents = array(
113 'my-account-with-avatar' => array( 'my-account', '3.3' ),
114 'my-blogs' => array( 'my-sites', '3.3' ),
117 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
118 list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
119 _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'] ) );
120 $args['parent'] = $new_parent;
123 $this->_set_node( $args );
126 final protected function _set_node( $args ) {
127 $this->nodes[ $args['id'] ] = (object) $args;
133 * @return object Node.
135 final public function get_node( $id ) {
136 if ( $node = $this->_get_node( $id ) )
140 final protected function _get_node( $id ) {
147 if ( isset( $this->nodes[ $id ] ) )
148 return $this->nodes[ $id ];
151 final public function get_nodes() {
152 if ( ! $nodes = $this->_get_nodes() )
155 foreach ( $nodes as &$node ) {
161 final protected function _get_nodes() {
169 * Add a group to a menu node.
173 * @param array $args - The arguments for each node.
174 * - id - string - The ID of the item.
175 * - parent - string - The ID of the parent node. Optional. Default root.
176 * - meta - array - Meta data including the following keys: class, onclick, target, title.
178 final public function add_group( $args ) {
179 $args['group'] = true;
181 $this->add_node( $args );
187 * @return object The removed node.
189 public function remove_node( $id ) {
190 $this->_unset_node( $id );
193 final protected function _unset_node( $id ) {
194 unset( $this->nodes[ $id ] );
197 public function render() {
198 $root = $this->_bind();
199 $this->_render( $root );
202 final protected function _bind() {
206 // Add the root node.
207 // Clear it first, just in case. Don't mess with The Root.
208 $this->remove_node( 'root' );
209 $this->add_node( array(
214 // Normalize nodes: define internal 'children' and 'type' properties.
215 foreach ( $this->_get_nodes() as $node ) {
216 $node->children = array();
217 $node->type = ( $node->group ) ? 'group' : 'item';
218 unset( $node->group );
220 // The Root wants your orphans. No lonely items allowed.
221 if ( ! $node->parent )
222 $node->parent = 'root';
225 foreach ( $this->_get_nodes() as $node ) {
226 if ( 'root' == $node->id )
229 // Fetch the parent node. If it isn't registered, ignore the node.
230 if ( ! $parent = $this->_get_node( $node->parent ) ) {
234 // Generate the group class (we distinguish between top level and other level groups).
235 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
237 if ( $node->type == 'group' ) {
238 if ( empty( $node->meta['class'] ) )
239 $node->meta['class'] = '';
240 $node->meta['class'] .= ' ' . $group_class;
243 // Items in items aren't allowed. Wrap nested items in 'default' groups.
244 if ( $parent->type == 'item' && $node->type == 'item' ) {
245 $default_id = $parent->id . '-default';
246 $default = $this->_get_node( $default_id );
248 // The default group is added here to allow groups that are
249 // added before standard menu items to render first.
251 // Use _set_node because add_node can be overloaded.
252 // Make sure to specify default settings for all properties.
253 $this->_set_node( array(
255 'parent' => $parent->id,
257 'children' => array(),
259 'class' => $group_class,
264 $default = $this->_get_node( $default_id );
265 $parent->children[] = $default;
269 // Groups in groups aren't allowed. Add a special 'container' node.
270 // The container will invisibly wrap both groups.
271 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
272 $container_id = $parent->id . '-container';
273 $container = $this->_get_node( $container_id );
275 // We need to create a container for this group, life is sad.
276 if ( ! $container ) {
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(
280 'id' => $container_id,
281 'type' => 'container',
282 'children' => array( $parent ),
289 $container = $this->_get_node( $container_id );
291 // Link the container node if a grandparent node exists.
292 $grandparent = $this->_get_node( $parent->parent );
294 if ( $grandparent ) {
295 $container->parent = $grandparent->id;
297 $index = array_search( $parent, $grandparent->children, true );
298 if ( $index === false )
299 $grandparent->children[] = $container;
301 array_splice( $grandparent->children, $index, 1, array( $container ) );
304 $parent->parent = $container->id;
307 $parent = $container;
310 // Update the parent ID (it might have changed).
311 $node->parent = $parent->id;
313 // Add the node to the tree.
314 $parent->children[] = $node;
317 $root = $this->_get_node( 'root' );
322 final protected function _render( $root ) {
323 global $is_IE, $is_iphone;
325 // Add browser classes.
326 // We have to do this here since admin bar shows on the front end.
327 $class = 'nojq nojs';
329 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
331 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
333 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
335 } elseif ( $is_iphone ) {
340 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
341 <div class="quicklinks">
342 <?php foreach ( $root->children as $group ) {
343 $this->_render_group( $group );
351 final protected function _render_container( $node ) {
352 if ( $node->type != 'container' || empty( $node->children ) )
355 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
356 foreach ( $node->children as $group ) {
357 $this->_render_group( $group );
362 final protected function _render_group( $node ) {
363 if ( $node->type == 'container' )
364 return $this->_render_container( $node );
366 if ( $node->type != 'group' || empty( $node->children ) )
369 $class = empty( $node->meta['class'] ) ? '' : $node->meta['class'];
371 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
372 foreach ( $node->children as $item ) {
373 $this->_render_item( $item );
378 final protected function _render_item( $node ) {
379 if ( $node->type != 'item' )
382 $is_parent = ! empty( $node->children );
383 $has_link = ! empty( $node->href );
385 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10;
388 $aria_attributes = 'tabindex="' . $tabindex . '"';
391 $menuclass = 'menupop';
392 $aria_attributes .= ' aria-haspopup="true"';
395 if ( ! empty( $node->meta['class'] ) )
396 $menuclass .= ' ' . $node->meta['class'];
400 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
402 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
403 if ( ! empty( $node->meta['onclick'] ) ) :
404 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
406 if ( ! empty( $node->meta['target'] ) ) :
407 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
409 if ( ! empty( $node->meta['title'] ) ) :
410 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
414 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
415 if ( ! empty( $node->meta['title'] ) ) :
416 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
430 ?><div class="ab-sub-wrapper"><?php
431 foreach ( $node->children as $group ) {
432 $this->_render_group( $group );
437 if ( ! empty( $node->meta['html'] ) )
438 echo $node->meta['html'];
444 public function recursive_render( $id, $node ) {
445 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
446 $this->_render_item( $node );
449 public function add_menus() {
450 // User related, aligned right.
451 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
452 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
453 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
456 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
457 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
458 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
459 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
462 if ( ! is_network_admin() && ! is_user_admin() ) {
463 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
464 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
466 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
468 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
470 do_action( 'add_admin_bar_menus' );