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

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

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