admin-bar.php 27 KB

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