]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/admin-bar.php
Wordpress 3.2.1
[autoinstalls/wordpress.git] / wp-includes / admin-bar.php
1 <?php
2 /**
3  * Admin Bar
4  *
5  * This code handles the building and rendering of the press bar.
6  */
7
8 /**
9  * Instantiate the admin bar object and set it up as a global for access elsewhere.
10  *
11  * To hide the admin bar, you're looking in the wrong place. Unhooking this function will not
12  * properly remove the admin bar. For that, use show_admin_bar(false) or the show_admin_bar filter.
13  *
14  * @since 3.1.0
15  * @access private
16  * @return bool Whether the admin bar was successfully initialized.
17  */
18 function _wp_admin_bar_init() {
19         global $wp_admin_bar;
20
21         if ( ! is_admin_bar_showing() )
22                 return false;
23
24         /* Load the admin bar class code ready for instantiation */
25         require( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
26
27         /* Instantiate the admin bar */
28         $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
29         if ( class_exists( $admin_bar_class ) )
30                 $wp_admin_bar = new $admin_bar_class;
31         else
32                 return false;
33
34         $wp_admin_bar->initialize();
35         $wp_admin_bar->add_menus();
36
37         return true;
38 }
39 add_action( 'init', '_wp_admin_bar_init' ); // Don't remove. Wrong way to disable.
40
41 /**
42  * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
43  * This is called very late on the footer actions so that it will render after anything else being
44  * added to the footer.
45  *
46  * It includes the action "admin_bar_menu" which should be used to hook in and
47  * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
48  * right before the admin bar is rendered. This also gives you access to the $post global, among others.
49  *
50  * @since 3.1.0
51  */
52 function wp_admin_bar_render() {
53         global $wp_admin_bar;
54
55         if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
56                 return false;
57
58         $wp_admin_bar->load_user_locale_translations();
59
60         do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
61
62         do_action( 'wp_before_admin_bar_render' );
63
64         $wp_admin_bar->render();
65
66         do_action( 'wp_after_admin_bar_render' );
67
68         $wp_admin_bar->unload_user_locale_translations();
69 }
70 add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
71 add_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
72
73 /**
74  * Add the "My Account" menu and all submenus.
75  *
76  * @since 3.1.0
77  */
78 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
79         global $user_identity;
80
81         $user_id = get_current_user_id();
82
83         if ( 0 != $user_id ) {
84                 /* Add the 'My Account' menu */
85                 $avatar = get_avatar( get_current_user_id(), 16 );
86                 $id = ( ! empty( $avatar ) ) ? 'my-account-with-avatar' : 'my-account';
87
88                 $wp_admin_bar->add_menu( array( 'id' => $id, 'title' => $avatar . $user_identity,  'href' => get_edit_profile_url( $user_id ) ) );
89
90                 /* Add the "My Account" sub menus */
91                 $wp_admin_bar->add_menu( array( 'id' => 'edit-profile', 'parent' => $id, 'title' => __( 'Edit My Profile' ), 'href' => get_edit_profile_url( $user_id ) ) );
92                 $wp_admin_bar->add_menu( array( 'id' => 'logout', 'parent' => $id, 'title' => __( 'Log Out' ), 'href' => wp_logout_url() ) );
93         }
94 }
95
96 /**
97  * Add the "Dashboard"/"Visit Site" menu.
98  *
99  * @since 3.2.0
100  */
101 function wp_admin_bar_dashboard_view_site_menu( $wp_admin_bar ) {
102         $user_id = get_current_user_id();
103
104         if ( 0 != $user_id ) {
105                 if ( is_admin() )
106                         $wp_admin_bar->add_menu( array( 'id' => 'view-site', 'title' => __( 'Visit Site' ), 'href' => home_url() ) );
107                 elseif ( is_multisite() )
108                         $wp_admin_bar->add_menu( array( 'id' => 'dashboard', 'title' => __( 'Dashboard' ), 'href' => get_dashboard_url( $user_id ) ) );
109                 else
110                         $wp_admin_bar->add_menu( array( 'id' => 'dashboard', 'title' => __( 'Dashboard' ), 'href' => admin_url() ) );
111         }
112 }
113
114 /**
115  * Add the "My Sites/[Site Name]" menu and all submenus.
116  *
117  * @since 3.1.0
118  */
119 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
120         global $wpdb;
121
122         /* Add the 'My Sites' menu if the user has more than one site. */
123         if ( count( $wp_admin_bar->user->blogs ) <= 1 )
124                 return;
125
126         $wp_admin_bar->add_menu( array(  'id' => 'my-blogs', 'title' => __( 'My Sites' ),  'href' => admin_url( 'my-sites.php' ) ) );
127
128         $default = includes_url('images/wpmini-blue.png');
129
130         foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
131                 // @todo Replace with some favicon lookup.
132                 //$blavatar = '<img src="' . esc_url( blavatar_url( blavatar_domain( $blog->siteurl ), 'img', 16, $default ) ) . '" alt="Blavatar" width="16" height="16" />';
133                 $blavatar = '<img src="' . esc_url($default) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
134
135                 $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
136
137                 $wp_admin_bar->add_menu( array( 'parent' => 'my-blogs', 'id' => 'blog-' . $blog->userblog_id, 'title' => $blavatar . $blogname,  'href' => get_admin_url($blog->userblog_id) ) );
138                 $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-d', 'title' => __( 'Dashboard' ), 'href' => get_admin_url($blog->userblog_id) ) );
139
140                 if ( current_user_can_for_blog( $blog->userblog_id, 'edit_posts' ) ) {
141                         $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-n', 'title' => __( 'New Post' ), 'href' => get_admin_url($blog->userblog_id, 'post-new.php') ) );
142                         $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-c', 'title' => __( 'Manage Comments' ), 'href' => get_admin_url($blog->userblog_id, 'edit-comments.php') ) );
143                 }
144
145                 $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-v', 'title' => __( 'Visit Site' ), 'href' => get_home_url($blog->userblog_id) ) );
146         }
147 }
148
149 /**
150  * Provide a shortlink.
151  *
152  * @since 3.1.0
153  */
154 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
155         $short = wp_get_shortlink( 0, 'query' );
156         $id = 'get-shortlink';
157
158         if ( empty( $short ) )
159                 return;
160
161         $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
162
163         $wp_admin_bar->add_menu( array(
164                 'id' => $id,
165                 'title' => __( 'Shortlink' ),
166                 'href' => $short,
167                 'meta' => array( 'html' => $html ),
168         ) );
169 }
170
171 /**
172  * Provide an edit link for posts and terms.
173  *
174  * @since 3.1.0
175  */
176 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
177         global $post, $tag;
178
179         if ( is_admin() ) {
180                 $current_screen = get_current_screen();
181
182                 if ( 'post' == $current_screen->base
183                         && 'add' != $current_screen->action
184                         && ( $post_type_object = get_post_type_object( $post->post_type ) )
185                         && current_user_can( $post_type_object->cap->read_post, $post->ID )
186                         && ( $post_type_object->public ) )
187                 {
188                         $wp_admin_bar->add_menu( array(
189                                 'id' => 'view',
190                                 'title' => $post_type_object->labels->view_item,
191                                 'href' => get_permalink( $post->ID )
192                         ) );
193                 } elseif ( 'edit-tags' == $current_screen->base
194                         && isset( $tag ) && is_object( $tag )
195                         && ( $tax = get_taxonomy( $tag->taxonomy ) )
196                         && $tax->public )
197                 {
198                         $wp_admin_bar->add_menu( array(
199                                 'id' => 'view',
200                                 'title' => $tax->labels->view_item,
201                                 'href' => get_term_link( $tag )
202                         ) );
203                 }
204         } else {
205                 $current_object = get_queried_object();
206
207                 if ( empty($current_object) )
208                         return;
209
210                 if ( ! empty( $current_object->post_type )
211                         && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
212                         && current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
213                         && ( $post_type_object->show_ui || 'attachment' == $current_object->post_type ) )
214                 {
215                         $wp_admin_bar->add_menu( array(
216                                 'id' => 'edit',
217                                 'title' => $post_type_object->labels->edit_item,
218                                 'href' => get_edit_post_link( $current_object->ID )
219                         ) );
220                 } elseif ( ! empty( $current_object->taxonomy )
221                         && ( $tax = get_taxonomy( $current_object->taxonomy ) )
222                         && current_user_can( $tax->cap->edit_terms )
223                         && $tax->show_ui )
224                 {
225                         $wp_admin_bar->add_menu( array(
226                                 'id' => 'edit',
227                                 'title' => $tax->labels->edit_item,
228                                 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
229                         ) );
230                 }
231         }
232 }
233
234 /**
235  * Add "Add New" menu.
236  *
237  * @since 3.1.0
238  */
239 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
240         $actions = array();
241         foreach ( (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' ) as $ptype_obj ) {
242                 if ( ! current_user_can( $ptype_obj->cap->edit_posts ) )
243                         continue;
244
245                 $actions[ 'post-new.php?post_type=' . $ptype_obj->name ] = array( $ptype_obj->labels->name_admin_bar, $ptype_obj->cap->edit_posts, 'new-' . $ptype_obj->name );
246         }
247
248         if ( current_user_can( 'upload_files' ) )
249                 $actions[ 'media-new.php' ] = array( _x( 'Media', 'add new from admin bar' ), 'upload_files', 'new-media' );
250
251         if ( current_user_can( 'manage_links' ) )
252                 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'manage_links', 'new-link' );
253
254         if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
255                 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'create_users', 'new-user' );
256
257         if ( ! is_multisite() && current_user_can( 'install_themes' ) )
258                 $actions[ 'theme-install.php' ] = array( _x( 'Theme', 'add new from admin bar' ), 'install_themes', 'new-theme' );
259
260         if ( ! is_multisite() && current_user_can( 'install_plugins' ) )
261                 $actions[ 'plugin-install.php' ] = array( _x( 'Plugin', 'add new from admin bar' ), 'install_plugins', 'new-plugin' );
262
263         if ( empty( $actions ) )
264                 return;
265
266         $wp_admin_bar->add_menu( array( 'id' => 'new-content', 'title' => _x( 'Add New', 'admin bar menu group label' ), 'href' => admin_url( array_shift( array_keys( $actions ) ) ) ) );
267
268         foreach ( $actions as $link => $action ) {
269                 $wp_admin_bar->add_menu( array( 'parent' => 'new-content', 'id' => $action[2], 'title' => $action[0], 'href' => admin_url($link) ) );
270         }
271 }
272
273 /**
274  * Add edit comments link with awaiting moderation count bubble.
275  *
276  * @since 3.1.0
277  */
278 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
279         if ( !current_user_can('edit_posts') )
280                 return;
281
282         $awaiting_mod = wp_count_comments();
283         $awaiting_mod = $awaiting_mod->moderated;
284
285         $awaiting_mod = $awaiting_mod ? "<span id='ab-awaiting-mod' class='pending-count'>" . number_format_i18n( $awaiting_mod ) . "</span>" : '';
286         $wp_admin_bar->add_menu( array( 'id' => 'comments', 'title' => sprintf( __('Comments %s'), $awaiting_mod ), 'href' => admin_url('edit-comments.php') ) );
287 }
288
289 /**
290  * Add "Appearance" menu with widget and nav menu submenu.
291  *
292  * @since 3.1.0
293  */
294 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
295         // You can have edit_theme_options but not switch_themes.
296         if ( ! current_user_can('switch_themes') && ! current_user_can( 'edit_theme_options' ) )
297                 return;
298
299         $wp_admin_bar->add_menu( array( 'id' => 'appearance', 'title' => __('Appearance'), 'href' => admin_url('themes.php') ) );
300
301         if ( ! current_user_can( 'edit_theme_options' ) )
302                 return;
303
304         if ( current_user_can( 'switch_themes' ) )
305                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
306
307         if ( current_theme_supports( 'widgets' )  )
308                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
309
310          if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
311                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
312
313         if ( current_theme_supports( 'custom-background' ) )
314                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
315
316         if ( current_theme_supports( 'custom-header' ) )
317                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
318 }
319
320 /**
321  * Provide an update link if theme/plugin/core updates are available.
322  *
323  * @since 3.1.0
324  */
325 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
326         if ( !current_user_can('install_plugins') )
327                 return;
328
329         $plugin_update_count = $theme_update_count = $wordpress_update_count = 0;
330         $update_plugins = get_site_transient( 'update_plugins' );
331         if ( !empty($update_plugins->response) )
332                 $plugin_update_count = count( $update_plugins->response );
333         $update_themes = get_site_transient( 'update_themes' );
334         if ( !empty($update_themes->response) )
335                 $theme_update_count = count( $update_themes->response );
336         /* @todo get_core_updates() is only available on admin page loads
337         $update_wordpress = get_core_updates( array('dismissed' => false) );
338         if ( !empty($update_wordpress) && !in_array( $update_wordpress[0]->response, array('development', 'latest') ) )
339                 $wordpress_update_count = 1;
340         */
341
342         $update_count = $plugin_update_count + $theme_update_count + $wordpress_update_count;
343
344         if ( !$update_count )
345                 return;
346
347         $update_title = array();
348         if ( $wordpress_update_count )
349                 $update_title[] = sprintf(__('%d WordPress Update'), $wordpress_update_count);
350         if ( $plugin_update_count )
351                 $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $plugin_update_count), $plugin_update_count);
352         if ( $theme_update_count )
353                 $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $theme_update_count), $theme_update_count);
354
355         $update_title = !empty($update_title) ? esc_attr(implode(', ', $update_title)) : '';
356
357         $update_title = "<span title='$update_title'>";
358         $update_title .= sprintf( __('Updates %s'), "<span id='ab-updates' class='update-count'>" . number_format_i18n($update_count) . '</span>' );
359         $update_title .= '</span>';
360
361         $wp_admin_bar->add_menu( array( 'id' => 'updates', 'title' => $update_title, 'href' => network_admin_url( 'update-core.php' ) ) );
362 }
363
364 /**
365  * Style and scripts for the admin bar.
366  *
367  * @since 3.1.0
368  *
369  */
370 function wp_admin_bar_header() { ?>
371 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
372 <?php
373 }
374
375 /**
376  * Default admin bar callback.
377  *
378  * @since 3.1.0
379  *
380  */
381 function _admin_bar_bump_cb() { ?>
382 <style type="text/css" media="screen">
383         html { margin-top: 28px !important; }
384         * html body { margin-top: 28px !important; }
385 </style>
386 <?php
387 }
388
389 /**
390  * Set the display status of the admin bar.
391  *
392  * This can be called immediately upon plugin load.  It does not need to be called from a function hooked to the init action.
393  *
394  * @since 3.1.0
395  *
396  * @param bool $show Whether to allow the admin bar to show.
397  * @return void
398  */
399 function show_admin_bar( $show ) {
400         global $show_admin_bar;
401         $show_admin_bar = (bool) $show;
402 }
403
404 /**
405  * Determine whether the admin bar should be showing.
406  *
407  * @since 3.1.0
408  *
409  * @return bool Whether the admin bar should be showing.
410  */
411 function is_admin_bar_showing() {
412         global $show_admin_bar, $pagenow;
413
414         /* For all these types of request we never want an admin bar period */
415         if ( defined('XMLRPC_REQUEST') || defined('APP_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
416                 return false;
417
418         if ( ! isset( $show_admin_bar ) ) {
419                 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
420                         $show_admin_bar = false;
421                 } else {
422                         $context = is_admin() ? 'admin' : 'front';
423                         $show_admin_bar = _get_admin_bar_pref( $context );
424                 }
425         }
426
427         $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
428
429         return $show_admin_bar;
430 }
431
432 /**
433  * Retrieve the admin bar display preference of a user based on context.
434  *
435  * @since 3.1.0
436  * @access private
437  *
438  * @param string $context Context of this preference check, either 'admin' or 'front'.
439  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
440  * @return bool Whether the admin bar should be showing for this user.
441  */
442 function _get_admin_bar_pref( $context, $user = 0 ) {
443         $pref = get_user_option( "show_admin_bar_{$context}", $user );
444         if ( false === $pref )
445                 return 'admin' != $context || is_multisite();
446
447         return 'true' === $pref;
448 }
449
450 ?>