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