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();
200 $this->_render( $root );
203 final protected function _bind() {
207 // Add the root node.
208 // Clear it first, just in case. Don't mess with The Root.
209 $this->remove_node( 'root' );
210 $this->add_node( array(
215 // Normalize nodes: define internal 'children' and 'type' properties.
216 foreach ( $this->_get_nodes() as $node ) {
217 $node->children = array();
218 $node->type = ( $node->group ) ? 'group' : 'item';
219 unset( $node->group );
221 // The Root wants your orphans. No lonely items allowed.
222 if ( ! $node->parent )
223 $node->parent = 'root';
226 foreach ( $this->_get_nodes() as $node ) {
227 if ( 'root' == $node->id )
230 // Fetch the parent node. If it isn't registered, ignore the node.
231 if ( ! $parent = $this->_get_node( $node->parent ) ) {
235 // Generate the group class (we distinguish between top level and other level groups).
236 $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
238 if ( $node->type == 'group' ) {
239 if ( empty( $node->meta['class'] ) )
240 $node->meta['class'] = '';
241 $node->meta['class'] .= ' ' . $group_class;
244 // Items in items aren't allowed. Wrap nested items in 'default' groups.
245 if ( $parent->type == 'item' && $node->type == 'item' ) {
246 $default_id = $parent->id . '-default';
247 $default = $this->_get_node( $default_id );
249 // The default group is added here to allow groups that are
250 // added before standard menu items to render first.
252 // Use _set_node because add_node can be overloaded.
253 // Make sure to specify default settings for all properties.
254 $this->_set_node( array(
256 'parent' => $parent->id,
258 'children' => array(),
260 'class' => $group_class,
265 $default = $this->_get_node( $default_id );
266 $parent->children[] = $default;
270 // Groups in groups aren't allowed. Add a special 'container' node.
271 // The container will invisibly wrap both groups.
272 } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
273 $container_id = $parent->id . '-container';
274 $container = $this->_get_node( $container_id );
276 // We need to create a container for this group, life is sad.
277 if ( ! $container ) {
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(
281 'id' => $container_id,
282 'type' => 'container',
283 'children' => array( $parent ),
290 $container = $this->_get_node( $container_id );
292 // Link the container node if a grandparent node exists.
293 $grandparent = $this->_get_node( $parent->parent );
295 if ( $grandparent ) {
296 $container->parent = $grandparent->id;
298 $index = array_search( $parent, $grandparent->children, true );
299 if ( $index === false )
300 $grandparent->children[] = $container;
302 array_splice( $grandparent->children, $index, 1, array( $container ) );
305 $parent->parent = $container->id;
308 $parent = $container;
311 // Update the parent ID (it might have changed).
312 $node->parent = $parent->id;
314 // Add the node to the tree.
315 $parent->children[] = $node;
318 $root = $this->_get_node( 'root' );
323 final protected function _render( $root ) {
324 global $is_IE, $is_iphone;
326 // Add browser classes.
327 // We have to do this here since admin bar shows on the front end.
328 $class = 'nojq nojs';
330 if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
332 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
334 elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
336 } elseif ( $is_iphone ) {
341 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
342 <div class="quicklinks">
343 <?php foreach ( $root->children as $group ) {
344 $this->_render_group( $group );
352 final protected function _render_container( $node ) {
353 if ( $node->type != 'container' || empty( $node->children ) )
356 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
357 foreach ( $node->children as $group ) {
358 $this->_render_group( $group );
363 final protected function _render_group( $node ) {
364 if ( $node->type == 'container' )
365 return $this->_render_container( $node );
367 if ( $node->type != 'group' || empty( $node->children ) )
370 $class = empty( $node->meta['class'] ) ? '' : $node->meta['class'];
372 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $class ); ?>"><?php
373 foreach ( $node->children as $item ) {
374 $this->_render_item( $item );
379 final protected function _render_item( $node ) {
380 if ( $node->type != 'item' )
383 $is_parent = ! empty( $node->children );
384 $has_link = ! empty( $node->href );
386 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : 10;
389 $aria_attributes = 'tabindex="' . $tabindex . '"';
392 $menuclass = 'menupop';
393 $aria_attributes .= ' aria-haspopup="true"';
396 if ( ! empty( $node->meta['class'] ) )
397 $menuclass .= ' ' . $node->meta['class'];
401 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="<?php echo esc_attr( $menuclass ); ?>"><?php
403 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
404 if ( ! empty( $node->meta['onclick'] ) ) :
405 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
407 if ( ! empty( $node->meta['target'] ) ) :
408 ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
410 if ( ! empty( $node->meta['title'] ) ) :
411 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
415 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
416 if ( ! empty( $node->meta['title'] ) ) :
417 ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
431 ?><div class="ab-sub-wrapper"><?php
432 foreach ( $node->children as $group ) {
433 $this->_render_group( $group );
438 if ( ! empty( $node->meta['html'] ) )
439 echo $node->meta['html'];
445 public function recursive_render( $id, $node ) {
446 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
447 $this->_render_item( $node );
450 public function add_menus() {
451 // User related, aligned right.
452 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
453 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
454 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
457 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
458 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
459 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
460 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
463 if ( ! is_network_admin() && ! is_user_admin() ) {
464 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
465 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
467 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
469 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
471 do_action( 'add_admin_bar_menus' );