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