~canonical-sysadmins/wordpress/4.7.4

« back to all changes in this revision

Viewing changes to wp-includes/admin-bar.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

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