PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/admin-bar.php

https://github.com/coffeant/wordpress
PHP | 376 lines | 187 code | 70 blank | 119 comment | 52 complexity | d39780698aa95e841f8448f3de47cbab MD5 | raw file
Possible License(s): AGPL-1.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. * 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. $wp_admin_bar->load_user_locale_translations();
  50. do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
  51. do_action( 'wp_before_admin_bar_render' );
  52. $wp_admin_bar->render();
  53. do_action( 'wp_after_admin_bar_render' );
  54. $wp_admin_bar->unload_user_locale_translations();
  55. }
  56. add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
  57. add_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
  58. /**
  59. * Add the "My Account" menu and all submenus.
  60. *
  61. * @since 3.1.0
  62. */
  63. function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
  64. global $user_identity;
  65. $user_id = get_current_user_id();
  66. if ( 0 != $user_id ) {
  67. /* Add the 'My Account' menu */
  68. $avatar = get_avatar( get_current_user_id(), 16 );
  69. $id = ( ! empty( $avatar ) ) ? 'my-account-with-avatar' : 'my-account';
  70. $wp_admin_bar->add_menu( array( 'id' => $id, 'title' => $avatar . $user_identity, 'href' => get_edit_profile_url( $user_id ) ) );
  71. /* Add the "My Account" sub menus */
  72. $wp_admin_bar->add_menu( array( 'parent' => $id, 'title' => __( 'Edit My Profile' ), 'href' => get_edit_profile_url( $user_id ) ) );
  73. if ( is_multisite() )
  74. $wp_admin_bar->add_menu( array( 'parent' => $id, 'title' => __( 'Dashboard' ), 'href' => get_dashboard_url( $user_id ) ) );
  75. else
  76. $wp_admin_bar->add_menu( array( 'parent' => $id, 'title' => __( 'Dashboard' ), 'href' => admin_url() ) );
  77. $wp_admin_bar->add_menu( array( 'parent' => $id, 'title' => __( 'Log Out' ), 'href' => wp_logout_url() ) );
  78. }
  79. }
  80. /**
  81. * Add the "My Sites/[Site Name]" menu and all submenus.
  82. *
  83. * @since 3.1.0
  84. */
  85. function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
  86. global $wpdb;
  87. /* Add the 'My Sites' menu if the user has more than one site. */
  88. if ( count( $wp_admin_bar->user->blogs ) <= 1 )
  89. return;
  90. $wp_admin_bar->add_menu( array( 'id' => 'my-blogs', 'title' => __( 'My Sites' ), 'href' => admin_url( 'my-sites.php' ) ) );
  91. $default = includes_url('images/wpmini-blue.png');
  92. foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
  93. // @todo Replace with some favicon lookup.
  94. //$blavatar = '<img src="' . esc_url( blavatar_url( blavatar_domain( $blog->siteurl ), 'img', 16, $default ) ) . '" alt="Blavatar" width="16" height="16" />';
  95. $blavatar = '<img src="' . esc_url($default) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
  96. $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
  97. $wp_admin_bar->add_menu( array( 'parent' => 'my-blogs', 'id' => 'blog-' . $blog->userblog_id, 'title' => $blavatar . $blogname, 'href' => get_admin_url($blog->userblog_id) ) );
  98. $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-d', 'title' => __( 'Dashboard' ), 'href' => get_admin_url($blog->userblog_id) ) );
  99. if ( current_user_can_for_blog( $blog->userblog_id, 'edit_posts' ) ) {
  100. $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-n', 'title' => __( 'New Post' ), 'href' => get_admin_url($blog->userblog_id, 'post-new.php') ) );
  101. $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-c', 'title' => __( 'Manage Comments' ), 'href' => get_admin_url($blog->userblog_id, 'edit-comments.php') ) );
  102. }
  103. $wp_admin_bar->add_menu( array( 'parent' => 'blog-' . $blog->userblog_id, 'id' => 'blog-' . $blog->userblog_id . '-v', 'title' => __( 'Visit Site' ), 'href' => get_home_url($blog->userblog_id) ) );
  104. }
  105. }
  106. /**
  107. * Provide a shortlink.
  108. *
  109. * @since 3.1.0
  110. */
  111. function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
  112. $short = wp_get_shortlink( 0, 'query' );
  113. $id = 'get-shortlink';
  114. if ( empty( $short ) )
  115. return;
  116. $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
  117. $wp_admin_bar->add_menu( array(
  118. 'id' => $id,
  119. 'title' => __( 'Shortlink' ),
  120. 'href' => $short,
  121. 'meta' => array( 'html' => $html ),
  122. ) );
  123. }
  124. /**
  125. * Provide an edit link for posts and terms.
  126. *
  127. * @since 3.1.0
  128. */
  129. function wp_admin_bar_edit_menu( $wp_admin_bar ) {
  130. $current_object = get_queried_object();
  131. if ( empty($current_object) )
  132. return;
  133. if ( ! empty( $current_object->post_type ) && ( $post_type_object = get_post_type_object( $current_object->post_type ) ) && current_user_can( $post_type_object->cap->edit_post, $current_object->ID ) && ( $post_type_object->show_ui || 'attachment' == $current_object->post_type ) ) {
  134. $wp_admin_bar->add_menu( array( 'id' => 'edit', 'title' => $post_type_object->labels->edit_item, 'href' => get_edit_post_link( $current_object->ID ) ) );
  135. } elseif ( ! empty( $current_object->taxonomy ) && ( $tax = get_taxonomy( $current_object->taxonomy ) ) && current_user_can( $tax->cap->edit_terms ) && $tax->show_ui ) {
  136. $wp_admin_bar->add_menu( array( 'id' => 'edit', 'title' => $tax->labels->edit_item, 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy ) ) );
  137. }
  138. }
  139. /**
  140. * Add "Add New" menu.
  141. *
  142. * @since 3.1.0
  143. */
  144. function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
  145. $actions = array();
  146. foreach ( (array) get_post_types( array( 'show_ui' => true ), 'objects' ) as $ptype_obj ) {
  147. if ( true !== $ptype_obj->show_in_menu || ! current_user_can( $ptype_obj->cap->edit_posts ) )
  148. continue;
  149. $actions[ 'post-new.php?post_type=' . $ptype_obj->name ] = array( $ptype_obj->labels->singular_name, $ptype_obj->cap->edit_posts, 'new-' . $ptype_obj->name );
  150. }
  151. if ( empty( $actions ) )
  152. return;
  153. $wp_admin_bar->add_menu( array( 'id' => 'new-content', 'title' => _x( 'Add New', 'admin bar menu group label' ), 'href' => admin_url( array_shift( array_keys( $actions ) ) ) ) );
  154. foreach ( $actions as $link => $action ) {
  155. $wp_admin_bar->add_menu( array( 'parent' => 'new-content', 'id' => $action[2], 'title' => $action[0], 'href' => admin_url($link) ) );
  156. }
  157. }
  158. /**
  159. * Add edit comments link with awaiting moderation count bubble.
  160. *
  161. * @since 3.1.0
  162. */
  163. function wp_admin_bar_comments_menu( $wp_admin_bar ) {
  164. if ( !current_user_can('edit_posts') )
  165. return;
  166. $awaiting_mod = wp_count_comments();
  167. $awaiting_mod = $awaiting_mod->moderated;
  168. $awaiting_mod = $awaiting_mod ? "<span id='ab-awaiting-mod' class='pending-count'>" . number_format_i18n( $awaiting_mod ) . "</span>" : '';
  169. $wp_admin_bar->add_menu( array( 'id' => 'comments', 'title' => sprintf( __('Comments %s'), $awaiting_mod ), 'href' => admin_url('edit-comments.php') ) );
  170. }
  171. /**
  172. * Add "Appearance" menu with widget and nav menu submenu.
  173. *
  174. * @since 3.1.0
  175. */
  176. function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
  177. // You can have edit_theme_options but not switch_themes.
  178. if ( ! current_user_can('switch_themes') && ! current_user_can( 'edit_theme_options' ) )
  179. return;
  180. $wp_admin_bar->add_menu( array( 'id' => 'appearance', 'title' => __('Appearance'), 'href' => admin_url('themes.php') ) );
  181. if ( ! current_user_can( 'edit_theme_options' ) )
  182. return;
  183. if ( current_user_can( 'switch_themes' ) )
  184. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
  185. if ( current_theme_supports( 'widgets' ) )
  186. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
  187. if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
  188. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
  189. if ( current_theme_supports( 'custom-background' ) )
  190. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
  191. if ( current_theme_supports( 'custom-header' ) )
  192. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
  193. }
  194. /**
  195. * Provide an update link if theme/plugin/core updates are available.
  196. *
  197. * @since 3.1.0
  198. */
  199. function wp_admin_bar_updates_menu( $wp_admin_bar ) {
  200. if ( !current_user_can('install_plugins') )
  201. return;
  202. $plugin_update_count = $theme_update_count = $wordpress_update_count = 0;
  203. $update_plugins = get_site_transient( 'update_plugins' );
  204. if ( !empty($update_plugins->response) )
  205. $plugin_update_count = count( $update_plugins->response );
  206. $update_themes = get_site_transient( 'update_themes' );
  207. if ( !empty($update_themes->response) )
  208. $theme_update_count = count( $update_themes->response );
  209. /* @todo get_core_updates() is only available on admin page loads
  210. $update_wordpress = get_core_updates( array('dismissed' => false) );
  211. if ( !empty($update_wordpress) && !in_array( $update_wordpress[0]->response, array('development', 'latest') ) )
  212. $wordpress_update_count = 1;
  213. */
  214. $update_count = $plugin_update_count + $theme_update_count + $wordpress_update_count;
  215. if ( !$update_count )
  216. return;
  217. $update_title = array();
  218. if ( $wordpress_update_count )
  219. $update_title[] = sprintf(__('%d WordPress Update'), $wordpress_update_count);
  220. if ( $plugin_update_count )
  221. $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $plugin_update_count), $plugin_update_count);
  222. if ( $theme_update_count )
  223. $update_title[] = sprintf(_n('%d Theme Update', '%d Themes Updates', $theme_update_count), $theme_update_count);
  224. $update_title = !empty($update_title) ? esc_attr(implode(', ', $update_title)) : '';
  225. $update_title = "<span title='$update_title'>";
  226. $update_title .= sprintf( __('Updates %s'), "<span id='ab-updates' class='update-count'>" . number_format_i18n($update_count) . '</span>' );
  227. $update_title .= '</span>';
  228. $wp_admin_bar->add_menu( array( 'id' => 'updates', 'title' => $update_title, 'href' => network_admin_url( 'update-core.php' ) ) );
  229. }
  230. /**
  231. * Style and scripts for the admin bar.
  232. *
  233. * @since 3.1.0
  234. *
  235. */
  236. function wp_admin_bar_header() { ?>
  237. <style type="text/css" media="print">#wpadminbar { display:none; }</style>
  238. <?php
  239. }
  240. /**
  241. * Default admin bar callback.
  242. *
  243. * @since 3.1.0
  244. *
  245. */
  246. function _admin_bar_bump_cb() { ?>
  247. <style type="text/css">
  248. html { margin-top: 28px !important; }
  249. * html body { margin-top: 28px !important; }
  250. </style>
  251. <?php
  252. }
  253. /**
  254. * Set the display status of the admin bar
  255. *
  256. * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
  257. *
  258. * @since 3.1.0
  259. *
  260. * @param bool $show Whether to allow the admin bar to show.
  261. * @return void
  262. */
  263. function show_admin_bar( $show ) {
  264. global $show_admin_bar;
  265. $show_admin_bar = (bool) $show;
  266. }
  267. /**
  268. * Determine whether the admin bar should be showing.
  269. *
  270. * @since 3.1.0
  271. *
  272. * @return bool Whether the admin bar should be showing.
  273. */
  274. function is_admin_bar_showing() {
  275. global $show_admin_bar, $pagenow;
  276. /* For all these types of request we never want an admin bar period */
  277. if ( defined('XMLRPC_REQUEST') || defined('APP_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
  278. return false;
  279. if ( ! isset( $show_admin_bar ) ) {
  280. if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
  281. $show_admin_bar = false;
  282. } else {
  283. $context = is_admin() ? 'admin' : 'front';
  284. $show_admin_bar = _get_admin_bar_pref( $context );
  285. }
  286. }
  287. $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
  288. return $show_admin_bar;
  289. }
  290. /**
  291. * Retrieve the admin bar display preference of a user based on context.
  292. *
  293. * @since 3.1.0
  294. * @access private
  295. *
  296. * @param string $context Context of this preference check, either 'admin' or 'front'
  297. * @param int $user Optional. ID of the user to check, defaults to 0 for current user
  298. * @return bool Whether the admin bar should be showing for this user
  299. */
  300. function _get_admin_bar_pref( $context, $user = 0 ) {
  301. $pref = get_user_option( "show_admin_bar_{$context}", $user );
  302. if ( false === $pref )
  303. return 'admin' != $context || is_multisite();
  304. return 'true' === $pref;
  305. }
  306. ?>