PluginProbe
bbPress / 2.1-rc4
bbPress v2.1-rc4
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / bbp-includes / bbp-extend-buddypress.php

bbp-extend-buddypress.php in bbPress 2.1-rc4, at bbp-includes/bbp-extend-buddypress.php

2,146 lines 58.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Main bbPress BuddyPress Class
5 *
6 * @package bbPress
7 * @subpackage BuddyPress
8 * @todo maybe move to BuddyPress Forums once bbPress 1.1 can be removed
9 */
10
11 // Exit if accessed directly
12 if ( !defined( 'ABSPATH' ) ) exit;
13
14 if ( !class_exists( 'BBP_BuddyPress' ) ) :
15 /**
16 * Loads BuddyPress extension
17 *
18 * @since bbPress (r3395)
19 *
20 * @package bbPress
21 * @subpackage BuddyPress
22 */
23 class BBP_BuddyPress {
24
25 /** Variables *************************************************************/
26
27 /**
28 * The name of the BuddyPress component, used in activity streams
29 *
30 * @var string
31 */
32 private $component = '';
33
34 /**
35 * Forum Create Activty Action
36 *
37 * @var string
38 */
39 private $forum_create = '';
40
41 /**
42 * Topic Create Activty Action
43 *
44 * @var string
45 */
46 private $topic_create = '';
47
48 /**
49 * Topic Close Activty Action
50 *
51 * @var string
52 */
53 private $topic_close = '';
54
55 /**
56 * Topic Edit Activty Action
57 *
58 * @var string
59 */
60 private $topic_edit = '';
61
62 /**
63 * Topic Open Activty Action
64 *
65 * @var string
66 */
67 private $topic_open = '';
68
69 /**
70 * Reply Create Activty Action
71 *
72 * @var string
73 */
74 private $reply_create = '';
75
76 /**
77 * Reply Edit Activty Action
78 *
79 * @var string
80 */
81 private $reply_edit = '';
82
83 /** Slugs *****************************************************************/
84
85 /**
86 * Forums slug
87 *
88 * @var string
89 */
90 private $forums_slug = '';
91
92 /**
93 * Topic slug
94 *
95 * @var string
96 */
97 private $topic_slug = '';
98
99 /**
100 * Reply slug
101 *
102 * @var string
103 */
104 private $reply_slug = '';
105
106 /** Setup Methods *********************************************************/
107
108 /**
109 * The main bbPress BuddyPress loader
110 *
111 * @since bbPress (r3395)
112 */
113 public function __construct() {
114 $this->setup_globals();
115 $this->setup_actions();
116 $this->setup_filters();
117 $this->fully_loaded();
118 }
119
120 /**
121 * Extension variables
122 *
123 * @since bbPress (r3395)
124 * @access private
125 * @uses apply_filters() Calls various filters
126 */
127 private function setup_globals() {
128
129 // The name of the BuddyPress component, used in activity streams
130 $this->component = 'bbpress';
131
132 // Forums
133 $this->forum_create = 'bbp_forum_create';
134
135 // Topics
136 $this->topic_create = 'bbp_topic_create';
137 $this->topic_edit = 'bbp_topic_edit';
138 $this->topic_close = 'bbp_topic_close';
139 $this->topic_open = 'bbp_topic_open';
140
141 // Replies
142 $this->reply_create = 'bbp_reply_create';
143 $this->reply_edit = 'bbp_reply_edit';
144
145 // Forum slugs, as used in BuddyPress
146 $this->forums_slug = 'forums';
147 $this->topic_slug = 'topic';
148 $this->reply_slug = 'reply';
149 }
150
151 /**
152 * Setup the actions
153 *
154 * @since bbPress (r3395)
155 * @access private
156 * @uses add_filter() To add various filters
157 * @uses add_action() To add various actions
158 */
159 private function setup_actions() {
160
161 /** Activity **********************************************************/
162
163 // Bail if activity is not active
164 if ( bp_is_active( 'activity' ) ) {
165
166 // Register the activity stream actions
167 add_action( 'bp_register_activity_actions', array( $this, 'register_activity_actions' ) );
168
169 // Hook into topic creation
170 add_action( 'bbp_new_topic', array( $this, 'topic_create' ), 10, 4 );
171
172 // Hook into reply creation
173 add_action( 'bbp_new_reply', array( $this, 'reply_create' ), 10, 5 );
174
175 // Append forum filters in site wide activity streams
176 add_action( 'bp_activity_filter_options', array( $this, 'activity_filter_options' ), 10 );
177
178 // Append forum filters in single member activity streams
179 add_action( 'bp_member_activity_filter_options', array( $this, 'activity_filter_options' ), 10 );
180
181 // Append forum filters in single group activity streams
182 add_action( 'bp_group_activity_filter_options', array( $this, 'activity_filter_options' ), 10 );
183 }
184
185 /** Favorites *********************************************************/
186
187 // Move handler to 'bp_actions' - BuddyPress bypasses template_loader
188 remove_action( 'template_redirect', 'bbp_favorites_handler', 1 );
189 add_action( 'bp_actions', 'bbp_favorites_handler', 1 );
190
191 /** Subscriptions *****************************************************/
192
193 // Move handler to 'bp_actions' - BuddyPress bypasses template_loader
194 remove_action( 'template_redirect', 'bbp_subscriptions_handler', 1 );
195 add_action( 'bp_actions', 'bbp_subscriptions_handler', 1 );
196 }
197
198 /**
199 * Setup the filters
200 *
201 * @since bbPress (r3395)
202 * @access private
203 * @uses add_filter() To add various filters
204 * @uses add_action() To add various actions
205 */
206 private function setup_filters() {
207
208 /** Activity **********************************************************/
209
210 // Obey BuddyPress commenting rules
211 add_filter( 'bp_activity_can_comment', array( $this, 'activity_can_comment' ) );
212
213 // Link directly to the topic or reply
214 add_filter( 'bp_activity_get_permalink', array( $this, 'activity_get_permalink' ), 10, 2 );
215
216 /** Profiles **********************************************************/
217
218 // Override bbPress user profile URL with BuddyPress profile URL
219 add_filter( 'bbp_pre_get_user_profile_url', array( $this, 'user_profile_url' ) );
220 add_filter( 'bbp_get_favorites_permalink', array( $this, 'get_favorites_permalink' ), 10, 2 );
221 add_filter( 'bbp_get_subscriptions_permalink', array( $this, 'get_subscriptions_permalink' ), 10, 2 );
222
223 // Map forum/topic/replys permalinks to their groups
224 add_filter( 'bbp_get_forum_permalink', array( $this, 'map_forum_permalink_to_group' ), 10, 2 );
225 add_filter( 'bbp_get_topic_permalink', array( $this, 'map_topic_permalink_to_group' ), 10, 2 );
226 add_filter( 'bbp_get_reply_permalink', array( $this, 'map_reply_permalink_to_group' ), 10, 2 );
227
228 // Canonical redirects for normal URLs
229 add_action( 'template_redirect', array( $this, 'redirect_canonical' ) );
230
231 /** Mentions **********************************************************/
232
233 // Only link mentions if activity component is active
234 if ( bp_is_active( 'activity' ) ) {
235
236 // Convert mentions into links on create
237 add_filter( 'bbp_new_topic_pre_content', 'bp_activity_at_name_filter' );
238 add_filter( 'bbp_new_reply_pre_content', 'bp_activity_at_name_filter' );
239
240 // Convert mentions into links on edit
241 add_filter( 'bbp_edit_topic_pre_content', 'bp_activity_at_name_filter' );
242 add_filter( 'bbp_edit_reply_pre_content', 'bp_activity_at_name_filter' );
243 }
244
245 // Revert links into text on edit
246 add_filter( 'bbp_get_form_topic_content', array( $this, 'strip_mentions_on_edit' ) );
247 add_filter( 'bbp_get_form_reply_content', array( $this, 'strip_mentions_on_edit' ) );
248 }
249
250 /**
251 * Allow the variables, actions, and filters to be modified by third party
252 * plugins and themes.
253 *
254 * @since bbPress (r3902)
255 */
256 private function fully_loaded() {
257 do_action_ref_array( 'bbp_buddypress_loaded', array( $this ) );
258 }
259
260 /** Methods ***************************************************************/
261
262 /**
263 * Strip out BuddyPress activity at-name HTML on topic/reply edit
264 *
265 * Copied from bp_forums_strip_mentions_on_post_edit() in case forums
266 * component is not active or is not loaded in yet.
267 *
268 * @since bbPress (r3475)
269 * @param type $content Optional
270 * @uses bp_get_root_domain()
271 * @uses bp_get_members_root_slug()
272 * @return string
273 */
274 public function strip_mentions_on_edit( $content = '' ) {
275
276 // Backwards compat for members root slug
277 if ( function_exists( 'bp_get_members_root_slug' ) )
278 $members_root = bp_get_members_root_slug();
279 elseif ( defined( 'BP_MEMBERS_SLUG' ) )
280 $members_root = BP_MEMBERS_SLUG;
281 else
282 $members_root = 'members';
283
284 $content = htmlspecialchars_decode( $content );
285 $pattern = "|<a href=&#039;" . bp_get_root_domain() . "/" . $members_root . "/[A-Za-z0-9-_\.]+/&#039; rel=&#039;nofollow&#039;>(@[A-Za-z0-9-_\.@]+)</a>|";
286 $content = preg_replace( $pattern, "$1", $content );
287
288 return $content;
289 }
290
291 /**
292 * Register our activity actions with BuddyPress
293 *
294 * @since bbPress (r3395)
295 * @uses bp_activity_set_action()
296 */
297 public function register_activity_actions() {
298 bp_activity_set_action( $this->component, $this->topic_create, __( 'New topic created', 'bbpress' ) );
299 bp_activity_set_action( $this->component, $this->reply_create, __( 'New reply created', 'bbpress' ) );
300 }
301
302 /**
303 * Wrapper for recoding bbPress actions to the BuddyPress activity stream
304 *
305 * @since bbPress (r3395)
306 * @param type $args Array of arguments for bp_activity_add()
307 * @uses bbp_get_current_user_id()
308 * @uses bp_core_current_time()
309 * @uses bbp_parse_args()
310 * @uses aplly_filters()
311 * @uses bp_activity_add()
312 * @return type Activity ID if successful, false if not
313 */
314 private function record_activity( $args = '' ) {
315
316 // Default activity args
317 $defaults = array (
318 'user_id' => bbp_get_current_user_id(),
319 'type' => '',
320 'action' => '',
321 'item_id' => '',
322 'secondary_item_id' => '',
323 'content' => '',
324 'primary_link' => '',
325 'component' => $this->component,
326 'recorded_time' => bp_core_current_time(),
327 'hide_sitewide' => false
328 );
329 $activity = bbp_parse_args( $args, $defaults, 'record_activity' );
330
331 // Add the activity
332 return bp_activity_add( $activity );
333 }
334
335 /**
336 * Wrapper for deleting bbPress actions from BuddyPress activity stream
337 *
338 * @since bbPress (r3395)
339 * @param type $args Array of arguments for bp_activity_add()
340 * @uses bbp_get_current_user_id()
341 * @uses bp_core_current_time()
342 * @uses bbp_parse_args()
343 * @uses aplly_filters()
344 * @uses bp_activity_add()
345 * @return type Activity ID if successful, false if not
346 */
347 public function delete_activity( $args = '' ) {
348
349 // Default activity args
350 $defaults = array(
351 'item_id' => false,
352 'component' => $this->component,
353 'type' => false,
354 'user_id' => false,
355 'secondary_item_id' => false
356 );
357 $activity = bbp_parse_args( $args, $defaults, 'delete_activity' );
358
359 // Delete the activity
360 bp_activity_delete_by_item_id( $activity );
361 }
362
363 /**
364 * Maybe disable activity stream comments on select actions
365 *
366 * @since bbPress (r3399)
367 * @global BP_Activity_Template $activities_template
368 * @param boolean $can_comment
369 * @uses bp_get_activity_action_name()
370 * @return boolean
371 */
372 public function activity_can_comment( $can_comment = true ) {
373 global $activities_template;
374
375 // Already forced off, so comply
376 if ( false === $can_comment )
377 return $can_comment;
378
379 // Check if blog & forum activity stream commenting is off
380 if ( ( false === $activities_template->disable_blogforum_replies ) || (int) $activities_template->disable_blogforum_replies ) {
381
382 // Get the current action name
383 $action_name = bp_get_activity_action_name();
384
385 // Setup the array of possibly disabled actions
386 $disabled_actions = array(
387 $this->topic_create,
388 $this->reply_create
389 );
390
391 // Check if this activity stream action is disabled
392 if ( in_array( $action_name, $disabled_actions ) ) {
393 $can_comment = false;
394 }
395 }
396
397 return $can_comment;
398 }
399
400 /**
401 * Maybe link directly to topics and replies in activity stream entries
402 *
403 * @since bbPress (r3399)
404 * @param string $link
405 * @param mixed $activity_object
406 * @return string The link to the activity stream item
407 */
408 public function activity_get_permalink( $link = '', $activity_object = false ) {
409
410 // Setup the array of actions to link directly to
411 $disabled_actions = array(
412 $this->topic_create,
413 $this->reply_create
414 );
415
416 // Check if this activity stream action is directly linked
417 if ( in_array( $activity_object->type, $disabled_actions ) ) {
418 $link = $activity_object->primary_link;
419 }
420
421 return $link;
422 }
423
424 /**
425 * Append forum options to activity filter select box
426 *
427 * @since bbPress (r3653)
428 */
429 function activity_filter_options() {
430 ?>
431
432 <option value="<?php echo $this->topic_create; ?>"><?php _e( 'Topics', 'bbpress' ); ?></option>
433 <option value="<?php echo $this->reply_create; ?>"><?php _e( 'Replies', 'bbpress' ); ?></option>
434
435 <?php
436 }
437
438 /**
439 * Override bbPress profile URL with BuddyPress profile URL
440 *
441 * @since bbPress (r3401)
442 * @param string $url
443 * @param int $user_id
444 * @param string $user_nicename
445 * @return string
446 */
447 public function user_profile_url( $user_id ) {
448
449 // Define local variable(s)
450 $profile_url = '';
451
452 // Special handling for forum component
453 if ( bp_is_current_component( 'forums' ) ) {
454
455 // Empty action or 'topics' action
456 if ( !bp_current_action() || bp_is_current_action( 'topics' ) ) {
457 $profile_url = bp_core_get_user_domain( $user_id ) . 'forums/topics';
458
459 // Empty action or 'topics' action
460 } elseif ( bp_is_current_action( 'replies' ) ) {
461 $profile_url = bp_core_get_user_domain( $user_id ) . 'forums/replies';
462
463 // 'favorites' action
464 } elseif ( bbp_is_favorites_active() && bp_is_current_action( 'favorites' ) ) {
465 $profile_url = $this->get_favorites_permalink( '', $user_id );
466
467 // 'subscriptions' action
468 } elseif ( bbp_is_subscriptions_active() && bp_is_current_action( 'subscriptions' ) ) {
469 $profile_url = $this->get_subscriptions_permalink( '', $user_id );
470 }
471
472 // Not in users' forums area
473 } else {
474 $profile_url = bp_core_get_user_domain( $user_id );
475 }
476
477 return trailingslashit( $profile_url );
478 }
479
480 /**
481 * Override bbPress favorites URL with BuddyPress profile URL
482 *
483 * @since bbPress (r3721)
484 * @param string $url
485 * @param int $user_id
486 * @return string
487 */
488 public function get_favorites_permalink( $url, $user_id ) {
489 $url = trailingslashit( bp_core_get_user_domain( $user_id ) . 'forums/favorites' );
490 return $url;
491 }
492
493 /**
494 * Override bbPress subscriptions URL with BuddyPress profile URL
495 *
496 * @since bbPress (r3721)
497 * @param string $url
498 * @param int $user_id
499 * @return string
500 */
501 public function get_subscriptions_permalink( $url, $user_id ) {
502 $url = trailingslashit( bp_core_get_user_domain( $user_id ) . 'forums/subscriptions' );
503 return $url;
504 }
505
506 /** Topics ****************************************************************/
507
508 /**
509 * Record an activity stream entry when a topic is created
510 *
511 * @since bbPress (r3395)
512 * @param int $topic_id
513 * @param int $forum_id
514 * @param array $anonymous_data
515 * @param int $topic_author_id
516 * @uses bbp_get_topic_id()
517 * @uses bbp_get_forum_id()
518 * @uses bbp_get_user_profile_link()
519 * @uses bbp_get_topic_permalink()
520 * @uses bbp_get_topic_title()
521 * @uses bbp_get_topic_content()
522 * @uses bbp_get_forum_permalink()
523 * @uses bbp_get_forum_title()
524 * @uses bp_create_excerpt()
525 * @uses apply_filters()
526 * @return Bail early if topic is by anonywous user
527 */
528 public function topic_create( $topic_id, $forum_id, $anonymous_data, $topic_author_id ) {
529
530 // Bail early if topic is by anonywous user
531 if ( !empty( $anonymous_data ) )
532 return;
533
534 // Bail if site is private
535 if ( !bbp_is_site_public() )
536 return;
537
538 // Validate activity data
539 $user_id = $topic_author_id;
540 $topic_id = bbp_get_topic_id( $topic_id );
541 $forum_id = bbp_get_forum_id( $forum_id );
542
543 // Bail if user is not active
544 if ( bbp_is_user_inactive( $user_id ) )
545 return;
546
547 // Bail if topic is not published
548 if ( !bbp_is_topic_published( $topic_id ) )
549 return;
550
551 // Bail if forum is not public
552 if ( !bbp_is_forum_public( $forum_id, false ) )
553 return;
554
555 // User link for topic author
556 $user_link = bbp_get_user_profile_link( $user_id );
557
558 // Topic
559 $topic_permalink = bbp_get_topic_permalink( $topic_id );
560 $topic_title = bbp_get_topic_title ( $topic_id );
561 $topic_content = bbp_get_topic_content ( $topic_id );
562 $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>';
563
564 // Forum
565 $forum_permalink = bbp_get_forum_permalink( $forum_id );
566 $forum_title = bbp_get_forum_title ( $forum_id );
567 $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>';
568
569 // Activity action & text
570 $activity_text = sprintf( __( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
571 $activity_action = apply_filters( 'bbp_activity_topic_create', $activity_text, $user_id, $topic_id, $forum_id );
572 $activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', bp_create_excerpt( $topic_content ), $topic_content );
573
574 // Compile the activity stream results
575 $activity = array(
576 'user_id' => $user_id,
577 'action' => $activity_action,
578 'content' => $activity_content,
579 'primary_link' => $topic_permalink,
580 'type' => $this->topic_create,
581 'item_id' => $topic_id,
582 'secondary_item_id' => $forum_id,
583 );
584
585 // Record the activity
586 $activity_id = $this->record_activity( $activity );
587
588 // Add the activity entry ID as a meta value to the topic
589 if ( !empty( $activity_id ) ) {
590 update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
591 }
592 }
593
594 /** Replies ***************************************************************/
595
596 /**
597 * Record an activity stream entry when a reply is created
598 *
599 * @since bbPress (r3395)
600 * @param int $topic_id
601 * @param int $forum_id
602 * @param array $anonymous_data
603 * @param int $topic_author_id
604 * @uses bbp_get_reply_id()
605 * @uses bbp_get_topic_id()
606 * @uses bbp_get_forum_id()
607 * @uses bbp_get_user_profile_link()
608 * @uses bbp_get_reply_url()
609 * @uses bbp_get_reply_content()
610 * @uses bbp_get_topic_permalink()
611 * @uses bbp_get_topic_title()
612 * @uses bbp_get_forum_permalink()
613 * @uses bbp_get_forum_title()
614 * @uses bp_create_excerpt()
615 * @uses apply_filters()
616 * @return Bail early if topic is by anonywous user
617 */
618 public function reply_create( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author_id ) {
619
620 // Do not log activity of anonymous users
621 if ( !empty( $anonymous_data ) )
622 return;
623
624 // Bail if site is private
625 if ( !bbp_is_site_public() )
626 return;
627
628 // Validate activity data
629 $user_id = $reply_author_id;
630 $reply_id = bbp_get_reply_id( $reply_id );
631 $topic_id = bbp_get_topic_id( $topic_id );
632 $forum_id = bbp_get_forum_id( $forum_id );
633
634 // Bail if user is not active
635 if ( bbp_is_user_inactive( $user_id ) )
636 return;
637
638 // Bail if forum is not public
639 if ( !bbp_is_forum_public( $forum_id, false ) )
640 return;
641
642 // Setup links for activity stream
643 $user_link = bbp_get_user_profile_link( $user_id );
644
645 // Reply
646 $reply_url = bbp_get_reply_url ( $reply_id );
647 $reply_content = bbp_get_reply_content( $reply_id );
648
649 // Topic
650 $topic_permalink = bbp_get_topic_permalink( $topic_id );
651 $topic_title = bbp_get_topic_title ( $topic_id );
652 $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>';
653
654 // Forum
655 $forum_permalink = bbp_get_forum_permalink( $forum_id );
656 $forum_title = bbp_get_forum_title ( $forum_id );
657 $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>';
658
659 // Activity action & text
660 $activity_text = sprintf( __( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
661 $activity_action = apply_filters( 'bbp_activity_reply_create', $activity_text, $user_id, $reply_id, $topic_id );
662 $activity_content = apply_filters( 'bbp_activity_reply_create_excerpt', bp_create_excerpt( $reply_content ), $reply_content );
663
664 // Compile the activity stream results
665 $activity = array(
666 'user_id' => $user_id,
667 'action' => $activity_action,
668 'content' => $activity_content,
669 'primary_link' => $reply_url,
670 'type' => $this->reply_create,
671 'item_id' => $reply_id,
672 'secondary_item_id' => $topic_id,
673 );
674
675 // Record the activity
676 $activity_id = $this->record_activity( $activity );
677
678 // Add the activity entry ID as a meta value to the reply
679 if ( !empty( $activity_id ) ) {
680 update_post_meta( $reply_id, '_bbp_activity_id', $activity_id );
681 }
682 }
683
684 /**
685 * Map a forum permalink to the corresponding group
686 *
687 * @since bbPress (r3802)
688 * @param string $url
689 * @param int $forum_id
690 * @return string
691 */
692 public function map_forum_permalink_to_group( $url, $forum_id ) {
693 $slug = get_post_field( 'post_name', $forum_id );
694 $group_ids = bbp_get_forum_group_ids( $forum_id );
695
696 // If the topic is not associated with a group, don't mess with it
697 if ( !empty( $group_ids ) ) {
698
699 // @todo Multiple group forums/forum groups
700 $group_id = $group_ids[0];
701 $group = groups_get_group( array( 'group_id' => $group_id ) );
702
703 if ( bp_is_group_admin_screen( $this->forums_slug ) ) {
704 $group_permalink = bp_get_group_admin_permalink( $group );
705 } else {
706 $group_permalink = bp_get_group_permalink( $group );
707 }
708
709 $url = trailingslashit( $group_permalink . $this->forums_slug . '/' . $slug );
710 }
711
712 return $url;
713 }
714
715 /**
716 * Map a topic permalink to the current group forum
717 *
718 * @since bbPress (r3802)
719 * @param string $url
720 * @param int $topic_id
721 * @return string
722 */
723 public function map_topic_permalink_to_group( $url, $topic_id ) {
724 $slug = get_post_field( 'post_name', $topic_id );
725 $forum_id = bbp_get_topic_forum_id( $topic_id );
726 $group_ids = bbp_get_forum_group_ids( $forum_id );
727
728 // If the topic is not associated with a group, don't mess with it
729 if ( !empty( $group_ids ) ) {
730
731 // @todo Multiple group forums/forum groups
732 $group_id = $group_ids[0];
733 $group = groups_get_group( array( 'group_id' => $group_id ) );
734
735 if ( bp_is_group_admin_screen( $this->forums_slug ) ) {
736 $group_permalink = bp_get_group_admin_permalink( $group );
737 } else {
738 $group_permalink = bp_get_group_permalink( $group );
739 }
740
741 $url = trailingslashit( $group_permalink . $this->forums_slug . '/' . $this->topic_slug . '/' . $slug );
742 }
743
744 return $url;
745 }
746
747 /**
748 * Map a topic permalink to the current group forum
749 *
750 * @since bbPress (r3802)
751 * @param string $url
752 * @param int $reply_id
753 * @return string
754 */
755 public function map_reply_permalink_to_group( $url, $reply_id ) {
756 $slug = get_post_field( 'post_name', $reply_id );
757 $forum_id = bbp_get_reply_forum_id( $reply_id );
758 $group_ids = bbp_get_forum_group_ids( $forum_id );
759
760 // If the topic is not associated with a group, don't mess with it
761 if ( !empty( $group_ids ) ) {
762
763 // @todo Multiple group forums/forum groups
764 $group_id = $group_ids[0];
765 $group = groups_get_group( array( 'group_id' => $group_id ) );
766
767 if ( bp_is_group_admin_screen( $this->forums_slug ) ) {
768 $group_permalink = bp_get_group_admin_permalink( $group );
769 } else {
770 $group_permalink = bp_get_group_permalink( $group );
771 }
772
773 $url = trailingslashit( $group_permalink . $this->forums_slug . '/' . $this->topic_slug . '/' . $slug );
774 }
775
776 return $url;
777 }
778
779 /**
780 * Ensure that forum content associated with a BuddyPress group can only be
781 * viewed via the group URL.
782 *
783 * @since bbPress (r3802)
784 */
785 function redirect_canonical() {
786
787 if ( bbp_is_single_forum() ) {
788 $forum_id = get_the_ID();
789 $group_ids = bbp_get_forum_group_ids( $forum_id );
790 } elseif ( bbp_is_single_topic() ) {
791 $topic_id = get_the_ID();
792 $slug = get_post_field( 'post_name', $topic_id );
793 $forum_id = bbp_get_topic_forum_id( $topic_id );
794 $group_ids = bbp_get_forum_group_ids( $forum_id );
795 }
796
797 if ( !empty( $group_ids ) ) {
798 // @todo Multiple group forums/forum groups
799 $group_id = $group_ids[0];
800 $group = groups_get_group( array( 'group_id' => $group_id ) );
801 $group_permalink = bp_get_group_permalink( $group );
802 $redirect_to = trailingslashit( $group_permalink . $this->forums_slug );
803
804 if ( !empty( $slug ) ) {
805 $redirect_to = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug );
806 }
807
808 bp_core_redirect( $redirect_to );
809 }
810 }
811 }
812 endif;
813
814
815 if ( !class_exists( 'BBP_Forums_Component' ) ) :
816 /**
817 * Loads Forums Component
818 *
819 * @since bbPress (r3552)
820 *
821 * @package bbPress
822 * @subpackage BuddyPress
823 */
824 class BBP_Forums_Component extends BP_Component {
825
826 /**
827 * Start the forums component creation process
828 *
829 * @since bbPress (r3552)
830 */
831 public function __construct() {
832 parent::start(
833 'forums',
834 __( 'Forums', 'bbpress' ),
835 BP_PLUGIN_DIR
836 );
837 $this->setup_globals();
838 $this->setup_nav();
839 }
840
841 /**
842 * Setup globals
843 *
844 * The BP_FORUMS_SLUG constant is deprecated, and only used here for
845 * backwards compatibility.
846 *
847 * @since bbPress (r3552)
848 * @global BuddyPress $bp
849 */
850 public function setup_globals() {
851 global $bp;
852
853 // Define the parent forum ID
854 if ( !defined( 'BP_FORUMS_PARENT_FORUM_ID' ) )
855 define( 'BP_FORUMS_PARENT_FORUM_ID', 1 );
856
857 // Define a slug, if necessary
858 if ( !defined( 'BP_FORUMS_SLUG' ) )
859 define( 'BP_FORUMS_SLUG', $this->id );
860
861 // All globals for messaging component.
862 $globals = array(
863 'path' => BP_PLUGIN_DIR,
864 'slug' => BP_FORUMS_SLUG,
865 'root_slug' => isset( $bp->pages->forums->slug ) ? $bp->pages->forums->slug : BP_FORUMS_SLUG,
866 'has_directory' => false,
867 'notification_callback' => 'messages_format_notifications',
868 'search_string' => __( 'Search Forums...', 'bbpress' ),
869 );
870
871 parent::setup_globals( $globals );
872 }
873
874 /**
875 * Setup BuddyBar navigation
876 *
877 * @since bbPress (r3552)
878 */
879 public function setup_nav() {
880
881 // Stop if there is no user displayed or logged in
882 if ( !is_user_logged_in() && !bp_displayed_user_id() )
883 return;
884
885 // Define local variable(s)
886 $sub_nav = array();
887 $user_domain = '';
888
889 // Add 'Forums' to the main navigation
890 $main_nav = array(
891 'name' => __( 'Forums', 'bbpress' ),
892 'slug' => $this->slug,
893 'position' => 80,
894 'screen_function' => 'bbp_member_forums_screen_topics',
895 'default_subnav_slug' => 'topics',
896 'item_css_id' => $this->id
897 );
898
899 // Determine user to use
900 if ( bp_displayed_user_id() )
901 $user_domain = bp_displayed_user_domain();
902 elseif ( bp_loggedin_user_domain() )
903 $user_domain = bp_loggedin_user_domain();
904 else
905 return;
906
907 // User link
908 $forums_link = trailingslashit( $user_domain . $this->slug );
909
910 // Topics started
911 $sub_nav[] = array(
912 'name' => __( 'Topics Started', 'bbpress' ),
913 'slug' => 'topics',
914 'parent_url' => $forums_link,
915 'parent_slug' => $this->slug,
916 'screen_function' => 'bbp_member_forums_screen_topics',
917 'position' => 20,
918 'item_css_id' => 'topics'
919 );
920
921 // Replies to topics
922 $sub_nav[] = array(
923 'name' => __( 'Topics Replied To', 'bbpress' ),
924 'slug' => 'replies',
925 'parent_url' => $forums_link,
926 'parent_slug' => $this->slug,
927 'screen_function' => 'bbp_member_forums_screen_replies',
928 'position' => 40,
929 'item_css_id' => 'replies'
930 );
931
932 // Favorite topics
933 $sub_nav[] = array(
934 'name' => __( 'Favorites', 'bbpress' ),
935 'slug' => 'favorites',
936 'parent_url' => $forums_link,
937 'parent_slug' => $this->slug,
938 'screen_function' => 'bbp_member_forums_screen_favorites',
939 'position' => 60,
940 'item_css_id' => 'favorites'
941 );
942
943 // Subscribed topics (my profile only)
944 if ( bp_is_my_profile() ) {
945 $sub_nav[] = array(
946 'name' => __( 'Subscriptions', 'bbpress' ),
947 'slug' => 'subscriptions',
948 'parent_url' => $forums_link,
949 'parent_slug' => $this->slug,
950 'screen_function' => 'bbp_member_forums_screen_subscriptions',
951 'position' => 60,
952 'item_css_id' => 'subscriptions'
953 );
954 }
955
956 parent::setup_nav( $main_nav, $sub_nav );
957 }
958
959 /**
960 * Set up the admin bar
961 *
962 * @since bbPress (r3552)
963 * @global BuddyPress $bp
964 */
965 public function setup_admin_bar() {
966 global $bp;
967
968 // Prevent debug notices
969 $wp_admin_nav = array();
970
971 // Menus for logged in user
972 if ( is_user_logged_in() ) {
973
974 // Setup the logged in user variables
975 $user_domain = bp_loggedin_user_domain();
976 $forums_link = trailingslashit( $user_domain . $this->slug );
977
978 // Add the "My Account" sub menus
979 $wp_admin_nav[] = array(
980 'parent' => $bp->my_account_menu_id,
981 'id' => 'my-account-' . $this->id,
982 'title' => __( 'Forums', 'bbpress' ),
983 'href' => trailingslashit( $forums_link )
984 );
985
986 // Topics
987 $wp_admin_nav[] = array(
988 'parent' => 'my-account-' . $this->id,
989 'id' => 'my-account-' . $this->id . '-topics',
990 'title' => __( 'Topics Started', 'bbpress' ),
991 'href' => trailingslashit( $forums_link . 'topics' )
992 );
993
994 // Replies
995 $wp_admin_nav[] = array(
996 'parent' => 'my-account-' . $this->id,
997 'id' => 'my-account-' . $this->id . '-replies',
998 'title' => __( 'Topics Replied To', 'bbpress' ),
999 'href' => trailingslashit( $forums_link . 'replies' )
1000 );
1001
1002 // Favorites
1003 $wp_admin_nav[] = array(
1004 'parent' => 'my-account-' . $this->id,
1005 'id' => 'my-account-' . $this->id . '-favorites',
1006 'title' => __( 'Favorite Topics', 'bbpress' ),
1007 'href' => trailingslashit( $forums_link . 'favorites' )
1008 );
1009
1010 // Subscriptions
1011 $wp_admin_nav[] = array(
1012 'parent' => 'my-account-' . $this->id,
1013 'id' => 'my-account-' . $this->id . '-subscriptions',
1014 'title' => __( 'Subscribed Topics', 'bbpress' ),
1015 'href' => trailingslashit( $forums_link . 'subscriptions' )
1016 );
1017 }
1018
1019 parent::setup_admin_bar( $wp_admin_nav );
1020 }
1021
1022 /**
1023 * Sets up the title for pages and <title>
1024 *
1025 * @since bbPress (r3552)
1026 *
1027 * @global BuddyPress $bp
1028 */
1029 public function setup_title() {
1030 global $bp;
1031
1032 // Adjust title based on view
1033 if ( bp_is_forums_component() ) {
1034 if ( bp_is_my_profile() ) {
1035 $bp->bp_options_title = __( 'Forums', 'bbpress' );
1036 } else {
1037 $bp->bp_options_avatar = bp_core_fetch_avatar( array(
1038 'item_id' => $bp->displayed_user->id,
1039 'type' => 'thumb'
1040 ) );
1041 $bp->bp_options_title = $bp->displayed_user->fullname;
1042 }
1043 }
1044
1045 parent::setup_title();
1046 }
1047 }
1048 endif;
1049
1050 if ( !class_exists( 'BBP_Forums_Group_Extension' ) && class_exists( 'BP_Group_Extension' ) ) :
1051 /**
1052 * Loads Group Extension for Forums Component
1053 *
1054 * @since bbPress (r3552)
1055 *
1056 * @package bbPress
1057 * @subpackage BuddyPress
1058 * @todo Everything
1059 */
1060 class BBP_Forums_Group_Extension extends BP_Group_Extension {
1061
1062 /**
1063 * Setup bbPress group extension variables
1064 *
1065 * @since bbPress (r3552)
1066 */
1067 public function __construct() {
1068
1069 // Name and slug
1070 $this->name = bbp_get_forum_archive_title();
1071 $this->nav_item_name = bbp_get_forum_archive_title();
1072 $this->slug = 'forums';
1073 $this->topic_slug = 'topic';
1074 $this->reply_slug = 'reply';
1075
1076 // Forum component is visible @todo configure?
1077 $this->visibility = 'public';
1078
1079 // Set positions towards end
1080 $this->create_step_position = 15;
1081 $this->nav_item_position = 10;
1082
1083 // Allow create step and show in nav
1084 $this->enable_create_step = true;
1085 $this->enable_nav_item = true;
1086 $this->enable_edit_item = true;
1087
1088 // I forget what these do
1089 $this->display_hook = 'bp_template_content';
1090 $this->template_file = 'groups/single/plugins';
1091
1092 // Add handlers to bp_actions
1093 add_action( 'bp_actions', 'bbp_new_forum_handler' );
1094 add_action( 'bp_actions', 'bbp_new_topic_handler' );
1095 add_action( 'bp_actions', 'bbp_new_reply_handler' );
1096 add_action( 'bp_actions', 'bbp_edit_forum_handler' );
1097 add_action( 'bp_actions', 'bbp_edit_topic_handler' );
1098 add_action( 'bp_actions', 'bbp_edit_reply_handler' );
1099
1100 // Tweak the redirect field
1101 add_filter( 'bbp_new_topic_redirect_to', array( $this, 'new_topic_redirect_to' ), 10, 3 );
1102 add_filter( 'bbp_new_reply_redirect_to', array( $this, 'new_reply_redirect_to' ), 10, 3 );
1103 }
1104
1105 /**
1106 * The primary display function for group forums
1107 */
1108 public function display() {
1109
1110 // Prevent Topic Parent from appearing
1111 add_action( 'bbp_theme_before_topic_form_forum', array( $this, 'ob_start' ) );
1112 add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'ob_end_clean' ) );
1113 add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'topic_parent' ) );
1114
1115 // Prevent Forum Parent from appearing
1116 add_action( 'bbp_theme_before_forum_form_parent', array( $this, 'ob_start' ) );
1117 add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'ob_end_clean' ) );
1118 add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'forum_parent' ) );
1119
1120 // Hide breadcrumb
1121 add_filter( 'bbp_no_breadcrumb', '__return_true' );
1122
1123 $this->display_forums( 0 );
1124 }
1125
1126 /** Edit ******************************************************************/
1127
1128 /**
1129 * Show forums and new forum form when editing a group
1130 *
1131 * @since bbPress (r3563)
1132 * @uses bbp_get_template_part()
1133 */
1134 public function edit_screen() {
1135
1136 // Add group admin actions to forum row actions
1137 add_action( 'bbp_forum_row_actions', array( $this, 'forum_row_actions' ) );
1138 add_action( 'bbp_topic_row_actions', array( $this, 'topic_row_actions' ) );
1139
1140 // Prevent Topic Parent from appearing
1141 add_action( 'bbp_theme_before_topic_form_forum', array( $this, 'ob_start' ) );
1142 add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'ob_end_clean' ) );
1143 add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'topic_parent' ) );
1144
1145 // Prevent Forum Parent from appearing
1146 add_action( 'bbp_theme_before_forum_form_parent', array( $this, 'ob_start' ) );
1147 add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'ob_end_clean' ) );
1148 add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'forum_parent' ) );
1149
1150 // Do not show the new topic form in the moderation area
1151 add_filter( 'bbp_current_user_can_access_create_topic_form', '__return_false' );
1152
1153 // Hide breadcrumb
1154 add_filter( 'bbp_no_breadcrumb', '__return_true' );
1155
1156 $this->display_forums( 1 );
1157 }
1158
1159 /**
1160 * Save the Group Forum data on edit
1161 *
1162 * @since bbPress (r3465)
1163 * @uses bbp_new_forum_handler() To check for forum creation
1164 * @uses bbp_edit_forum_handler() To check for forum edit
1165 */
1166 public function edit_screen_save() {
1167
1168 // Bail if not a POST action
1169 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1170 return;
1171
1172 // Bail if action is empty
1173 if ( empty( $_POST['action'] ) )
1174 return;
1175
1176 // Handle the different actions that can happen here
1177 switch ( $_POST['action'] ) {
1178
1179 // New forum
1180 case 'bbp-new-forum' :
1181
1182 // Redirect back here, not to the new forum
1183 add_filter( 'bbp_new_forum_redirect_to', array( $this, 'edit_redirect_to' ) );
1184
1185 // Add actions to bbp_new_forum
1186 add_action( 'bbp_new_forum', array( $this, 'new_forum' ), 10, 4 );
1187
1188 // Handle the new forum
1189 bbp_new_forum_handler();
1190
1191 break;
1192
1193 // Edit existing forum
1194 case 'bbp-edit-forum' :
1195
1196 // Redirect back here, not to the new forum
1197 add_filter( 'bbp_edit_forum_redirect_to', array( $this, 'edit_redirect_to' ) );
1198
1199 // Handle the forum edit
1200 bbp_edit_forum_handler();
1201
1202 break;
1203
1204 // Trash a forum
1205 case 'bbp-trash-forum' :
1206 //bbp_trash_forum_handler();
1207 break;
1208
1209 // Permanently delet a forum
1210 case 'bbp-delete-forum' :
1211 //bbp_delete_forum_handler();
1212 break;
1213 }
1214 }
1215
1216 /** Create ****************************************************************/
1217
1218 /**
1219 * Show forums and new forum form when creating a group
1220 *
1221 * @since bbPress (r3465)
1222 */
1223 public function create_screen() {
1224
1225 // Bail if not looking at this screen
1226 if ( !bp_is_group_creation_step( $this->slug ) )
1227 return false;
1228
1229 $checked = bp_get_new_group_enable_forum() || groups_get_groupmeta( bp_get_new_group_id(), 'forum_id' ); ?>
1230
1231 <h4><?php _e( 'Group Forums', 'bbpress' ); ?></h4>
1232
1233 <p><?php _e( 'Create a discussion forum to allow members of this group to communicate in a structured, bulletin-board style fashion.', 'bbpress' ); ?></p>
1234
1235 <div class="checkbox">
1236 <label><input type="checkbox" name="bbp-create-group-forum" id="bbp-create-group-forum" value="1"<?php checked( $checked ); ?> /> <?php _e( 'Yes. I want this group to have a forum.', 'bbpress' ); ?></label>
1237 </div>
1238
1239 <?php
1240
1241 // Verify intent
1242 wp_nonce_field( 'groups_create_save_' . $this->slug );
1243 }
1244
1245 /**
1246 * Save the Group Forum data on create
1247 *
1248 * @since bbPress (r3465)
1249 */
1250 public function create_screen_save() {
1251
1252 check_admin_referer( 'groups_create_save_' . $this->slug );
1253
1254 $create_forum = !empty( $_POST['bbp-create-group-forum'] ) ? true : false;
1255 $forum_id = 0;
1256 $forum_ids = bbp_get_group_forum_ids( bp_get_new_group_id() );
1257 if ( !empty( $forum_ids ) )
1258 $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids;
1259
1260 // Create a forum, or not
1261 switch ( $create_forum ) {
1262 case true :
1263
1264 // Bail if initial content was already created
1265 if ( !empty( $forum_id ) )
1266 return;
1267
1268 // Set the default forum status
1269 switch ( bp_get_new_group_status() ) {
1270 case 'hidden' :
1271 $status = bbp_get_hidden_status_id();
1272 break;
1273 case 'private' :
1274 $status = bbp_get_private_status_id();
1275 break;
1276 case 'public' :
1277 default :
1278 $status = bbp_get_public_status_id();
1279 break;
1280 }
1281
1282 // Create the initial forum
1283 $forum_id = bbp_insert_forum( array(
1284 'post_parent' => bbp_get_group_forums_root_id(),
1285 'post_title' => bp_get_new_group_name(),
1286 'post_content' => bp_get_new_group_description(),
1287 'post_status' => $status
1288 ) );
1289
1290 // Run the BP-specific functions for new groups
1291 $this->new_forum( array( 'forum_id' => $forum_id ) );
1292
1293 break;
1294 case false :
1295
1296 // Forum was created but is now being undone
1297 if ( !empty( $forum_id ) ) {
1298 wp_delete_post( $forum_id, true );
1299 groups_delete_groupmeta( bp_get_new_group_id(), 'forum_id' );
1300 }
1301
1302 break;
1303 }
1304 }
1305
1306 /**
1307 * Used to start an output buffer
1308 */
1309 public function ob_start() {
1310 ob_start();
1311 }
1312
1313 /**
1314 * Used to end an output buffer
1315 */
1316 public function ob_end_clean() {
1317 ob_end_clean();
1318 }
1319
1320 /**
1321 * Creating a group forum or category (including root for group)
1322 *
1323 * @since bbPress (r3653)
1324 * @param type $forum_args
1325 * @uses bbp_get_forum_id()
1326 * @uses bp_get_current_group_id()
1327 * @uses bbp_add_forum_id_to_group()
1328 * @uses bbp_add_group_id_to_forum()
1329 * @return if no forum_id is available
1330 */
1331 public function new_forum( $forum_args = array() ) {
1332
1333 // Bail if no forum_id was passed
1334 if ( empty( $forum_args['forum_id'] ) )
1335 return;
1336
1337 // Validate forum_id
1338 $forum_id = bbp_get_forum_id( $forum_args['forum_id'] );
1339 $group_id = bp_get_current_group_id();
1340
1341 bbp_add_forum_id_to_group( $group_id, $forum_id );
1342 bbp_add_group_id_to_forum( $forum_id, $group_id );
1343 }
1344
1345 /**
1346 * Removing a group forum or category (including root for group)
1347 *
1348 * @since bbPress (r3653)
1349 * @param type $forum_args
1350 * @uses bbp_get_forum_id()
1351 * @uses bp_get_current_group_id()
1352 * @uses bbp_add_forum_id_to_group()
1353 * @uses bbp_add_group_id_to_forum()
1354 * @return if no forum_id is available
1355 */
1356 public function remove_forum( $forum_args = array() ) {
1357
1358 // Bail if no forum_id was passed
1359 if ( empty( $forum_args['forum_id'] ) )
1360 return;
1361
1362 // Validate forum_id
1363 $forum_id = bbp_get_forum_id( $forum_args['forum_id'] );
1364 $group_id = bp_get_current_group_id();
1365
1366 bbp_remove_forum_id_from_group( $group_id, $forum_id );
1367 bbp_remove_group_id_from_forum( $forum_id, $group_id );
1368 }
1369
1370 /** Display Methods *******************************************************/
1371
1372 /**
1373 * Output the forums for a group in the edit screens
1374 *
1375 * @since bbPress (r3653)
1376 * @uses bp_get_current_group_id()
1377 * @uses bbp_get_group_forum_ids()
1378 * @uses bbp_has_forums()
1379 * @uses bbp_get_template_part()
1380 */
1381 public function display_forums( $offset = 0 ) {
1382 global $wpdb;
1383
1384 $bbp = bbpress();
1385
1386 // Forum data
1387 $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() );
1388 $forum_args = array( 'post__in' => $forum_ids, 'post_parent' => null ); ?>
1389
1390 <div id="bbpress-forums">
1391
1392 <?php
1393
1394 // Looking at the group forum root
1395 if ( ! bp_action_variable( $offset ) ) :
1396
1397 // Query forums and show them if
1398 if ( !empty( $forum_ids ) && bbp_has_forums( $forum_args ) ) :
1399
1400 bbp_the_forum();
1401
1402 // Only one forum found
1403 if ( $bbp->forum_query->post_count == 1 ) :?>
1404
1405 <h3><?php _e( 'Forum', 'bbpress' ); ?></h3>
1406
1407 <?php bbp_set_query_name( 'bbp_single_forum' ); ?>
1408
1409 <?php if ( bbp_has_topics( array( 'post_parent' => bbp_get_forum_id() ) ) ) : ?>
1410
1411 <?php bbp_get_template_part( 'pagination', 'topics' ); ?>
1412
1413 <?php bbp_get_template_part( 'loop', 'topics' ); ?>
1414
1415 <?php bbp_get_template_part( 'pagination', 'topics' ); ?>
1416
1417 <?php bbp_get_template_part( 'form', 'topic' ); ?>
1418
1419 <?php else : ?>
1420
1421 <?php bbp_get_template_part( 'feedback', 'no-topics' ); ?>
1422
1423 <?php bbp_get_template_part( 'form', 'topic' ); ?>
1424
1425 <?php endif;
1426
1427 // More than 1 forum found
1428 elseif ( $bbp->forum_query->post_count > 1 ) : ?>
1429
1430 <h3><?php _e( 'Forums', 'bbpress' ); ?></h3>
1431
1432 <?php bbp_get_template_part( 'loop', 'forums' ); ?>
1433
1434 <h3><?php _e( 'Topics', 'bbpress' ); ?></h3>
1435
1436 <?php if ( bbp_has_topics( array( 'post_parent__in' => $forum_ids ) ) ) : ?>
1437
1438 <?php bbp_get_template_part( 'pagination', 'topics' ); ?>
1439
1440 <?php bbp_get_template_part( 'loop', 'topics' ); ?>
1441
1442 <?php bbp_get_template_part( 'pagination', 'topics' ); ?>
1443
1444 <?php bbp_get_template_part( 'form', 'topic' ); ?>
1445
1446 <?php else : ?>
1447
1448 <?php bbp_get_template_part( 'feedback', 'no-topics' ); ?>
1449
1450 <?php bbp_get_template_part( 'form', 'topic' ); ?>
1451
1452 <?php endif;
1453
1454 // No forums found
1455 else : ?>
1456
1457 <div id="message" class="info">
1458 <p><?php _e( 'This group does not currently have any forums.', 'bbpress' ); ?></p>
1459 </div>
1460
1461 <?php if ( bp_is_group_admin_screen( $this->slug ) ) :
1462 bbp_get_template_part( 'form', 'forum' );
1463 endif;
1464 endif;
1465
1466 // No forums found
1467 else : ?>
1468
1469 <div id="message" class="info">
1470 <p><?php _e( 'This group does not currently have any forums.', 'bbpress' ); ?></p>
1471 </div>
1472
1473 <?php if ( bp_is_group_admin_screen( $this->slug ) ) :
1474 bbp_get_template_part( 'form', 'forum' );
1475 endif;
1476 endif;
1477
1478 // Single forum
1479 elseif ( ( bp_action_variable( $offset ) != $this->slug ) && ( bp_action_variable( $offset ) != $this->topic_slug ) ) :
1480
1481 // Get the forum
1482 $forum_post_type = bbp_get_forum_post_type();
1483 $forum_slug = bp_action_variable( $offset );
1484 $forums = $wpdb->get_row( "SELECT ID FROM {$wpdb->posts} WHERE post_name = '{$forum_slug}' AND post_type = '{$forum_post_type}'", ARRAY_N );
1485
1486 // Forum exists
1487 if ( !empty( $forums ) ) :
1488 $forum_id = $forums[0];
1489 $bbp->current_forum_id = $forum_id;
1490 bbp_set_query_name( 'bbp_single_forum' ); ?>
1491
1492 <h3><?php bbp_forum_title(); ?></h3>
1493
1494 <?php bbp_get_template_part( 'content', 'single-forum' ); ?>
1495
1496 <?php else : ?>
1497
1498 <?php bbp_get_template_part( 'feedback', 'no-topics' ); ?>
1499
1500 <?php bbp_get_template_part( 'form', 'topic' ); ?>
1501
1502 <?php endif;
1503
1504 // Single topic
1505 elseif ( ( bp_action_variable( $offset ) != $this->slug ) && ( bp_action_variable( $offset ) == $this->topic_slug ) ) :
1506
1507 // Get the topic
1508 $topic_post_type = bbp_get_topic_post_type();
1509 $topic_slug = bp_action_variable( $offset + 1 );
1510 $topics = $wpdb->get_row( "SELECT ID FROM {$wpdb->posts} WHERE post_name = '{$topic_slug}' AND post_type = '{$topic_post_type}'", ARRAY_N );
1511
1512 // Topic exists
1513 if ( !empty( $topics ) ) :
1514 $topic_id = $topics[0];
1515 $bbp->current_topic_id = $topic_id;
1516 bbp_set_query_name( 'bbp_single_topic' ); ?>
1517
1518 <h3><?php bbp_topic_title(); ?></h3>
1519
1520 <?php bbp_get_template_part( 'content', 'single-topic' ); ?>
1521
1522 <?php else : ?>
1523
1524 <?php bbp_get_template_part( 'feedback', 'no-topics' ); ?>
1525
1526 <?php bbp_get_template_part( 'form', 'topic' ); ?>
1527
1528 <?php endif;
1529
1530 endif; ?>
1531
1532 </div>
1533
1534 <?php
1535 }
1536
1537 /**
1538 * Add forum row action HTML when viewing group forum admin
1539 *
1540 * @since bbPress (r3653)
1541 * @uses bp_is_item_admin()
1542 * @uses bbp_get_forum_id()
1543 */
1544 public function forum_row_actions() {
1545
1546 // Only admins can take actions on forums
1547 if ( is_super_admin() || current_user_can( 'moderate' ) || bp_is_item_admin() || bp_is_item_mod() ) : ?>
1548
1549 <div class="row-actions">
1550
1551 <?php echo 'Edit | View | Trash'; ?>
1552
1553 </div>
1554
1555 <?php endif;
1556 }
1557
1558 /**
1559 * Add topic row action HTML when viewing group forum admin
1560 *
1561 * @since bbPress (r3653)
1562 * @uses bp_is_item_admin()
1563 * @uses bbp_get_forum_id()
1564 */
1565 public function topic_row_actions() {
1566
1567 // Only admins can take actions on forums
1568 if ( is_super_admin() || current_user_can( 'moderate' ) || bp_is_item_admin() || bp_is_item_mod() ) : ?>
1569
1570 <div class="row-actions">
1571
1572 <?php echo 'Edit | View | Trash | Close | Stick (To Front) | Spam'; ?>
1573
1574 </div>
1575
1576 <?php endif;
1577 }
1578
1579 /** Redirect Helpers ******************************************************/
1580
1581 /**
1582 * Redirect to the group forum screen
1583 *
1584 * @since bbPress (r3653)
1585 * @param str $redirect_url
1586 * @param str $redirect_to
1587 */
1588 public function new_topic_redirect_to( $redirect_url = '', $redirect_to = '', $topic_id = 0 ) {
1589 if ( bp_is_group() ) {
1590 $topic = bbp_get_topic( $topic_id );
1591 $topic_hash = '#post-' . $topic_id;
1592 $redirect_url = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ) . $topic_hash;
1593 }
1594
1595 return $redirect_url;
1596 }
1597
1598 /**
1599 * Redirect to the group forum screen
1600 *
1601 * @since bbPress (r3653)
1602 */
1603 public function new_reply_redirect_to( $redirect_url = '', $redirect_to = '', $reply_id = 0 ) {
1604 global $wp_rewrite;
1605
1606 if ( bp_is_group() ) {
1607 $topic_id = bbp_get_reply_topic_id( $reply_id );
1608 $topic = bbp_get_topic( $topic_id );
1609 $reply_position = bbp_get_reply_position( $reply_id, $topic_id );
1610 $reply_page = ceil( (int) $reply_position / (int) bbp_get_replies_per_page() );
1611 $reply_hash = '#post-' . $reply_id;
1612 $topic_url = trailingslashit( bp_get_group_permalink( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name );
1613
1614 // Don't include pagination if on first page
1615 if ( 1 >= $reply_page ) {
1616 $redirect_url = trailingslashit( $topic_url ) . $reply_hash;
1617
1618 // Include pagination
1619 } else {
1620 $redirect_url = trailingslashit( $topic_url ) . trailingslashit( $wp_rewrite->pagination_base ) . trailingslashit( $reply_page ) . $reply_hash;
1621 }
1622
1623 // Add topic view query arg back to end if it is set
1624 if ( bbp_get_view_all() ) {
1625 $redirect_url = bbp_add_view_all( $redirect_url );
1626 }
1627 }
1628
1629 return $redirect_url;
1630 }
1631
1632 /**
1633 * Redirect to the group admin forum edit screen
1634 *
1635 * @since bbPress (r3653)
1636 * @uses groups_get_current_group()
1637 * @uses bp_is_group_admin_screen()
1638 * @uses trailingslashit()
1639 * @uses bp_get_root_domain()
1640 * @uses bp_get_groups_root_slug()
1641 */
1642 public function edit_redirect_to( $redirect_url = '' ) {
1643
1644 // Get the current group, if there is one
1645 $group = groups_get_current_group();
1646
1647 // If this is a group of any kind, empty out the redirect URL
1648 if ( bp_is_group_admin_screen( $this->slug ) )
1649 $redirect_url = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group->slug . '/admin/' . $this->slug );
1650
1651 return $redirect_url;
1652 }
1653
1654 /** Form Helpers **********************************************************/
1655
1656 public function forum_parent() {
1657 ?>
1658
1659 <input type="hidden" name="bbp_forum_parent_id" id="bbp_forum_parent_id" value="<?php bbp_group_forums_root_id(); ?>" />
1660
1661 <?php
1662 }
1663
1664 public function topic_parent() {
1665
1666 $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() ); ?>
1667
1668 <p>
1669 <label for="bbp_forum_id"><?php _e( 'Forum:', 'bbpress' ); ?></label><br />
1670 <?php bbp_dropdown( array( 'include' => $forum_ids, 'selected' => bbp_get_form_topic_forum() ) ); ?>
1671 </p>
1672
1673 <?php
1674 }
1675 }
1676 endif;
1677
1678 /**
1679 * Creates the Forums component in BuddyPress
1680 *
1681 * @since bbPress (r3653)
1682 *
1683 * @global type $bp
1684 * @return If bbPress is not active
1685 */
1686 function bbp_setup_buddypress_component() {
1687 global $bp;
1688
1689 // Bail if no BuddyPress
1690 if ( !empty( $bp->maintenance_mode ) || !defined( 'BP_VERSION' ) ) return;
1691
1692 // Bail if BuddyPress Forums are already active
1693 if ( bp_is_active( 'forums' ) && bp_forums_is_installed_correctly() ) return;
1694
1695 // Create the new BuddyPress Forums component
1696 $bp->forums = new BBP_Forums_Component();
1697
1698 // Register the group extension only if groups are active
1699 if ( bbp_is_group_forums_active() && bp_is_active( 'groups' ) ) {
1700 bp_register_group_extension( 'BBP_Forums_Group_Extension' );
1701 }
1702 }
1703
1704 /** BuddyPress Helpers ********************************************************/
1705
1706 /**
1707 * Filter the current bbPress user ID with the current BuddyPress user ID
1708 *
1709 * @since bbPress (r3552)
1710 *
1711 * @global BuddyPress $bp
1712 * @param int $user_id
1713 * @param bool $displayed_user_fallback
1714 * @param bool $current_user_fallback
1715 * @return int User ID
1716 */
1717 function bbp_filter_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
1718
1719 // Define local variable
1720 $bbp_user_id = 0;
1721
1722 // Get possible user ID's
1723 $did = bp_displayed_user_id();
1724 $lid = bp_loggedin_user_id();
1725
1726 // Easy empty checking
1727 if ( !empty( $user_id ) && is_numeric( $user_id ) )
1728 $bbp_user_id = $user_id;
1729
1730 // Currently viewing or editing a user
1731 elseif ( ( true == $displayed_user_fallback ) && !empty( $did ) )
1732 $bbp_user_id = $did;
1733
1734 // Maybe fallback on the current_user ID
1735 elseif ( ( true == $current_user_fallback ) && !empty( $lid ) )
1736 $bbp_user_id = $lid;
1737
1738 return $bbp_user_id;
1739 }
1740 add_filter( 'bbp_get_user_id', 'bbp_filter_user_id', 10, 3 );
1741
1742 /**
1743 * Filter the bbPress is_single_user function with BuddyPress eqivalent
1744 *
1745 * @since bbPress (r3552)
1746 *
1747 * @param bool $is Optional. Default false
1748 * @return bool True if viewing single user, false if not
1749 */
1750 function bbp_filter_is_single_user( $is = false ) {
1751 if ( !empty( $is ) )
1752 return $is;
1753
1754 return bp_is_user();
1755 }
1756 add_filter( 'bbp_is_single_user', 'bbp_filter_is_single_user', 10, 1 );
1757
1758 /**
1759 * Filter the bbPress is_user_home function with BuddyPress eqivalent
1760 *
1761 * @since bbPress (r3552)
1762 *
1763 * @param bool $is Optional. Default false
1764 * @return bool True if viewing single user, false if not
1765 */
1766 function bbp_filter_is_user_home( $is = false ) {
1767 if ( !empty( $is ) )
1768 return $is;
1769
1770 return bp_is_my_profile();
1771 }
1772 add_filter( 'bbp_is_user_home', 'bbp_filter_is_user_home', 10, 1 );
1773
1774 /** BuddyPress Screens ********************************************************/
1775
1776 /**
1777 * Hook bbPress topics template into plugins template
1778 *
1779 * @since bbPress (r3552)
1780 *
1781 * @uses add_action() To add the content hook
1782 * @uses bp_core_load_template() To load the plugins template
1783 */
1784 function bbp_member_forums_screen_topics() {
1785 add_action( 'bp_template_content', 'bbp_member_forums_topics_content' );
1786 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_topics', 'members/single/plugins' ) );
1787 }
1788
1789 /**
1790 * Hook bbPress replies template into plugins template
1791 *
1792 * @since bbPress (r3552)
1793 *
1794 * @uses add_action() To add the content hook
1795 * @uses bp_core_load_template() To load the plugins template
1796 */
1797 function bbp_member_forums_screen_replies() {
1798 add_action( 'bp_template_content', 'bbp_member_forums_replies_content' );
1799 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_replies', 'members/single/plugins' ) );
1800 }
1801
1802 /**
1803 * Hook bbPress favorites template into plugins template
1804 *
1805 * @since bbPress (r3552)
1806 *
1807 * @uses add_action() To add the content hook
1808 * @uses bp_core_load_template() To load the plugins template
1809 */
1810 function bbp_member_forums_screen_favorites() {
1811 add_action( 'bp_template_content', 'bbp_member_forums_favorites_content' );
1812 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_favorites', 'members/single/plugins' ) );
1813 }
1814
1815 /**
1816 * Hook bbPress subscriptions template into plugins template
1817 *
1818 * @since bbPress (r3552)
1819 *
1820 * @uses add_action() To add the content hook
1821 * @uses bp_core_load_template() To load the plugins template
1822 */
1823 function bbp_member_forums_screen_subscriptions() {
1824 add_action( 'bp_template_content', 'bbp_member_forums_subscriptions_content' );
1825 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_subscriptions', 'members/single/plugins' ) );
1826 }
1827
1828 /** BuddyPress Templates ******************************************************/
1829
1830 /**
1831 * Get the topics created template part
1832 *
1833 * @since bbPress (r3552)
1834 *
1835 * @uses bbp_get_template_part()s
1836 */
1837 function bbp_member_forums_topics_content() {
1838 ?>
1839
1840 <div id="bbpress-forums">
1841
1842 <?php bbp_get_template_part( 'user', 'topics-created' ); ?>
1843
1844 </div>
1845
1846 <?php
1847 }
1848
1849 /**
1850 * Get the topics replied to template part
1851 *
1852 * @since bbPress (r3552)
1853 *
1854 * @uses bbp_get_template_part()
1855 */
1856 function bbp_member_forums_replies_content() {
1857 ?>
1858
1859 <div id="bbpress-forums">
1860
1861 <?php bbp_get_template_part( 'user', 'topics-replied-to' ); ?>
1862
1863 </div>
1864
1865 <?php
1866 }
1867
1868 /**
1869 * Get the topics favorited template part
1870 *
1871 * @since bbPress (r3552)
1872 *
1873 * @uses bbp_get_template_part()
1874 */
1875 function bbp_member_forums_favorites_content() {
1876 ?>
1877
1878 <div id="bbpress-forums">
1879
1880 <?php bbp_get_template_part( 'user', 'favorites' ); ?>
1881
1882 </div>
1883
1884 <?php
1885 }
1886
1887 /**
1888 * Get the topics subscribed template part
1889 *
1890 * @since bbPress (r3552)
1891 *
1892 * @uses bbp_get_template_part()
1893 */
1894 function bbp_member_forums_subscriptions_content() {
1895 ?>
1896
1897 <div id="bbpress-forums">
1898
1899 <?php bbp_get_template_part( 'user', 'subscriptions' ); ?>
1900
1901 </div>
1902
1903 <?php
1904 }
1905
1906 /** Forum/Group Sync **********************************************************/
1907
1908 /**
1909 * These functions are used to keep the many-to-many relationships between
1910 * groups and forums synchronized. Each forum and group stores ponters to each
1911 * other in their respective meta. This way if a group or forum is deleted
1912 * their associattions can be updated without much effort.
1913 */
1914
1915 /**
1916 * Get forum ID's for a group
1917 *
1918 * @param type $group_id
1919 * @since bbPress (r3653)
1920 */
1921 function bbp_get_group_forum_ids( $group_id = 0 ) {
1922
1923 // Assume no forums
1924 $forum_ids = array();
1925
1926 // Use current group if none is set
1927 if ( empty( $group_id ) )
1928 $group_id = bp_get_current_group_id();
1929
1930 // Get the forums
1931 if ( !empty( $group_id ) )
1932 $forum_ids = groups_get_groupmeta( $group_id, 'forum_id' );
1933
1934 // Make sure result is an array
1935 if ( !is_array( $forum_ids ) )
1936 $forum_ids = (array) $forum_ids;
1937
1938 // Trim out any empty array items
1939 $forum_ids = array_filter( $forum_ids );
1940
1941 return (array) apply_filters( 'bbp_get_group_forum_ids', $forum_ids, $group_id );
1942 }
1943
1944 /**
1945 * Get group ID's for a forum
1946 *
1947 * @param type $forum_id
1948 * @since bbPress (r3653)
1949 */
1950 function bbp_get_forum_group_ids( $forum_id = 0 ) {
1951
1952 // Assume no forums
1953 $group_ids = array();
1954
1955 // Use current group if none is set
1956 if ( empty( $forum_id ) )
1957 $forum_id = bbp_get_forum_id();
1958
1959 // Get the forums
1960 if ( !empty( $forum_id ) )
1961 $group_ids = get_post_meta( $forum_id, '_bbp_group_ids', true );
1962
1963 // Make sure result is an array
1964 if ( !is_array( $group_ids ) )
1965 $group_ids = (array) $group_ids;
1966
1967 // Trim out any empty array items
1968 $group_ids = array_filter( $group_ids );
1969
1970 return (array) apply_filters( 'bbp_get_forum_group_ids', $group_ids, $forum_id );
1971 }
1972
1973 /**
1974 * Get forum ID's for a group
1975 *
1976 * @param type $group_id
1977 * @since bbPress (r3653)
1978 */
1979 function bbp_update_group_forum_ids( $group_id = 0, $forum_ids = array() ) {
1980
1981 // Use current group if none is set
1982 if ( empty( $group_id ) )
1983 $group_id = bp_get_current_group_id();
1984
1985 // Trim out any empties
1986 $forum_ids = array_filter( $forum_ids );
1987
1988 // Get the forums
1989 return groups_update_groupmeta( $group_id, 'forum_id', $forum_ids );
1990 }
1991
1992 /**
1993 * Update group ID's for a forum
1994 *
1995 * @param type $forum_id
1996 * @since bbPress (r3653)
1997 */
1998 function bbp_update_forum_group_ids( $forum_id = 0, $group_ids = array() ) {
1999 $forum_id = bbp_get_forum_id( $forum_id );
2000
2001 // Trim out any empties
2002 $group_ids = array_filter( $group_ids );
2003
2004 // Get the forums
2005 return update_post_meta( $forum_id, '_bbp_group_ids', $group_ids );
2006 }
2007
2008 /**
2009 * Add a group to a forum
2010 *
2011 * @param type $group_id
2012 * @since bbPress (r3653)
2013 */
2014 function bbp_add_group_id_to_forum( $forum_id = 0, $group_id = 0 ) {
2015
2016 // Validate forum_id
2017 $forum_id = bbp_get_forum_id( $forum_id );
2018
2019 // Use current group if none is set
2020 if ( empty( $group_id ) )
2021 $group_id = bp_get_current_group_id();
2022
2023 // Get current group IDs
2024 $group_ids = bbp_get_forum_group_ids( $forum_id );
2025
2026 // Maybe update the groups forums
2027 if ( !in_array( $group_id, $group_ids ) ) {
2028 $group_ids[] = $group_id;
2029 return bbp_update_forum_group_ids( $forum_id, $group_ids );
2030 }
2031 }
2032
2033 /**
2034 * Remove a forum from a group
2035 *
2036 * @param type $group_id
2037 * @since bbPress (r3653)
2038 */
2039 function bbp_add_forum_id_to_group( $group_id = 0, $forum_id = 0 ) {
2040
2041 // Validate forum_id
2042 $forum_id = bbp_get_forum_id( $forum_id );
2043
2044 // Use current group if none is set
2045 if ( empty( $group_id ) )
2046 $group_id = bp_get_current_group_id();
2047
2048 // Get current group IDs
2049 $forum_ids = bbp_get_group_forum_ids( $group_id );
2050
2051 // Maybe update the groups forums
2052 if ( !in_array( $forum_id, $forum_ids ) ) {
2053 $forum_ids[] = $forum_id;
2054 return bbp_update_group_forum_ids( $group_id, $forum_ids );
2055 }
2056 }
2057
2058 /**
2059 * Remove a group from a forum
2060 *
2061 * @param type $group_id
2062 * @since bbPress (r3653)
2063 */
2064 function bbp_remove_group_id_from_forum( $forum_id = 0, $group_id = 0 ) {
2065
2066 // Validate forum_id
2067 $forum_id = bbp_get_forum_id( $forum_id );
2068
2069 // Use current group if none is set
2070 if ( empty( $group_id ) )
2071 $group_id = bp_get_current_group_id();
2072
2073 // Get current group IDs
2074 $group_ids = bbp_get_forum_group_ids( $forum_id );
2075
2076 // Maybe update the groups forums
2077 if ( in_array( $group_id, $group_ids ) ) {
2078 unset( $group_ids[$group_id] );
2079 return bbp_update_forum_group_ids( $forum_id, $group_ids );
2080 }
2081 }
2082
2083 /**
2084 * Remove a forum from a group
2085 *
2086 * @param type $group_id
2087 * @since bbPress (r3653)
2088 */
2089 function bbp_remove_forum_id_from_group( $group_id = 0, $forum_id = 0 ) {
2090
2091 // Validate forum_id
2092 $forum_id = bbp_get_forum_id( $forum_id );
2093
2094 // Use current group if none is set
2095 if ( empty( $group_id ) )
2096 $group_id = bp_get_current_group_id();
2097
2098 // Get current group IDs
2099 $forum_ids = bbp_get_group_forum_ids( $group_id );
2100
2101 // Maybe update the groups forums
2102 if ( in_array( $forum_id, $forum_ids ) ) {
2103 unset( $forum_ids[$forum_id] );
2104 return bbp_update_group_forum_ids( $group_id, $forum_ids );
2105 }
2106 }
2107
2108 /**
2109 * Remove a group from aall forums
2110 *
2111 * @param type $group_id
2112 * @since bbPress (r3653)
2113 */
2114 function bbp_remove_group_id_from_all_forums( $group_id = 0 ) {
2115
2116 // Use current group if none is set
2117 if ( empty( $group_id ) )
2118 $group_id = bp_get_current_group_id();
2119
2120 // Get current group IDs
2121 $forum_ids = bbp_get_group_forum_ids( $group_id );
2122
2123 // Loop through forums and remove this group from each one
2124 foreach( (array) $forum_ids as $forum_id ) {
2125 bbp_remove_group_id_from_forum( $group_id, $forum_id );
2126 }
2127 }
2128
2129 /**
2130 * Remove a forum from all groups
2131 *
2132 * @param type $forum_id
2133 * @since bbPress (r3653)
2134 */
2135 function bbp_remove_forum_id_from_all_groups( $forum_id = 0 ) {
2136
2137 // Validate
2138 $forum_id = bbp_get_forum_id( $forum_id );
2139 $group_ids = bbp_get_forum_group_ids( $forum_id );
2140
2141 // Loop through groups and remove this forum from each one
2142 foreach( (array) $group_ids as $group_id ) {
2143 bbp_remove_forum_id_from_group( $forum_id, $group_id );
2144 }
2145 }
2146