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