]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/admin-bar.php
WordPress 3.8
[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  * UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR.
12  * 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
29         /**
30          * Filter the admin bar class to instantiate.
31          *
32          * @since 3.1.0
33          *
34          * @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
35          */
36         $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
37         if ( class_exists( $admin_bar_class ) )
38                 $wp_admin_bar = new $admin_bar_class;
39         else
40                 return false;
41
42         $wp_admin_bar->initialize();
43         $wp_admin_bar->add_menus();
44
45         return true;
46 }
47 // Don't remove. Wrong way to disable.
48 add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
49 add_action( 'admin_init', '_wp_admin_bar_init' );
50
51 /**
52  * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
53  * This is called very late on the footer actions so that it will render after anything else being
54  * added to the footer.
55  *
56  * It includes the action "admin_bar_menu" which should be used to hook in and
57  * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
58  * right before the admin bar is rendered. This also gives you access to the $post global, among others.
59  *
60  * @since 3.1.0
61  */
62 function wp_admin_bar_render() {
63         global $wp_admin_bar;
64
65         if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
66                 return false;
67
68         /**
69          * Load all necessary admin bar items.
70          *
71          * This is the hook used to add, remove, or manipulate admin bar items.
72          *
73          * @since 3.1.0
74          *
75          * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
76          */
77         do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
78
79         /**
80          * Fires before the admin bar is rendered.
81          *
82          * @since 3.1.0
83          */
84         do_action( 'wp_before_admin_bar_render' );
85
86         $wp_admin_bar->render();
87
88         /**
89          * Fires after the admin bar is rendered.
90          *
91          * @since 3.1.0
92          */
93         do_action( 'wp_after_admin_bar_render' );
94 }
95 add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
96 add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
97
98 /**
99  * Add the WordPress logo menu.
100  *
101  * @since 3.3.0
102  *
103  * @param WP_Admin_Bar $wp_admin_bar
104  */
105 function wp_admin_bar_wp_menu( $wp_admin_bar ) {
106         $wp_admin_bar->add_menu( array(
107                 'id'    => 'wp-logo',
108                 'title' => '<span class="ab-icon"></span>',
109                 'href'  => self_admin_url( 'about.php' ),
110                 'meta'  => array(
111                         'title' => __('About WordPress'),
112                 ),
113         ) );
114
115         if ( is_user_logged_in() ) {
116                 // Add "About WordPress" link
117                 $wp_admin_bar->add_menu( array(
118                         'parent' => 'wp-logo',
119                         'id'     => 'about',
120                         'title'  => __('About WordPress'),
121                         'href'  => self_admin_url( 'about.php' ),
122                 ) );
123         }
124
125         // Add WordPress.org link
126         $wp_admin_bar->add_menu( array(
127                 'parent'    => 'wp-logo-external',
128                 'id'        => 'wporg',
129                 'title'     => __('WordPress.org'),
130                 'href'      => __('http://wordpress.org/'),
131         ) );
132
133         // Add codex link
134         $wp_admin_bar->add_menu( array(
135                 'parent'    => 'wp-logo-external',
136                 'id'        => 'documentation',
137                 'title'     => __('Documentation'),
138                 'href'      => __('http://codex.wordpress.org/'),
139         ) );
140
141         // Add forums link
142         $wp_admin_bar->add_menu( array(
143                 'parent'    => 'wp-logo-external',
144                 'id'        => 'support-forums',
145                 'title'     => __('Support Forums'),
146                 'href'      => __('http://wordpress.org/support/'),
147         ) );
148
149         // Add feedback link
150         $wp_admin_bar->add_menu( array(
151                 'parent'    => 'wp-logo-external',
152                 'id'        => 'feedback',
153                 'title'     => __('Feedback'),
154                 'href'      => __('http://wordpress.org/support/forum/requests-and-feedback'),
155         ) );
156 }
157
158 /**
159  * Add the sidebar toggle button.
160  *
161  * @since 3.8.0
162  *
163  * @param WP_Admin_Bar $wp_admin_bar
164  */
165 function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) {
166         if ( is_admin() ) {
167                 $wp_admin_bar->add_menu( array(
168                         'id'    => 'menu-toggle',
169                         'title' => '<span class="ab-icon"></span>',
170                         'href'  => '#',
171                         'meta'  => array(
172                                 'title' => __( 'Menu' ),
173                         ),
174                 ) );
175         }
176 }
177
178 /**
179  * Add the "My Account" item.
180  *
181  * @since 3.3.0
182  *
183  * @param WP_Admin_Bar $wp_admin_bar
184  */
185 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
186         $user_id      = get_current_user_id();
187         $current_user = wp_get_current_user();
188         $profile_url  = get_edit_profile_url( $user_id );
189
190         if ( ! $user_id )
191                 return;
192
193         $avatar = get_avatar( $user_id, 26 );
194         $howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
195         $class  = empty( $avatar ) ? '' : 'with-avatar';
196
197         $wp_admin_bar->add_menu( array(
198                 'id'        => 'my-account',
199                 'parent'    => 'top-secondary',
200                 'title'     => $howdy . $avatar,
201                 'href'      => $profile_url,
202                 'meta'      => array(
203                         'class'     => $class,
204                         'title'     => __('My Account'),
205                 ),
206         ) );
207 }
208
209 /**
210  * Add the "My Account" submenu items.
211  *
212  * @since 3.1.0
213  *
214  * @param WP_Admin_Bar $wp_admin_bar
215  */
216 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
217         $user_id      = get_current_user_id();
218         $current_user = wp_get_current_user();
219         $profile_url  = get_edit_profile_url( $user_id );
220
221         if ( ! $user_id )
222                 return;
223
224         $wp_admin_bar->add_group( array(
225                 'parent' => 'my-account',
226                 'id'     => 'user-actions',
227         ) );
228
229         $user_info  = get_avatar( $user_id, 64 );
230         $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
231
232         if ( $current_user->display_name !== $current_user->user_login )
233                 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
234
235         $wp_admin_bar->add_menu( array(
236                 'parent' => 'user-actions',
237                 'id'     => 'user-info',
238                 'title'  => $user_info,
239                 'href'   => $profile_url,
240                 'meta'   => array(
241                         'tabindex' => -1,
242                 ),
243         ) );
244         $wp_admin_bar->add_menu( array(
245                 'parent' => 'user-actions',
246                 'id'     => 'edit-profile',
247                 'title'  => __( 'Edit My Profile' ),
248                 'href' => $profile_url,
249         ) );
250         $wp_admin_bar->add_menu( array(
251                 'parent' => 'user-actions',
252                 'id'     => 'logout',
253                 'title'  => __( 'Log Out' ),
254                 'href'   => wp_logout_url(),
255         ) );
256 }
257
258 /**
259  * Add the "Site Name" menu.
260  *
261  * @since 3.3.0
262  *
263  * @param WP_Admin_Bar $wp_admin_bar
264  */
265 function wp_admin_bar_site_menu( $wp_admin_bar ) {
266         // Don't show for logged out users.
267         if ( ! is_user_logged_in() )
268                 return;
269
270         // Show only when the user is a member of this site, or they're a super admin.
271         if ( ! is_user_member_of_blog() && ! is_super_admin() )
272                 return;
273
274         $blogname = get_bloginfo('name');
275
276         if ( empty( $blogname ) )
277                 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
278
279         if ( is_network_admin() ) {
280                 $blogname = sprintf( __('Network Admin: %s'), esc_html( get_current_site()->site_name ) );
281         } elseif ( is_user_admin() ) {
282                 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( get_current_site()->site_name ) );
283         }
284
285         $title = wp_html_excerpt( $blogname, 40, '&hellip;' );
286
287         $wp_admin_bar->add_menu( array(
288                 'id'    => 'site-name',
289                 'title' => $title,
290                 'href'  => is_admin() ? home_url( '/' ) : admin_url(),
291         ) );
292
293         // Create submenu items.
294
295         if ( is_admin() ) {
296                 // Add an option to visit the site.
297                 $wp_admin_bar->add_menu( array(
298                         'parent' => 'site-name',
299                         'id'     => 'view-site',
300                         'title'  => __( 'Visit Site' ),
301                         'href'   => home_url( '/' ),
302                 ) );
303
304                 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
305                         $wp_admin_bar->add_menu( array(
306                                 'parent' => 'site-name',
307                                 'id'     => 'edit-site',
308                                 'title'  => __( 'Edit Site' ),
309                                 'href'   => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
310                         ) );
311                 }
312
313         } else {
314                 // We're on the front end, link to the Dashboard.
315                 $wp_admin_bar->add_menu( array(
316                         'parent' => 'site-name',
317                         'id'     => 'dashboard',
318                         'title'  => __( 'Dashboard' ),
319                         'href'   => admin_url(),
320                 ) );
321
322                 // Add the appearance submenu items.
323                 wp_admin_bar_appearance_menu( $wp_admin_bar );
324         }
325 }
326
327 /**
328  * Add the "My Sites/[Site Name]" menu and all submenus.
329  *
330  * @since 3.1.0
331  *
332  * @param WP_Admin_Bar $wp_admin_bar
333  */
334 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
335         // Don't show for logged out users or single site mode.
336         if ( ! is_user_logged_in() || ! is_multisite() )
337                 return;
338
339         // Show only when the user has at least one site, or they're a super admin.
340         if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
341                 return;
342
343         $wp_admin_bar->add_menu( array(
344                 'id'    => 'my-sites',
345                 'title' => __( 'My Sites' ),
346                 'href'  => admin_url( 'my-sites.php' ),
347         ) );
348
349         if ( is_super_admin() ) {
350                 $wp_admin_bar->add_group( array(
351                         'parent' => 'my-sites',
352                         'id'     => 'my-sites-super-admin',
353                 ) );
354
355                 $wp_admin_bar->add_menu( array(
356                         'parent' => 'my-sites-super-admin',
357                         'id'     => 'network-admin',
358                         'title'  => __('Network Admin'),
359                         'href'   => network_admin_url(),
360                 ) );
361
362                 $wp_admin_bar->add_menu( array(
363                         'parent' => 'network-admin',
364                         'id'     => 'network-admin-d',
365                         'title'  => __( 'Dashboard' ),
366                         'href'   => network_admin_url(),
367                 ) );
368                 $wp_admin_bar->add_menu( array(
369                         'parent' => 'network-admin',
370                         'id'     => 'network-admin-s',
371                         'title'  => __( 'Sites' ),
372                         'href'   => network_admin_url( 'sites.php' ),
373                 ) );
374                 $wp_admin_bar->add_menu( array(
375                         'parent' => 'network-admin',
376                         'id'     => 'network-admin-u',
377                         'title'  => __( 'Users' ),
378                         'href'   => network_admin_url( 'users.php' ),
379                 ) );
380                 $wp_admin_bar->add_menu( array(
381                         'parent' => 'network-admin',
382                         'id'     => 'network-admin-t',
383                         'title'  => __( 'Themes' ),
384                         'href'   => network_admin_url( 'themes.php' ),
385                 ) );
386                 $wp_admin_bar->add_menu( array(
387                         'parent' => 'network-admin',
388                         'id'     => 'network-admin-p',
389                         'title'  => __( 'Plugins' ),
390                         'href'   => network_admin_url( 'plugins.php' ),
391                 ) );
392         }
393
394         // Add site links
395         $wp_admin_bar->add_group( array(
396                 'parent' => 'my-sites',
397                 'id'     => 'my-sites-list',
398                 'meta'   => array(
399                         'class' => is_super_admin() ? 'ab-sub-secondary' : '',
400                 ),
401         ) );
402
403         foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
404                 switch_to_blog( $blog->userblog_id );
405
406                 $blavatar = '<div class="blavatar"></div>';
407
408                 $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
409                 $menu_id  = 'blog-' . $blog->userblog_id;
410
411                 $wp_admin_bar->add_menu( array(
412                         'parent'    => 'my-sites-list',
413                         'id'        => $menu_id,
414                         'title'     => $blavatar . $blogname,
415                         'href'      => admin_url(),
416                 ) );
417
418                 $wp_admin_bar->add_menu( array(
419                         'parent' => $menu_id,
420                         'id'     => $menu_id . '-d',
421                         'title'  => __( 'Dashboard' ),
422                         'href'   => admin_url(),
423                 ) );
424
425                 if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
426                         $wp_admin_bar->add_menu( array(
427                                 'parent' => $menu_id,
428                                 'id'     => $menu_id . '-n',
429                                 'title'  => __( 'New Post' ),
430                                 'href'   => admin_url( 'post-new.php' ),
431                         ) );
432                 }
433
434                 if ( current_user_can( 'edit_posts' ) ) {
435                         $wp_admin_bar->add_menu( array(
436                                 'parent' => $menu_id,
437                                 'id'     => $menu_id . '-c',
438                                 'title'  => __( 'Manage Comments' ),
439                                 'href'   => admin_url( 'edit-comments.php' ),
440                         ) );
441                 }
442
443                 $wp_admin_bar->add_menu( array(
444                         'parent' => $menu_id,
445                         'id'     => $menu_id . '-v',
446                         'title'  => __( 'Visit Site' ),
447                         'href'   => home_url( '/' ),
448                 ) );
449
450                 restore_current_blog();
451         }
452 }
453
454 /**
455  * Provide a shortlink.
456  *
457  * @since 3.1.0
458  *
459  * @param WP_Admin_Bar $wp_admin_bar
460  */
461 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
462         $short = wp_get_shortlink( 0, 'query' );
463         $id = 'get-shortlink';
464
465         if ( empty( $short ) )
466                 return;
467
468         $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
469
470         $wp_admin_bar->add_menu( array(
471                 'id' => $id,
472                 'title' => __( 'Shortlink' ),
473                 'href' => $short,
474                 'meta' => array( 'html' => $html ),
475         ) );
476 }
477
478 /**
479  * Provide an edit link for posts and terms.
480  *
481  * @since 3.1.0
482  *
483  * @param WP_Admin_Bar $wp_admin_bar
484  */
485 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
486         global $tag, $wp_the_query;
487
488         if ( is_admin() ) {
489                 $current_screen = get_current_screen();
490                 $post = get_post();
491
492                 if ( 'post' == $current_screen->base
493                         && 'add' != $current_screen->action
494                         && ( $post_type_object = get_post_type_object( $post->post_type ) )
495                         && current_user_can( 'read_post', $post->ID )
496                         && ( $post_type_object->public )
497                         && ( $post_type_object->show_in_admin_bar ) )
498                 {
499                         $wp_admin_bar->add_menu( array(
500                                 'id' => 'view',
501                                 'title' => $post_type_object->labels->view_item,
502                                 'href' => get_permalink( $post->ID )
503                         ) );
504                 } elseif ( 'edit-tags' == $current_screen->base
505                         && isset( $tag ) && is_object( $tag )
506                         && ( $tax = get_taxonomy( $tag->taxonomy ) )
507                         && $tax->public )
508                 {
509                         $wp_admin_bar->add_menu( array(
510                                 'id' => 'view',
511                                 'title' => $tax->labels->view_item,
512                                 'href' => get_term_link( $tag )
513                         ) );
514                 }
515         } else {
516                 $current_object = $wp_the_query->get_queried_object();
517
518                 if ( empty( $current_object ) )
519                         return;
520
521                 if ( ! empty( $current_object->post_type )
522                         && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
523                         && current_user_can( 'edit_post', $current_object->ID )
524                         && $post_type_object->show_ui && $post_type_object->show_in_admin_bar )
525                 {
526                         $wp_admin_bar->add_menu( array(
527                                 'id' => 'edit',
528                                 'title' => $post_type_object->labels->edit_item,
529                                 'href' => get_edit_post_link( $current_object->ID )
530                         ) );
531                 } elseif ( ! empty( $current_object->taxonomy )
532                         && ( $tax = get_taxonomy( $current_object->taxonomy ) )
533                         && current_user_can( $tax->cap->edit_terms )
534                         && $tax->show_ui )
535                 {
536                         $wp_admin_bar->add_menu( array(
537                                 'id' => 'edit',
538                                 'title' => $tax->labels->edit_item,
539                                 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
540                         ) );
541                 }
542         }
543 }
544
545 /**
546  * Add "Add New" menu.
547  *
548  * @since 3.1.0
549  *
550  * @param WP_Admin_Bar $wp_admin_bar
551  */
552 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
553         $actions = array();
554
555         $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
556
557         if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
558                 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
559
560         if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
561                 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
562
563         if ( current_user_can( 'manage_links' ) )
564                 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
565
566         if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
567                 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
568
569         unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
570
571         // Add any additional custom post types.
572         foreach ( $cpts as $cpt ) {
573                 if ( ! current_user_can( $cpt->cap->create_posts ) )
574                         continue;
575
576                 $key = 'post-new.php?post_type=' . $cpt->name;
577                 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
578         }
579         // Avoid clash with parent node and a 'content' post type.
580         if ( isset( $actions['post-new.php?post_type=content'] ) )
581                 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
582
583         if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
584                 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
585
586         if ( ! $actions )
587                 return;
588
589         $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
590
591         $wp_admin_bar->add_menu( array(
592                 'id'    => 'new-content',
593                 'title' => $title,
594                 'href'  => admin_url( current( array_keys( $actions ) ) ),
595                 'meta'  => array(
596                         'title' => _x( 'Add New', 'admin bar menu group label' ),
597                 ),
598         ) );
599
600         foreach ( $actions as $link => $action ) {
601                 list( $title, $id ) = $action;
602
603                 $wp_admin_bar->add_menu( array(
604                         'parent'    => 'new-content',
605                         'id'        => $id,
606                         'title'     => $title,
607                         'href'      => admin_url( $link )
608                 ) );
609         }
610 }
611
612 /**
613  * Add edit comments link with awaiting moderation count bubble.
614  *
615  * @since 3.1.0
616  *
617  * @param WP_Admin_Bar $wp_admin_bar
618  */
619 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
620         if ( !current_user_can('edit_posts') )
621                 return;
622
623         $awaiting_mod = wp_count_comments();
624         $awaiting_mod = $awaiting_mod->moderated;
625         $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
626
627         $icon  = '<span class="ab-icon"></span>';
628         $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
629
630         $wp_admin_bar->add_menu( array(
631                 'id'    => 'comments',
632                 'title' => $icon . $title,
633                 'href'  => admin_url('edit-comments.php'),
634                 'meta'  => array( 'title' => $awaiting_title ),
635         ) );
636 }
637
638 /**
639  * Add appearance submenu items to the "Site Name" menu.
640  *
641  * @since 3.1.0
642  *
643  * @param WP_Admin_Bar $wp_admin_bar
644  */
645 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
646         $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
647
648         if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
649                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
650
651         if ( ! current_user_can( 'edit_theme_options' ) )
652                 return;
653
654         $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
655         $wp_admin_bar->add_menu( array(
656                 'parent' => 'appearance',
657                 'id'     => 'customize',
658                 'title'  => __('Customize'),
659                 'href'   => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
660                 'meta'   => array(
661                         'class' => 'hide-if-no-customize',
662                 ),
663         ) );
664         add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
665
666         if ( current_theme_supports( 'widgets' )  )
667                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
668
669         if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
670                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
671
672         if ( current_theme_supports( 'custom-background' ) )
673                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
674
675         if ( current_theme_supports( 'custom-header' ) )
676                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
677 }
678
679 /**
680  * Provide an update link if theme/plugin/core updates are available.
681  *
682  * @since 3.1.0
683  *
684  * @param WP_Admin_Bar $wp_admin_bar
685  */
686 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
687
688         $update_data = wp_get_update_data();
689
690         if ( !$update_data['counts']['total'] )
691                 return;
692
693         $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
694         $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
695
696         $wp_admin_bar->add_menu( array(
697                 'id'    => 'updates',
698                 'title' => $title,
699                 'href'  => network_admin_url( 'update-core.php' ),
700                 'meta'  => array(
701                         'title' => $update_data['title'],
702                 ),
703         ) );
704 }
705
706 /**
707  * Add search form.
708  *
709  * @since 3.3.0
710  *
711  * @param WP_Admin_Bar $wp_admin_bar
712  */
713 function wp_admin_bar_search_menu( $wp_admin_bar ) {
714         if ( is_admin() )
715                 return;
716
717         $form  = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
718         $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
719         $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
720         $form .= '</form>';
721
722         $wp_admin_bar->add_menu( array(
723                 'parent' => 'top-secondary',
724                 'id'     => 'search',
725                 'title'  => $form,
726                 'meta'   => array(
727                         'class'    => 'admin-bar-search',
728                         'tabindex' => -1,
729                 )
730         ) );
731 }
732
733 /**
734  * Add secondary menus.
735  *
736  * @since 3.3.0
737  *
738  * @param WP_Admin_Bar $wp_admin_bar
739  */
740 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
741         $wp_admin_bar->add_group( array(
742                 'id'     => 'top-secondary',
743                 'meta'   => array(
744                         'class' => 'ab-top-secondary',
745                 ),
746         ) );
747
748         $wp_admin_bar->add_group( array(
749                 'parent' => 'wp-logo',
750                 'id'     => 'wp-logo-external',
751                 'meta'   => array(
752                         'class' => 'ab-sub-secondary',
753                 ),
754         ) );
755 }
756
757 /**
758  * Style and scripts for the admin bar.
759  *
760  * @since 3.1.0
761  */
762 function wp_admin_bar_header() { ?>
763 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
764 <?php
765 }
766
767 /**
768  * Default admin bar callback.
769  *
770  * @since 3.1.0
771  */
772 function _admin_bar_bump_cb() { ?>
773 <style type="text/css" media="screen">
774         html { margin-top: 32px !important; }
775         * html body { margin-top: 32px !important; }
776         @media screen and ( max-width: 782px ) {
777                 html { margin-top: 46px !important; }
778                 * html body { margin-top: 46px !important; }
779         }
780 </style>
781 <?php
782 }
783
784 /**
785  * Set the display status of the admin bar.
786  *
787  * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
788  *
789  * @since 3.1.0
790  *
791  * @param bool $show Whether to allow the admin bar to show.
792  * @return void
793  */
794 function show_admin_bar( $show ) {
795         global $show_admin_bar;
796         $show_admin_bar = (bool) $show;
797 }
798
799 /**
800  * Determine whether the admin bar should be showing.
801  *
802  * @since 3.1.0
803  *
804  * @return bool Whether the admin bar should be showing.
805  */
806 function is_admin_bar_showing() {
807         global $show_admin_bar, $pagenow;
808
809         // For all these types of requests, we never want an admin bar.
810         if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
811                 return false;
812
813         // Integrated into the admin.
814         if ( is_admin() )
815                 return true;
816
817         if ( ! isset( $show_admin_bar ) ) {
818                 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
819                         $show_admin_bar = false;
820                 } else {
821                         $show_admin_bar = _get_admin_bar_pref();
822                 }
823         }
824
825         /**
826          * Filter whether to show the admin bar.
827          *
828          * Returning false to this hook is the recommended way to hide the admin bar.
829          * The user's display preference is used for logged in users.
830          *
831          * @since 3.1.0
832          *
833          * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
834          */
835         $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
836
837         return $show_admin_bar;
838 }
839
840 /**
841  * Retrieve the admin bar display preference of a user.
842  *
843  * @since 3.1.0
844  * @access private
845  *
846  * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
847  *      preference is no longer used.
848  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
849  * @return bool Whether the admin bar should be showing for this user.
850  */
851 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
852         $pref = get_user_option( "show_admin_bar_{$context}", $user );
853         if ( false === $pref )
854                 return true;
855
856         return 'true' === $pref;
857 }