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