WP_Admin_Bar

Core class used to implement the Toolbar API.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-admin-bar.php  
  1. class WP_Admin_Bar { 
  2. private $nodes = array(); 
  3. private $bound = false; 
  4. public $user; 
  5.  
  6. /** 
  7. * @param string $name 
  8. * @return string|array|void 
  9. */ 
  10. public function __get( $name ) { 
  11. switch ( $name ) { 
  12. case 'proto' : 
  13. return is_ssl() ? 'https://' : 'http://'; 
  14.  
  15. case 'menu' : 
  16. _deprecated_argument( 'WP_Admin_Bar', '3.3.0', '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.' ); 
  17. return array(); // Sorry, folks. 
  18.  
  19. /** 
  20. * @access public 
  21. */ 
  22. public function initialize() { 
  23. $this->user = new stdClass; 
  24.  
  25. if ( is_user_logged_in() ) { 
  26. /** Populate settings we need for the menu based on the current user. */ 
  27. $this->user->blogs = get_blogs_of_user( get_current_user_id() ); 
  28. if ( is_multisite() ) { 
  29. $this->user->active_blog = get_active_blog_for_user( get_current_user_id() ); 
  30. $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) ); 
  31. $this->user->account_domain = $this->user->domain; 
  32. } else { 
  33. $this->user->active_blog = $this->user->blogs[get_current_blog_id()]; 
  34. $this->user->domain = trailingslashit( home_url() ); 
  35. $this->user->account_domain = $this->user->domain; 
  36.  
  37. add_action( 'wp_head', 'wp_admin_bar_header' ); 
  38.  
  39. add_action( 'admin_head', 'wp_admin_bar_header' ); 
  40.  
  41. if ( current_theme_supports( 'admin-bar' ) ) { 
  42. /** 
  43. * To remove the default padding styles from WordPress for the Toolbar, use the following code: 
  44. * add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) ); 
  45. */ 
  46. $admin_bar_args = get_theme_support( 'admin-bar' ); 
  47. $header_callback = $admin_bar_args[0]['callback']; 
  48.  
  49. if ( empty($header_callback) ) 
  50. $header_callback = '_admin_bar_bump_cb'; 
  51.  
  52. add_action('wp_head', $header_callback); 
  53.  
  54. wp_enqueue_script( 'admin-bar' ); 
  55. wp_enqueue_style( 'admin-bar' ); 
  56.  
  57. /** 
  58. * Fires after WP_Admin_Bar is initialized. 
  59. * @since 3.1.0 
  60. */ 
  61. do_action( 'admin_bar_init' ); 
  62.  
  63. /** 
  64. * @param array $node 
  65. */ 
  66. public function add_menu( $node ) { 
  67. $this->add_node( $node ); 
  68.  
  69. /** 
  70. * @param string $id 
  71. */ 
  72. public function remove_menu( $id ) { 
  73. $this->remove_node( $id ); 
  74.  
  75. /** 
  76. * Adds a node to the menu. 
  77. * @since 3.1.0 
  78. * @since 4.5.0 Added the ability to pass 'lang' and 'dir' meta data. 
  79. * @access public 
  80. * @param array $args { 
  81. * Arguments for adding a node. 
  82. * @type string $id ID of the item. 
  83. * @type string $title Title of the node. 
  84. * @type string $parent Optional. ID of the parent node. 
  85. * @type string $href Optional. Link for the item. 
  86. * @type bool $group Optional. Whether or not the node is a group. Default false. 
  87. * @type array $meta Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',  
  88. * 'onclick', 'target', 'title', 'tabindex'. Default empty. 
  89. * } 
  90. */ 
  91. public function add_node( $args ) { 
  92. // Shim for old method signature: add_node( $parent_id, $menu_obj, $args ) 
  93. if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) ) 
  94. $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) ); 
  95.  
  96. if ( is_object( $args ) ) 
  97. $args = get_object_vars( $args ); 
  98.  
  99. // Ensure we have a valid title. 
  100. if ( empty( $args['id'] ) ) { 
  101. if ( empty( $args['title'] ) ) 
  102. return; 
  103.  
  104. _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3.0' ); 
  105. // Deprecated: Generate an ID from the title. 
  106. $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) ); 
  107.  
  108. $defaults = array( 
  109. 'id' => false,  
  110. 'title' => false,  
  111. 'parent' => false,  
  112. 'href' => false,  
  113. 'group' => false,  
  114. 'meta' => array(),  
  115. ); 
  116.  
  117. // If the node already exists, keep any data that isn't provided. 
  118. if ( $maybe_defaults = $this->get_node( $args['id'] ) ) 
  119. $defaults = get_object_vars( $maybe_defaults ); 
  120.  
  121. // Do the same for 'meta' items. 
  122. if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) ) 
  123. $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] ); 
  124.  
  125. $args = wp_parse_args( $args, $defaults ); 
  126.  
  127. $back_compat_parents = array( 
  128. 'my-account-with-avatar' => array( 'my-account', '3.3' ),  
  129. 'my-blogs' => array( 'my-sites', '3.3' ),  
  130. ); 
  131.  
  132. if ( isset( $back_compat_parents[ $args['parent'] ] ) ) { 
  133. list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ]; 
  134. _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'] ) ); 
  135. $args['parent'] = $new_parent; 
  136.  
  137. $this->_set_node( $args ); 
  138.  
  139. /** 
  140. * @param array $args 
  141. */ 
  142. final protected function _set_node( $args ) { 
  143. $this->nodes[ $args['id'] ] = (object) $args; 
  144.  
  145. /** 
  146. * Gets a node. 
  147. * @param string $id 
  148. * @return object Node. 
  149. */ 
  150. final public function get_node( $id ) { 
  151. if ( $node = $this->_get_node( $id ) ) 
  152. return clone $node; 
  153.  
  154. /** 
  155. * @param string $id 
  156. * @return object|void 
  157. */ 
  158. final protected function _get_node( $id ) { 
  159. if ( $this->bound ) 
  160. return; 
  161.  
  162. if ( empty( $id ) ) 
  163. $id = 'root'; 
  164.  
  165. if ( isset( $this->nodes[ $id ] ) ) 
  166. return $this->nodes[ $id ]; 
  167.  
  168. /** 
  169. * @return array|void 
  170. */ 
  171. final public function get_nodes() { 
  172. if ( ! $nodes = $this->_get_nodes() ) 
  173. return; 
  174.  
  175. foreach ( $nodes as &$node ) { 
  176. $node = clone $node; 
  177. return $nodes; 
  178.  
  179. /** 
  180. * @return array|void 
  181. */ 
  182. final protected function _get_nodes() { 
  183. if ( $this->bound ) 
  184. return; 
  185.  
  186. return $this->nodes; 
  187.  
  188. /** 
  189. * Add a group to a menu node. 
  190. * @since 3.3.0 
  191. * @param array $args { 
  192. * Array of arguments for adding a group. 
  193. * @type string $id ID of the item. 
  194. * @type string $parent Optional. ID of the parent node. Default 'root'. 
  195. * @type array $meta Meta data for the group including the following keys: 
  196. * 'class', 'onclick', 'target', and 'title'. 
  197. * } 
  198. */ 
  199. final public function add_group( $args ) { 
  200. $args['group'] = true; 
  201.  
  202. $this->add_node( $args ); 
  203.  
  204. /** 
  205. * Remove a node. 
  206. * @param string $id The ID of the item. 
  207. */ 
  208. public function remove_node( $id ) { 
  209. $this->_unset_node( $id ); 
  210.  
  211. /** 
  212. * @param string $id 
  213. */ 
  214. final protected function _unset_node( $id ) { 
  215. unset( $this->nodes[ $id ] ); 
  216.  
  217. /** 
  218. * @access public 
  219. */ 
  220. public function render() { 
  221. $root = $this->_bind(); 
  222. if ( $root ) 
  223. $this->_render( $root ); 
  224.  
  225. /** 
  226. * @return object|void 
  227. */ 
  228. final protected function _bind() { 
  229. if ( $this->bound ) 
  230. return; 
  231.  
  232. // Add the root node. 
  233. // Clear it first, just in case. Don't mess with The Root. 
  234. $this->remove_node( 'root' ); 
  235. $this->add_node( array( 
  236. 'id' => 'root',  
  237. 'group' => false,  
  238. ) ); 
  239.  
  240. // Normalize nodes: define internal 'children' and 'type' properties. 
  241. foreach ( $this->_get_nodes() as $node ) { 
  242. $node->children = array(); 
  243. $node->type = ( $node->group ) ? 'group' : 'item'; 
  244. unset( $node->group ); 
  245.  
  246. // The Root wants your orphans. No lonely items allowed. 
  247. if ( ! $node->parent ) 
  248. $node->parent = 'root'; 
  249.  
  250. foreach ( $this->_get_nodes() as $node ) { 
  251. if ( 'root' == $node->id ) 
  252. continue; 
  253.  
  254. // Fetch the parent node. If it isn't registered, ignore the node. 
  255. if ( ! $parent = $this->_get_node( $node->parent ) ) { 
  256. continue; 
  257.  
  258. // Generate the group class (we distinguish between top level and other level groups). 
  259. $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu'; 
  260.  
  261. if ( $node->type == 'group' ) { 
  262. if ( empty( $node->meta['class'] ) ) 
  263. $node->meta['class'] = $group_class; 
  264. else 
  265. $node->meta['class'] .= ' ' . $group_class; 
  266.  
  267. // Items in items aren't allowed. Wrap nested items in 'default' groups. 
  268. if ( $parent->type == 'item' && $node->type == 'item' ) { 
  269. $default_id = $parent->id . '-default'; 
  270. $default = $this->_get_node( $default_id ); 
  271.  
  272. // The default group is added here to allow groups that are 
  273. // added before standard menu items to render first. 
  274. if ( ! $default ) { 
  275. // Use _set_node because add_node can be overloaded. 
  276. // Make sure to specify default settings for all properties. 
  277. $this->_set_node( array( 
  278. 'id' => $default_id,  
  279. 'parent' => $parent->id,  
  280. 'type' => 'group',  
  281. 'children' => array(),  
  282. 'meta' => array( 
  283. 'class' => $group_class,  
  284. ),  
  285. 'title' => false,  
  286. 'href' => false,  
  287. ) ); 
  288. $default = $this->_get_node( $default_id ); 
  289. $parent->children[] = $default; 
  290. $parent = $default; 
  291.  
  292. // Groups in groups aren't allowed. Add a special 'container' node. 
  293. // The container will invisibly wrap both groups. 
  294. } elseif ( $parent->type == 'group' && $node->type == 'group' ) { 
  295. $container_id = $parent->id . '-container'; 
  296. $container = $this->_get_node( $container_id ); 
  297.  
  298. // We need to create a container for this group, life is sad. 
  299. if ( ! $container ) { 
  300. // Use _set_node because add_node can be overloaded. 
  301. // Make sure to specify default settings for all properties. 
  302. $this->_set_node( array( 
  303. 'id' => $container_id,  
  304. 'type' => 'container',  
  305. 'children' => array( $parent ),  
  306. 'parent' => false,  
  307. 'title' => false,  
  308. 'href' => false,  
  309. 'meta' => array(),  
  310. ) ); 
  311.  
  312. $container = $this->_get_node( $container_id ); 
  313.  
  314. // Link the container node if a grandparent node exists. 
  315. $grandparent = $this->_get_node( $parent->parent ); 
  316.  
  317. if ( $grandparent ) { 
  318. $container->parent = $grandparent->id; 
  319.  
  320. $index = array_search( $parent, $grandparent->children, true ); 
  321. if ( $index === false ) 
  322. $grandparent->children[] = $container; 
  323. else 
  324. array_splice( $grandparent->children, $index, 1, array( $container ) ); 
  325.  
  326. $parent->parent = $container->id; 
  327.  
  328. $parent = $container; 
  329.  
  330. // Update the parent ID (it might have changed). 
  331. $node->parent = $parent->id; 
  332.  
  333. // Add the node to the tree. 
  334. $parent->children[] = $node; 
  335.  
  336. $root = $this->_get_node( 'root' ); 
  337. $this->bound = true; 
  338. return $root; 
  339.  
  340. /** 
  341. * @global bool $is_IE 
  342. * @param object $root 
  343. */ 
  344. final protected function _render( $root ) { 
  345. global $is_IE; 
  346.  
  347. // Add browser classes. 
  348. // We have to do this here since admin bar shows on the front end. 
  349. $class = 'nojq nojs'; 
  350. if ( $is_IE ) { 
  351. if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) ) 
  352. $class .= ' ie7'; 
  353. elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) ) 
  354. $class .= ' ie8'; 
  355. elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) ) 
  356. $class .= ' ie9'; 
  357. } elseif ( wp_is_mobile() ) { 
  358. $class .= ' mobile'; 
  359.  
  360. ?> 
  361. <div id="wpadminbar" class="<?php echo $class; ?>"> 
  362. <?php if ( ! is_admin() ) { ?> 
  363. <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a> 
  364. <?php } ?> 
  365. <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>" tabindex="0"> 
  366. <?php foreach ( $root->children as $group ) { 
  367. $this->_render_group( $group ); 
  368. } ?> 
  369. </div> 
  370. <?php if ( is_user_logged_in() ) : ?> 
  371. <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a> 
  372. <?php endif; ?> 
  373. </div> 
  374.  
  375. <?php 
  376.  
  377. /** 
  378. * @param object $node 
  379. */ 
  380. final protected function _render_container( $node ) { 
  381. if ( $node->type != 'container' || empty( $node->children ) ) 
  382. return; 
  383.  
  384. ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php 
  385. foreach ( $node->children as $group ) { 
  386. $this->_render_group( $group ); 
  387. ?></div><?php 
  388.  
  389. /** 
  390. * @param object $node 
  391. */ 
  392. final protected function _render_group( $node ) { 
  393. if ( $node->type == 'container' ) { 
  394. $this->_render_container( $node ); 
  395. return; 
  396. if ( $node->type != 'group' || empty( $node->children ) ) 
  397. return; 
  398.  
  399. if ( ! empty( $node->meta['class'] ) ) 
  400. $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"'; 
  401. else 
  402. $class = ''; 
  403.  
  404. ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php 
  405. foreach ( $node->children as $item ) { 
  406. $this->_render_item( $item ); 
  407. ?></ul><?php 
  408.  
  409. /** 
  410. * @param object $node 
  411. */ 
  412. final protected function _render_item( $node ) { 
  413. if ( $node->type != 'item' ) 
  414. return; 
  415.  
  416. $is_parent = ! empty( $node->children ); 
  417. $has_link = ! empty( $node->href ); 
  418.  
  419. // Allow only numeric values, then casted to integers, and allow a tabindex value of `0` for a11y. 
  420. $tabindex = ( isset( $node->meta['tabindex'] ) && is_numeric( $node->meta['tabindex'] ) ) ? (int) $node->meta['tabindex'] : ''; 
  421. $aria_attributes = ( '' !== $tabindex ) ? ' tabindex="' . $tabindex . '"' : ''; 
  422.  
  423. $menuclass = ''; 
  424.  
  425. if ( $is_parent ) { 
  426. $menuclass = 'menupop '; 
  427. $aria_attributes .= ' aria-haspopup="true"'; 
  428.  
  429. if ( ! empty( $node->meta['class'] ) ) 
  430. $menuclass .= $node->meta['class']; 
  431.  
  432. if ( $menuclass ) 
  433. $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"'; 
  434.  
  435. ?> 
  436.  
  437. <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php 
  438. if ( $has_link ): 
  439. ?><a class="ab-item"<?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php 
  440. if ( ! empty( $node->meta['onclick'] ) ) : 
  441. ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php 
  442. endif; 
  443. if ( ! empty( $node->meta['target'] ) ) : 
  444. ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php 
  445. endif; 
  446. if ( ! empty( $node->meta['title'] ) ) : 
  447. ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php 
  448. endif; 
  449. if ( ! empty( $node->meta['rel'] ) ) : 
  450. ?> rel="<?php echo esc_attr( $node->meta['rel'] ); ?>"<?php 
  451. endif; 
  452. if ( ! empty( $node->meta['lang'] ) ) : 
  453. ?> lang="<?php echo esc_attr( $node->meta['lang'] ); ?>"<?php 
  454. endif; 
  455. if ( ! empty( $node->meta['dir'] ) ) : 
  456. ?> dir="<?php echo esc_attr( $node->meta['dir'] ); ?>"<?php 
  457. endif; 
  458. ?>><?php 
  459. else: 
  460. ?><div class="ab-item ab-empty-item"<?php echo $aria_attributes; 
  461. if ( ! empty( $node->meta['title'] ) ) : 
  462. ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php 
  463. endif; 
  464. if ( ! empty( $node->meta['lang'] ) ) : 
  465. ?> lang="<?php echo esc_attr( $node->meta['lang'] ); ?>"<?php 
  466. endif; 
  467. if ( ! empty( $node->meta['dir'] ) ) : 
  468. ?> dir="<?php echo esc_attr( $node->meta['dir'] ); ?>"<?php 
  469. endif; 
  470. ?>><?php 
  471. endif; 
  472.  
  473. echo $node->title; 
  474.  
  475. if ( $has_link ) : 
  476. ?></a><?php 
  477. else: 
  478. ?></div><?php 
  479. endif; 
  480.  
  481. if ( $is_parent ) : 
  482. ?><div class="ab-sub-wrapper"><?php 
  483. foreach ( $node->children as $group ) { 
  484. $this->_render_group( $group ); 
  485. ?></div><?php 
  486. endif; 
  487.  
  488. if ( ! empty( $node->meta['html'] ) ) 
  489. echo $node->meta['html']; 
  490.  
  491. ?> 
  492. </li><?php 
  493.  
  494. /** 
  495. * @param string $id Unused. 
  496. * @param object $node 
  497. */ 
  498. public function recursive_render( $id, $node ) { 
  499. _deprecated_function( __METHOD__, '3.3.0', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' ); 
  500. $this->_render_item( $node ); 
  501.  
  502. /** 
  503. * @access public 
  504. */ 
  505. public function add_menus() { 
  506. // User related, aligned right. 
  507. add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 ); 
  508. add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 ); 
  509. add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); 
  510.  
  511. // Site related. 
  512. add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 ); 
  513. add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); 
  514. add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 ); 
  515. add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 ); 
  516. add_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 ); 
  517. add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 ); 
  518.  
  519. // Content related. 
  520. if ( ! is_network_admin() && ! is_user_admin() ) { 
  521. add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); 
  522. add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 ); 
  523. add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 ); 
  524.  
  525. add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 ); 
  526.  
  527. /** 
  528. * Fires after menus are added to the menu bar. 
  529. * @since 3.1.0 
  530. */ 
  531. do_action( 'add_admin_bar_menus' );