Skip to:
Content

BuddyPress.org

Ticket #382: bp-core-adminbar.php

File bp-core-adminbar.php, 8.9 KB (added by chriscree, 17 years ago)

Modified Admin Bar File

Line 
1<?php
2
3function bp_core_admin_bar() {
4        global $bp, $wpdb, $current_blog, $doing_admin_bar;
5       
6        $doing_admin_bar = true;
7       
8        if ( !(int)get_site_option( 'show-loggedout-adminbar' ) && !is_user_logged_in() )
9                return false;
10
11        echo '<div id="wp-admin-bar">';
12
13        // **** Do bp-adminbar-logo Actions ********
14        do_action( 'bp_adminbar_logo' );
15
16        echo '<ul class="main-nav">';
17       
18        // **** Do bp-adminbar-menus Actions ********
19        do_action( 'bp_adminbar_menus' );
20
21        echo '</ul>';
22        echo '</div>';
23}
24
25// **** Default BuddyPress admin bar logo ********
26function bp_adminbar_logo() {
27        echo '<a href="' . get_blog_option( 1, 'siteurl' ) . '"><img id="admin-bar-logo" src="' . apply_filters( 'bp_admin_bar_logo_src', site_url( MUPLUGINDIR . '/bp-core/images/admin_bar_logo.gif' ) ) . '" alt="' . apply_filters( 'bp_admin_bar_logo_alt_text', __( 'BuddyPress', 'buddypress' ) ) . '" /></a>';
28}
29
30// **** "Log In" and "Sign Up" links (Visible when not logged in) ********
31function bp_adminbar_login_menu() {
32        if ( !is_user_logged_in() ) {   
33                echo '<li class="bp-login no-arrow"><a href="' . get_blog_option( 1, 'siteurl') . '/wp-login.php?redirect_to=' . urlencode(get_blog_option(1, 'siteurl') . $_SERVER['REQUEST_URI']) . '">' . __( 'Log In', 'buddypress' ) . '</a></li>';
34               
35                // Show "Sign Up" link if registrations are allowed
36                if ( get_site_option( 'registration' ) != 'none' ) {
37                        echo '<li class="bp-signup no-arrow"><a href="' . get_blog_option( 1, 'siteurl') . '/register">' . __( 'Sign Up', 'buddypress' ) . '</a></li>';
38                }
39        }
40}
41
42// **** "My Account" Menu ******
43function bp_adminbar_account_menu() {
44        global $bp;
45       
46        /* Sort the nav by key as the array has been put together in different locations */
47        $bp['bp_nav'] = bp_core_sort_nav_items( $bp['bp_nav'] );
48       
49        if ( is_user_logged_in() ) {
50               
51                echo '<li><a href="">';
52       
53                echo __( 'My Account', 'buddypress' ) . '</a>';
54                echo '<ul>';
55       
56                /* Loop through each navigation item */
57                $counter = 0;
58                foreach( $bp['bp_nav'] as $nav_item ) {
59                        $alt = ( $counter % 2 == 0 ) ? ' class="alt"' : '';
60                       
61                        echo '<li' . $alt . '>';
62                        echo '<a id="bp-admin-' . $nav_item['css_id'] . '" href="' . $nav_item['link'] . '">' . $nav_item['name'] . '</a>';
63
64                        if ( is_array( $bp['bp_options_nav'][$nav_item['css_id']] ) ) {
65                                echo '<ul>';
66                                $sub_counter = 0;
67                                foreach( $bp['bp_options_nav'][$nav_item['css_id']] as $subnav_item ) {
68                                        $alt = ( $sub_counter % 2 == 0 ) ? ' class="alt"' : '';
69                                        echo '<li' . $alt . '><a id="bp-admin-' . $subnav_item['css_id'] . '" href="' . $subnav_item['link'] . '">' . $subnav_item['name'] . '</a></li>';                               
70                                        $sub_counter++;
71                                }
72                                echo '</ul>';
73                        }
74               
75                        echo '</li>';
76                       
77                        $counter++;
78                }
79       
80                $alt = ( $counter % 2 == 0 ) ? ' class="alt"' : '';
81
82                if ( function_exists('wp_logout_url') ) { 
83                        echo '<li' . $alt . '><a id="bp-admin-logout" href="' . wp_logout_url(site_url()) . '">' . __( 'Log Out', 'buddypress' ) . '</a></li>';                   
84                } else { 
85                        echo '<li' . $alt . '><a id="bp-admin-logout" href="' . site_url() . '/wp-login.php?action=logout&amp;redirect_to=' . site_url() . '">' . __( 'Log Out', 'buddypress' ) . '</a></li>'; 
86                } 
87                         
88                echo '</ul>';
89                echo '</li>';
90        }
91}
92
93// return a string indicating user's role in that blog
94function get_blog_role_for_user( $user, $blog ) {
95       
96        // If the user is a site admin, just display admin.
97        if ( is_site_admin() ) 
98                return __( 'Admin', 'buddypress');
99       
100        $roles = get_usermeta( $user, 'wp_' . $blog . '_capabilities' );
101
102        if ( isset( $roles['subscriber'] ) )
103                $role = __( 'Subscriber', 'buddypress' ); 
104        elseif  ( isset( $roles['contributor'] ) )
105                $role = __( 'Contributor', 'buddypress' );
106        elseif  ( isset( $roles['author'] ) )
107                $role = __( 'Author', 'buddypress' );
108        elseif ( isset( $roles['editor'] ) )
109                $role = __( 'Editor', 'buddypress' );
110        elseif ( isset( $roles['administrator'] ) )
111                $role = __( 'Admin', 'buddypress' );
112        else
113                return false;
114       
115        return $role;
116}
117
118// *** "My Blogs" Menu ********
119function bp_adminbar_blogs_menu() {
120        if ( is_user_logged_in() ) {
121                global $bp; 
122       
123                if ( function_exists('bp_blogs_install') ) {
124                        $blogs = get_blogs_of_user( $bp['loggedin_userid'] ); // find *all* blogs with any kind of role
125
126                        echo '<li><a href="' . $bp['loggedin_domain'] . $bp['blogs']['slug'] . '/my-blogs">';
127                       
128                        _e( 'My Blogs', 'buddypress' );
129                       
130                        echo '</a>';
131       
132                        echo '<ul>';                   
133                        if ( is_array( $blogs )) {
134               
135                                $counter = 0;
136                                foreach( $blogs as $blog ) {
137                                        $role = get_blog_role_for_user( $bp['loggedin_userid'], $blog->userblog_id );
138
139                                        $alt = ( $counter % 2 == 0 ) ? ' class="alt"' : '';
140                                        echo '<li' . $alt . '>';
141                                        echo '<a href="' . $blog->siteurl . '">' . $blog->blogname . ' (' . $role . ')</a>';
142                                        if (!('Subscriber' == $role)) { // then they have something to display on the flyout menu
143                                                echo '<ul>';
144                                                echo '<li class="alt"><a href="' . $blog->siteurl  . '/wp-admin/">' . __('Dashboard', 'buddypress') . '</a></li>';
145                                                echo '<li><a href="' . $blog->siteurl  . '/wp-admin/post-new.php">' . __('New Post', 'buddypress') . '</a></li>';
146                                                echo '<li class="alt"><a href="' . $blog->siteurl  . '/wp-admin/edit.php">' . __('Manage Posts', 'buddypress') . '</a></li>';
147                                                echo '<li class="alt"><a href="' . $blog->siteurl  . '/wp-admin/edit-comments.php">' . __('Manage Comments', 'buddypress') . '</a></li>';                                       
148                                                if ('Admin' == $role) { 
149                                                        echo '<li><a href="' . $blog->siteurl  . '/wp-admin/themes.php">' . __('Switch Theme', 'buddypress') . '</a></li>'; 
150                                                }                                       
151                                                echo '</ul>';                                   
152                                        }
153                                        echo '</li>';
154                                        $counter++;
155                                }
156                        }
157       
158                        $alt = ( $counter % 2 == 0 ) ? ' class="alt"' : '';
159
160                        echo '<li' . $alt . '>';
161                        echo '<a href="' . $bp['loggedin_domain'] . $bp['blogs']['slug'] . '/create-a-blog">' . __('Create a Blog!', 'buddypress') . '</a>';
162                        echo '</li>';
163       
164                        echo '</ul>';
165                        echo '</li>';
166                }
167        }
168}       
169
170// **** "Notifications" Menu *********
171function bp_adminbar_notifications_menu() {     
172        if ( is_user_logged_in() ) {
173                global $bp;
174               
175                echo '<li id="bp-admin-notifications_menu"><a href="' . $bp['loggedin_domain'] . '">';
176                _e( 'Notifications', 'buddypress' );
177       
178                if ( $notifications = bp_core_get_notifications_for_user( $bp['loggedin_userid']) ) { ?>
179                        <span><?php echo count($notifications) ?></span>
180                <?php
181                }
182               
183                echo '</a>';
184                echo '<ul>';
185               
186                if ( $notifications ) { ?>
187                        <?php $counter = 0; ?>
188                        <?php for ( $i = 0; $i < count($notifications); $i++ ) { ?>
189                                <?php $alt = ( $counter % 2 == 0 ) ? ' class="alt"' : ''; ?>
190                                <li<?php echo $alt ?>><?php echo $notifications[$i] ?></li>
191                                <?php $counter++; ?>
192                        <?php } ?>
193                <?php } else { ?>
194                        <li><a href="<?php echo $bp['loggedin_domain'] ?>"><?php _e( 'No new notifications.', 'buddypress' ); ?></a></li>
195                <?php
196                }
197               
198                echo '</ul>';
199                echo '</li>';
200        }
201}
202
203// **** "Blog Authors" Menu (visible when not logged in) ********
204function bp_adminbar_authors_menu() {
205        global $current_blog;
206       
207        if ( $current_blog->blog_id > 1 ) {
208                $authors = get_users_of_blog(); 
209       
210                if ( is_array( $authors ) ) {
211                        /* This is a blog, render a menu with links to all authors */
212                        echo '<li><a href="/">';
213                        _e('Blog Authors', 'buddypress');
214                        echo '</a>';
215               
216                        echo '<ul class="author-list">';
217                        foreach( $authors as $author ) {
218                                $author = new BP_Core_User( $author->user_id );
219                                echo '<li>';
220
221                                echo '<a href="' . $author->user_url . '">';
222                                echo $author->avatar_mini;
223                                echo ' ' . $author->fullname;
224                                echo '<span class="activity">' . $author->last_active . '</span>';
225                                echo '</a>';
226                                echo '<div class="admin-bar-clear"></div>';
227                                echo '</li>';
228                        }
229                        echo '</ul>';
230                        echo '</li>';
231                }
232        }
233}
234       
235// **** "Random" Menu (visible when not logged in) ********
236function bp_adminbar_random_menu() { 
237        global $bp; ?>
238        <li class="align-right">
239                <a href="#"><?php _e( 'Visit', 'buddypress' ) ?></a>
240                <ul class="random-list">
241                        <li><a href="<?php echo $bp['root_domain'] . '/' . MEMBERS_SLUG . '/?random' ?>"><?php _e( 'Random Member', 'buddypress' ) ?></a></li>
242
243                        <?php if ( function_exists('groups_install') ) : ?>
244                        <li class="alt"><a href="<?php echo $bp['root_domain'] . '/' . $bp['groups']['slug'] . '/?random' ?>"><?php _e( 'Random Group', 'buddypress' ) ?></a></li>
245                        <?php endif; ?>
246
247                        <?php if ( function_exists('bp_blogs_install') ) : ?>
248                        <li><a href="<?php echo $bp['root_domain'] . '/' . $bp['blogs']['slug'] . '/?random-blog' ?>"><?php _e( 'Random Blog', 'buddypress' ) ?></a></li>
249                       
250                        <?php endif; ?>
251                </ul>
252        </li>
253        <?php
254
255        $doing_admin_bar = false;
256}
257
258add_action( 'bp_adminbar_logo', 'bp_adminbar_logo' );
259add_action( 'bp_adminbar_menus', 'bp_adminbar_login_menu', 2 );
260add_action( 'bp_adminbar_menus', 'bp_adminbar_account_menu', 4 );
261add_action( 'bp_adminbar_menus', 'bp_adminbar_blogs_menu', 6 );
262add_action( 'bp_adminbar_menus', 'bp_adminbar_notifications_menu', 8 );
263add_action( 'bp_adminbar_menus', 'bp_adminbar_authors_menu', 12 );
264add_action( 'bp_adminbar_menus', 'bp_adminbar_random_menu', 100 );
265
266add_action( 'wp_footer', 'bp_core_admin_bar' );
267add_action( 'admin_footer', 'bp_core_admin_bar' );
268
269?>