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