admin-bar.php 30 KB

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