Нет описания

class-wp-admin-bar.php 17KB

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