PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

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