3 var $changed_locale = false;
5 var $need_to_change_locale = false;
6 var $proto = 'http://';
9 function initialize() {
10 /* Set the protocol used throughout this code */
12 $this->proto = 'https://';
14 $this->user = new stdClass;
15 $this->menu = new stdClass;
17 /* Populate settings we need for the menu based on the current user. */
18 $this->user->blogs = get_blogs_of_user( get_current_user_id() );
19 if ( is_multisite() ) {
20 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
21 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
22 $this->user->account_domain = $this->user->domain;
24 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
25 $this->user->domain = trailingslashit( home_url() );
26 $this->user->account_domain = $this->user->domain;
28 $this->user->locale = get_locale();
30 add_action( 'wp_head', 'wp_admin_bar_header' );
32 add_action( 'admin_head', 'wp_admin_bar_header' );
34 if ( current_theme_supports( 'admin-bar' ) ) {
35 $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
36 $header_callback = $admin_bar_args[0]['callback'];
39 if ( empty($header_callback) )
40 $header_callback = '_admin_bar_bump_cb';
42 add_action('wp_head', $header_callback);
44 wp_enqueue_script( 'admin-bar' );
45 wp_enqueue_style( 'admin-bar' );
47 do_action( 'admin_bar_init' );
50 function add_menu( $args = array() ) {
54 'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
55 'id' => false, // defaults to a sanitized title value.
56 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
59 $r = wp_parse_args( $args, $defaults );
60 extract( $r, EXTR_SKIP );
62 if ( empty( $title ) )
65 /* Make sure we have a valid ID */
67 $id = esc_attr( sanitize_title( trim( $title ) ) );
69 if ( ! empty( $parent ) ) {
70 /* Add the menu to the parent item */
71 $child = array( 'id' => $id, 'title' => $title, 'href' => $href );
73 if ( ! empty( $meta ) )
74 $child['meta'] = $meta;
76 $this->add_node( $parent, $this->menu, $child );
78 /* Add the menu item */
79 $this->menu->{$id} = array( 'title' => $title, 'href' => $href );
81 if ( ! empty( $meta ) )
82 $this->menu->{$id}['meta'] = $meta;
86 function remove_menu( $id ) {
87 return $this->remove_node( $id, $this->menu );
93 <div class="quicklinks">
95 <?php foreach ( (array) $this->menu as $id => $menu_item ) : ?>
96 <?php $this->recursive_render( $id, $menu_item ) ?>
101 <div id="adminbarsearch-wrap">
102 <form action="<?php echo home_url(); ?>" method="get" id="adminbarsearch">
103 <input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />
104 <input type="submit" class="adminbar-button" value="<?php _e('Search'); ?>"/>
110 /* Wipe the menu, might reduce memory usage, but probably not. */
115 function recursive_render( $id, &$menu_item ) { ?>
117 $is_parent = ! empty( $menu_item['children'] );
119 $menuclass = $is_parent ? 'menupop' : '';
120 if ( ! empty( $menu_item['meta']['class'] ) )
121 $menuclass .= ' ' . $menu_item['meta']['class'];
124 <li id="<?php echo esc_attr( "wp-admin-bar-$id" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
125 <a href="<?php echo esc_url( $menu_item['href'] ) ?>"<?php
126 if ( ! empty( $menu_item['meta']['onclick'] ) ) :
127 ?> onclick="<?php echo esc_js( $menu_item['meta']['onclick'] ); ?>"<?php
129 if ( ! empty( $menu_item['meta']['target'] ) ) :
130 ?> target="<?php echo esc_attr( $menu_item['meta']['target'] ); ?>"<?php
132 if ( ! empty( $menu_item['meta']['title'] ) ) :
133 ?> title="<?php echo esc_attr( $menu_item['meta']['title'] ); ?>"<?php
142 echo $menu_item['title'];
150 <?php if ( $is_parent ) : ?>
152 <?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?>
153 <?php $this->recursive_render( $child_id, $child_menu_item ); ?>
158 <?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?>
159 <?php echo $menu_item['meta']['html']; ?>
164 function add_node( $parent_id, &$menu, $child ) {
165 foreach( $menu as $id => $menu_item ) {
166 if ( $parent_id == $id ) {
167 $menu->{$parent_id}['children']->{$child['id']} = $child;
172 if ( ! empty( $menu->{$id}['children'] ) )
173 $this->add_node( $parent_id, $menu->{$id}['children'], $child );
181 function add_menus() {
182 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
183 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
184 add_action( 'admin_bar_menu', 'wp_admin_bar_dashboard_view_site_menu', 25 );
185 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 30 );
186 add_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 80 );
187 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 70 );
189 if ( !is_network_admin() && !is_user_admin() ) {
190 add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 40 );
191 add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 50 );
192 add_action( 'admin_bar_menu', 'wp_admin_bar_appearance_menu', 60 );
195 do_action( 'add_admin_bar_menus' );
198 function remove_node( $id, &$menu ) {
199 if ( isset( $menu->$id ) ) {
204 foreach( $menu as $menu_item_id => $menu_item ) {
205 if ( ! empty( $menu->{$menu_item_id}['children'] ) )
206 $this->remove_node( $id, $menu->{$menu_item_id}['children'] );
212 // TODO: Convert to a core feature for multisite or remove
213 function load_user_locale_translations() {
214 $this->need_to_change_locale = ( get_locale() != $this->user->locale );
215 if ( ! $this->need_to_change_locale )
218 $this->previous_translations = get_translations_for_domain( 'default' );
219 $this->adminbar_locale_filter = lambda( '$_', '$GLOBALS["wp_admin_bar"]->user->locale;' );
220 unload_textdomain( 'default' );
221 add_filter( 'locale', $this->adminbar_locale_filter );
222 load_default_textdomain();
223 $this->changed_locale = true;
227 function unload_user_locale_translations() {
229 if ( ! $this->changed_locale )
232 remove_filter( 'locale', $this->adminbar_locale_filter );
233 $l10n['default'] = &$this->previous_translations;