class-wp-admin-bar.php 17 KB

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