Нема описа

class-wp-admin-bar.php 17KB

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