説明なし

admin-bar.php 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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 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. * Load 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 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. * Add the WordPress logo menu.
  102. *
  103. * @since 3.3.0
  104. *
  105. * @param WP_Admin_Bar $wp_admin_bar
  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. * Add the sidebar toggle button.
  177. *
  178. * @since 3.8.0
  179. *
  180. * @param WP_Admin_Bar $wp_admin_bar
  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. * Add the "My Account" item.
  195. *
  196. * @since 3.3.0
  197. *
  198. * @param WP_Admin_Bar $wp_admin_bar
  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. * Add the "My Account" submenu items.
  231. *
  232. * @since 3.1.0
  233. *
  234. * @param WP_Admin_Bar $wp_admin_bar
  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. * Add the "Site Name" menu.
  292. *
  293. * @since 3.3.0
  294. *
  295. * @param WP_Admin_Bar $wp_admin_bar
  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 "Customize" link to the Toolbar.
  362. *
  363. * @since 4.3.0
  364. *
  365. * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
  366. * @global WP_Customize_Manager $wp_customize
  367. */
  368. function wp_admin_bar_customize_menu( $wp_admin_bar ) {
  369. global $wp_customize;
  370. // Don't show for users who can't access the customizer or when in the admin.
  371. if ( ! current_user_can( 'customize' ) || is_admin() ) {
  372. return;
  373. }
  374. // Don't show if the user cannot edit a given customize_changeset post currently being previewed.
  375. if ( is_customize_preview() && $wp_customize->changeset_post_id()
  376. && ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $wp_customize->changeset_post_id() )
  377. ) {
  378. return;
  379. }
  380. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  381. if ( is_customize_preview() && $wp_customize->changeset_uuid() ) {
  382. $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url );
  383. }
  384. $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() );
  385. if ( is_customize_preview() ) {
  386. $customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url );
  387. }
  388. $wp_admin_bar->add_node(
  389. array(
  390. 'id' => 'customize',
  391. 'title' => __( 'Customize' ),
  392. 'href' => $customize_url,
  393. 'meta' => array(
  394. 'class' => 'hide-if-no-customize',
  395. ),
  396. )
  397. );
  398. add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
  399. }
  400. /**
  401. * Add the "My Sites/[Site Name]" menu and all submenus.
  402. *
  403. * @since 3.1.0
  404. *
  405. * @param WP_Admin_Bar $wp_admin_bar
  406. */
  407. function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
  408. // Don't show for logged out users or single site mode.
  409. if ( ! is_user_logged_in() || ! is_multisite() ) {
  410. return;
  411. }
  412. // Show only when the user has at least one site, or they're a super admin.
  413. if ( count( $wp_admin_bar->user->blogs ) < 1 && ! current_user_can( 'manage_network' ) ) {
  414. return;
  415. }
  416. if ( $wp_admin_bar->user->active_blog ) {
  417. $my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' );
  418. } else {
  419. $my_sites_url = admin_url( 'my-sites.php' );
  420. }
  421. $wp_admin_bar->add_node(
  422. array(
  423. 'id' => 'my-sites',
  424. 'title' => __( 'My Sites' ),
  425. 'href' => $my_sites_url,
  426. )
  427. );
  428. if ( current_user_can( 'manage_network' ) ) {
  429. $wp_admin_bar->add_group(
  430. array(
  431. 'parent' => 'my-sites',
  432. 'id' => 'my-sites-super-admin',
  433. )
  434. );
  435. $wp_admin_bar->add_node(
  436. array(
  437. 'parent' => 'my-sites-super-admin',
  438. 'id' => 'network-admin',
  439. 'title' => __( 'Network Admin' ),
  440. 'href' => network_admin_url(),
  441. )
  442. );
  443. $wp_admin_bar->add_node(
  444. array(
  445. 'parent' => 'network-admin',
  446. 'id' => 'network-admin-d',
  447. 'title' => __( 'Dashboard' ),
  448. 'href' => network_admin_url(),
  449. )
  450. );
  451. if ( current_user_can( 'manage_sites' ) ) {
  452. $wp_admin_bar->add_node(
  453. array(
  454. 'parent' => 'network-admin',
  455. 'id' => 'network-admin-s',
  456. 'title' => __( 'Sites' ),
  457. 'href' => network_admin_url( 'sites.php' ),
  458. )
  459. );
  460. }
  461. if ( current_user_can( 'manage_network_users' ) ) {
  462. $wp_admin_bar->add_node(
  463. array(
  464. 'parent' => 'network-admin',
  465. 'id' => 'network-admin-u',
  466. 'title' => __( 'Users' ),
  467. 'href' => network_admin_url( 'users.php' ),
  468. )
  469. );
  470. }
  471. if ( current_user_can( 'manage_network_themes' ) ) {
  472. $wp_admin_bar->add_node(
  473. array(
  474. 'parent' => 'network-admin',
  475. 'id' => 'network-admin-t',
  476. 'title' => __( 'Themes' ),
  477. 'href' => network_admin_url( 'themes.php' ),
  478. )
  479. );
  480. }
  481. if ( current_user_can( 'manage_network_plugins' ) ) {
  482. $wp_admin_bar->add_node(
  483. array(
  484. 'parent' => 'network-admin',
  485. 'id' => 'network-admin-p',
  486. 'title' => __( 'Plugins' ),
  487. 'href' => network_admin_url( 'plugins.php' ),
  488. )
  489. );
  490. }
  491. if ( current_user_can( 'manage_network_options' ) ) {
  492. $wp_admin_bar->add_node(
  493. array(
  494. 'parent' => 'network-admin',
  495. 'id' => 'network-admin-o',
  496. 'title' => __( 'Settings' ),
  497. 'href' => network_admin_url( 'settings.php' ),
  498. )
  499. );
  500. }
  501. }
  502. // Add site links.
  503. $wp_admin_bar->add_group(
  504. array(
  505. 'parent' => 'my-sites',
  506. 'id' => 'my-sites-list',
  507. 'meta' => array(
  508. 'class' => current_user_can( 'manage_network' ) ? 'ab-sub-secondary' : '',
  509. ),
  510. )
  511. );
  512. foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
  513. switch_to_blog( $blog->userblog_id );
  514. if ( has_site_icon() ) {
  515. $blavatar = sprintf(
  516. '<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16" />',
  517. esc_url( get_site_icon_url( 16 ) ),
  518. esc_url( get_site_icon_url( 32 ) )
  519. );
  520. } else {
  521. $blavatar = '<div class="blavatar"></div>';
  522. }
  523. $blogname = $blog->blogname;
  524. if ( ! $blogname ) {
  525. $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
  526. }
  527. $menu_id = 'blog-' . $blog->userblog_id;
  528. if ( current_user_can( 'read' ) ) {
  529. $wp_admin_bar->add_node(
  530. array(
  531. 'parent' => 'my-sites-list',
  532. 'id' => $menu_id,
  533. 'title' => $blavatar . $blogname,
  534. 'href' => admin_url(),
  535. )
  536. );
  537. $wp_admin_bar->add_node(
  538. array(
  539. 'parent' => $menu_id,
  540. 'id' => $menu_id . '-d',
  541. 'title' => __( 'Dashboard' ),
  542. 'href' => admin_url(),
  543. )
  544. );
  545. } else {
  546. $wp_admin_bar->add_node(
  547. array(
  548. 'parent' => 'my-sites-list',
  549. 'id' => $menu_id,
  550. 'title' => $blavatar . $blogname,
  551. 'href' => home_url(),
  552. )
  553. );
  554. }
  555. if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
  556. $wp_admin_bar->add_node(
  557. array(
  558. 'parent' => $menu_id,
  559. 'id' => $menu_id . '-n',
  560. 'title' => get_post_type_object( 'post' )->labels->new_item,
  561. 'href' => admin_url( 'post-new.php' ),
  562. )
  563. );
  564. }
  565. if ( current_user_can( 'edit_posts' ) ) {
  566. $wp_admin_bar->add_node(
  567. array(
  568. 'parent' => $menu_id,
  569. 'id' => $menu_id . '-c',
  570. 'title' => __( 'Manage Comments' ),
  571. 'href' => admin_url( 'edit-comments.php' ),
  572. )
  573. );
  574. }
  575. $wp_admin_bar->add_node(
  576. array(
  577. 'parent' => $menu_id,
  578. 'id' => $menu_id . '-v',
  579. 'title' => __( 'Visit Site' ),
  580. 'href' => home_url( '/' ),
  581. )
  582. );
  583. restore_current_blog();
  584. }
  585. }
  586. /**
  587. * Provide a shortlink.
  588. *
  589. * @since 3.1.0
  590. *
  591. * @param WP_Admin_Bar $wp_admin_bar
  592. */
  593. function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
  594. $short = wp_get_shortlink( 0, 'query' );
  595. $id = 'get-shortlink';
  596. if ( empty( $short ) ) {
  597. return;
  598. }
  599. $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
  600. $wp_admin_bar->add_node(
  601. array(
  602. 'id' => $id,
  603. 'title' => __( 'Shortlink' ),
  604. 'href' => $short,
  605. 'meta' => array( 'html' => $html ),
  606. )
  607. );
  608. }
  609. /**
  610. * Provide an edit link for posts and terms.
  611. *
  612. * @since 3.1.0
  613. * @since 5.5.0 Added a "View Post" link on Comments screen for a single post.
  614. *
  615. * @global WP_Term $tag
  616. * @global WP_Query $wp_the_query WordPress Query object.
  617. * @global int $user_id The ID of the user being edited. Not to be confused with the
  618. * global $user_ID, which contains the ID of the current user.
  619. * @global int $post_id The ID of the post when editing comments for a single post.
  620. *
  621. * @param WP_Admin_Bar $wp_admin_bar
  622. */
  623. function wp_admin_bar_edit_menu( $wp_admin_bar ) {
  624. global $tag, $wp_the_query, $user_id, $post_id;
  625. if ( is_admin() ) {
  626. $current_screen = get_current_screen();
  627. $post = get_post();
  628. $post_type_object = null;
  629. if ( 'post' === $current_screen->base ) {
  630. $post_type_object = get_post_type_object( $post->post_type );
  631. } elseif ( 'edit' === $current_screen->base ) {
  632. $post_type_object = get_post_type_object( $current_screen->post_type );
  633. } elseif ( 'edit-comments' === $current_screen->base && $post_id ) {
  634. $post = get_post( $post_id );
  635. if ( $post ) {
  636. $post_type_object = get_post_type_object( $post->post_type );
  637. }
  638. }
  639. if ( ( 'post' === $current_screen->base || 'edit-comments' === $current_screen->base )
  640. && 'add' !== $current_screen->action
  641. && ( $post_type_object )
  642. && current_user_can( 'read_post', $post->ID )
  643. && ( $post_type_object->public )
  644. && ( $post_type_object->show_in_admin_bar ) ) {
  645. if ( 'draft' === $post->post_status ) {
  646. $preview_link = get_preview_post_link( $post );
  647. $wp_admin_bar->add_node(
  648. array(
  649. 'id' => 'preview',
  650. 'title' => $post_type_object->labels->view_item,
  651. 'href' => esc_url( $preview_link ),
  652. 'meta' => array( 'target' => 'wp-preview-' . $post->ID ),
  653. )
  654. );
  655. } else {
  656. $wp_admin_bar->add_node(
  657. array(
  658. 'id' => 'view',
  659. 'title' => $post_type_object->labels->view_item,
  660. 'href' => get_permalink( $post->ID ),
  661. )
  662. );
  663. }
  664. } elseif ( 'edit' === $current_screen->base
  665. && ( $post_type_object )
  666. && ( $post_type_object->public )
  667. && ( $post_type_object->show_in_admin_bar )
  668. && ( get_post_type_archive_link( $post_type_object->name ) )
  669. && ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) ) {
  670. $wp_admin_bar->add_node(
  671. array(
  672. 'id' => 'archive',
  673. 'title' => $post_type_object->labels->view_items,
  674. 'href' => get_post_type_archive_link( $current_screen->post_type ),
  675. )
  676. );
  677. } elseif ( 'term' === $current_screen->base && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) ) {
  678. $tax = get_taxonomy( $tag->taxonomy );
  679. if ( is_taxonomy_viewable( $tax ) ) {
  680. $wp_admin_bar->add_node(
  681. array(
  682. 'id' => 'view',
  683. 'title' => $tax->labels->view_item,
  684. 'href' => get_term_link( $tag ),
  685. )
  686. );
  687. }
  688. } elseif ( 'user-edit' === $current_screen->base && isset( $user_id ) ) {
  689. $user_object = get_userdata( $user_id );
  690. $view_link = get_author_posts_url( $user_object->ID );
  691. if ( $user_object->exists() && $view_link ) {
  692. $wp_admin_bar->add_node(
  693. array(
  694. 'id' => 'view',
  695. 'title' => __( 'View User' ),
  696. 'href' => $view_link,
  697. )
  698. );
  699. }
  700. }
  701. } else {
  702. $current_object = $wp_the_query->get_queried_object();
  703. if ( empty( $current_object ) ) {
  704. return;
  705. }
  706. if ( ! empty( $current_object->post_type ) ) {
  707. $post_type_object = get_post_type_object( $current_object->post_type );
  708. $edit_post_link = get_edit_post_link( $current_object->ID );
  709. if ( $post_type_object
  710. && $edit_post_link
  711. && current_user_can( 'edit_post', $current_object->ID )
  712. && $post_type_object->show_in_admin_bar ) {
  713. $wp_admin_bar->add_node(
  714. array(
  715. 'id' => 'edit',
  716. 'title' => $post_type_object->labels->edit_item,
  717. 'href' => $edit_post_link,
  718. )
  719. );
  720. }
  721. } elseif ( ! empty( $current_object->taxonomy ) ) {
  722. $tax = get_taxonomy( $current_object->taxonomy );
  723. $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy );
  724. if ( $tax && $edit_term_link && current_user_can( 'edit_term', $current_object->term_id ) ) {
  725. $wp_admin_bar->add_node(
  726. array(
  727. 'id' => 'edit',
  728. 'title' => $tax->labels->edit_item,
  729. 'href' => $edit_term_link,
  730. )
  731. );
  732. }
  733. } elseif ( is_a( $current_object, 'WP_User' ) && current_user_can( 'edit_user', $current_object->ID ) ) {
  734. $edit_user_link = get_edit_user_link( $current_object->ID );
  735. if ( $edit_user_link ) {
  736. $wp_admin_bar->add_node(
  737. array(
  738. 'id' => 'edit',
  739. 'title' => __( 'Edit User' ),
  740. 'href' => $edit_user_link,
  741. )
  742. );
  743. }
  744. }
  745. }
  746. }
  747. /**
  748. * Add "Add New" menu.
  749. *
  750. * @since 3.1.0
  751. *
  752. * @param WP_Admin_Bar $wp_admin_bar
  753. */
  754. function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
  755. $actions = array();
  756. $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
  757. if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) ) {
  758. $actions['post-new.php'] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
  759. }
  760. if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) ) {
  761. $actions['media-new.php'] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
  762. }
  763. if ( current_user_can( 'manage_links' ) ) {
  764. $actions['link-add.php'] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
  765. }
  766. if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) ) {
  767. $actions['post-new.php?post_type=page'] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
  768. }
  769. unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
  770. // Add any additional custom post types.
  771. foreach ( $cpts as $cpt ) {
  772. if ( ! current_user_can( $cpt->cap->create_posts ) ) {
  773. continue;
  774. }
  775. $key = 'post-new.php?post_type=' . $cpt->name;
  776. $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
  777. }
  778. // Avoid clash with parent node and a 'content' post type.
  779. if ( isset( $actions['post-new.php?post_type=content'] ) ) {
  780. $actions['post-new.php?post_type=content'][1] = 'add-new-content';
  781. }
  782. if ( current_user_can( 'create_users' ) || ( is_multisite() && current_user_can( 'promote_users' ) ) ) {
  783. $actions['user-new.php'] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
  784. }
  785. if ( ! $actions ) {
  786. return;
  787. }
  788. $title = '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
  789. $wp_admin_bar->add_node(
  790. array(
  791. 'id' => 'new-content',
  792. 'title' => $title,
  793. 'href' => admin_url( current( array_keys( $actions ) ) ),
  794. )
  795. );
  796. foreach ( $actions as $link => $action ) {
  797. list( $title, $id ) = $action;
  798. $wp_admin_bar->add_node(
  799. array(
  800. 'parent' => 'new-content',
  801. 'id' => $id,
  802. 'title' => $title,
  803. 'href' => admin_url( $link ),
  804. )
  805. );
  806. }
  807. }
  808. /**
  809. * Add edit comments link with awaiting moderation count bubble.
  810. *
  811. * @since 3.1.0
  812. *
  813. * @param WP_Admin_Bar $wp_admin_bar
  814. */
  815. function wp_admin_bar_comments_menu( $wp_admin_bar ) {
  816. if ( ! current_user_can( 'edit_posts' ) ) {
  817. return;
  818. }
  819. $awaiting_mod = wp_count_comments();
  820. $awaiting_mod = $awaiting_mod->moderated;
  821. $awaiting_text = sprintf(
  822. /* translators: %s: Number of comments. */
  823. _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod ),
  824. number_format_i18n( $awaiting_mod )
  825. );
  826. $icon = '<span class="ab-icon" aria-hidden="true"></span>';
  827. $title = '<span class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '" aria-hidden="true">' . number_format_i18n( $awaiting_mod ) . '</span>';
  828. $title .= '<span class="screen-reader-text comments-in-moderation-text">' . $awaiting_text . '</span>';
  829. $wp_admin_bar->add_node(
  830. array(
  831. 'id' => 'comments',
  832. 'title' => $icon . $title,
  833. 'href' => admin_url( 'edit-comments.php' ),
  834. )
  835. );
  836. }
  837. /**
  838. * Add appearance submenu items to the "Site Name" menu.
  839. *
  840. * @since 3.1.0
  841. *
  842. * @param WP_Admin_Bar $wp_admin_bar
  843. */
  844. function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
  845. $wp_admin_bar->add_group(
  846. array(
  847. 'parent' => 'site-name',
  848. 'id' => 'appearance',
  849. )
  850. );
  851. if ( current_user_can( 'switch_themes' ) ) {
  852. $wp_admin_bar->add_node(
  853. array(
  854. 'parent' => 'appearance',
  855. 'id' => 'themes',
  856. 'title' => __( 'Themes' ),
  857. 'href' => admin_url( 'themes.php' ),
  858. )
  859. );
  860. }
  861. if ( ! current_user_can( 'edit_theme_options' ) ) {
  862. return;
  863. }
  864. if ( current_theme_supports( 'widgets' ) ) {
  865. $wp_admin_bar->add_node(
  866. array(
  867. 'parent' => 'appearance',
  868. 'id' => 'widgets',
  869. 'title' => __( 'Widgets' ),
  870. 'href' => admin_url( 'widgets.php' ),
  871. )
  872. );
  873. }
  874. if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
  875. $wp_admin_bar->add_node(
  876. array(
  877. 'parent' => 'appearance',
  878. 'id' => 'menus',
  879. 'title' => __( 'Menus' ),
  880. 'href' => admin_url( 'nav-menus.php' ),
  881. )
  882. );
  883. }
  884. if ( current_theme_supports( 'custom-background' ) ) {
  885. $wp_admin_bar->add_node(
  886. array(
  887. 'parent' => 'appearance',
  888. 'id' => 'background',
  889. 'title' => __( 'Background' ),
  890. 'href' => admin_url( 'themes.php?page=custom-background' ),
  891. 'meta' => array(
  892. 'class' => 'hide-if-customize',
  893. ),
  894. )
  895. );
  896. }
  897. if ( current_theme_supports( 'custom-header' ) ) {
  898. $wp_admin_bar->add_node(
  899. array(
  900. 'parent' => 'appearance',
  901. 'id' => 'header',
  902. 'title' => __( 'Header' ),
  903. 'href' => admin_url( 'themes.php?page=custom-header' ),
  904. 'meta' => array(
  905. 'class' => 'hide-if-customize',
  906. ),
  907. )
  908. );
  909. }
  910. }
  911. /**
  912. * Provide an update link if theme/plugin/core updates are available.
  913. *
  914. * @since 3.1.0
  915. *
  916. * @param WP_Admin_Bar $wp_admin_bar
  917. */
  918. function wp_admin_bar_updates_menu( $wp_admin_bar ) {
  919. $update_data = wp_get_update_data();
  920. if ( ! $update_data['counts']['total'] ) {
  921. return;
  922. }
  923. $updates_text = sprintf(
  924. /* translators: %s: Total number of updates available. */
  925. _n( '%s update available', '%s updates available', $update_data['counts']['total'] ),
  926. number_format_i18n( $update_data['counts']['total'] )
  927. );
  928. $icon = '<span class="ab-icon" aria-hidden="true"></span>';
  929. $title = '<span class="ab-label" aria-hidden="true">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
  930. $title .= '<span class="screen-reader-text updates-available-text">' . $updates_text . '</span>';
  931. $wp_admin_bar->add_node(
  932. array(
  933. 'id' => 'updates',
  934. 'title' => $icon . $title,
  935. 'href' => network_admin_url( 'update-core.php' ),
  936. )
  937. );
  938. }
  939. /**
  940. * Add search form.
  941. *
  942. * @since 3.3.0
  943. *
  944. * @param WP_Admin_Bar $wp_admin_bar
  945. */
  946. function wp_admin_bar_search_menu( $wp_admin_bar ) {
  947. if ( is_admin() ) {
  948. return;
  949. }
  950. $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
  951. $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
  952. $form .= '<label for="adminbar-search" class="screen-reader-text">' . __( 'Search' ) . '</label>';
  953. $form .= '<input type="submit" class="adminbar-button" value="' . __( 'Search' ) . '" />';
  954. $form .= '</form>';
  955. $wp_admin_bar->add_node(
  956. array(
  957. 'parent' => 'top-secondary',
  958. 'id' => 'search',
  959. 'title' => $form,
  960. 'meta' => array(
  961. 'class' => 'admin-bar-search',
  962. 'tabindex' => -1,
  963. ),
  964. )
  965. );
  966. }
  967. /**
  968. * Add a link to exit recovery mode when Recovery Mode is active.
  969. *
  970. * @since 5.2.0
  971. *
  972. * @param WP_Admin_Bar $wp_admin_bar
  973. */
  974. function wp_admin_bar_recovery_mode_menu( $wp_admin_bar ) {
  975. if ( ! wp_is_recovery_mode() ) {
  976. return;
  977. }
  978. $url = wp_login_url();
  979. $url = add_query_arg( 'action', WP_Recovery_Mode::EXIT_ACTION, $url );
  980. $url = wp_nonce_url( $url, WP_Recovery_Mode::EXIT_ACTION );
  981. $wp_admin_bar->add_node(
  982. array(
  983. 'parent' => 'top-secondary',
  984. 'id' => 'recovery-mode',
  985. 'title' => __( 'Exit Recovery Mode' ),
  986. 'href' => $url,
  987. )
  988. );
  989. }
  990. /**
  991. * Add secondary menus.
  992. *
  993. * @since 3.3.0
  994. *
  995. * @param WP_Admin_Bar $wp_admin_bar
  996. */
  997. function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
  998. $wp_admin_bar->add_group(
  999. array(
  1000. 'id' => 'top-secondary',
  1001. 'meta' => array(
  1002. 'class' => 'ab-top-secondary',
  1003. ),
  1004. )
  1005. );
  1006. $wp_admin_bar->add_group(
  1007. array(
  1008. 'parent' => 'wp-logo',
  1009. 'id' => 'wp-logo-external',
  1010. 'meta' => array(
  1011. 'class' => 'ab-sub-secondary',
  1012. ),
  1013. )
  1014. );
  1015. }
  1016. /**
  1017. * Style and scripts for the admin bar.
  1018. *
  1019. * @since 3.1.0
  1020. */
  1021. function wp_admin_bar_header() {
  1022. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1023. ?>
  1024. <style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style>
  1025. <?php
  1026. }
  1027. /**
  1028. * Default admin bar callback.
  1029. *
  1030. * @since 3.1.0
  1031. */
  1032. function _admin_bar_bump_cb() {
  1033. $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  1034. ?>
  1035. <style<?php echo $type_attr; ?> media="screen">
  1036. html { margin-top: 32px !important; }
  1037. * html body { margin-top: 32px !important; }
  1038. @media screen and ( max-width: 782px ) {
  1039. html { margin-top: 46px !important; }
  1040. * html body { margin-top: 46px !important; }
  1041. }
  1042. </style>
  1043. <?php
  1044. }
  1045. /**
  1046. * Sets the display status of the admin bar.
  1047. *
  1048. * This can be called immediately upon plugin load. It does not need to be called
  1049. * from a function hooked to the {@see 'init'} action.
  1050. *
  1051. * @since 3.1.0
  1052. *
  1053. * @global bool $show_admin_bar
  1054. *
  1055. * @param bool $show Whether to allow the admin bar to show.
  1056. */
  1057. function show_admin_bar( $show ) {
  1058. global $show_admin_bar;
  1059. $show_admin_bar = (bool) $show;
  1060. }
  1061. /**
  1062. * Determines whether the admin bar should be showing.
  1063. *
  1064. * For more information on this and similar theme functions, check out
  1065. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1066. * Conditional Tags} article in the Theme Developer Handbook.
  1067. *
  1068. * @since 3.1.0
  1069. *
  1070. * @global bool $show_admin_bar
  1071. * @global string $pagenow
  1072. *
  1073. * @return bool Whether the admin bar should be showing.
  1074. */
  1075. function is_admin_bar_showing() {
  1076. global $show_admin_bar, $pagenow;
  1077. // For all these types of requests, we never want an admin bar.
  1078. if ( defined( 'XMLRPC_REQUEST' ) || defined( 'DOING_AJAX' ) || defined( 'IFRAME_REQUEST' ) || wp_is_json_request() ) {
  1079. return false;
  1080. }
  1081. if ( is_embed() ) {
  1082. return false;
  1083. }
  1084. // Integrated into the admin.
  1085. if ( is_admin() ) {
  1086. return true;
  1087. }
  1088. if ( ! isset( $show_admin_bar ) ) {
  1089. if ( ! is_user_logged_in() || 'wp-login.php' === $pagenow ) {
  1090. $show_admin_bar = false;
  1091. } else {
  1092. $show_admin_bar = _get_admin_bar_pref();
  1093. }
  1094. }
  1095. /**
  1096. * Filters whether to show the admin bar.
  1097. *
  1098. * Returning false to this hook is the recommended way to hide the admin bar.
  1099. * The user's display preference is used for logged in users.
  1100. *
  1101. * @since 3.1.0
  1102. *
  1103. * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
  1104. */
  1105. $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
  1106. return $show_admin_bar;
  1107. }
  1108. /**
  1109. * Retrieve the admin bar display preference of a user.
  1110. *
  1111. * @since 3.1.0
  1112. * @access private
  1113. *
  1114. * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
  1115. * preference is no longer used.
  1116. * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
  1117. * @return bool Whether the admin bar should be showing for this user.
  1118. */
  1119. function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
  1120. $pref = get_user_option( "show_admin_bar_{$context}", $user );
  1121. if ( false === $pref ) {
  1122. return true;
  1123. }
  1124. return 'true' === $pref;
  1125. }