]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/admin-bar.php
WordPress 4.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  * 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'      => __('https://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'      => __('https://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'      => __('https://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><span class="screen-reader-text">' . __( 'Menu' ) . '</span>',
170                         'href'  => '#',
171                 ) );
172         }
173 }
174
175 /**
176  * Add the "My Account" item.
177  *
178  * @since 3.3.0
179  *
180  * @param WP_Admin_Bar $wp_admin_bar
181  */
182 function wp_admin_bar_my_account_item( $wp_admin_bar ) {
183         $user_id      = get_current_user_id();
184         $current_user = wp_get_current_user();
185         $profile_url  = get_edit_profile_url( $user_id );
186
187         if ( ! $user_id )
188                 return;
189
190         $avatar = get_avatar( $user_id, 26 );
191         $howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
192         $class  = empty( $avatar ) ? '' : 'with-avatar';
193
194         $wp_admin_bar->add_menu( array(
195                 'id'        => 'my-account',
196                 'parent'    => 'top-secondary',
197                 'title'     => $howdy . $avatar,
198                 'href'      => $profile_url,
199                 'meta'      => array(
200                         'class'     => $class,
201                         'title'     => __('My Account'),
202                 ),
203         ) );
204 }
205
206 /**
207  * Add the "My Account" submenu items.
208  *
209  * @since 3.1.0
210  *
211  * @param WP_Admin_Bar $wp_admin_bar
212  */
213 function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
214         $user_id      = get_current_user_id();
215         $current_user = wp_get_current_user();
216         $profile_url  = get_edit_profile_url( $user_id );
217
218         if ( ! $user_id )
219                 return;
220
221         $wp_admin_bar->add_group( array(
222                 'parent' => 'my-account',
223                 'id'     => 'user-actions',
224         ) );
225
226         $user_info  = get_avatar( $user_id, 64 );
227         $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
228
229         if ( $current_user->display_name !== $current_user->user_login )
230                 $user_info .= "<span class='username'>{$current_user->user_login}</span>";
231
232         $wp_admin_bar->add_menu( array(
233                 'parent' => 'user-actions',
234                 'id'     => 'user-info',
235                 'title'  => $user_info,
236                 'href'   => $profile_url,
237                 'meta'   => array(
238                         'tabindex' => -1,
239                 ),
240         ) );
241         $wp_admin_bar->add_menu( array(
242                 'parent' => 'user-actions',
243                 'id'     => 'edit-profile',
244                 'title'  => __( 'Edit My Profile' ),
245                 'href' => $profile_url,
246         ) );
247         $wp_admin_bar->add_menu( array(
248                 'parent' => 'user-actions',
249                 'id'     => 'logout',
250                 'title'  => __( 'Log Out' ),
251                 'href'   => wp_logout_url(),
252         ) );
253 }
254
255 /**
256  * Add the "Site Name" menu.
257  *
258  * @since 3.3.0
259  *
260  * @param WP_Admin_Bar $wp_admin_bar
261  */
262 function wp_admin_bar_site_menu( $wp_admin_bar ) {
263         // Don't show for logged out users.
264         if ( ! is_user_logged_in() )
265                 return;
266
267         // Show only when the user is a member of this site, or they're a super admin.
268         if ( ! is_user_member_of_blog() && ! is_super_admin() )
269                 return;
270
271         $blogname = get_bloginfo('name');
272
273         if ( ! $blogname ) {
274                 $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
275         }
276
277         if ( is_network_admin() ) {
278                 $blogname = sprintf( __('Network Admin: %s'), esc_html( get_current_site()->site_name ) );
279         } elseif ( is_user_admin() ) {
280                 $blogname = sprintf( __('Global Dashboard: %s'), esc_html( get_current_site()->site_name ) );
281         }
282
283         $title = wp_html_excerpt( $blogname, 40, '&hellip;' );
284
285         $wp_admin_bar->add_menu( array(
286                 'id'    => 'site-name',
287                 'title' => $title,
288                 'href'  => is_admin() ? home_url( '/' ) : admin_url(),
289         ) );
290
291         // Create submenu items.
292
293         if ( is_admin() ) {
294                 // Add an option to visit the site.
295                 $wp_admin_bar->add_menu( array(
296                         'parent' => 'site-name',
297                         'id'     => 'view-site',
298                         'title'  => __( 'Visit Site' ),
299                         'href'   => home_url( '/' ),
300                 ) );
301
302                 if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
303                         $wp_admin_bar->add_menu( array(
304                                 'parent' => 'site-name',
305                                 'id'     => 'edit-site',
306                                 'title'  => __( 'Edit Site' ),
307                                 'href'   => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
308                         ) );
309                 }
310
311         } else {
312                 // We're on the front end, link to the Dashboard.
313                 $wp_admin_bar->add_menu( array(
314                         'parent' => 'site-name',
315                         'id'     => 'dashboard',
316                         'title'  => __( 'Dashboard' ),
317                         'href'   => admin_url(),
318                 ) );
319
320                 // Add the appearance submenu items.
321                 wp_admin_bar_appearance_menu( $wp_admin_bar );
322         }
323 }
324
325 /**
326  * Add the "My Sites/[Site Name]" menu and all submenus.
327  *
328  * @since 3.1.0
329  *
330  * @param WP_Admin_Bar $wp_admin_bar
331  */
332 function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
333         // Don't show for logged out users or single site mode.
334         if ( ! is_user_logged_in() || ! is_multisite() )
335                 return;
336
337         // Show only when the user has at least one site, or they're a super admin.
338         if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
339                 return;
340
341         $wp_admin_bar->add_menu( array(
342                 'id'    => 'my-sites',
343                 'title' => __( 'My Sites' ),
344                 'href'  => admin_url( 'my-sites.php' ),
345         ) );
346
347         if ( is_super_admin() ) {
348                 $wp_admin_bar->add_group( array(
349                         'parent' => 'my-sites',
350                         'id'     => 'my-sites-super-admin',
351                 ) );
352
353                 $wp_admin_bar->add_menu( array(
354                         'parent' => 'my-sites-super-admin',
355                         'id'     => 'network-admin',
356                         'title'  => __('Network Admin'),
357                         'href'   => network_admin_url(),
358                 ) );
359
360                 $wp_admin_bar->add_menu( array(
361                         'parent' => 'network-admin',
362                         'id'     => 'network-admin-d',
363                         'title'  => __( 'Dashboard' ),
364                         'href'   => network_admin_url(),
365                 ) );
366                 $wp_admin_bar->add_menu( array(
367                         'parent' => 'network-admin',
368                         'id'     => 'network-admin-s',
369                         'title'  => __( 'Sites' ),
370                         'href'   => network_admin_url( 'sites.php' ),
371                 ) );
372                 $wp_admin_bar->add_menu( array(
373                         'parent' => 'network-admin',
374                         'id'     => 'network-admin-u',
375                         'title'  => __( 'Users' ),
376                         'href'   => network_admin_url( 'users.php' ),
377                 ) );
378                 $wp_admin_bar->add_menu( array(
379                         'parent' => 'network-admin',
380                         'id'     => 'network-admin-t',
381                         'title'  => __( 'Themes' ),
382                         'href'   => network_admin_url( 'themes.php' ),
383                 ) );
384                 $wp_admin_bar->add_menu( array(
385                         'parent' => 'network-admin',
386                         'id'     => 'network-admin-p',
387                         'title'  => __( 'Plugins' ),
388                         'href'   => network_admin_url( 'plugins.php' ),
389                 ) );
390         }
391
392         // Add site links
393         $wp_admin_bar->add_group( array(
394                 'parent' => 'my-sites',
395                 'id'     => 'my-sites-list',
396                 'meta'   => array(
397                         'class' => is_super_admin() ? 'ab-sub-secondary' : '',
398                 ),
399         ) );
400
401         foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
402                 switch_to_blog( $blog->userblog_id );
403
404                 $blavatar = '<div class="blavatar"></div>';
405
406                 $blogname = $blog->blogname;
407
408                 if ( ! $blogname ) {
409                         $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
410                 }
411
412                 $menu_id  = 'blog-' . $blog->userblog_id;
413
414                 $wp_admin_bar->add_menu( array(
415                         'parent'    => 'my-sites-list',
416                         'id'        => $menu_id,
417                         'title'     => $blavatar . $blogname,
418                         'href'      => admin_url(),
419                 ) );
420
421                 $wp_admin_bar->add_menu( array(
422                         'parent' => $menu_id,
423                         'id'     => $menu_id . '-d',
424                         'title'  => __( 'Dashboard' ),
425                         'href'   => admin_url(),
426                 ) );
427
428                 if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
429                         $wp_admin_bar->add_menu( array(
430                                 'parent' => $menu_id,
431                                 'id'     => $menu_id . '-n',
432                                 'title'  => __( 'New Post' ),
433                                 'href'   => admin_url( 'post-new.php' ),
434                         ) );
435                 }
436
437                 if ( current_user_can( 'edit_posts' ) ) {
438                         $wp_admin_bar->add_menu( array(
439                                 'parent' => $menu_id,
440                                 'id'     => $menu_id . '-c',
441                                 'title'  => __( 'Manage Comments' ),
442                                 'href'   => admin_url( 'edit-comments.php' ),
443                         ) );
444                 }
445
446                 $wp_admin_bar->add_menu( array(
447                         'parent' => $menu_id,
448                         'id'     => $menu_id . '-v',
449                         'title'  => __( 'Visit Site' ),
450                         'href'   => home_url( '/' ),
451                 ) );
452
453                 restore_current_blog();
454         }
455 }
456
457 /**
458  * Provide a shortlink.
459  *
460  * @since 3.1.0
461  *
462  * @param WP_Admin_Bar $wp_admin_bar
463  */
464 function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
465         $short = wp_get_shortlink( 0, 'query' );
466         $id = 'get-shortlink';
467
468         if ( empty( $short ) )
469                 return;
470
471         $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
472
473         $wp_admin_bar->add_menu( array(
474                 'id' => $id,
475                 'title' => __( 'Shortlink' ),
476                 'href' => $short,
477                 'meta' => array( 'html' => $html ),
478         ) );
479 }
480
481 /**
482  * Provide an edit link for posts and terms.
483  *
484  * @since 3.1.0
485  *
486  * @param WP_Admin_Bar $wp_admin_bar
487  */
488 function wp_admin_bar_edit_menu( $wp_admin_bar ) {
489         global $tag, $wp_the_query;
490
491         if ( is_admin() ) {
492                 $current_screen = get_current_screen();
493                 $post = get_post();
494
495                 if ( 'post' == $current_screen->base
496                         && 'add' != $current_screen->action
497                         && ( $post_type_object = get_post_type_object( $post->post_type ) )
498                         && current_user_can( 'read_post', $post->ID )
499                         && ( $post_type_object->public )
500                         && ( $post_type_object->show_in_admin_bar ) )
501                 {
502                         if( 'draft' == $post->post_status ) {
503                                 $preview_link = set_url_scheme( get_permalink( $post->ID ) );
504                                 /** This filter is documented in wp-admin/includes/meta-boxes.php */
505                                 $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
506                                 $wp_admin_bar->add_menu( array(
507                                         'id' => 'preview',
508                                         'title' => $post_type_object->labels->view_item,
509                                         'href' => esc_url( $preview_link ),
510                                         'meta' => array( 'target' => 'wp-preview-' . $post->ID ),
511                                 ) );
512                         } else {
513                                 $wp_admin_bar->add_menu( array(
514                                         'id' => 'view',
515                                         'title' => $post_type_object->labels->view_item,
516                                         'href' => get_permalink( $post->ID )
517                                 ) );
518                         }
519                 } elseif ( 'edit-tags' == $current_screen->base
520                         && isset( $tag ) && is_object( $tag )
521                         && ( $tax = get_taxonomy( $tag->taxonomy ) )
522                         && $tax->public )
523                 {
524                         $wp_admin_bar->add_menu( array(
525                                 'id' => 'view',
526                                 'title' => $tax->labels->view_item,
527                                 'href' => get_term_link( $tag )
528                         ) );
529                 }
530         } else {
531                 $current_object = $wp_the_query->get_queried_object();
532
533                 if ( empty( $current_object ) )
534                         return;
535
536                 if ( ! empty( $current_object->post_type )
537                         && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
538                         && current_user_can( 'edit_post', $current_object->ID )
539                         && $post_type_object->show_ui && $post_type_object->show_in_admin_bar
540                         && $edit_post_link = get_edit_post_link( $current_object->ID ) )
541                 {
542                         $wp_admin_bar->add_menu( array(
543                                 'id' => 'edit',
544                                 'title' => $post_type_object->labels->edit_item,
545                                 'href' => $edit_post_link
546                         ) );
547                 } elseif ( ! empty( $current_object->taxonomy )
548                         && ( $tax = get_taxonomy( $current_object->taxonomy ) )
549                         && current_user_can( $tax->cap->edit_terms )
550                         && $tax->show_ui
551                         && $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ) )
552                 {
553                         $wp_admin_bar->add_menu( array(
554                                 'id' => 'edit',
555                                 'title' => $tax->labels->edit_item,
556                                 'href' => $edit_term_link
557                         ) );
558                 }
559         }
560 }
561
562 /**
563  * Add "Add New" menu.
564  *
565  * @since 3.1.0
566  *
567  * @param WP_Admin_Bar $wp_admin_bar
568  */
569 function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
570         $actions = array();
571
572         $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
573
574         if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
575                 $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
576
577         if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
578                 $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
579
580         if ( current_user_can( 'manage_links' ) )
581                 $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
582
583         if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
584                 $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
585
586         unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
587
588         // Add any additional custom post types.
589         foreach ( $cpts as $cpt ) {
590                 if ( ! current_user_can( $cpt->cap->create_posts ) )
591                         continue;
592
593                 $key = 'post-new.php?post_type=' . $cpt->name;
594                 $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
595         }
596         // Avoid clash with parent node and a 'content' post type.
597         if ( isset( $actions['post-new.php?post_type=content'] ) )
598                 $actions['post-new.php?post_type=content'][1] = 'add-new-content';
599
600         if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
601                 $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
602
603         if ( ! $actions )
604                 return;
605
606         $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
607
608         $wp_admin_bar->add_menu( array(
609                 'id'    => 'new-content',
610                 'title' => $title,
611                 'href'  => admin_url( current( array_keys( $actions ) ) ),
612                 'meta'  => array(
613                         'title' => _x( 'Add New', 'admin bar menu group label' ),
614                 ),
615         ) );
616
617         foreach ( $actions as $link => $action ) {
618                 list( $title, $id ) = $action;
619
620                 $wp_admin_bar->add_menu( array(
621                         'parent'    => 'new-content',
622                         'id'        => $id,
623                         'title'     => $title,
624                         'href'      => admin_url( $link )
625                 ) );
626         }
627 }
628
629 /**
630  * Add edit comments link with awaiting moderation count bubble.
631  *
632  * @since 3.1.0
633  *
634  * @param WP_Admin_Bar $wp_admin_bar
635  */
636 function wp_admin_bar_comments_menu( $wp_admin_bar ) {
637         if ( !current_user_can('edit_posts') )
638                 return;
639
640         $awaiting_mod = wp_count_comments();
641         $awaiting_mod = $awaiting_mod->moderated;
642         $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
643
644         $icon  = '<span class="ab-icon"></span>';
645         $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
646
647         $wp_admin_bar->add_menu( array(
648                 'id'    => 'comments',
649                 'title' => $icon . $title,
650                 'href'  => admin_url('edit-comments.php'),
651                 'meta'  => array( 'title' => $awaiting_title ),
652         ) );
653 }
654
655 /**
656  * Add appearance submenu items to the "Site Name" menu.
657  *
658  * @since 3.1.0
659  *
660  * @param WP_Admin_Bar $wp_admin_bar
661  */
662 function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
663         $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
664
665         if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
666                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
667
668         if ( ! current_user_can( 'edit_theme_options' ) )
669                 return;
670
671         $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
672         $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
673         if ( current_user_can( 'customize' ) ) {
674                 $wp_admin_bar->add_menu( array(
675                         'parent' => 'appearance',
676                         'id'     => 'customize',
677                         'title'  => __('Customize'),
678                         'href'   => $customize_url,
679                         'meta'   => array(
680                                 'class' => 'hide-if-no-customize',
681                         ),
682                 ) );
683                 add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
684         }
685
686         if ( current_theme_supports( 'widgets' )  )
687                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
688
689         if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
690                 $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
691
692         if ( current_theme_supports( 'custom-background' ) ) {
693                 $wp_admin_bar->add_menu( array(
694                         'parent' => 'appearance',
695                         'id'     => 'background',
696                         'title'  => __( 'Background' ),
697                         'href'   => admin_url( 'themes.php?page=custom-background' ),
698                         'meta'   => array(
699                                 'class' => 'hide-if-customize',
700                         ),
701                 ) );
702
703                 if ( current_user_can( 'customize' ) ) {
704                         $wp_admin_bar->add_menu( array(
705                                 'parent' => 'appearance',
706                                 'id'     => 'customize-background',
707                                 'title'  => __( 'Background' ),
708                                 'href'   => add_query_arg( urlencode( 'autofocus[control]' ), 'background_image', $customize_url ), // urlencode() needed due to #16859
709                                 'meta'   => array(
710                                         'class' => 'hide-if-no-customize',
711                                 ),
712                         ) );
713                 }
714         }
715
716         if ( current_theme_supports( 'custom-header' ) ) {
717                 $wp_admin_bar->add_menu( array(
718                         'parent' => 'appearance',
719                         'id'     => 'header',
720                         'title'  => __( 'Header' ),
721                         'href'   => admin_url( 'themes.php?page=custom-header' ),
722                         'meta'   => array(
723                                 'class' => 'hide-if-customize',
724                         ),
725                 ) );
726
727                 if ( current_user_can( 'customize' ) ) {
728                         $wp_admin_bar->add_menu( array(
729                                 'parent' => 'appearance',
730                                 'id'     => 'customize-header',
731                                 'title'  => __( 'Header' ),
732                                 'href'   => add_query_arg( urlencode( 'autofocus[control]' ), 'header_image', $customize_url ), // urlencode() needed due to #16859
733                                 'meta'   => array(
734                                         'class' => 'hide-if-no-customize',
735                                 ),
736                         ) );
737                 }
738         }
739
740 }
741
742 /**
743  * Provide an update link if theme/plugin/core updates are available.
744  *
745  * @since 3.1.0
746  *
747  * @param WP_Admin_Bar $wp_admin_bar
748  */
749 function wp_admin_bar_updates_menu( $wp_admin_bar ) {
750
751         $update_data = wp_get_update_data();
752
753         if ( !$update_data['counts']['total'] )
754                 return;
755
756         $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
757         $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
758
759         $wp_admin_bar->add_menu( array(
760                 'id'    => 'updates',
761                 'title' => $title,
762                 'href'  => network_admin_url( 'update-core.php' ),
763                 'meta'  => array(
764                         'title' => $update_data['title'],
765                 ),
766         ) );
767 }
768
769 /**
770  * Add search form.
771  *
772  * @since 3.3.0
773  *
774  * @param WP_Admin_Bar $wp_admin_bar
775  */
776 function wp_admin_bar_search_menu( $wp_admin_bar ) {
777         if ( is_admin() )
778                 return;
779
780         $form  = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
781         $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
782         $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
783         $form .= '</form>';
784
785         $wp_admin_bar->add_menu( array(
786                 'parent' => 'top-secondary',
787                 'id'     => 'search',
788                 'title'  => $form,
789                 'meta'   => array(
790                         'class'    => 'admin-bar-search',
791                         'tabindex' => -1,
792                 )
793         ) );
794 }
795
796 /**
797  * Add secondary menus.
798  *
799  * @since 3.3.0
800  *
801  * @param WP_Admin_Bar $wp_admin_bar
802  */
803 function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
804         $wp_admin_bar->add_group( array(
805                 'id'     => 'top-secondary',
806                 'meta'   => array(
807                         'class' => 'ab-top-secondary',
808                 ),
809         ) );
810
811         $wp_admin_bar->add_group( array(
812                 'parent' => 'wp-logo',
813                 'id'     => 'wp-logo-external',
814                 'meta'   => array(
815                         'class' => 'ab-sub-secondary',
816                 ),
817         ) );
818 }
819
820 /**
821  * Style and scripts for the admin bar.
822  *
823  * @since 3.1.0
824  */
825 function wp_admin_bar_header() { ?>
826 <style type="text/css" media="print">#wpadminbar { display:none; }</style>
827 <?php
828 }
829
830 /**
831  * Default admin bar callback.
832  *
833  * @since 3.1.0
834  */
835 function _admin_bar_bump_cb() { ?>
836 <style type="text/css" media="screen">
837         html { margin-top: 32px !important; }
838         * html body { margin-top: 32px !important; }
839         @media screen and ( max-width: 782px ) {
840                 html { margin-top: 46px !important; }
841                 * html body { margin-top: 46px !important; }
842         }
843 </style>
844 <?php
845 }
846
847 /**
848  * Set the display status of the admin bar.
849  *
850  * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
851  *
852  * @since 3.1.0
853  *
854  * @param bool $show Whether to allow the admin bar to show.
855  * @return void
856  */
857 function show_admin_bar( $show ) {
858         global $show_admin_bar;
859         $show_admin_bar = (bool) $show;
860 }
861
862 /**
863  * Determine whether the admin bar should be showing.
864  *
865  * @since 3.1.0
866  *
867  * @return bool Whether the admin bar should be showing.
868  */
869 function is_admin_bar_showing() {
870         global $show_admin_bar, $pagenow;
871
872         // For all these types of requests, we never want an admin bar.
873         if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
874                 return false;
875
876         // Integrated into the admin.
877         if ( is_admin() )
878                 return true;
879
880         if ( ! isset( $show_admin_bar ) ) {
881                 if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
882                         $show_admin_bar = false;
883                 } else {
884                         $show_admin_bar = _get_admin_bar_pref();
885                 }
886         }
887
888         /**
889          * Filter whether to show the admin bar.
890          *
891          * Returning false to this hook is the recommended way to hide the admin bar.
892          * The user's display preference is used for logged in users.
893          *
894          * @since 3.1.0
895          *
896          * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
897          */
898         $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
899
900         return $show_admin_bar;
901 }
902
903 /**
904  * Retrieve the admin bar display preference of a user.
905  *
906  * @since 3.1.0
907  * @access private
908  *
909  * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
910  *      preference is no longer used.
911  * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
912  * @return bool Whether the admin bar should be showing for this user.
913  */
914 function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
915         $pref = get_user_option( "show_admin_bar_{$context}", $user );
916         if ( false === $pref )
917                 return true;
918
919         return 'true' === $pref;
920 }