You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1006 lines
27 KiB

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