class-wp-admin-bar.php 17 KB

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