PageRenderTime 39ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

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