PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/admin-bar.php

https://bitbucket.org/zachisit/zachis.it-m
PHP | 763 lines | 499 code | 112 blank | 152 comment | 83 complexity | cd19f18c6ae131315dd2af2d3998b5f3 MD5 | raw file
  1. <?php
  2. /**
  3. * Admin Bar
  4. *
  5. * This code handles the building and rendering of the press bar.
  6. */
  7. /**
  8. * Instantiate the admin bar object and set it up as a global for access elsewhere.
  9. *
  10. * To hide the admin bar, you're looking in the wrong place. Unhooking this function will not
  11. * properly remove the admin bar. For that, use show_admin_bar(false) or the show_admin_bar filter.
  12. *
  13. * @since 3.1.0
  14. * @access private
  15. * @return bool Whether the admin bar was successfully initialized.
  16. */
  17. function _wp_admin_bar_init() {
  18. global $wp_admin_bar;
  19. if ( ! is_admin_bar_showing() )
  20. return false;
  21. /* Load the admin bar class code ready for instantiation */
  22. require( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
  23. /* Instantiate the admin bar */
  24. $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
  25. if ( class_exists( $admin_bar_class ) )
  26. $wp_admin_bar = new $admin_bar_class;
  27. else
  28. return false;
  29. $wp_admin_bar->initialize();
  30. $wp_admin_bar->add_menus();
  31. return true;
  32. }
  33. add_action( 'init', '_wp_admin_bar_init' ); // Don't remove. Wrong way to disable.
  34. /**
  35. * Render the admin bar to the page based on the $wp_admin_bar->menu member var.
  36. * This is called very late on the footer actions so that it will render after anything else being
  37. * added to the footer.
  38. *
  39. * It includes the action "admin_bar_menu" which should be used to hook in and
  40. * add new menus to the admin bar. That way you can be sure that you are adding at most optimal point,
  41. * right before the admin bar is rendered. This also gives you access to the $post global, among others.
  42. *
  43. * @since 3.1.0
  44. */
  45. function wp_admin_bar_render() {
  46. global $wp_admin_bar;
  47. if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
  48. return false;
  49. do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
  50. do_action( 'wp_before_admin_bar_render' );
  51. $wp_admin_bar->render();
  52. do_action( 'wp_after_admin_bar_render' );
  53. }
  54. add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
  55. add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
  56. /**
  57. * Add the WordPress logo menu.
  58. *
  59. * @since 3.3.0
  60. */
  61. function wp_admin_bar_wp_menu( $wp_admin_bar ) {
  62. $wp_admin_bar->add_menu( array(
  63. 'id' => 'wp-logo',
  64. 'title' => '<span class="ab-icon"></span>',
  65. 'href' => self_admin_url( 'about.php' ),
  66. 'meta' => array(
  67. 'title' => __('About WordPress'),
  68. ),
  69. ) );
  70. if ( is_user_logged_in() ) {
  71. // Add "About WordPress" link
  72. $wp_admin_bar->add_menu( array(
  73. 'parent' => 'wp-logo',
  74. 'id' => 'about',
  75. 'title' => __('About WordPress'),
  76. 'href' => self_admin_url( 'about.php' ),
  77. ) );
  78. }
  79. // Add WordPress.org link
  80. $wp_admin_bar->add_menu( array(
  81. 'parent' => 'wp-logo-external',
  82. 'id' => 'wporg',
  83. 'title' => __('WordPress.org'),
  84. 'href' => __('http://wordpress.org/'),
  85. ) );
  86. // Add codex link
  87. $wp_admin_bar->add_menu( array(
  88. 'parent' => 'wp-logo-external',
  89. 'id' => 'documentation',
  90. 'title' => __('Documentation'),
  91. 'href' => __('http://codex.wordpress.org/'),
  92. ) );
  93. // Add forums link
  94. $wp_admin_bar->add_menu( array(
  95. 'parent' => 'wp-logo-external',
  96. 'id' => 'support-forums',
  97. 'title' => __('Support Forums'),
  98. 'href' => __('http://wordpress.org/support/'),
  99. ) );
  100. // Add feedback link
  101. $wp_admin_bar->add_menu( array(
  102. 'parent' => 'wp-logo-external',
  103. 'id' => 'feedback',
  104. 'title' => __('Feedback'),
  105. 'href' => __('http://wordpress.org/support/forum/requests-and-feedback'),
  106. ) );
  107. }
  108. /**
  109. * Add the "My Account" item.
  110. *
  111. * @since 3.3.0
  112. */
  113. function wp_admin_bar_my_account_item( $wp_admin_bar ) {
  114. $user_id = get_current_user_id();
  115. $current_user = wp_get_current_user();
  116. $profile_url = get_edit_profile_url( $user_id );
  117. if ( ! $user_id )
  118. return;
  119. $avatar = get_avatar( $user_id, 16 );
  120. $howdy = sprintf( __('Howdy, %1$s'), $current_user->display_name );
  121. $class = empty( $avatar ) ? '' : 'with-avatar';
  122. $wp_admin_bar->add_menu( array(
  123. 'id' => 'my-account',
  124. 'parent' => 'top-secondary',
  125. 'title' => $howdy . $avatar,
  126. 'href' => $profile_url,
  127. 'meta' => array(
  128. 'class' => $class,
  129. 'title' => __('My Account'),
  130. ),
  131. ) );
  132. }
  133. /**
  134. * Add the "My Account" submenu items.
  135. *
  136. * @since 3.1.0
  137. */
  138. function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
  139. $user_id = get_current_user_id();
  140. $current_user = wp_get_current_user();
  141. $profile_url = get_edit_profile_url( $user_id );
  142. if ( ! $user_id )
  143. return;
  144. $wp_admin_bar->add_group( array(
  145. 'parent' => 'my-account',
  146. 'id' => 'user-actions',
  147. ) );
  148. $user_info = get_avatar( $user_id, 64 );
  149. $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
  150. if ( $current_user->display_name !== $current_user->user_nicename )
  151. $user_info .= "<span class='username'>{$current_user->user_nicename}</span>";
  152. $wp_admin_bar->add_menu( array(
  153. 'parent' => 'user-actions',
  154. 'id' => 'user-info',
  155. 'title' => $user_info,
  156. 'href' => $profile_url,
  157. 'meta' => array(
  158. 'tabindex' => -1,
  159. ),
  160. ) );
  161. $wp_admin_bar->add_menu( array(
  162. 'parent' => 'user-actions',
  163. 'id' => 'edit-profile',
  164. 'title' => __( 'Edit My Profile' ),
  165. 'href' => $profile_url,
  166. ) );
  167. $wp_admin_bar->add_menu( array(
  168. 'parent' => 'user-actions',
  169. 'id' => 'logout',
  170. 'title' => __( 'Log Out' ),
  171. 'href' => wp_logout_url(),
  172. ) );
  173. }
  174. /**
  175. * Add the "Site Name" menu.
  176. *
  177. * @since 3.3.0
  178. */
  179. function wp_admin_bar_site_menu( $wp_admin_bar ) {
  180. global $current_site;
  181. // Don't show for logged out users.
  182. if ( ! is_user_logged_in() )
  183. return;
  184. // Show only when the user is a member of this site, or they're a super admin.
  185. if ( ! is_user_member_of_blog() && ! is_super_admin() )
  186. return;
  187. $blogname = get_bloginfo('name');
  188. if ( empty( $blogname ) )
  189. $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() );
  190. if ( is_network_admin() ) {
  191. $blogname = sprintf( __('Network Admin: %s'), esc_html( $current_site->site_name ) );
  192. } elseif ( is_user_admin() ) {
  193. $blogname = sprintf( __('Global Dashboard: %s'), esc_html( $current_site->site_name ) );
  194. }
  195. $title = wp_html_excerpt( $blogname, 40 );
  196. if ( $title != $blogname )
  197. $title = trim( $title ) . '&hellip;';
  198. $wp_admin_bar->add_menu( array(
  199. 'id' => 'site-name',
  200. 'title' => $title,
  201. 'href' => is_admin() ? home_url( '/' ) : admin_url(),
  202. ) );
  203. // Create submenu items.
  204. if ( is_admin() ) {
  205. // Add an option to visit the site.
  206. $wp_admin_bar->add_menu( array(
  207. 'parent' => 'site-name',
  208. 'id' => 'view-site',
  209. 'title' => __( 'Visit Site' ),
  210. 'href' => home_url( '/' ),
  211. ) );
  212. if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) {
  213. $wp_admin_bar->add_menu( array(
  214. 'parent' => 'site-name',
  215. 'id' => 'edit-site',
  216. 'title' => __( 'Edit Site' ),
  217. 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ),
  218. ) );
  219. }
  220. } else {
  221. // We're on the front end, link to the Dashboard.
  222. $wp_admin_bar->add_menu( array(
  223. 'parent' => 'site-name',
  224. 'id' => 'dashboard',
  225. 'title' => __( 'Dashboard' ),
  226. 'href' => admin_url(),
  227. ) );
  228. // Add the appearance submenu items.
  229. wp_admin_bar_appearance_menu( $wp_admin_bar );
  230. }
  231. }
  232. /**
  233. * Add the "My Sites/[Site Name]" menu and all submenus.
  234. *
  235. * @since 3.1.0
  236. */
  237. function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
  238. global $wpdb;
  239. // Don't show for logged out users or single site mode.
  240. if ( ! is_user_logged_in() || ! is_multisite() )
  241. return;
  242. // Show only when the user has at least one site, or they're a super admin.
  243. if ( count( $wp_admin_bar->user->blogs ) < 1 && ! is_super_admin() )
  244. return;
  245. $wp_admin_bar->add_menu( array(
  246. 'id' => 'my-sites',
  247. 'title' => __( 'My Sites' ),
  248. 'href' => admin_url( 'my-sites.php' ),
  249. ) );
  250. if ( is_super_admin() ) {
  251. $wp_admin_bar->add_group( array(
  252. 'parent' => 'my-sites',
  253. 'id' => 'my-sites-super-admin',
  254. ) );
  255. $wp_admin_bar->add_menu( array(
  256. 'parent' => 'my-sites-super-admin',
  257. 'id' => 'network-admin',
  258. 'title' => __('Network Admin'),
  259. 'href' => network_admin_url(),
  260. ) );
  261. $wp_admin_bar->add_menu( array(
  262. 'parent' => 'network-admin',
  263. 'id' => 'network-admin-d',
  264. 'title' => __( 'Dashboard' ),
  265. 'href' => network_admin_url(),
  266. ) );
  267. $wp_admin_bar->add_menu( array(
  268. 'parent' => 'network-admin',
  269. 'id' => 'network-admin-s',
  270. 'title' => __( 'Sites' ),
  271. 'href' => network_admin_url( 'sites.php' ),
  272. ) );
  273. $wp_admin_bar->add_menu( array(
  274. 'parent' => 'network-admin',
  275. 'id' => 'network-admin-u',
  276. 'title' => __( 'Users' ),
  277. 'href' => network_admin_url( 'users.php' ),
  278. ) );
  279. $wp_admin_bar->add_menu( array(
  280. 'parent' => 'network-admin',
  281. 'id' => 'network-admin-v',
  282. 'title' => __( 'Visit Network' ),
  283. 'href' => network_home_url(),
  284. ) );
  285. }
  286. // Add site links
  287. $wp_admin_bar->add_group( array(
  288. 'parent' => 'my-sites',
  289. 'id' => 'my-sites-list',
  290. 'meta' => array(
  291. 'class' => is_super_admin() ? 'ab-sub-secondary' : '',
  292. ),
  293. ) );
  294. $blue_wp_logo_url = includes_url('images/wpmini-blue.png');
  295. foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
  296. // @todo Replace with some favicon lookup.
  297. //$blavatar = '<img src="' . esc_url( blavatar_url( blavatar_domain( $blog->siteurl ), 'img', 16, $blue_wp_logo_url ) ) . '" alt="Blavatar" width="16" height="16" />';
  298. $blavatar = '<img src="' . esc_url($blue_wp_logo_url) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
  299. $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
  300. $menu_id = 'blog-' . $blog->userblog_id;
  301. $wp_admin_bar->add_menu( array(
  302. 'parent' => 'my-sites-list',
  303. 'id' => $menu_id,
  304. 'title' => $blavatar . $blogname,
  305. 'href' => get_admin_url( $blog->userblog_id ),
  306. ) );
  307. $wp_admin_bar->add_menu( array(
  308. 'parent' => $menu_id,
  309. 'id' => $menu_id . '-d',
  310. 'title' => __( 'Dashboard' ),
  311. 'href' => get_admin_url( $blog->userblog_id ),
  312. ) );
  313. if ( current_user_can_for_blog( $blog->userblog_id, 'edit_posts' ) ) {
  314. $wp_admin_bar->add_menu( array(
  315. 'parent' => $menu_id,
  316. 'id' => $menu_id . '-n',
  317. 'title' => __( 'New Post' ),
  318. 'href' => get_admin_url( $blog->userblog_id, 'post-new.php' ),
  319. ) );
  320. $wp_admin_bar->add_menu( array(
  321. 'parent' => $menu_id,
  322. 'id' => $menu_id . '-c',
  323. 'title' => __( 'Manage Comments' ),
  324. 'href' => get_admin_url( $blog->userblog_id, 'edit-comments.php' ),
  325. ) );
  326. }
  327. $wp_admin_bar->add_menu( array(
  328. 'parent' => $menu_id,
  329. 'id' => $menu_id . '-v',
  330. 'title' => __( 'Visit Site' ),
  331. 'href' => get_home_url( $blog->userblog_id, '/' ),
  332. ) );
  333. }
  334. }
  335. /**
  336. * Provide a shortlink.
  337. *
  338. * @since 3.1.0
  339. */
  340. function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
  341. $short = wp_get_shortlink( 0, 'query' );
  342. $id = 'get-shortlink';
  343. if ( empty( $short ) )
  344. return;
  345. $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
  346. $wp_admin_bar->add_menu( array(
  347. 'id' => $id,
  348. 'title' => __( 'Shortlink' ),
  349. 'href' => $short,
  350. 'meta' => array( 'html' => $html ),
  351. ) );
  352. }
  353. /**
  354. * Provide an edit link for posts and terms.
  355. *
  356. * @since 3.1.0
  357. */
  358. function wp_admin_bar_edit_menu( $wp_admin_bar ) {
  359. global $post, $tag, $wp_the_query;
  360. if ( is_admin() ) {
  361. $current_screen = get_current_screen();
  362. if ( 'post' == $current_screen->base
  363. && 'add' != $current_screen->action
  364. && ( $post_type_object = get_post_type_object( $post->post_type ) )
  365. && current_user_can( $post_type_object->cap->read_post, $post->ID )
  366. && ( $post_type_object->public ) )
  367. {
  368. $wp_admin_bar->add_menu( array(
  369. 'id' => 'view',
  370. 'title' => $post_type_object->labels->view_item,
  371. 'href' => get_permalink( $post->ID )
  372. ) );
  373. } elseif ( 'edit-tags' == $current_screen->base
  374. && isset( $tag ) && is_object( $tag )
  375. && ( $tax = get_taxonomy( $tag->taxonomy ) )
  376. && $tax->public )
  377. {
  378. $wp_admin_bar->add_menu( array(
  379. 'id' => 'view',
  380. 'title' => $tax->labels->view_item,
  381. 'href' => get_term_link( $tag )
  382. ) );
  383. }
  384. } else {
  385. $current_object = $wp_the_query->get_queried_object();
  386. if ( empty( $current_object ) )
  387. return;
  388. if ( ! empty( $current_object->post_type )
  389. && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
  390. && current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
  391. && ( $post_type_object->show_ui || 'attachment' == $current_object->post_type ) )
  392. {
  393. $wp_admin_bar->add_menu( array(
  394. 'id' => 'edit',
  395. 'title' => $post_type_object->labels->edit_item,
  396. 'href' => get_edit_post_link( $current_object->ID )
  397. ) );
  398. } elseif ( ! empty( $current_object->taxonomy )
  399. && ( $tax = get_taxonomy( $current_object->taxonomy ) )
  400. && current_user_can( $tax->cap->edit_terms )
  401. && $tax->show_ui )
  402. {
  403. $wp_admin_bar->add_menu( array(
  404. 'id' => 'edit',
  405. 'title' => $tax->labels->edit_item,
  406. 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
  407. ) );
  408. }
  409. }
  410. }
  411. /**
  412. * Add "Add New" menu.
  413. *
  414. * @since 3.1.0
  415. */
  416. function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
  417. $actions = array();
  418. $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
  419. if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->edit_posts ) ) {
  420. $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
  421. unset( $cpts['post'] );
  422. }
  423. if ( current_user_can( 'upload_files' ) )
  424. $actions[ 'media-new.php' ] = array( _x( 'Media', 'add new from admin bar' ), 'new-media' );
  425. if ( current_user_can( 'manage_links' ) )
  426. $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
  427. if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->edit_posts ) ) {
  428. $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
  429. unset( $cpts['page'] );
  430. }
  431. // Add any additional custom post types.
  432. foreach ( $cpts as $cpt ) {
  433. if ( ! current_user_can( $cpt->cap->edit_posts ) )
  434. continue;
  435. $key = 'post-new.php?post_type=' . $cpt->name;
  436. $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name );
  437. }
  438. if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
  439. $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
  440. if ( ! $actions )
  441. return;
  442. $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
  443. $wp_admin_bar->add_menu( array(
  444. 'id' => 'new-content',
  445. 'title' => $title,
  446. 'href' => admin_url( current( array_keys( $actions ) ) ),
  447. 'meta' => array(
  448. 'title' => _x( 'Add New', 'admin bar menu group label' ),
  449. ),
  450. ) );
  451. foreach ( $actions as $link => $action ) {
  452. list( $title, $id ) = $action;
  453. $wp_admin_bar->add_menu( array(
  454. 'parent' => 'new-content',
  455. 'id' => $id,
  456. 'title' => $title,
  457. 'href' => admin_url( $link )
  458. ) );
  459. }
  460. }
  461. /**
  462. * Add edit comments link with awaiting moderation count bubble.
  463. *
  464. * @since 3.1.0
  465. */
  466. function wp_admin_bar_comments_menu( $wp_admin_bar ) {
  467. if ( !current_user_can('edit_posts') )
  468. return;
  469. $awaiting_mod = wp_count_comments();
  470. $awaiting_mod = $awaiting_mod->moderated;
  471. $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
  472. $icon = '<span class="ab-icon"></span>';
  473. $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
  474. $wp_admin_bar->add_menu( array(
  475. 'id' => 'comments',
  476. 'title' => $icon . $title,
  477. 'href' => admin_url('edit-comments.php'),
  478. 'meta' => array( 'title' => $awaiting_title ),
  479. ) );
  480. }
  481. /**
  482. * Add appearance submenu items to the "Site Name" menu.
  483. *
  484. * @since 3.1.0
  485. */
  486. function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
  487. $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
  488. if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
  489. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
  490. if ( ! current_user_can( 'edit_theme_options' ) )
  491. return;
  492. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  493. $wp_admin_bar->add_menu( array(
  494. 'parent' => 'appearance',
  495. 'id' => 'customize',
  496. 'title' => __('Customize'),
  497. 'href' => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
  498. 'meta' => array(
  499. 'class' => 'hide-if-no-customize',
  500. ),
  501. ) );
  502. add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
  503. if ( current_theme_supports( 'widgets' ) )
  504. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
  505. if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
  506. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
  507. if ( current_theme_supports( 'custom-background' ) )
  508. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
  509. if ( current_theme_supports( 'custom-header' ) )
  510. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
  511. }
  512. /**
  513. * Provide an update link if theme/plugin/core updates are available.
  514. *
  515. * @since 3.1.0
  516. */
  517. function wp_admin_bar_updates_menu( $wp_admin_bar ) {
  518. $update_data = wp_get_update_data();
  519. if ( !$update_data['counts']['total'] )
  520. return;
  521. $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
  522. $wp_admin_bar->add_menu( array(
  523. 'id' => 'updates',
  524. 'title' => $title,
  525. 'href' => network_admin_url( 'update-core.php' ),
  526. 'meta' => array(
  527. 'title' => $update_data['title'],
  528. ),
  529. ) );
  530. }
  531. /**
  532. * Add search form.
  533. *
  534. * @since 3.3.0
  535. */
  536. function wp_admin_bar_search_menu( $wp_admin_bar ) {
  537. if ( is_admin() )
  538. return;
  539. $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
  540. $form .= '<input class="adminbar-input" name="s" id="adminbar-search" tabindex="10" type="text" value="" maxlength="150" />';
  541. $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
  542. $form .= '</form>';
  543. $wp_admin_bar->add_menu( array(
  544. 'parent' => 'top-secondary',
  545. 'id' => 'search',
  546. 'title' => $form,
  547. 'meta' => array(
  548. 'class' => 'admin-bar-search',
  549. 'tabindex' => -1,
  550. )
  551. ) );
  552. }
  553. /**
  554. * Add secondary menus.
  555. *
  556. * @since 3.3.0
  557. */
  558. function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
  559. $wp_admin_bar->add_group( array(
  560. 'id' => 'top-secondary',
  561. 'meta' => array(
  562. 'class' => 'ab-top-secondary',
  563. ),
  564. ) );
  565. $wp_admin_bar->add_group( array(
  566. 'parent' => 'wp-logo',
  567. 'id' => 'wp-logo-external',
  568. 'meta' => array(
  569. 'class' => 'ab-sub-secondary',
  570. ),
  571. ) );
  572. }
  573. /**
  574. * Style and scripts for the admin bar.
  575. *
  576. * @since 3.1.0
  577. *
  578. */
  579. function wp_admin_bar_header() { ?>
  580. <style type="text/css" media="print">#wpadminbar { display:none; }</style>
  581. <?php
  582. }
  583. /**
  584. * Default admin bar callback.
  585. *
  586. * @since 3.1.0
  587. *
  588. */
  589. function _admin_bar_bump_cb() { ?>
  590. <style type="text/css" media="screen">
  591. html { margin-top: 28px !important; }
  592. * html body { margin-top: 28px !important; }
  593. </style>
  594. <?php
  595. }
  596. /**
  597. * Set the display status of the admin bar.
  598. *
  599. * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
  600. *
  601. * @since 3.1.0
  602. *
  603. * @param bool $show Whether to allow the admin bar to show.
  604. * @return void
  605. */
  606. function show_admin_bar( $show ) {
  607. global $show_admin_bar;
  608. $show_admin_bar = (bool) $show;
  609. }
  610. /**
  611. * Determine whether the admin bar should be showing.
  612. *
  613. * @since 3.1.0
  614. *
  615. * @return bool Whether the admin bar should be showing.
  616. */
  617. function is_admin_bar_showing() {
  618. global $show_admin_bar, $pagenow;
  619. // For all these types of requests, we never want an admin bar.
  620. if ( defined('XMLRPC_REQUEST') || defined('APP_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
  621. return false;
  622. // Integrated into the admin.
  623. if ( is_admin() )
  624. return true;
  625. if ( ! isset( $show_admin_bar ) ) {
  626. if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
  627. $show_admin_bar = false;
  628. } else {
  629. $show_admin_bar = _get_admin_bar_pref();
  630. }
  631. }
  632. $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
  633. return $show_admin_bar;
  634. }
  635. /**
  636. * Retrieve the admin bar display preference of a user.
  637. *
  638. * @since 3.1.0
  639. * @access private
  640. *
  641. * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
  642. * preference is no longer used.
  643. * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
  644. * @return bool Whether the admin bar should be showing for this user.
  645. */
  646. function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
  647. $pref = get_user_option( "show_admin_bar_{$context}", $user );
  648. if ( false === $pref )
  649. return true;
  650. return 'true' === $pref;
  651. }