PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/admin-bar.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 450 lines | 247 code | 79 blank | 124 comment | 75 complexity | 8a70960c397aebdc8180f634e60d8d9a MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.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. $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( 'id' => 'edit-profile', 'parent' => $id, 'title' => __( 'Edit My Profile' ), 'href' => get_edit_profile_url( $user_id ) ) );
  73. $wp_admin_bar->add_menu( array( 'id' => 'logout', 'parent' => $id, 'title' => __( 'Log Out' ), 'href' => wp_logout_url() ) );
  74. }
  75. }
  76. /**
  77. * Add the "Dashboard"/"Visit Site" menu.
  78. *
  79. * @since 3.2.0
  80. */
  81. function wp_admin_bar_dashboard_view_site_menu( $wp_admin_bar ) {
  82. $user_id = get_current_user_id();
  83. if ( 0 != $user_id ) {
  84. if ( is_admin() )
  85. $wp_admin_bar->add_menu( array( 'id' => 'view-site', 'title' => __( 'Visit Site' ), 'href' => home_url() ) );
  86. elseif ( is_multisite() )
  87. $wp_admin_bar->add_menu( array( 'id' => 'dashboard', 'title' => __( 'Dashboard' ), 'href' => get_dashboard_url( $user_id ) ) );
  88. else
  89. $wp_admin_bar->add_menu( array( 'id' => 'dashboard', 'title' => __( 'Dashboard' ), 'href' => admin_url() ) );
  90. }
  91. }
  92. /**
  93. * Add the "My Sites/[Site Name]" menu and all submenus.
  94. *
  95. * @since 3.1.0
  96. */
  97. function wp_admin_bar_my_sites_menu( $wp_admin_bar ) {
  98. global $wpdb;
  99. /* Add the 'My Sites' menu if the user has more than one site. */
  100. if ( count( $wp_admin_bar->user->blogs ) <= 1 )
  101. return;
  102. $wp_admin_bar->add_menu( array( 'id' => 'my-blogs', 'title' => __( 'My Sites' ), 'href' => admin_url( 'my-sites.php' ) ) );
  103. $default = includes_url('images/wpmini-blue.png');
  104. foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
  105. // @todo Replace with some favicon lookup.
  106. //$blavatar = '<img src="' . esc_url( blavatar_url( blavatar_domain( $blog->siteurl ), 'img', 16, $default ) ) . '" alt="Blavatar" width="16" height="16" />';
  107. $blavatar = '<img src="' . esc_url($default) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
  108. $blogname = empty( $blog->blogname ) ? $blog->domain : $blog->blogname;
  109. $wp_admin_bar->add_menu( array( 'parent' => 'my-blogs', 'id' => 'blog-' . $blog->userblog_id, 'title' => $blavatar . $blogname, 'href' => get_admin_url($blog->userblog_id) ) );
  110. $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) ) );
  111. if ( current_user_can_for_blog( $blog->userblog_id, 'edit_posts' ) ) {
  112. $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') ) );
  113. $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') ) );
  114. }
  115. $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) ) );
  116. }
  117. }
  118. /**
  119. * Provide a shortlink.
  120. *
  121. * @since 3.1.0
  122. */
  123. function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
  124. $short = wp_get_shortlink( 0, 'query' );
  125. $id = 'get-shortlink';
  126. if ( empty( $short ) )
  127. return;
  128. $html = '<input class="shortlink-input" type="text" readonly="readonly" value="' . esc_attr( $short ) . '" />';
  129. $wp_admin_bar->add_menu( array(
  130. 'id' => $id,
  131. 'title' => __( 'Shortlink' ),
  132. 'href' => $short,
  133. 'meta' => array( 'html' => $html ),
  134. ) );
  135. }
  136. /**
  137. * Provide an edit link for posts and terms.
  138. *
  139. * @since 3.1.0
  140. */
  141. function wp_admin_bar_edit_menu( $wp_admin_bar ) {
  142. global $post, $tag;
  143. if ( is_admin() ) {
  144. $current_screen = get_current_screen();
  145. if ( 'post' == $current_screen->base
  146. && 'add' != $current_screen->action
  147. && ( $post_type_object = get_post_type_object( $post->post_type ) )
  148. && current_user_can( $post_type_object->cap->read_post, $post->ID )
  149. && ( $post_type_object->public ) )
  150. {
  151. $wp_admin_bar->add_menu( array(
  152. 'id' => 'view',
  153. 'title' => $post_type_object->labels->view_item,
  154. 'href' => get_permalink( $post->ID )
  155. ) );
  156. } elseif ( 'edit-tags' == $current_screen->base
  157. && isset( $tag ) && is_object( $tag )
  158. && ( $tax = get_taxonomy( $tag->taxonomy ) )
  159. && $tax->public )
  160. {
  161. $wp_admin_bar->add_menu( array(
  162. 'id' => 'view',
  163. 'title' => $tax->labels->view_item,
  164. 'href' => get_term_link( $tag )
  165. ) );
  166. }
  167. } else {
  168. $current_object = get_queried_object();
  169. if ( empty($current_object) )
  170. return;
  171. if ( ! empty( $current_object->post_type )
  172. && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
  173. && current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
  174. && ( $post_type_object->show_ui || 'attachment' == $current_object->post_type ) )
  175. {
  176. $wp_admin_bar->add_menu( array(
  177. 'id' => 'edit',
  178. 'title' => $post_type_object->labels->edit_item,
  179. 'href' => get_edit_post_link( $current_object->ID )
  180. ) );
  181. } elseif ( ! empty( $current_object->taxonomy )
  182. && ( $tax = get_taxonomy( $current_object->taxonomy ) )
  183. && current_user_can( $tax->cap->edit_terms )
  184. && $tax->show_ui )
  185. {
  186. $wp_admin_bar->add_menu( array(
  187. 'id' => 'edit',
  188. 'title' => $tax->labels->edit_item,
  189. 'href' => get_edit_term_link( $current_object->term_id, $current_object->taxonomy )
  190. ) );
  191. }
  192. }
  193. }
  194. /**
  195. * Add "Add New" menu.
  196. *
  197. * @since 3.1.0
  198. */
  199. function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
  200. $actions = array();
  201. foreach ( (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' ) as $ptype_obj ) {
  202. if ( ! current_user_can( $ptype_obj->cap->edit_posts ) )
  203. continue;
  204. $actions[ 'post-new.php?post_type=' . $ptype_obj->name ] = array( $ptype_obj->labels->name_admin_bar, $ptype_obj->cap->edit_posts, 'new-' . $ptype_obj->name );
  205. }
  206. if ( current_user_can( 'upload_files' ) )
  207. $actions[ 'media-new.php' ] = array( _x( 'Media', 'add new from admin bar' ), 'upload_files', 'new-media' );
  208. if ( current_user_can( 'manage_links' ) )
  209. $actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'manage_links', 'new-link' );
  210. if ( current_user_can( 'create_users' ) || current_user_can( 'promote_users' ) )
  211. $actions[ 'user-new.php' ] = array( _x( 'User', 'add new from admin bar' ), 'create_users', 'new-user' );
  212. if ( ! is_multisite() && current_user_can( 'install_themes' ) )
  213. $actions[ 'theme-install.php' ] = array( _x( 'Theme', 'add new from admin bar' ), 'install_themes', 'new-theme' );
  214. if ( ! is_multisite() && current_user_can( 'install_plugins' ) )
  215. $actions[ 'plugin-install.php' ] = array( _x( 'Plugin', 'add new from admin bar' ), 'install_plugins', 'new-plugin' );
  216. if ( empty( $actions ) )
  217. return;
  218. $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 ) ) ) ) );
  219. foreach ( $actions as $link => $action ) {
  220. $wp_admin_bar->add_menu( array( 'parent' => 'new-content', 'id' => $action[2], 'title' => $action[0], 'href' => admin_url($link) ) );
  221. }
  222. }
  223. /**
  224. * Add edit comments link with awaiting moderation count bubble.
  225. *
  226. * @since 3.1.0
  227. */
  228. function wp_admin_bar_comments_menu( $wp_admin_bar ) {
  229. if ( !current_user_can('edit_posts') )
  230. return;
  231. $awaiting_mod = wp_count_comments();
  232. $awaiting_mod = $awaiting_mod->moderated;
  233. $awaiting_mod = $awaiting_mod ? "<span id='ab-awaiting-mod' class='pending-count'>" . number_format_i18n( $awaiting_mod ) . "</span>" : '';
  234. $wp_admin_bar->add_menu( array( 'id' => 'comments', 'title' => sprintf( __('Comments %s'), $awaiting_mod ), 'href' => admin_url('edit-comments.php') ) );
  235. }
  236. /**
  237. * Add "Appearance" menu with widget and nav menu submenu.
  238. *
  239. * @since 3.1.0
  240. */
  241. function wp_admin_bar_appearance_menu( $wp_admin_bar ) {
  242. // You can have edit_theme_options but not switch_themes.
  243. if ( ! current_user_can('switch_themes') && ! current_user_can( 'edit_theme_options' ) )
  244. return;
  245. $wp_admin_bar->add_menu( array( 'id' => 'appearance', 'title' => __('Appearance'), 'href' => admin_url('themes.php') ) );
  246. if ( ! current_user_can( 'edit_theme_options' ) )
  247. return;
  248. if ( current_user_can( 'switch_themes' ) )
  249. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __('Themes'), 'href' => admin_url('themes.php') ) );
  250. if ( current_theme_supports( 'widgets' ) )
  251. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __('Widgets'), 'href' => admin_url('widgets.php') ) );
  252. if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
  253. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __('Menus'), 'href' => admin_url('nav-menus.php') ) );
  254. if ( current_theme_supports( 'custom-background' ) )
  255. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'background', 'title' => __('Background'), 'href' => admin_url('themes.php?page=custom-background') ) );
  256. if ( current_theme_supports( 'custom-header' ) )
  257. $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'header', 'title' => __('Header'), 'href' => admin_url('themes.php?page=custom-header') ) );
  258. }
  259. /**
  260. * Provide an update link if theme/plugin/core updates are available.
  261. *
  262. * @since 3.1.0
  263. */
  264. function wp_admin_bar_updates_menu( $wp_admin_bar ) {
  265. if ( !current_user_can('install_plugins') )
  266. return;
  267. $plugin_update_count = $theme_update_count = $wordpress_update_count = 0;
  268. $update_plugins = get_site_transient( 'update_plugins' );
  269. if ( !empty($update_plugins->response) )
  270. $plugin_update_count = count( $update_plugins->response );
  271. $update_themes = get_site_transient( 'update_themes' );
  272. if ( !empty($update_themes->response) )
  273. $theme_update_count = count( $update_themes->response );
  274. /* @todo get_core_updates() is only available on admin page loads
  275. $update_wordpress = get_core_updates( array('dismissed' => false) );
  276. if ( !empty($update_wordpress) && !in_array( $update_wordpress[0]->response, array('development', 'latest') ) )
  277. $wordpress_update_count = 1;
  278. */
  279. $update_count = $plugin_update_count + $theme_update_count + $wordpress_update_count;
  280. if ( !$update_count )
  281. return;
  282. $update_title = array();
  283. if ( $wordpress_update_count )
  284. $update_title[] = sprintf(__('%d WordPress Update'), $wordpress_update_count);
  285. if ( $plugin_update_count )
  286. $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $plugin_update_count), $plugin_update_count);
  287. if ( $theme_update_count )
  288. $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $theme_update_count), $theme_update_count);
  289. $update_title = !empty($update_title) ? esc_attr(implode(', ', $update_title)) : '';
  290. $update_title = "<span title='$update_title'>";
  291. $update_title .= sprintf( __('Updates %s'), "<span id='ab-updates' class='update-count'>" . number_format_i18n($update_count) . '</span>' );
  292. $update_title .= '</span>';
  293. $wp_admin_bar->add_menu( array( 'id' => 'updates', 'title' => $update_title, 'href' => network_admin_url( 'update-core.php' ) ) );
  294. }
  295. /**
  296. * Style and scripts for the admin bar.
  297. *
  298. * @since 3.1.0
  299. *
  300. */
  301. function wp_admin_bar_header() { ?>
  302. <style type="text/css" media="print">#wpadminbar { display:none; }</style>
  303. <?php
  304. }
  305. /**
  306. * Default admin bar callback.
  307. *
  308. * @since 3.1.0
  309. *
  310. */
  311. function _admin_bar_bump_cb() { ?>
  312. <style type="text/css" media="screen">
  313. html { margin-top: 28px !important; }
  314. * html body { margin-top: 28px !important; }
  315. </style>
  316. <?php
  317. }
  318. /**
  319. * Set the display status of the admin bar.
  320. *
  321. * This can be called immediately upon plugin load. It does not need to be called from a function hooked to the init action.
  322. *
  323. * @since 3.1.0
  324. *
  325. * @param bool $show Whether to allow the admin bar to show.
  326. * @return void
  327. */
  328. function show_admin_bar( $show ) {
  329. global $show_admin_bar;
  330. $show_admin_bar = (bool) $show;
  331. }
  332. /**
  333. * Determine whether the admin bar should be showing.
  334. *
  335. * @since 3.1.0
  336. *
  337. * @return bool Whether the admin bar should be showing.
  338. */
  339. function is_admin_bar_showing() {
  340. global $show_admin_bar, $pagenow;
  341. /* For all these types of request we never want an admin bar period */
  342. if ( defined('XMLRPC_REQUEST') || defined('APP_REQUEST') || defined('DOING_AJAX') || defined('IFRAME_REQUEST') )
  343. return false;
  344. if ( ! isset( $show_admin_bar ) ) {
  345. if ( ! is_user_logged_in() || 'wp-login.php' == $pagenow ) {
  346. $show_admin_bar = false;
  347. } else {
  348. $context = is_admin() ? 'admin' : 'front';
  349. $show_admin_bar = _get_admin_bar_pref( $context );
  350. }
  351. }
  352. $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );
  353. return $show_admin_bar;
  354. }
  355. /**
  356. * Retrieve the admin bar display preference of a user based on context.
  357. *
  358. * @since 3.1.0
  359. * @access private
  360. *
  361. * @param string $context Context of this preference check, either 'admin' or 'front'.
  362. * @param int $user Optional. ID of the user to check, defaults to 0 for current user.
  363. * @return bool Whether the admin bar should be showing for this user.
  364. */
  365. function _get_admin_bar_pref( $context, $user = 0 ) {
  366. $pref = get_user_option( "show_admin_bar_{$context}", $user );
  367. if ( false === $pref )
  368. return 'admin' != $context || is_multisite();
  369. return 'true' === $pref;
  370. }
  371. ?>