PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/admin-bar.php

https://bitbucket.org/dkrzos/phc
PHP | 770 lines | 504 code | 115 blank | 151 comment | 85 complexity | 541cfed36f6462c8950259271a899827 MD5 | raw file
Possible License(s): GPL-2.0
  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. foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
  295. switch_to_blog( $blog->userblog_id );
  296. $blavatar = '<div class="blavatar"></div>';
  297. $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
  298. $menu_id = 'blog-' . $blog->userblog_id;
  299. $wp_admin_bar->add_menu( array(
  300. 'parent' => 'my-sites-list',
  301. 'id' => $menu_id,
  302. 'title' => $blavatar . $blogname,
  303. 'href' => admin_url(),
  304. ) );
  305. $wp_admin_bar->add_menu( array(
  306. 'parent' => $menu_id,
  307. 'id' => $menu_id . '-d',
  308. 'title' => __( 'Dashboard' ),
  309. 'href' => admin_url(),
  310. ) );
  311. if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
  312. $wp_admin_bar->add_menu( array(
  313. 'parent' => $menu_id,
  314. 'id' => $menu_id . '-n',
  315. 'title' => __( 'New Post' ),
  316. 'href' => admin_url( 'post-new.php' ),
  317. ) );
  318. }
  319. if ( current_user_can( 'edit_posts' ) ) {
  320. $wp_admin_bar->add_menu( array(
  321. 'parent' => $menu_id,
  322. 'id' => $menu_id . '-c',
  323. 'title' => __( 'Manage Comments' ),
  324. 'href' => admin_url( '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' => home_url( '/' ),
  332. ) );
  333. restore_current_blog();
  334. }
  335. }
  336. /**
  337. * Provide a shortlink.
  338. *
  339. * @since 3.1.0
  340. */
  341. function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
  342. $short = wp_get_shortlink( 0, 'query' );
  343. $id = 'get-shortlink';
  344. if ( empty( $short ) )
  345. return;
  346. $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
  347. $wp_admin_bar->add_menu( array(
  348. 'id' => $id,
  349. 'title' => __( 'Shortlink' ),
  350. 'href' => $short,
  351. 'meta' => array( 'html' => $html ),
  352. ) );
  353. }
  354. /**
  355. * Provide an edit link for posts and terms.
  356. *
  357. * @since 3.1.0
  358. */
  359. function wp_admin_bar_edit_menu( $wp_admin_bar ) {
  360. global $tag, $wp_the_query;
  361. if ( is_admin() ) {
  362. $current_screen = get_current_screen();
  363. $post = get_post();
  364. if ( 'post' == $current_screen->base
  365. && 'add' != $current_screen->action
  366. && ( $post_type_object = get_post_type_object( $post->post_type ) )
  367. && current_user_can( $post_type_object->cap->read_post, $post->ID )
  368. && ( $post_type_object->public )
  369. && ( $post_type_object->show_in_admin_bar ) )
  370. {
  371. $wp_admin_bar->add_menu( array(
  372. 'id' => 'view',
  373. 'title' => $post_type_object->labels->view_item,
  374. 'href' => get_permalink( $post->ID )
  375. ) );
  376. } elseif ( 'edit-tags' == $current_screen->base
  377. && isset( $tag ) && is_object( $tag )
  378. && ( $tax = get_taxonomy( $tag->taxonomy ) )
  379. && $tax->public )
  380. {
  381. $wp_admin_bar->add_menu( array(
  382. 'id' => 'view',
  383. 'title' => $tax->labels->view_item,
  384. 'href' => get_term_link( $tag )
  385. ) );
  386. }
  387. } else {
  388. $current_object = $wp_the_query->get_queried_object();
  389. if ( empty( $current_object ) )
  390. return;
  391. if ( ! empty( $current_object->post_type )
  392. && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
  393. && current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
  394. && $post_type_object->show_ui && $post_type_object->show_in_admin_bar )
  395. {
  396. $wp_admin_bar->add_menu( array(
  397. 'id' => 'edit',
  398. 'title' => $post_type_object->labels->edit_item,
  399. 'href' => get_edit_post_link( $current_object->ID )
  400. ) );
  401. } elseif ( ! empty( $current_object->taxonomy )
  402. && ( $tax = get_taxonomy( $current_object->taxonomy ) )
  403. && current_user_can( $tax->cap->edit_terms )
  404. && $tax->show_ui )
  405. {
  406. $wp_admin_bar->add_menu( array(
  407. 'id' => 'edit',
  408. 'title' => $tax->labels->edit_item,
  409. 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
  410. ) );
  411. }
  412. }
  413. }
  414. /**
  415. * Add "Add New" menu.
  416. *
  417. * @since 3.1.0
  418. */
  419. function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
  420. $actions = array();
  421. $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
  422. if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) )
  423. $actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
  424. if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
  425. $actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
  426. if ( current_user_can( 'manage_links' ) )
  427. $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
  428. if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) )
  429. $actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
  430. unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
  431. // Add any additional custom post types.
  432. foreach ( $cpts as $cpt ) {
  433. if ( ! current_user_can( $cpt->cap->create_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. // Avoid clash with parent node and a 'content' post type.
  439. if ( isset( $actions['post-new.php?post_type=content'] ) )
  440. $actions['post-new.php?post_type=content'][1] = 'add-new-content';
  441. if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
  442. $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'new-user' );
  443. if ( ! $actions )
  444. return;
  445. $title = '<span class="ab-icon"></span><span class="ab-label">' . _x( 'New', 'admin bar menu group label' ) . '</span>';
  446. $wp_admin_bar->add_menu( array(
  447. 'id' => 'new-content',
  448. 'title' => $title,
  449. 'href' => admin_url( current( array_keys( $actions ) ) ),
  450. 'meta' => array(
  451. 'title' => _x( 'Add New', 'admin bar menu group label' ),
  452. ),
  453. ) );
  454. foreach ( $actions as $link => $action ) {
  455. list( $title, $id ) = $action;
  456. $wp_admin_bar->add_menu( array(
  457. 'parent' => 'new-content',
  458. 'id' => $id,
  459. 'title' => $title,
  460. 'href' => admin_url( $link )
  461. ) );
  462. }
  463. }
  464. /**
  465. * Add edit comments link with awaiting moderation count bubble.
  466. *
  467. * @since 3.1.0
  468. */
  469. function wp_admin_bar_comments_menu( $wp_admin_bar ) {
  470. if ( !current_user_can('edit_posts') )
  471. return;
  472. $awaiting_mod = wp_count_comments();
  473. $awaiting_mod = $awaiting_mod->moderated;
  474. $awaiting_title = esc_attr( sprintf( _n( '%s comment awaiting moderation', '%s comments awaiting moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ) );
  475. $icon = '<span class="ab-icon"></span>';
  476. $title = '<span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-' . $awaiting_mod . '">' . number_format_i18n( $awaiting_mod ) . '</span>';
  477. $wp_admin_bar->add_menu( array(
  478. 'id' => 'comments',
  479. 'title' => $icon . $title,
  480. 'href' => admin_url('edit-comments.php'),
  481. 'meta' => array( 'title' => $awaiting_title ),
  482. ) );
  483. }
  484. /**
  485. * Add appearance submenu items to the "Site Name" menu.
  486. *
  487. * @since 3.1.0
  488. */
  489. function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
  490. $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance' ) );
  491. if ( current_user_can( 'switch_themes' ) || current_user_can( 'edit_theme_options' ) )
  492. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
  493. if ( ! current_user_can( 'edit_theme_options' ) )
  494. return;
  495. $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  496. $wp_admin_bar->add_menu( array(
  497. 'parent' => 'appearance',
  498. 'id' => 'customize',
  499. 'title' => __('Customize'),
  500. 'href' => add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ),
  501. 'meta' => array(
  502. 'class' => 'hide-if-no-customize',
  503. ),
  504. ) );
  505. add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
  506. if ( current_theme_supports( 'widgets' ) )
  507. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
  508. if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
  509. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
  510. if ( current_theme_supports( 'custom-background' ) )
  511. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
  512. if ( current_theme_supports( 'custom-header' ) )
  513. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
  514. }
  515. /**
  516. * Provide an update link if theme/plugin/core updates are available.
  517. *
  518. * @since 3.1.0
  519. */
  520. function wp_admin_bar_updates_menu( $wp_admin_bar ) {
  521. $update_data = wp_get_update_data();
  522. if ( !$update_data['counts']['total'] )
  523. return;
  524. $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
  525. $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
  526. $wp_admin_bar->add_menu( array(
  527. 'id' => 'updates',
  528. 'title' => $title,
  529. 'href' => network_admin_url( 'update-core.php' ),
  530. 'meta' => array(
  531. 'title' => $update_data['title'],
  532. ),
  533. ) );
  534. }
  535. /**
  536. * Add search form.
  537. *
  538. * @since 3.3.0
  539. */
  540. function wp_admin_bar_search_menu( $wp_admin_bar ) {
  541. if ( is_admin() )
  542. return;
  543. $form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">';
  544. $form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />';
  545. $form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
  546. $form .= '</form>';
  547. $wp_admin_bar->add_menu( array(
  548. 'parent' => 'top-secondary',
  549. 'id' => 'search',
  550. 'title' => $form,
  551. 'meta' => array(
  552. 'class' => 'admin-bar-search',
  553. 'tabindex' => -1,
  554. )
  555. ) );
  556. }
  557. /**
  558. * Add secondary menus.
  559. *
  560. * @since 3.3.0
  561. */
  562. function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
  563. $wp_admin_bar->add_group( array(
  564. 'id' => 'top-secondary',
  565. 'meta' => array(
  566. 'class' => 'ab-top-secondary',
  567. ),
  568. ) );
  569. $wp_admin_bar->add_group( array(
  570. 'parent' => 'wp-logo',
  571. 'id' => 'wp-logo-external',
  572. 'meta' => array(
  573. 'class' => 'ab-sub-secondary',
  574. ),
  575. ) );
  576. }
  577. /**
  578. * Style and scripts for the admin bar.
  579. *
  580. * @since 3.1.0
  581. *
  582. */
  583. function wp_admin_bar_header() { ?>
  584. <style type="text/css" media="print">#wpadminbar { display:none; }</style>
  585. <?php
  586. }
  587. /**
  588. * Default admin bar callback.
  589. *
  590. * @since 3.1.0
  591. *
  592. */
  593. function _admin_bar_bump_cb() { ?>
  594. <style type="text/css" media="screen">
  595. html { margin-top: 28px !important; }
  596. * html body { margin-top: 28px !important; }
  597. </style>
  598. <?php
  599. }
  600. /**
  601. * Set the display status of the admin bar.
  602. *
  603. * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
  604. *
  605. * @since 3.1.0
  606. *
  607. * @param bool $show Whether to allow the admin bar to show.
  608. * @return void
  609. */
  610. function show_admin_bar( $show ) {
  611. global $show_admin_bar;
  612. $show_admin_bar = (bool) $show;
  613. }
  614. /**
  615. * Determine whether the admin bar should be showing.
  616. *
  617. * @since 3.1.0
  618. *
  619. * @return bool Whether the admin bar should be showing.
  620. */
  621. function is_admin_bar_showing() {
  622. global $show_admin_bar, $pagenow;
  623. // For all these types of requests, we never want an admin bar.
  624. if ( defined('XMLRPC_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
  625. return false;
  626. // Integrated into the admin.
  627. if ( is_admin() )
  628. return true;
  629. if ( ! isset( $show_admin_bar ) ) {
  630. if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
  631. $show_admin_bar = false;
  632. } else {
  633. $show_admin_bar = _get_admin_bar_pref();
  634. }
  635. }
  636. $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
  637. return $show_admin_bar;
  638. }
  639. /**
  640. * Retrieve the admin bar display preference of a user.
  641. *
  642. * @since 3.1.0
  643. * @access private
  644. *
  645. * @param string $context Context of this preference check. Defaults to 'front'. The 'admin'
  646. * preference is no longer used.
  647. * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
  648. * @return bool Whether the admin bar should be showing for this user.
  649. */
  650. function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
  651. $pref = get_user_option( "show_admin_bar_{$context}", $user );
  652. if ( false === $pref )
  653. return true;
  654. return 'true' === $pref;
  655. }