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

https://github.com/abdullah1213/wordpress · PHP · 215 lines · 144 code · 49 blank · 22 comment · 23 complexity · e1328ac1e226e1991665a0e81cb1b1f6 MD5 · raw file

  1. <?php
  2. class WP_Admin_Bar {
  3. private $nodes = array();
  4. private $root = array();
  5. public $proto = 'http://';
  6. public $user;
  7. function initialize() {
  8. /* Set the protocol used throughout this code */
  9. if ( is_ssl() )
  10. $this->proto = 'https://';
  11. $this->user = new stdClass;
  12. if ( is_user_logged_in() ) {
  13. /* Populate settings we need for the menu based on the current user. */
  14. $this->user->blogs = get_blogs_of_user( get_current_user_id() );
  15. if ( is_multisite() ) {
  16. $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
  17. $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
  18. $this->user->account_domain = $this->user->domain;
  19. } else {
  20. $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
  21. $this->user->domain = trailingslashit( home_url() );
  22. $this->user->account_domain = $this->user->domain;
  23. }
  24. }
  25. add_action( 'wp_head', 'wp_admin_bar_header' );
  26. add_action( 'admin_head', 'wp_admin_bar_header' );
  27. if ( current_theme_supports( 'admin-bar' ) ) {
  28. $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
  29. $header_callback = $admin_bar_args[0]['callback'];
  30. }
  31. if ( empty($header_callback) )
  32. $header_callback = '_admin_bar_bump_cb';
  33. add_action('wp_head', $header_callback);
  34. wp_enqueue_script( 'admin-bar' );
  35. wp_enqueue_style( 'admin-bar' );
  36. do_action( 'admin_bar_init' );
  37. }
  38. public function add_menu( $node ) {
  39. $this->add_node( $node );
  40. }
  41. public function remove_menu( $id ) {
  42. $this->remove_node( $id );
  43. }
  44. /**
  45. * Add a node to the menu.
  46. *
  47. * @param array $args - The arguments for each node.
  48. * - id - string - The ID of the item.
  49. * - title - string - The title of the node.
  50. * - parent - string - The ID of the parent node. Optional.
  51. * - href - string - The link for the item. Optional.
  52. * - meta - array - Meta data including the following keys: html, class, onclick, target, title.
  53. */
  54. public function add_node( $args ) {
  55. // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
  56. if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
  57. $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
  58. // Ensure we have a valid title.
  59. if ( empty( $args['title'] ) )
  60. return false;
  61. if ( empty( $args['id'] ) ) {
  62. _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
  63. $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
  64. }
  65. $defaults = array(
  66. 'id' => false,
  67. 'title' => false,
  68. 'parent' => false,
  69. 'href' => false,
  70. 'meta' => array(),
  71. );
  72. // If the node already exists, keep any data that isn't provided.
  73. if ( isset( $this->nodes[ $args['id'] ] ) )
  74. $defaults = (array) $this->nodes[ $args['id'] ];
  75. $args = wp_parse_args( $args, $defaults );
  76. $this->nodes[ $args['id'] ] = (object) $args;
  77. }
  78. public function remove_node( $id ) {
  79. unset( $this->nodes[ $id ] );
  80. }
  81. public function render() {
  82. // Link nodes to parents.
  83. foreach ( $this->nodes as $node ) {
  84. // Handle root menu items
  85. if ( empty( $node->parent ) ) {
  86. $this->root[] = $node;
  87. continue;
  88. }
  89. // If the parent node isn't registered, ignore the node.
  90. if ( ! isset( $this->nodes[ $node->parent ] ) )
  91. continue;
  92. $parent = $this->nodes[ $node->parent ];
  93. if ( ! isset( $parent->children ) )
  94. $parent->children = array();
  95. $parent->children[] = $node;
  96. }
  97. ?>
  98. <div id="wpadminbar" class="nojq nojs">
  99. <div class="quicklinks">
  100. <ul class="ab-top-menu"><?php
  101. foreach ( $this->root as $node ) {
  102. $this->recursive_render( $node );
  103. }
  104. ?></ul>
  105. </div>
  106. </div>
  107. <?php
  108. }
  109. function recursive_render( $node ) {
  110. $is_parent = ! empty( $node->children );
  111. $menuclass = $is_parent ? 'menupop' : '';
  112. if ( ! empty( $node->meta['class'] ) )
  113. $menuclass .= ' ' . $node->meta['class'];
  114. ?>
  115. <li id="<?php echo esc_attr( "wp-admin-bar-{$node->id}" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
  116. <a tabindex="1" href="<?php echo esc_url( $node->href ) ?>"<?php
  117. if ( ! empty( $node->meta['onclick'] ) ) :
  118. ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
  119. endif;
  120. if ( ! empty( $node->meta['target'] ) ) :
  121. ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
  122. endif;
  123. if ( ! empty( $node->meta['title'] ) ) :
  124. ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
  125. endif;
  126. ?>><?php
  127. if ( $is_parent ) :
  128. ?><span><?php
  129. endif;
  130. echo $node->title;
  131. if ( $is_parent ) :
  132. ?></span><?php
  133. endif;
  134. ?></a>
  135. <?php if ( $is_parent ) : ?>
  136. <ul><?php
  137. // Render children.
  138. foreach ( $node->children as $child_node ) {
  139. $this->recursive_render( $child_node );
  140. }
  141. ?></ul>
  142. <?php endif;
  143. if ( ! empty( $node->meta['html'] ) )
  144. echo $node->meta['html'];
  145. ?>
  146. </li><?php
  147. }
  148. function add_menus() {
  149. // User related, aligned right.
  150. add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
  151. // Site related.
  152. add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
  153. add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
  154. add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
  155. add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
  156. // Content related.
  157. add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
  158. add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
  159. add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
  160. add_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 90 );
  161. if ( ! is_admin() )
  162. add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 100 );
  163. do_action( 'add_admin_bar_menus' );
  164. }
  165. }
  166. ?>