admin-bar.php 33 KB

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