Wordpress 3.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-admin-bar.php
1 <?php
2 class WP_Admin_Bar {
3         var $changed_locale = false;
4         var $menu;
5         var $need_to_change_locale = false;
6         var $proto = 'http://';
7         var $user;
8
9         function initialize() {
10                 /* Set the protocol used throughout this code */
11                 if ( is_ssl() )
12                         $this->proto = 'https://';
13
14                 $this->user = new stdClass;
15                 $this->menu = new stdClass;
16
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;
23                 } else {
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;
27                 }
28                 $this->user->locale = get_locale();
29
30                 add_action( 'wp_head', 'wp_admin_bar_header' );
31
32                 add_action( 'admin_head', 'wp_admin_bar_header' );
33
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'];
37                 }
38
39                 if ( empty($header_callback) )
40                         $header_callback = '_admin_bar_bump_cb';
41
42                 add_action('wp_head', $header_callback);
43
44                 wp_enqueue_script( 'admin-bar' );
45                 wp_enqueue_style( 'admin-bar' );
46
47                 do_action( 'admin_bar_init' );
48         }
49
50         function add_menu( $args = array() ) {
51                 $defaults = array(
52                         'title' => false,
53                         'href' => false,
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 => '' );
57                 );
58
59                 $r = wp_parse_args( $args, $defaults );
60                 extract( $r, EXTR_SKIP );
61
62                 if ( empty( $title ) )
63                         return false;
64
65                 /* Make sure we have a valid ID */
66                 if ( empty( $id ) )
67                         $id = esc_attr( sanitize_title( trim( $title ) ) );
68
69                 if ( ! empty( $parent ) ) {
70                         /* Add the menu to the parent item */
71                         $child = array( 'id' => $id, 'title' => $title, 'href' => $href );
72
73                         if ( ! empty( $meta ) )
74                                 $child['meta'] = $meta;
75
76                         $this->add_node( $parent, $this->menu, $child );
77                 } else {
78                         /* Add the menu item */
79                         $this->menu->{$id} = array( 'title' => $title, 'href' => $href );
80
81                         if ( ! empty( $meta ) )
82                                 $this->menu->{$id}['meta'] = $meta;
83                 }
84         }
85
86         function remove_menu( $id ) {
87                 return $this->remove_node( $id, $this->menu );
88         }
89
90         function render() {
91                 ?>
92                 <div id="wpadminbar">
93                         <div class="quicklinks">
94                                 <ul>
95                                         <?php foreach ( (array) $this->menu as $id => $menu_item ) : ?>
96                                                 <?php $this->recursive_render( $id, $menu_item ) ?>
97                                         <?php endforeach; ?>
98                                 </ul>
99                         </div>
100
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'); ?>"/>
105                                 </form>
106                         </div>
107                 </div>
108
109                 <?php
110                 /* Wipe the menu, might reduce memory usage, but probably not. */
111                 $this->menu = null;
112         }
113
114         /* Helpers */
115         function recursive_render( $id, &$menu_item ) { ?>
116                 <?php
117                 $is_parent =  ! empty( $menu_item['children'] );
118
119                 $menuclass = $is_parent ? 'menupop' : '';
120                 if ( ! empty( $menu_item['meta']['class'] ) )
121                         $menuclass .= ' ' . $menu_item['meta']['class'];
122                 ?>
123
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
128                                 endif;
129                         if ( ! empty( $menu_item['meta']['target'] ) ) :
130                                 ?> target="<?php echo esc_attr( $menu_item['meta']['target'] ); ?>"<?php
131                         endif;
132                         if ( ! empty( $menu_item['meta']['title'] ) ) :
133                                 ?> title="<?php echo esc_attr( $menu_item['meta']['title'] ); ?>"<?php
134                         endif;
135
136                         ?>><?php
137
138                         if ( $is_parent ) :
139                                 ?><span><?php
140                         endif;
141
142                         echo $menu_item['title'];
143
144                         if ( $is_parent ) :
145                                 ?></span><?php
146                         endif;
147
148                         ?></a>
149
150                         <?php if ( $is_parent ) : ?>
151                         <ul>
152                                 <?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?>
153                                         <?php $this->recursive_render( $child_id, $child_menu_item ); ?>
154                                 <?php endforeach; ?>
155                         </ul>
156                         <?php endif; ?>
157
158                         <?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?>
159                                 <?php echo $menu_item['meta']['html']; ?>
160                         <?php endif; ?>
161                 </li><?php
162         }
163
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;
168                                 $child = null;
169                                 return true;
170                         }
171
172                         if ( ! empty( $menu->{$id}['children'] ) )
173                                 $this->add_node( $parent_id, $menu->{$id}['children'], $child );
174                 }
175
176                 $child = null;
177
178                 return false;
179         }
180
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 );
188
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 );
193                 }
194
195                 do_action( 'add_admin_bar_menus' );
196         }
197
198         function remove_node( $id, &$menu ) {
199                 if ( isset( $menu->$id ) ) {
200                         unset( $menu->$id );
201                         return true;
202                 }
203
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'] );
207                 }
208
209                 return false;
210         }
211
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 )
216                         return;
217                 /*
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;
224                 */
225         }
226
227         function unload_user_locale_translations() {
228                 global $l10n;
229                 if ( ! $this->changed_locale )
230                         return;
231                 /*
232                 remove_filter( 'locale', $this->adminbar_locale_filter );
233                 $l10n['default'] = &$this->previous_translations;
234                 */
235         }
236 }
237 ?>