PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/class-wp-admin-bar.php

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