|
1 <?php |
|
2 |
|
3 /** |
|
4 * bbPress BuddyPress Component Class |
|
5 * |
|
6 * bbPress and BuddyPress are designed to connect together seamlessly and |
|
7 * invisibly, and this is the hunk of code necessary to make that happen. |
|
8 * |
|
9 * The code in this BuddyPress Extension does some pretty complicated stuff, |
|
10 * far outside the realm of the simplicity bbPress is traditionally known for. |
|
11 * |
|
12 * While the rest of bbPress serves as an example of how to write pretty, simple |
|
13 * code, what's in these files is pure madness. It should not be used as an |
|
14 * example of anything other than successfully juggling chainsaws and puppy-dogs. |
|
15 * |
|
16 * @package bbPress |
|
17 * @subpackage BuddyPress |
|
18 */ |
|
19 |
|
20 // Exit if accessed directly |
|
21 if ( !defined( 'ABSPATH' ) ) exit; |
|
22 |
|
23 if ( !class_exists( 'BBP_Forums_Component' ) ) : |
|
24 /** |
|
25 * Loads Forums Component |
|
26 * |
|
27 * @since bbPress (r3552) |
|
28 * |
|
29 * @package bbPress |
|
30 * @subpackage BuddyPress |
|
31 */ |
|
32 class BBP_Forums_Component extends BP_Component { |
|
33 |
|
34 /** |
|
35 * Start the forums component creation process |
|
36 * |
|
37 * @since bbPress (r3552) |
|
38 */ |
|
39 public function __construct() { |
|
40 parent::start( |
|
41 'forums', |
|
42 __( 'Forums', 'bbpress' ), |
|
43 BP_PLUGIN_DIR |
|
44 ); |
|
45 $this->includes(); |
|
46 $this->setup_globals(); |
|
47 $this->setup_actions(); |
|
48 $this->setup_nav(); |
|
49 $this->fully_loaded(); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Include BuddyPress classes and functions |
|
54 */ |
|
55 public function includes() { |
|
56 |
|
57 // Helper BuddyPress functions |
|
58 require( bbpress()->includes_dir . 'extend/buddypress/functions.php' ); |
|
59 |
|
60 // Members modifications |
|
61 require( bbpress()->includes_dir . 'extend/buddypress/members.php' ); |
|
62 |
|
63 // BuddyPress Activity Extension class |
|
64 if ( bp_is_active( 'activity' ) ) { |
|
65 require( bbpress()->includes_dir . 'extend/buddypress/activity.php' ); |
|
66 } |
|
67 |
|
68 // BuddyPress Group Extension class |
|
69 if ( bbp_is_group_forums_active() && bp_is_active( 'groups' ) ) { |
|
70 require( bbpress()->includes_dir . 'extend/buddypress/group.php' ); |
|
71 } |
|
72 } |
|
73 |
|
74 /** |
|
75 * Setup globals |
|
76 * |
|
77 * The BP_FORUMS_SLUG constant is deprecated, and only used here for |
|
78 * backwards compatibility. |
|
79 * |
|
80 * @since bbPress (r3552) |
|
81 */ |
|
82 public function setup_globals() { |
|
83 $bp = buddypress(); |
|
84 |
|
85 // Define the parent forum ID |
|
86 if ( !defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) |
|
87 define( 'BP_FORUMS_PARENT_FORUM_ID', 1 ); |
|
88 |
|
89 // Define a slug, if necessary |
|
90 if ( !defined( 'BP_FORUMS_SLUG' ) ) |
|
91 define( 'BP_FORUMS_SLUG', $this->id ); |
|
92 |
|
93 // All globals for messaging component. |
|
94 $globals = array( |
|
95 'path' => BP_PLUGIN_DIR, |
|
96 'slug' => BP_FORUMS_SLUG, |
|
97 'root_slug' => isset( $bp->pages->forums->slug ) ? $bp->pages->forums->slug : BP_FORUMS_SLUG, |
|
98 'has_directory' => false, |
|
99 'notification_callback' => 'messages_format_notifications', |
|
100 'search_string' => __( 'Search Forums...', 'bbpress' ), |
|
101 ); |
|
102 |
|
103 parent::setup_globals( $globals ); |
|
104 } |
|
105 |
|
106 /** |
|
107 * Instantiate classes for BuddyPress integration |
|
108 * |
|
109 * @since bbPress (r3395) |
|
110 */ |
|
111 public function setup_components() { |
|
112 |
|
113 // Always load the members component |
|
114 bbpress()->extend->buddypress->members = new BBP_BuddyPress_Members; |
|
115 |
|
116 // Create new activity class |
|
117 if ( bp_is_active( 'activity' ) ) { |
|
118 bbpress()->extend->buddypress->activity = new BBP_BuddyPress_Activity; |
|
119 } |
|
120 |
|
121 // Register the group extension only if groups are active |
|
122 if ( bbp_is_group_forums_active() && bp_is_active( 'groups' ) ) { |
|
123 bp_register_group_extension( 'BBP_Forums_Group_Extension' ); |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * Setup the actions |
|
129 * |
|
130 * @since bbPress (r3395) |
|
131 * @access private |
|
132 * @uses add_filter() To add various filters |
|
133 * @uses add_action() To add various actions |
|
134 */ |
|
135 public function setup_actions() { |
|
136 |
|
137 // Setup the components |
|
138 add_action( 'bp_init', array( $this, 'setup_components' ) ); |
|
139 |
|
140 parent::setup_actions(); |
|
141 } |
|
142 |
|
143 /** |
|
144 * Allow the variables, actions, and filters to be modified by third party |
|
145 * plugins and themes. |
|
146 * |
|
147 * @since bbPress (r3902) |
|
148 */ |
|
149 private function fully_loaded() { |
|
150 do_action_ref_array( 'bbp_buddypress_loaded', array( $this ) ); |
|
151 } |
|
152 |
|
153 /** |
|
154 * Setup BuddyBar navigation |
|
155 * |
|
156 * @since bbPress (r3552) |
|
157 */ |
|
158 public function setup_nav() { |
|
159 |
|
160 // Stop if there is no user displayed or logged in |
|
161 if ( !is_user_logged_in() && !bp_displayed_user_id() ) |
|
162 return; |
|
163 |
|
164 // Define local variable(s) |
|
165 $sub_nav = array(); |
|
166 $user_domain = ''; |
|
167 |
|
168 // Add 'Forums' to the main navigation |
|
169 $main_nav = array( |
|
170 'name' => __( 'Forums', 'bbpress' ), |
|
171 'slug' => $this->slug, |
|
172 'position' => 80, |
|
173 'screen_function' => 'bbp_member_forums_screen_topics', |
|
174 'default_subnav_slug' => 'topics', |
|
175 'item_css_id' => $this->id |
|
176 ); |
|
177 |
|
178 // Determine user to use |
|
179 if ( bp_displayed_user_id() ) |
|
180 $user_domain = bp_displayed_user_domain(); |
|
181 elseif ( bp_loggedin_user_domain() ) |
|
182 $user_domain = bp_loggedin_user_domain(); |
|
183 else |
|
184 return; |
|
185 |
|
186 // User link |
|
187 $forums_link = trailingslashit( $user_domain . $this->slug ); |
|
188 |
|
189 // Topics started |
|
190 $sub_nav[] = array( |
|
191 'name' => __( 'Topics Started', 'bbpress' ), |
|
192 'slug' => 'topics', |
|
193 'parent_url' => $forums_link, |
|
194 'parent_slug' => $this->slug, |
|
195 'screen_function' => 'bbp_member_forums_screen_topics', |
|
196 'position' => 20, |
|
197 'item_css_id' => 'topics' |
|
198 ); |
|
199 |
|
200 // Replies to topics |
|
201 $sub_nav[] = array( |
|
202 'name' => __( 'Replies Created', 'bbpress' ), |
|
203 'slug' => 'replies', |
|
204 'parent_url' => $forums_link, |
|
205 'parent_slug' => $this->slug, |
|
206 'screen_function' => 'bbp_member_forums_screen_replies', |
|
207 'position' => 40, |
|
208 'item_css_id' => 'replies' |
|
209 ); |
|
210 |
|
211 // Favorite topics |
|
212 $sub_nav[] = array( |
|
213 'name' => __( 'Favorites', 'bbpress' ), |
|
214 'slug' => 'favorites', |
|
215 'parent_url' => $forums_link, |
|
216 'parent_slug' => $this->slug, |
|
217 'screen_function' => 'bbp_member_forums_screen_favorites', |
|
218 'position' => 60, |
|
219 'item_css_id' => 'favorites' |
|
220 ); |
|
221 |
|
222 // Subscribed topics (my profile only) |
|
223 if ( bp_is_my_profile() ) { |
|
224 $sub_nav[] = array( |
|
225 'name' => __( 'Subscriptions', 'bbpress' ), |
|
226 'slug' => 'subscriptions', |
|
227 'parent_url' => $forums_link, |
|
228 'parent_slug' => $this->slug, |
|
229 'screen_function' => 'bbp_member_forums_screen_subscriptions', |
|
230 'position' => 60, |
|
231 'item_css_id' => 'subscriptions' |
|
232 ); |
|
233 } |
|
234 |
|
235 parent::setup_nav( $main_nav, $sub_nav ); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Set up the admin bar |
|
240 * |
|
241 * @since bbPress (r3552) |
|
242 */ |
|
243 public function setup_admin_bar() { |
|
244 |
|
245 // Prevent debug notices |
|
246 $wp_admin_nav = array(); |
|
247 |
|
248 // Menus for logged in user |
|
249 if ( is_user_logged_in() ) { |
|
250 |
|
251 // Setup the logged in user variables |
|
252 $user_domain = bp_loggedin_user_domain(); |
|
253 $forums_link = trailingslashit( $user_domain . $this->slug ); |
|
254 |
|
255 // Add the "My Account" sub menus |
|
256 $wp_admin_nav[] = array( |
|
257 'parent' => buddypress()->my_account_menu_id, |
|
258 'id' => 'my-account-' . $this->id, |
|
259 'title' => __( 'Forums', 'bbpress' ), |
|
260 'href' => trailingslashit( $forums_link ) |
|
261 ); |
|
262 |
|
263 // Topics |
|
264 $wp_admin_nav[] = array( |
|
265 'parent' => 'my-account-' . $this->id, |
|
266 'id' => 'my-account-' . $this->id . '-topics', |
|
267 'title' => __( 'Topics Started', 'bbpress' ), |
|
268 'href' => trailingslashit( $forums_link . 'topics' ) |
|
269 ); |
|
270 |
|
271 // Replies |
|
272 $wp_admin_nav[] = array( |
|
273 'parent' => 'my-account-' . $this->id, |
|
274 'id' => 'my-account-' . $this->id . '-replies', |
|
275 'title' => __( 'Replies Created', 'bbpress' ), |
|
276 'href' => trailingslashit( $forums_link . 'replies' ) |
|
277 ); |
|
278 |
|
279 // Favorites |
|
280 $wp_admin_nav[] = array( |
|
281 'parent' => 'my-account-' . $this->id, |
|
282 'id' => 'my-account-' . $this->id . '-favorites', |
|
283 'title' => __( 'Favorite Topics', 'bbpress' ), |
|
284 'href' => trailingslashit( $forums_link . 'favorites' ) |
|
285 ); |
|
286 |
|
287 // Subscriptions |
|
288 $wp_admin_nav[] = array( |
|
289 'parent' => 'my-account-' . $this->id, |
|
290 'id' => 'my-account-' . $this->id . '-subscriptions', |
|
291 'title' => __( 'Subscribed Topics', 'bbpress' ), |
|
292 'href' => trailingslashit( $forums_link . 'subscriptions' ) |
|
293 ); |
|
294 } |
|
295 |
|
296 parent::setup_admin_bar( $wp_admin_nav ); |
|
297 } |
|
298 |
|
299 /** |
|
300 * Sets up the title for pages and <title> |
|
301 * |
|
302 * @since bbPress (r3552) |
|
303 */ |
|
304 public function setup_title() { |
|
305 $bp = buddypress(); |
|
306 |
|
307 // Adjust title based on view |
|
308 if ( bp_is_forums_component() ) { |
|
309 if ( bp_is_my_profile() ) { |
|
310 $bp->bp_options_title = __( 'Forums', 'bbpress' ); |
|
311 } elseif ( bp_is_user() ) { |
|
312 $bp->bp_options_avatar = bp_core_fetch_avatar( array( |
|
313 'item_id' => bp_displayed_user_id(), |
|
314 'type' => 'thumb' |
|
315 ) ); |
|
316 $bp->bp_options_title = bp_get_displayed_user_fullname(); |
|
317 } |
|
318 } |
|
319 |
|
320 parent::setup_title(); |
|
321 } |
|
322 } |
|
323 endif; |