| 1 | <?php |
|---|
| 2 | class WP_Admin_Bar { |
|---|
| 3 | var $user; |
|---|
| 4 | var $menu; |
|---|
| 5 | var $need_to_change_locale = false; |
|---|
| 6 | var $changed_locale = false; |
|---|
| 7 | |
|---|
| 8 | function WP_Admin_Bar() { |
|---|
| 9 | global $current_user, $blog_id; |
|---|
| 10 | |
|---|
| 11 | $this->user = new stdClass; |
|---|
| 12 | $this->menu = new stdClass; |
|---|
| 13 | |
|---|
| 14 | /* Populate settings we need for the menu based on the current user. */ |
|---|
| 15 | $this->user->blogs = get_ordered_blogs_of_user( $current_user->id ); |
|---|
| 16 | if ( is_multisite() ) { |
|---|
| 17 | $this->user->active_blog = get_active_blog_for_user( $current_user->id ); |
|---|
| 18 | $this->user->domain = ( $this->user->active_blog == 'username only' ) ? get_dashboard_blog() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) ); |
|---|
| 19 | $this->user->account_domain = $this->user->domain; |
|---|
| 20 | } else { |
|---|
| 21 | $this->user->active_blog = $this->user->blogs[$blog_id]; |
|---|
| 22 | $this->user->domain = home_url(); |
|---|
| 23 | $this->user->account_domain = home_url(); |
|---|
| 24 | } |
|---|
| 25 | $this->user->locale = get_locale(); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | function add_menu( $args = array() ) { |
|---|
| 29 | $defaults = array( |
|---|
| 30 | 'title' => false, |
|---|
| 31 | 'href' => false, |
|---|
| 32 | 'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu. |
|---|
| 33 | 'id' => false, // defaults to a sanitized title value. |
|---|
| 34 | 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '' ); |
|---|
| 35 | ); |
|---|
| 36 | |
|---|
| 37 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 38 | extract( $r, EXTR_SKIP ); |
|---|
| 39 | |
|---|
| 40 | if ( empty( $title ) ) |
|---|
| 41 | return false; |
|---|
| 42 | |
|---|
| 43 | /* Make sure we have a valid ID */ |
|---|
| 44 | if ( empty( $id ) ) |
|---|
| 45 | $id = esc_attr( sanitize_title( trim( $title ) ) ); |
|---|
| 46 | |
|---|
| 47 | if ( !empty( $parent ) ) { |
|---|
| 48 | /* Add the menu to the parent item */ |
|---|
| 49 | $child = array( |
|---|
| 50 | 'id' => $id, |
|---|
| 51 | 'title' => $title, |
|---|
| 52 | 'href' => $href |
|---|
| 53 | ); |
|---|
| 54 | |
|---|
| 55 | if ( !empty( $meta ) ) |
|---|
| 56 | $child['meta'] = $meta; |
|---|
| 57 | |
|---|
| 58 | $this->add_node( $parent, $this->menu, $child ); |
|---|
| 59 | } else { |
|---|
| 60 | /* Add the menu item */ |
|---|
| 61 | $this->menu->{$id} = array( |
|---|
| 62 | 'title' => $title, |
|---|
| 63 | 'href' => $href |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | if ( !empty( $meta ) ) |
|---|
| 67 | $this->menu->{$id}['meta'] = $meta; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | function remove_menu( $id ) { |
|---|
| 72 | return $this->remove_node( $id, $this->menu ); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | function render() { |
|---|
| 76 | ?> |
|---|
| 77 | <div id="wpadminbar" class="snap_nopreview no-grav"> |
|---|
| 78 | <div class="quicklinks"> |
|---|
| 79 | <ul> |
|---|
| 80 | <?php foreach ( (array) $this->menu as $id => $menu_item ) : ?> |
|---|
| 81 | <?php $this->recursive_render( $id, $menu_item ) ?> |
|---|
| 82 | <?php endforeach; ?> |
|---|
| 83 | </ul> |
|---|
| 84 | </div> |
|---|
| 85 | |
|---|
| 86 | <div id="adminbarsearch-wrap"> |
|---|
| 87 | <form action="<?php echo home_url(); ?>" method="get" id="adminbarsearch"> |
|---|
| 88 | <input class="adminbar-input" name="s" id="s" type="text" value="<?php esc_attr_e( 'Search' ); ?>" maxlength="150" onfocus="this.value=(this.value=='<?php esc_attr_e( 'Search' ); ?>') ? '' : this.value;" onblur="this.value=(this.value=='') ? '<?php esc_attr_e( 'Search' ); ?>' : this.value;" /> <button type="submit" class="adminbar-button"><span><?php _e('Search'); ?></span></button> |
|---|
| 89 | </form> |
|---|
| 90 | </div> |
|---|
| 91 | </div> |
|---|
| 92 | |
|---|
| 93 | <?php |
|---|
| 94 | /* Wipe the menu, might reduce memory usage, but probably not. */ |
|---|
| 95 | $this->menu = null; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /* Helpers */ |
|---|
| 99 | function recursive_render( $id, &$menu_item ) { ?> |
|---|
| 100 | <?php $menuclass = ( !empty( $menu_item['children'] ) ) ? 'menupop ' : ''; ?> |
|---|
| 101 | |
|---|
| 102 | <li class="<?php echo $menuclass . "ab-$id" ?><?php if ( !empty( $menu_item['meta']['class'] ) ) : ?><?php echo ' ' . $menu_item['meta']['class'] ?><?php endif; ?>"> |
|---|
| 103 | <a href="<?php echo strip_tags( $menu_item['href'] ) ?>"<?php if ( !empty( $menu_item['meta']['onclick'] ) ) :?>onclick="<?php echo $menu_item['meta']['onclick'] ?>"<?php endif; ?><?php if ( !empty( $menu_item['meta']['target'] ) ) :?>target="<?php echo $menu_item['meta']['target'] ?>"<?php endif; ?>><?php if ( !empty( $menuclass ) ) : ?><span><?php endif; ?><?php echo $menu_item['title'] ?><?php if ( !empty( $menuclass ) ) : ?></span><?php endif; ?></a> |
|---|
| 104 | |
|---|
| 105 | <?php if ( !empty( $menu_item['children'] ) ) : ?> |
|---|
| 106 | <ul> |
|---|
| 107 | <?php foreach ( $menu_item['children'] as $child_id => $child_menu_item ) : ?> |
|---|
| 108 | <?php $this->recursive_render( $child_id, $child_menu_item ); ?> |
|---|
| 109 | <?php endforeach; ?> |
|---|
| 110 | </ul> |
|---|
| 111 | <?php endif; ?> |
|---|
| 112 | |
|---|
| 113 | <?php if ( !empty( $menu_item['meta']['html'] ) ) : ?> |
|---|
| 114 | <?php echo $menu_item['meta']['html']; ?> |
|---|
| 115 | <?php endif; ?> |
|---|
| 116 | </li><?php |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | function add_node( $parent_id, &$menu, $child ) { |
|---|
| 120 | foreach( $menu as $id => &$menu_item ) { |
|---|
| 121 | if ( $parent_id == $id ) { |
|---|
| 122 | $menu->{$parent_id}['children']->{$child['id']} = $child; |
|---|
| 123 | $child = null; |
|---|
| 124 | return true; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if ( !empty( $menu->{$id}['children'] ) ) |
|---|
| 128 | $this->add_node( $parent_id, $menu->{$id}['children'], $child ); |
|---|
| 129 | } |
|---|
| 130 | $child = null; |
|---|
| 131 | |
|---|
| 132 | return false; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | function remove_node( $id, &$menu ) { |
|---|
| 136 | foreach( $menu as $menu_item_id => &$menu_item ) { |
|---|
| 137 | if ( $menu_item_id == $id ) { |
|---|
| 138 | $menu_item = null; |
|---|
| 139 | return true; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | if ( !empty( $menu->{$menu_item_id}['children'] ) ) |
|---|
| 143 | $this->remove_node( $id, $menu->{$menu_item_id}['children'] ); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return false; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | function load_user_locale_translations() { |
|---|
| 150 | $this->need_to_change_locale = ( get_locale() != $this->user->locale ); |
|---|
| 151 | if ( !$this->need_to_change_locale ) return; |
|---|
| 152 | $this->previous_translations = get_translations_for_domain( 'default' ); |
|---|
| 153 | $this->adminbar_locale_filter = lambda( '$_', '$GLOBALS["wp_admin_bar"]->user->locale;' ); |
|---|
| 154 | unload_textdomain( 'default' ); |
|---|
| 155 | add_filter( 'locale', $this->adminbar_locale_filter ); |
|---|
| 156 | load_default_textdomain(); |
|---|
| 157 | $this->changed_locale = true; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | function unload_user_locale_translations() { |
|---|
| 161 | global $l10n; |
|---|
| 162 | if ( !$this->changed_locale ) return; |
|---|
| 163 | remove_filter( 'locale', $this->adminbar_locale_filter ); |
|---|
| 164 | $l10n['default'] = &$this->previous_translations; |
|---|
| 165 | |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | ?> |
|---|