class-wp-admin-bar.php 17 KB

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