Advertisement
Guest User

classes.php

a guest
Dec 19th, 2011
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. <?php
  2. //Custom Admin Menu Panel
  3. class Custom_Admin_Bar {
  4. var $changed_locale = false;
  5. var $menu;
  6. var $need_to_change_locale = false;
  7. var $proto = 'http://';
  8. var $user;
  9.  
  10. function initialize() {
  11. /* Set the protocol used throughout this code */
  12. if ( is_ssl() )
  13. $this->proto = 'https://';
  14.  
  15. $this->user = new stdClass;
  16. $this->menu = new stdClass;
  17.  
  18. /* Populate settings we need for the menu based on the current user. */
  19. $this->user->blogs = get_blogs_of_user( get_current_user_id() );
  20. if ( is_multisite() ) {
  21. $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
  22. $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
  23. $this->user->account_domain = $this->user->domain;
  24. } else {
  25. $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
  26. $this->user->domain = trailingslashit( home_url() );
  27. $this->user->account_domain = $this->user->domain;
  28. }
  29. $this->user->locale = get_locale();
  30.  
  31. add_action( 'wp_head', 'wp_admin_bar_header' );
  32.  
  33. add_action( 'admin_head', 'wp_admin_bar_header' );
  34.  
  35. if ( current_theme_supports( 'admin-bar' ) ) {
  36. $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
  37. $header_callback = $admin_bar_args[0]['callback'];
  38. }
  39.  
  40. if ( empty($header_callback) )
  41. $header_callback = '_admin_bar_bump_cb';
  42.  
  43. add_action('wp_head', $header_callback);
  44.  
  45. wp_enqueue_script( 'admin-bar' );
  46. wp_enqueue_style( 'admin-bar' );
  47.  
  48. do_action( 'admin_bar_init' );
  49. }
  50.  
  51. function add_menu( $args = array() ) {
  52. $defaults = array(
  53. 'title' => false,
  54. 'href' => false,
  55. 'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
  56. 'id' => false, // defaults to a sanitized title value.
  57. 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
  58. );
  59.  
  60. $r = wp_parse_args( $args, $defaults );
  61. extract( $r, EXTR_SKIP );
  62.  
  63. if ( empty( $title ) )
  64. return false;
  65.  
  66. /* Make sure we have a valid ID */
  67. if ( empty( $id ) )
  68. $id = esc_attr( sanitize_title( trim( $title ) ) );
  69.  
  70. if ( ! empty( $parent ) ) {
  71. /* Add the menu to the parent item */
  72. $child = array( 'id' => $id, 'title' => $title, 'href' => $href );
  73.  
  74. if ( ! empty( $meta ) )
  75. $child['meta'] = $meta;
  76.  
  77. $this->add_node( $parent, $this->menu, $child );
  78. } else {
  79. /* Add the menu item */
  80. $this->menu->{$id} = array( 'title' => $title, 'href' => $href );
  81.  
  82. if ( ! empty( $meta ) )
  83. $this->menu->{$id}['meta'] = $meta;
  84. }
  85. }
  86.  
  87. function remove_menu( $id ) {
  88. return $this->remove_node( $id, $this->menu );
  89. }
  90.  
  91. function render() {
  92. ?>
  93. <div id="wpadminbar">
  94. <div class="quicklinks">
  95. <ul>
  96. <?php foreach ( (array) $this->menu as $id => $menu_item ) : ?>
  97. <?php $this->recursive_render( $id, $menu_item ) ?>
  98. <?php endforeach; ?>
  99. </ul>
  100. </div>
  101.  
  102. <div id="adminbarsearch-wrap">
  103. <form action="<?php echo home_url(); ?>" method="get" id="adminbarsearch">
  104. <fieldset>
  105. <input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />
  106. <input type="submit" class="adminbar-button" value="<?php _e('Search'); ?>"/>
  107. </fieldset>
  108. </form>
  109. </div>
  110. </div>
  111.  
  112. <?php
  113. /* Wipe the menu, might reduce memory usage, but probably not. */
  114. $this->menu = null;
  115. }
  116.  
  117. /* Helpers */
  118. function recursive_render( $id, &$menu_item ) { ?>
  119. <?php
  120. $is_parent = ! empty( $menu_item['children'] );
  121.  
  122. $menuclass = $is_parent ? 'menupop' : '';
  123. if ( ! empty( $menu_item['meta']['class'] ) )
  124. $menuclass .= ' ' . $menu_item['meta']['class'];
  125. ?>
  126.  
  127. <li id="<?php echo esc_attr( "wp-admin-bar-$id" ); ?>" class="<?php echo esc_attr( $menuclass ); ?>">
  128. <a href="<?php echo esc_url( $menu_item['href'] ) ?>"<?php
  129. if ( ! empty( $menu_item['meta']['onclick'] ) ) :
  130. ?> onclick="<?php echo esc_js( $menu_item['meta']['onclick'] ); ?>"<?php
  131. endif;
  132. if ( ! empty( $menu_item['meta']['target'] ) ) :
  133. ?> target="<?php echo esc_attr( $menu_item['meta']['target'] ); ?>"<?php
  134. endif;
  135. if ( ! empty( $menu_item['meta']['title'] ) ) :
  136. ?> title="<?php echo esc_attr( $menu_item['meta']['title'] ); ?>"<?php
  137. endif;
  138.  
  139. ?>><?php
  140.  
  141. if ( $is_parent ) :
  142. ?><span><?php
  143. endif;
  144.  
  145. echo $menu_item['title'];
  146.  
  147. if ( $is_parent ) :
  148. ?></span><?php
  149. endif;
  150.  
  151. ?></a>
  152.  
  153. <?php if ( $is_parent ) : ?>
  154. <ul>
  155. <?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?>
  156. <?php $this->recursive_render( $child_id, $child_menu_item ); ?>
  157. <?php endforeach; ?>
  158. </ul>
  159. <?php endif; ?>
  160.  
  161. <?php if ( ! empty( $menu_item['meta']['html'] ) ) : ?>
  162. <?php echo $menu_item['meta']['html']; ?>
  163. <?php endif; ?>
  164. </li><?php
  165. }
  166.  
  167. function add_node( $parent_id, &$menu, $child ) {
  168. foreach( $menu as $id => $menu_item ) {
  169. if ( $parent_id == $id ) {
  170. $menu->{$parent_id}['children']->{$child['id']} = $child;
  171. $child = null;
  172. return true;
  173. }
  174.  
  175. if ( ! empty( $menu->{$id}['children'] ) )
  176. $this->add_node( $parent_id, $menu->{$id}['children'], $child );
  177. }
  178.  
  179. $child = null;
  180.  
  181. return false;
  182. }
  183.  
  184. function add_menus() {
  185. add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 10 );
  186. add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
  187. add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 30 );
  188. add_action( 'admin_bar_menu', 'wp_admin_bar_shortlink_menu', 80 );
  189. add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 70 );
  190.  
  191. if ( !is_network_admin() && !is_user_admin() ) {
  192. add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 40 );
  193. add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 50 );
  194. add_action( 'admin_bar_menu', 'wp_admin_bar_appearance_menu', 60 );
  195. }
  196.  
  197. do_action( 'add_admin_bar_menus' );
  198. }
  199.  
  200. function remove_node( $id, &$menu ) {
  201. if ( isset( $menu->$id ) ) {
  202. unset( $menu->$id );
  203. return true;
  204. }
  205.  
  206. foreach( $menu as $menu_item_id => $menu_item ) {
  207. if ( ! empty( $menu->{$menu_item_id}['children'] ) )
  208. $this->remove_node( $id, $menu->{$menu_item_id}['children'] );
  209. }
  210.  
  211. return false;
  212. }
  213.  
  214. // TODO: Convert to a core feature for multisite or remove
  215. function load_user_locale_translations() {
  216. $this->need_to_change_locale = ( get_locale() != $this->user->locale );
  217. if ( ! $this->need_to_change_locale )
  218. return;
  219. /*
  220. $this->previous_translations = get_translations_for_domain( 'default' );
  221. $this->adminbar_locale_filter = lambda( '$_', '$GLOBALS["wp_admin_bar"]->user->locale;' );
  222. unload_textdomain( 'default' );
  223. add_filter( 'locale', $this->adminbar_locale_filter );
  224. load_default_textdomain();
  225. $this->changed_locale = true;
  226. */
  227. }
  228.  
  229. function unload_user_locale_translations() {
  230. global $l10n;
  231. if ( ! $this->changed_locale )
  232. return;
  233. /*
  234. remove_filter( 'locale', $this->adminbar_locale_filter );
  235. $l10n['default'] = &$this->previous_translations;
  236. */
  237. }
  238. }
  239. add_filter('wp_admin_bar_class', create_function('', 'return "Custom_Admin_Bar";'));
  240. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement