PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/abnopanda/wordpress
PHP | 490 lines | 333 code | 88 blank | 69 comment | 68 complexity | 1228aadf7f4cdb1d4f0dfa5788ebf547 MD5 | raw file
  1. <?php
  2. /**
  3. * The WordPress Toolbar
  4. *
  5. * @since 3.1.0
  6. *
  7. * @package WordPress
  8. * @subpackage Toolbar
  9. */
  10. class WP_Admin_Bar {
  11. private $nodes = array();
  12. private $bound = false;
  13. public $user;
  14. public function __get( $name ) {
  15. switch ( $name ) {
  16. case 'proto' :
  17. return is_ssl() ? 'https://' : 'http://';
  18. break;
  19. case 'menu' :
  20. _deprecated_argument( 'WP_Admin_Bar', '3.3', 'Modify admin bar nodes with WP_Admin_Bar::get_node(), WP_Admin_Bar::add_node(), and WP_Admin_Bar::remove_node(), not the <code>menu</code> property.' );
  21. return array(); // Sorry, folks.
  22. break;
  23. }
  24. }
  25. public function initialize() {
  26. $this->user = new stdClass;
  27. if ( is_user_logged_in() ) {
  28. /* Populate settings we need for the menu based on the current user. */
  29. $this->user->blogs = get_blogs_of_user( get_current_user_id() );
  30. if ( is_multisite() ) {
  31. $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
  32. $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
  33. $this->user->account_domain = $this->user->domain;
  34. } else {
  35. $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
  36. $this->user->domain = trailingslashit( home_url() );
  37. $this->user->account_domain = $this->user->domain;
  38. }
  39. }
  40. add_action( 'wp_head', 'wp_admin_bar_header' );
  41. add_action( 'admin_head', 'wp_admin_bar_header' );
  42. if ( current_theme_supports( 'admin-bar' ) ) {
  43. $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
  44. $header_callback = $admin_bar_args[0]['callback'];
  45. }
  46. if ( empty($header_callback) )
  47. $header_callback = '_admin_bar_bump_cb';
  48. add_action('wp_head', $header_callback);
  49. wp_enqueue_script( 'admin-bar' );
  50. wp_enqueue_style( 'admin-bar' );
  51. do_action( 'admin_bar_init' );
  52. }
  53. public function add_menu( $node ) {
  54. $this->add_node( $node );
  55. }
  56. public function remove_menu( $id ) {
  57. $this->remove_node( $id );
  58. }
  59. /**
  60. * Add a node to the menu.
  61. *
  62. * @param array $args - The arguments for each node.
  63. * - id - string - The ID of the item.
  64. * - title - string - The title of the node.
  65. * - parent - string - The ID of the parent node. Optional.
  66. * - href - string - The link for the item. Optional.
  67. * - group - boolean - If the node is a group. Optional. Default false.
  68. * - meta - array - Meta data including the following keys: html, class, onclick, target, title, tabindex.
  69. */
  70. public function add_node( $args ) {
  71. // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
  72. if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
  73. $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
  74. if ( is_object( $args ) )
  75. $args = get_object_vars( $args );
  76. // Ensure we have a valid title.
  77. if ( empty( $args['id'] ) ) {
  78. if ( empty( $args['title'] ) )
  79. return;
  80. _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
  81. // Deprecated: Generate an ID from the title.
  82. $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
  83. }
  84. $defaults = array(
  85. 'id' => false,
  86. 'title' => false,
  87. 'parent' => false,
  88. 'href' => false,
  89. 'group' => false,
  90. 'meta' => array(),
  91. );
  92. // If the node already exists, keep any data that isn't provided.
  93. if ( $maybe_defaults = $this->get_node( $args['id'] ) )
  94. $defaults = get_object_vars( $maybe_defaults );
  95. // Do the same for 'meta' items.
  96. if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
  97. $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
  98. $args = wp_parse_args( $args, $defaults );
  99. $back_compat_parents = array(
  100. 'my-account-with-avatar' => array( 'my-account', '3.3' ),
  101. 'my-blogs' => array( 'my-sites', '3.3' ),
  102. );
  103. if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
  104. list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
  105. _deprecated_argument( __METHOD__, $version, sprintf( 'Use <code>%s</code> as the parent for the <code>%s</code> admin bar node instead of <code>%s</code>.', $new_parent, $args['id'], $args['parent'] ) );
  106. $args['parent'] = $new_parent;
  107. }
  108. $this->_set_node( $args );
  109. }
  110. final protected function _set_node( $args ) {
  111. $this->nodes[ $args['id'] ] = (object) $args;
  112. }
  113. /**
  114. * Gets a node.
  115. *
  116. * @return object Node.
  117. */
  118. final public function get_node( $id ) {
  119. if ( $node = $this->_get_node( $id ) )
  120. return clone $node;
  121. }
  122. final protected function _get_node( $id ) {
  123. if ( $this->bound )
  124. return;
  125. if ( empty( $id ) )
  126. $id = 'root';
  127. if ( isset( $this->nodes[ $id ] ) )
  128. return $this->nodes[ $id ];
  129. }
  130. final public function get_nodes() {
  131. if ( ! $nodes = $this->_get_nodes() )
  132. return;
  133. foreach ( $nodes as &$node ) {
  134. $node = clone $node;
  135. }
  136. return $nodes;
  137. }
  138. final protected function _get_nodes() {
  139. if ( $this->bound )
  140. return;
  141. return $this->nodes;
  142. }
  143. /**
  144. * Add a group to a menu node.
  145. *
  146. * @since 3.3.0
  147. *
  148. * @param array $args - The arguments for each node.
  149. * - id - string - The ID of the item.
  150. * - parent - string - The ID of the parent node. Optional. Default root.
  151. * - meta - array - Meta data including the following keys: class, onclick, target, title.
  152. */
  153. final public function add_group( $args ) {
  154. $args['group'] = true;
  155. $this->add_node( $args );
  156. }
  157. /**
  158. * Remove a node.
  159. *
  160. * @param string The ID of the item.
  161. */
  162. public function remove_node( $id ) {
  163. $this->_unset_node( $id );
  164. }
  165. final protected function _unset_node( $id ) {
  166. unset( $this->nodes[ $id ] );
  167. }
  168. public function render() {
  169. $root = $this->_bind();
  170. if ( $root )
  171. $this->_render( $root );
  172. }
  173. final protected function _bind() {
  174. if ( $this->bound )
  175. return;
  176. // Add the root node.
  177. // Clear it first, just in case. Don't mess with The Root.
  178. $this->remove_node( 'root' );
  179. $this->add_node( array(
  180. 'id' => 'root',
  181. 'group' => false,
  182. ) );
  183. // Normalize nodes: define internal 'children' and 'type' properties.
  184. foreach ( $this->_get_nodes() as $node ) {
  185. $node->children = array();
  186. $node->type = ( $node->group ) ? 'group' : 'item';
  187. unset( $node->group );
  188. // The Root wants your orphans. No lonely items allowed.
  189. if ( ! $node->parent )
  190. $node->parent = 'root';
  191. }
  192. foreach ( $this->_get_nodes() as $node ) {
  193. if ( 'root' == $node->id )
  194. continue;
  195. // Fetch the parent node. If it isn't registered, ignore the node.
  196. if ( ! $parent = $this->_get_node( $node->parent ) ) {
  197. continue;
  198. }
  199. // Generate the group class (we distinguish between top level and other level groups).
  200. $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
  201. if ( $node->type == 'group' ) {
  202. if ( empty( $node->meta['class'] ) )
  203. $node->meta['class'] = $group_class;
  204. else
  205. $node->meta['class'] .= ' ' . $group_class;
  206. }
  207. // Items in items aren't allowed. Wrap nested items in 'default' groups.
  208. if ( $parent->type == 'item' && $node->type == 'item' ) {
  209. $default_id = $parent->id . '-default';
  210. $default = $this->_get_node( $default_id );
  211. // The default group is added here to allow groups that are
  212. // added before standard menu items to render first.
  213. if ( ! $default ) {
  214. // Use _set_node because add_node can be overloaded.
  215. // Make sure to specify default settings for all properties.
  216. $this->_set_node( array(
  217. 'id' => $default_id,
  218. 'parent' => $parent->id,
  219. 'type' => 'group',
  220. 'children' => array(),
  221. 'meta' => array(
  222. 'class' => $group_class,
  223. ),
  224. 'title' => false,
  225. 'href' => false,
  226. ) );
  227. $default = $this->_get_node( $default_id );
  228. $parent->children[] = $default;
  229. }
  230. $parent = $default;
  231. // Groups in groups aren't allowed. Add a special 'container' node.
  232. // The container will invisibly wrap both groups.
  233. } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
  234. $container_id = $parent->id . '-container';
  235. $container = $this->_get_node( $container_id );
  236. // We need to create a container for this group, life is sad.
  237. if ( ! $container ) {
  238. // Use _set_node because add_node can be overloaded.
  239. // Make sure to specify default settings for all properties.
  240. $this->_set_node( array(
  241. 'id' => $container_id,
  242. 'type' => 'container',
  243. 'children' => array( $parent ),
  244. 'parent' => false,
  245. 'title' => false,
  246. 'href' => false,
  247. 'meta' => array(),
  248. ) );
  249. $container = $this->_get_node( $container_id );
  250. // Link the container node if a grandparent node exists.
  251. $grandparent = $this->_get_node( $parent->parent );
  252. if ( $grandparent ) {
  253. $container->parent = $grandparent->id;
  254. $index = array_search( $parent, $grandparent->children, true );
  255. if ( $index === false )
  256. $grandparent->children[] = $container;
  257. else
  258. array_splice( $grandparent->children, $index, 1, array( $container ) );
  259. }
  260. $parent->parent = $container->id;
  261. }
  262. $parent = $container;
  263. }
  264. // Update the parent ID (it might have changed).
  265. $node->parent = $parent->id;
  266. // Add the node to the tree.
  267. $parent->children[] = $node;
  268. }
  269. $root = $this->_get_node( 'root' );
  270. $this->bound = true;
  271. return $root;
  272. }
  273. final protected function _render( $root ) {
  274. global $is_IE;
  275. // Add browser classes.
  276. // We have to do this here since admin bar shows on the front end.
  277. $class = 'nojq nojs';
  278. if ( $is_IE ) {
  279. if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
  280. $class .= ' ie7';
  281. elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
  282. $class .= ' ie8';
  283. elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
  284. $class .= ' ie9';
  285. } elseif ( wp_is_mobile() ) {
  286. $class .= ' mobile';
  287. }
  288. ?>
  289. <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
  290. <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
  291. <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
  292. <?php foreach ( $root->children as $group ) {
  293. $this->_render_group( $group );
  294. } ?>
  295. </div>
  296. <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
  297. </div>
  298. <?php
  299. }
  300. final protected function _render_container( $node ) {
  301. if ( $node->type != 'container' || empty( $node->children ) )
  302. return;
  303. ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
  304. foreach ( $node->children as $group ) {
  305. $this->_render_group( $group );
  306. }
  307. ?></div><?php
  308. }
  309. final protected function _render_group( $node ) {
  310. if ( $node->type == 'container' )
  311. return $this->_render_container( $node );
  312. if ( $node->type != 'group' || empty( $node->children ) )
  313. return;
  314. if ( ! empty( $node->meta['class'] ) )
  315. $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
  316. else
  317. $class = '';
  318. ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
  319. foreach ( $node->children as $item ) {
  320. $this->_render_item( $item );
  321. }
  322. ?></ul><?php
  323. }
  324. final protected function _render_item( $node ) {
  325. if ( $node->type != 'item' )
  326. return;
  327. $is_parent = ! empty( $node->children );
  328. $has_link = ! empty( $node->href );
  329. $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
  330. $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
  331. $menuclass = '';
  332. if ( $is_parent ) {
  333. $menuclass = 'menupop ';
  334. $aria_attributes .= ' aria-haspopup="true"';
  335. }
  336. if ( ! empty( $node->meta['class'] ) )
  337. $menuclass .= $node->meta['class'];
  338. if ( $menuclass )
  339. $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
  340. ?>
  341. <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
  342. if ( $has_link ):
  343. ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
  344. if ( ! empty( $node->meta['onclick'] ) ) :
  345. ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
  346. endif;
  347. if ( ! empty( $node->meta['target'] ) ) :
  348. ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
  349. endif;
  350. if ( ! empty( $node->meta['title'] ) ) :
  351. ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
  352. endif;
  353. ?>><?php
  354. else:
  355. ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
  356. if ( ! empty( $node->meta['title'] ) ) :
  357. ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
  358. endif;
  359. ?>><?php
  360. endif;
  361. echo $node->title;
  362. if ( $has_link ) :
  363. ?></a><?php
  364. else:
  365. ?></div><?php
  366. endif;
  367. if ( $is_parent ) :
  368. ?><div class="ab-sub-wrapper"><?php
  369. foreach ( $node->children as $group ) {
  370. $this->_render_group( $group );
  371. }
  372. ?></div><?php
  373. endif;
  374. if ( ! empty( $node->meta['html'] ) )
  375. echo $node->meta['html'];
  376. ?>
  377. </li><?php
  378. }
  379. public function recursive_render( $id, $node ) {
  380. _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
  381. $this->_render_item( $node );
  382. }
  383. public function add_menus() {
  384. // User related, aligned right.
  385. add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
  386. add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
  387. add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
  388. // Site related.
  389. add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
  390. add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
  391. add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
  392. add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
  393. // Content related.
  394. if ( ! is_network_admin() && ! is_user_admin() ) {
  395. add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
  396. add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
  397. }
  398. add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
  399. add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
  400. do_action( 'add_admin_bar_menus' );
  401. }
  402. }