admin-bar.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. if ( current_user_can( 'read' ) ) {
  450. $wp_admin_bar->add_menu( array(
  451. 'parent' => 'my-sites-list',
  452. 'id' => $menu_id,
  453. 'title' => $blavatar . $blogname,
  454. 'href' => admin_url(),
  455. ) );
  456. $wp_admin_bar->add_menu( array(
  457. 'parent' => $menu_id,
  458. 'id' => $menu_id . '-d',
  459. 'title' => __( 'Dashboard' ),
  460. 'href' => admin_url(),
  461. ) );
  462. } else {
  463. $wp_admin_bar->add_menu( array(
  464. 'parent' => 'my-sites-list',
  465. 'id' => $menu_id,
  466. 'title' => $blavatar . $blogname,
  467. 'href' => home_url(),
  468. ) );
  469. }
  470. if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
  471. $wp_admin_bar->add_menu( array(
  472. 'parent' => $menu_id,
  473. 'id' => $menu_id . '-n',
  474. 'title' => __( 'New Post' ),
  475. 'href' => admin_url( 'post-new.php' ),
  476. ) );
  477. }
  478. if ( current_user_can( 'edit_posts' ) ) {
  479. $wp_admin_bar->add_menu( array(
  480. 'parent' => $menu_id,
  481. 'id' => $menu_id . '-c',
  482. 'title' => __( 'Manage Comments' ),
  483. 'href' => admin_url( 'edit-comments.php' ),
  484. ) );
  485. }
  486. $wp_admin_bar->add_menu( array(
  487. 'parent' => $menu_id,
  488. 'id' => $menu_id . '-v',
  489. 'title' => __( 'Visit Site' ),
  490. 'href' => home_url( '/' ),
  491. ) );
  492. restore_current_blog();
  493. }
  494. }
  495. /**
  496. * Provide a shortlink.
  497. *
  498. * @since 3.1.0
  499. *
  500. * @param WP_Admin_Bar $wp_admin_bar
  501. */
  502. function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
  503. $short = wp_get_shortlink( 0, 'query' );
  504. $id = 'get-shortlink';
  505. if ( empty( $short ) )
  506. return;
  507. $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
  508. $wp_admin_bar->add_menu( array(
  509. 'id' => $id,
  510. 'title' => __( 'Shortlink' ),
  511. 'href' => $short,
  512. 'meta' => array( 'html' => $html ),
  513. ) );
  514. }
  515. /**
  516. * Provide an edit link for posts and terms.
  517. *
  518. * @since 3.1.0
  519. *
  520. * @global WP_Term $tag
  521. * @global WP_Query $wp_the_query
  522. *
  523. * @param WP_Admin_Bar $wp_admin_bar
  524. */
  525. function wp_admin_bar_edit_menu( $wp_admin_bar ) {
  526. global $tag, $wp_the_query, $user_id;
  527. if ( is_admin() ) {
  528. $current_screen = get_current_screen();
  529. $post = get_post();
  530. if ( 'post' == $current_screen->base
  531. && 'add' != $current_screen->action
  532. && ( $post_type_object = get_post_type_object( $post->post_type ) )
  533. && current_user_can( 'read_post', $post->ID )
  534. && ( $post_type_object->public )
  535. && ( $post_type_object->show_in_admin_bar ) )
  536. {
  537. if ( 'draft' == $post->post_status ) {
  538. $preview_link = get_preview_post_link( $post );
  539. $wp_admin_bar->add_menu( array(
  540. 'id' => 'preview',
  541. 'title' => $post_type_object->labels->view_item,
  542. 'href' => esc_url( $preview_link ),
  543. 'meta' => array( 'target' => 'wp-preview-' . $post->ID ),
  544. ) );
  545. } else {
  546. $wp_admin_bar->add_menu( array(
  547. 'id' => 'view',
  548. 'title' => $post_type_object->labels->view_item,
  549. 'href' => get_permalink( $post->ID )
  550. ) );
  551. }
  552. } elseif ( 'edit' == $current_screen->base
  553. && ( $post_type_object = get_post_type_object( $current_screen->post_type ) )
  554. && ( $post_type_object->public )
  555. && ( $post_type_object->show_in_admin_bar )
  556. && ( get_post_type_archive_link( $post_type_object->name ) )
  557. && ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) )
  558. {
  559. $wp_admin_bar->add_node( array(
  560. 'id' => 'archive',
  561. 'title' => $post_type_object->labels->view_items,
  562. 'href' => get_post_type_archive_link( $current_screen->post_type )
  563. ) );
  564. } elseif ( 'term' == $current_screen->base
  565. && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag )
  566. && ( $tax = get_taxonomy( $tag->taxonomy ) )
  567. && $tax->public )
  568. {
  569. $wp_admin_bar->add_menu( array(
  570. 'id' => 'view',
  571. 'title' => $tax->labels->view_item,
  572. 'href' => get_term_link( $tag )
  573. ) );
  574. } elseif ( 'user-edit' == $current_screen->base
  575. && isset( $user_id )
  576. && ( $user_object = get_userdata( $user_id ) )
  577. && $user_object->exists()
  578. && $view_link = get_author_posts_url( $user_object->ID ) )
  579. {
  580. $wp_admin_bar->add_menu( array(
  581. 'id' => 'view',
  582. 'title' => __( 'View User' ),
  583. 'href' => $view_link,
  584. ) );
  585. }
  586. } else {
  587. $current_object = $wp_the_query->get_queried_object();
  588. if ( empty( $current_object ) )
  589. return;
  590. if ( ! empty( $current_object->post_type )
  591. && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
  592. && current_user_can( 'edit_post', $current_object->ID )
  593. && $post_type_object->show_in_admin_bar
  594. && $edit_post_link = get_edit_post_link( $current_object->ID ) )
  595. {
  596. $wp_admin_bar->add_menu( array(
  597. 'id' => 'edit',
  598. 'title' => $post_type_object->labels->edit_item,
  599. 'href' => $edit_post_link
  600. ) );
  601. } elseif ( ! empty( $current_object->taxonomy )
  602. && ( $tax = get_taxonomy( $current_object->taxonomy ) )
  603. && current_user_can( 'edit_term', $current_object->term_id )
  604. && $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ) )
  605. {
  606. $wp_admin_bar->add_menu( array(
  607. 'id' => 'edit',
  608. 'title' => $tax->labels->edit_item,
  609. 'href' => $edit_term_link
  610. ) );
  611. } elseif ( is_a( $current_object, 'WP_User' )
  612. && current_user_can( 'edit_user', $current_object->ID )
  613. && $edit_user_link = get_edit_user_link( $current_object->ID ) )
  614. {
  615. $wp_admin_bar->add_menu( array(
  616. 'id' => 'edit',
  617. 'title' => __( 'Edit User' ),
  618. 'href' => $edit_user_link,
  619. ) );
  620. }
  621. }
  622. }
  623. /**
  624. * Add "Add New" menu.
  625. *
  626. * @since 3.1.0
  627. *
  628. * @param WP_Admin_Bar $wp_admin_bar
  629. */
  630. function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
  631. $actions = array();
  632. $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
  633. if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
  634. $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
  635. if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
  636. $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
  637. if ( current_user_can( 'manage_links' ) )
  638. $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
  639. if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
  640. $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
  641. unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
  642. // Add any additional custom post types.
  643. foreach ( $cpts as $cpt ) {
  644. if ( ! current_user_can( $cpt->cap->create_posts ) )
  645. continue;
  646. $key = 'post-new.php?post_type=' . $cpt->name;
  647. $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
  648. }
  649. // Avoid clash with parent node and a 'content' post type.
  650. if ( isset( $actions['post-new.php?post_type=content'] ) )
  651. $actions['post-new.php?post_type=content'][1] = 'add-new-content';
  652. if ( current_user_can( 'create_users' ) || ( is_multisite() && current_user_can( 'promote_users' ) ) ) {
  653. $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
  654. }
  655. if ( ! $actions )
  656. return;
  657. $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
  658. $wp_admin_bar->add_menu( array(
  659. 'id' => 'new-content',
  660. 'title' => $title,
  661. 'href' => admin_url( current( array_keys( $actions ) ) ),
  662. ) );
  663. foreach ( $actions as $link => $action ) {
  664. list( $title, $id ) = $action;
  665. $wp_admin_bar->add_menu( array(
  666. 'parent' => 'new-content',
  667. 'id' => $id,
  668. 'title' => $title,
  669. 'href' => admin_url( $link )
  670. ) );
  671. }
  672. }
  673. /**
  674. * Add edit comments link with awaiting moderation count bubble.
  675. *
  676. * @since 3.1.0
  677. *
  678. * @param WP_Admin_Bar $wp_admin_bar
  679. */
  680. function wp_admin_bar_comments_menu( $wp_admin_bar ) {
  681. if ( !current_user_can('edit_posts') )
  682. return;
  683. $awaiting_mod = wp_count_comments();
  684. $awaiting_mod = $awaiting_mod->moderated;
  685. $awaiting_text = sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) );
  686. $icon = '<span class="ab-icon"></span>';
  687. $title = '<span class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>';
  688. $title .= '<span class="screen-reader-text">' . $awaiting_text . '</span>';
  689. $wp_admin_bar->add_menu( array(
  690. 'id' => 'comments',
  691. 'title' => $icon . $title,
  692. 'href' => admin_url('edit-comments.php'),
  693. ) );
  694. }
  695. /**
  696. * Add appearance submenu items to the "Site Name" menu.
  697. *
  698. * @since 3.1.0
  699. *
  700. * @param WP_Admin_Bar $wp_admin_bar
  701. */
  702. function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
  703. $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
  704. if ( current_user_can( 'switch_themes' ) ) {
  705. $wp_admin_bar->add_menu( array(
  706. 'parent' => 'appearance',
  707. 'id' => 'themes',
  708. 'title' => __( 'Themes' ),
  709. 'href' => admin_url( 'themes.php' ),
  710. ) );
  711. }
  712. if ( ! current_user_can( 'edit_theme_options' ) ) {
  713. return;
  714. }
  715. if ( current_theme_supports( 'widgets' ) ) {
  716. $wp_admin_bar->add_menu( array(
  717. 'parent' => 'appearance',
  718. 'id' => 'widgets',
  719. 'title' => __( 'Widgets' ),
  720. 'href' => admin_url( 'widgets.php' ),
  721. ) );
  722. }
  723. if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
  724. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
  725. if ( current_theme_supports( 'custom-background' ) ) {
  726. $wp_admin_bar->add_menu( array(
  727. 'parent' => 'appearance',
  728. 'id' => 'background',
  729. 'title' => __( 'Background' ),
  730. 'href' => admin_url( 'themes.php?page=custom-background' ),
  731. 'meta' => array(
  732. 'class' => 'hide-if-customize',
  733. ),
  734. ) );
  735. }
  736. if ( current_theme_supports( 'custom-header' ) ) {
  737. $wp_admin_bar->add_menu( array(
  738. 'parent' => 'appearance',
  739. 'id' => 'header',
  740. 'title' => __( 'Header' ),
  741. 'href' => admin_url( 'themes.php?page=custom-header' ),
  742. 'meta' => array(
  743. 'class' => 'hide-if-customize',
  744. ),
  745. ) );
  746. }
  747. }
  748. /**
  749. * Provide an update link if theme/plugin/core updates are available.
  750. *
  751. * @since 3.1.0
  752. *
  753. * @param WP_Admin_Bar $wp_admin_bar
  754. */
  755. function wp_admin_bar_updates_menu( $wp_admin_bar ) {
  756. $update_data = wp_get_update_data();
  757. if ( !$update_data['counts']['total'] )
  758. return;
  759. $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
  760. $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
  761. $wp_admin_bar->add_menu( array(
  762. 'id' => 'updates',
  763. 'title' => $title,
  764. 'href' => network_admin_url( 'update-core.php' ),
  765. 'meta' => array(
  766. 'title' => $update_data['title'],
  767. ),
  768. ) );
  769. }
  770. /**
  771. * Add search form.
  772. *
  773. * @since 3.3.0
  774. *
  775. * @param WP_Admin_Bar $wp_admin_bar
  776. */
  777. function wp_admin_bar_search_menu( $wp_admin_bar ) {
  778. if ( is_admin() )
  779. return;
  780. $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
  781. $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
  782. $form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>';
  783. $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
  784. $form .= '</form>';
  785. $wp_admin_bar->add_menu( array(
  786. 'parent' => 'top-secondary',
  787. 'id' => 'search',
  788. 'title' => $form,
  789. 'meta' => array(
  790. 'class' => 'admin-bar-search',
  791. 'tabindex' => -1,
  792. )
  793. ) );
  794. }
  795. /**
  796. * Add secondary menus.
  797. *
  798. * @since 3.3.0
  799. *
  800. * @param WP_Admin_Bar $wp_admin_bar
  801. */
  802. function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
  803. $wp_admin_bar->add_group( array(
  804. 'id' => 'top-secondary',
  805. 'meta' => array(
  806. 'class' => 'ab-top-secondary',
  807. ),
  808. ) );
  809. $wp_admin_bar->add_group( array(
  810. 'parent' => 'wp-logo',
  811. 'id' => 'wp-logo-external',
  812. 'meta' => array(
  813. 'class' => 'ab-sub-secondary',
  814. ),
  815. ) );
  816. }
  817. /**
  818. * Style and scripts for the admin bar.
  819. *
  820. * @since 3.1.0
  821. */
  822. function wp_admin_bar_header() { ?>
  823. <style type="text/css" media="print">#wpadminbar { display:none; }</style>
  824. <?php
  825. }
  826. /**
  827. * Default admin bar callback.
  828. *
  829. * @since 3.1.0
  830. */
  831. function _admin_bar_bump_cb() { ?>
  832. <style type="text/css" media="screen">
  833. html { margin-top: 32px !important; }
  834. * html body { margin-top: 32px !important; }
  835. @media screen and ( max-width: 782px ) {
  836. html { margin-top: 46px !important; }
  837. * html body { margin-top: 46px !important; }
  838. }
  839. </style>
  840. <?php
  841. }
  842. /**
  843. * Sets the display status of the admin bar.
  844. *
  845. * This can be called immediately upon plugin load. It does not need to be called
  846. * from a function hooked to the {@see 'init'} action.
  847. *
  848. * @since 3.1.0
  849. *
  850. * @global bool $show_admin_bar
  851. *
  852. * @param bool $show Whether to allow the admin bar to show.
  853. */
  854. function show_admin_bar( $show ) {
  855. global $show_admin_bar;
  856. $show_admin_bar = (bool) $show;
  857. }
  858. /**
  859. * Determine whether the admin bar should be showing.
  860. *
  861. * @since 3.1.0
  862. *
  863. * @global bool $show_admin_bar
  864. * @global string $pagenow
  865. *
  866. * @return bool Whether the admin bar should be showing.
  867. */
  868. function is_admin_bar_showing() {
  869. global $show_admin_bar, $pagenow;
  870. // For all these types of requests, we never want an admin bar.
  871. if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
  872. return false;
  873. if ( is_embed() ) {
  874. return false;
  875. }
  876. // Integrated into the admin.
  877. if ( is_admin() )
  878. return true;
  879. if ( ! isset( $show_admin_bar ) ) {
  880. if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
  881. $show_admin_bar = false;
  882. } else {
  883. $show_admin_bar = _get_admin_bar_pref();
  884. }
  885. }
  886. /**
  887. * Filters whether to show the admin bar.
  888. *
  889. * Returning false to this hook is the recommended way to hide the admin bar.
  890. * The user's display preference is used for logged in users.
  891. *
  892. * @since 3.1.0
  893. *
  894. * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
  895. */
  896. $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
  897. return $show_admin_bar;
  898. }
  899. /**
  900. * Retrieve the admin bar display preference of a user.
  901. *
  902. * @since 3.1.0
  903. * @access private
  904. *
  905. * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
  906. * preference is no longer used.
  907. * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
  908. * @return bool Whether the admin bar should be showing for this user.
  909. */
  910. function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
  911. $pref = get_user_option( "show_admin_bar_{$context}", $user );
  912. if ( false === $pref )
  913. return true;
  914. return 'true' === $pref;
  915. }