PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 2.5.0
Forumax – AI Powered Advanced Community Forum Plugin v2.5.0
2.5.0 2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 All 30 releases
bbp-core / lib / bbpress / includes / extend / buddypress / groups.php

groups.php in Forumax – AI Powered Advanced Community Forum Plugin 2.5.0, at lib/bbpress/includes/extend/buddypress/groups.php

1,845 lines 49.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress BuddyPress Group Extension Class
5 *
6 * This file is responsible for connecting bbPress to the BuddyPress Groups
7 * Component. It's a great example of how to perform both simple and advanced
8 * techniques to manipulate the default output provided by both.
9 *
10 * @package bbPress
11 * @subpackage BuddyPress
12 */
13
14 // Exit if accessed directly
15 defined( 'ABSPATH' ) || exit;
16
17 if ( ! class_exists( 'BBP_Forums_Group_Extension' ) && class_exists( 'BP_Group_Extension' ) ) :
18 /**
19 * Loads Group Extension for Forums Component
20 *
21 * @since 2.1.0 bbPress (r3552)
22 *
23 * @package bbPress
24 * @subpackage BuddyPress
25 */
26 class BBP_Forums_Group_Extension extends BP_Group_Extension {
27
28 /** Methods ***************************************************************/
29
30 /**
31 * Setup bbPress group extension variables
32 *
33 * @since 2.1.0 bbPress (r3552)
34 */
35 public function __construct() {
36 $this->setup_variables();
37 $this->setup_actions();
38 $this->setup_filters();
39 $this->maybe_unset_forum_menu();
40 $this->fully_loaded();
41 }
42
43 /**
44 * Setup the group forums class variables
45 *
46 * @since 2.1.0 bbPress (r3552)
47 */
48 private function setup_variables() {
49
50 // Component Name
51 $this->name = esc_html__( 'Forum', 'bbpress' );
52 $this->nav_item_name = esc_html__( 'Forum', 'bbpress' );
53
54 // Component slugs (hardcoded to match bbPress 1.x functionality)
55 $this->slug = 'forum';
56 $this->topic_slug = 'topic';
57 $this->reply_slug = 'reply';
58
59 // Forum component is visible
60 $this->visibility = 'public';
61
62 // Set positions towards end
63 $this->create_step_position = 15;
64 $this->nav_item_position = 10;
65
66 // Allow create step and show in nav
67 $this->enable_create_step = true;
68 $this->enable_nav_item = true;
69 $this->enable_edit_item = true;
70
71 // Template file to load, and action to hook display on to
72 $this->template_file = 'groups/single/plugins';
73 $this->display_hook = 'bp_template_content';
74 }
75
76 /**
77 * Setup the group forums class actions
78 *
79 * @since 2.3.0 bbPress (r4552)
80 */
81 private function setup_actions() {
82
83 // Possibly redirect
84 add_action( 'bbp_template_redirect', array( $this, 'redirect_canonical' ) );
85
86 // Remove group forum cap map when view is done
87 add_action( 'bbp_after_group_forum_display', array( $this, 'remove_group_forum_meta_cap_map' ) );
88
89 // Validate group IDs when editing topics & replies
90 add_action( 'bbp_edit_topic_pre_extras', array( $this, 'validate_topic_forum_id' ) );
91 add_action( 'bbp_edit_reply_pre_extras', array( $this, 'validate_reply_to_id' ) );
92
93 // Check if group-forum attributes should be changed
94 add_action( 'groups_group_after_save', array( $this, 'update_group_forum' ) );
95
96 // bbPress needs to listen to BuddyPress group deletion
97 add_action( 'groups_before_delete_group', array( $this, 'disconnect_forum_from_group' ) );
98
99 // Adds a bbPress meta-box to the new BuddyPress Group Admin UI
100 add_action( 'bp_groups_admin_meta_boxes', array( $this, 'group_admin_ui_edit_screen' ) );
101
102 // Saves the bbPress options if they come from the BuddyPress Group Admin UI
103 add_action( 'bp_group_admin_edit_after', array( $this, 'edit_screen_save' ) );
104
105 // Adds a hidden input value to the "Group Settings" page
106 add_action( 'bp_before_group_settings_admin', array( $this, 'group_settings_hidden_field' ) );
107 }
108
109 /**
110 * Setup the group forums class filters
111 *
112 * @since 2.3.0 bbPress (r4552)
113 */
114 private function setup_filters() {
115
116 // Group forum pagination
117 add_filter( 'bbp_topic_pagination', array( $this, 'topic_pagination' ) );
118 add_filter( 'bbp_replies_pagination', array( $this, 'replies_pagination' ) );
119 add_filter( 'bbp_get_global_object', array( $this, 'rewrite_pagination' ), 10, 2 );
120
121 // Tweak the redirect field
122 add_filter( 'bbp_new_topic_redirect_to', array( $this, 'new_topic_redirect_to' ), 10, 3 );
123 add_filter( 'bbp_new_reply_redirect_to', array( $this, 'new_reply_redirect_to' ), 10, 3 );
124
125 // Map forum/topic/reply permalinks to their groups
126 add_filter( 'bbp_get_forum_permalink', array( $this, 'map_forum_permalink_to_group' ), 10, 2 );
127 add_filter( 'bbp_get_topic_permalink', array( $this, 'map_topic_permalink_to_group' ), 10, 2 );
128 add_filter( 'bbp_get_reply_permalink', array( $this, 'map_reply_permalink_to_group' ), 10, 2 );
129
130 // Map reply edit links to their groups
131 add_filter( 'bbp_get_reply_edit_url', array( $this, 'map_reply_edit_url_to_group' ), 10, 2 );
132
133 // Map assorted template function permalinks
134 add_filter( 'post_link', array( $this, 'post_link' ), 10, 2 );
135 add_filter( 'page_link', array( $this, 'page_link' ), 10, 2 );
136 add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 );
137
138 // Map group forum activity items to groups
139 add_filter( 'bbp_before_record_activity_parse_args', array( $this, 'map_activity_to_group' ) );
140
141 /** Caps **************************************************************/
142
143 // Only add these filters if inside a group forum
144 if ( bp_is_single_item() && bp_is_group() && bp_is_current_action( $this->slug ) ) {
145
146 // Ensure bbp_is_single_forum() returns true on group forums.
147 add_filter( 'bbp_is_single_forum', array( $this, 'is_single_forum' ) );
148
149 // Ensure bbp_is_single_topic() returns true on group forum topics.
150 add_filter( 'bbp_is_single_topic', array( $this, 'is_single_topic' ) );
151
152 // Allow group member to view private/hidden forums
153 add_filter( 'bbp_map_meta_caps', array( $this, 'map_group_forum_meta_caps' ), 10, 4 );
154
155 // Group member permissions to view the topic and reply forms
156 add_filter( 'bbp_current_user_can_access_create_topic_form', array( $this, 'form_permissions' ) );
157 add_filter( 'bbp_current_user_can_access_create_reply_form', array( $this, 'form_permissions' ) );
158 }
159 }
160
161 /**
162 * Allow the variables, actions, and filters to be modified by third party
163 * plugins and themes.
164 *
165 * @since 2.6.0 bbPress (r6808)
166 */
167 private function fully_loaded() {
168 do_action_ref_array( 'bbp_buddypress_groups_loaded', array( $this ) );
169 }
170
171 /**
172 * Ensure that bbp_is_single_forum() returns true on group forum pages.
173 *
174 * @see https://bbpress.trac.wordpress.org/ticket/2974
175 *
176 * @since 2.6.0 bbPress (r6366)
177 *
178 * @param bool $retval Current boolean.
179 * @return bool
180 */
181 public function is_single_forum( $retval = false ) {
182
183 // Additional BuddyPress specific single-forum conditionals
184 if ( ( false === $retval ) && ! bp_is_action_variable( $this->topic_slug, 0 ) ) {
185 $retval = true;
186 }
187
188 return $retval;
189 }
190
191 /**
192 * Ensure that bbp_is_single_topic() returns true on group forum topic pages.
193 *
194 * @see https://bbpress.trac.wordpress.org/ticket/2974
195 *
196 * @since 2.6.0 bbPress (r6220)
197 *
198 * @param bool $retval Current boolean.
199 * @return bool
200 */
201 public function is_single_topic( $retval = false ) {
202
203 // Additional BuddyPress specific single-topic conditionals
204 if ( ( false === $retval ) && bp_is_action_variable( $this->topic_slug, 0 ) && bp_action_variable( 1 ) ) {
205 $retval = true;
206 }
207
208 return $retval;
209 }
210
211 /**
212 * The primary display function for group forums
213 *
214 * @since 2.1.0 bbPress (r3746)
215 *
216 * @param int $group_id ID of the current group. Available only on BP 2.2+.
217 */
218 public function display( $group_id = null ) {
219
220 // Prevent Topic Parent from appearing
221 add_action( 'bbp_theme_before_topic_form_forum', array( $this, 'ob_start' ) );
222 add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'ob_end_clean' ) );
223 add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'topic_parent' ) );
224
225 // Prevent Forum Parent from appearing
226 add_action( 'bbp_theme_before_forum_form_parent', array( $this, 'ob_start' ) );
227 add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'ob_end_clean' ) );
228 add_action( 'bbp_theme_after_forum_form_parent', array( $this, 'forum_parent' ) );
229
230 // Hide breadcrumb
231 add_filter( 'bbp_no_breadcrumb', '__return_true' );
232
233 $this->display_forums( 0 );
234 }
235
236 /**
237 * Maybe unset the group forum nav item if group does not have a forum
238 *
239 * @since 2.3.0 bbPress (r4552)
240 *
241 * @return If not viewing a single group
242 */
243 public function maybe_unset_forum_menu() {
244
245 // Bail if not viewing a single group
246 if ( ! bp_is_group() ) {
247 return;
248 }
249
250 // Are forums enabled for this group?
251 $checked = bp_get_new_group_enable_forum() || groups_get_groupmeta( bp_get_new_group_id(), 'forum_id' );
252
253 // Tweak the nav item variable based on if group has forum or not
254 $this->enable_nav_item = (bool) $checked;
255 }
256
257 /**
258 * Allow group members to have advanced privileges in group forum topics.
259 *
260 * @since 2.2.0 bbPress (r4434)
261 *
262 * @param array $caps
263 * @param string $cap
264 * @param int $user_id
265 * @param array $args
266 * @return array
267 */
268 public function map_group_forum_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
269
270 switch ( $cap ) {
271
272 // If user is a group mmember, allow them to create content.
273 case 'read_forum' :
274 case 'publish_replies' :
275 case 'publish_topics' :
276 case 'read_hidden_forums' :
277 case 'read_private_forums' :
278 if ( bbp_group_is_banned() ) {
279 $caps = array( 'do_not_allow' );
280 } elseif ( bbp_group_is_member() || bbp_group_is_mod() || bbp_group_is_admin() ) {
281 $caps = array( 'participate' );
282 }
283 break;
284
285 // If user is a group mod ar admin, map to participate cap.
286 case 'moderate' :
287 case 'edit_topic' :
288 case 'edit_reply' :
289 case 'view_trash' :
290 case 'edit_others_replies' :
291 case 'edit_others_topics' :
292 if ( bbp_group_is_mod() || bbp_group_is_admin() ) {
293 $caps = array( 'participate' );
294 }
295 break;
296
297 // If user is a group admin, allow them to delete topics and replies.
298 case 'delete_topic' :
299 case 'delete_reply' :
300 if ( bbp_group_is_admin() ) {
301 $caps = array( 'participate' );
302 }
303 break;
304 }
305
306 // Filter & return
307 return (array) apply_filters( 'bbp_map_group_forum_topic_meta_caps', $caps, $cap, $user_id, $args );
308 }
309
310 /**
311 * Remove the topic meta cap map, so it doesn't interfere with sidebars.
312 *
313 * @since 2.2.0 bbPress (r4434)
314 */
315 public function remove_group_forum_meta_cap_map() {
316 remove_filter( 'bbp_map_meta_caps', array( $this, 'map_group_forum_meta_caps' ), 99, 4 );
317 }
318
319 /**
320 * Validate the forum ID for a topic in a group forum.
321 *
322 * This method ensures that when a topic is saved, it is only allowed to be
323 * saved to a forum that is either:
324 *
325 * - A forum in the current group
326 * - A forum the current user can moderate outside of this group
327 *
328 * If all checks fail, an error gets added to prevent the topic from saving.
329 *
330 * @since 2.6.14
331 *
332 * @param int $topic_id
333 */
334 public function validate_topic_forum_id( $topic_id = 0 ) {
335
336 // Bail if no topic
337 if ( empty( $topic_id ) ) {
338 return;
339 }
340
341 // Get current forum ID
342 $forum_id = bbp_get_topic_forum_id( $topic_id );
343
344 // Bail if not a group forum
345 if ( ! bbp_is_forum_group_forum( $forum_id ) ) {
346 return;
347 }
348
349 // Get current group ID
350 $group_id = bp_get_current_group_id();
351
352 // Bail if unknown group ID
353 if ( empty( $group_id ) ) {
354 return;
355 }
356
357 // Get group forum IDs
358 $forum_ids = bbp_get_group_forum_ids( $group_id );
359
360 // Get posted forum ID
361 $new_forum_id = ! empty( $_POST['bbp_forum_id'] )
362 ? absint( $_POST['bbp_forum_id'] )
363 : 0;
364
365 // Bail if new forum ID is a forum in this group
366 if ( in_array( $new_forum_id, $forum_ids, true ) ) {
367 return;
368 }
369
370 // Get the current user ID
371 $user_id = bbp_get_current_user_id();
372
373 // Bail if current user can moderate the new forum ID
374 if ( bbp_is_user_forum_moderator( $user_id, $new_forum_id ) ) {
375 return;
376 }
377
378 // If everything else has failed, then something is wrong and we need
379 // to add an error to prevent this topic from saving.
380 bbp_add_error( 'bbp_topic_forum_id', __( '<strong>Error</strong>: Forum ID is invalid.', 'bbpress' ) );
381 }
382
383 /**
384 * Validate the reply to for a reply in a group forum.
385 *
386 * This method ensures that when a reply to is saved, it is only allowed to
387 * be saved to the current topic.
388 *
389 * If all checks fail, an error gets added to prevent the reply from saving.
390 *
391 * @since 2.6.14
392 *
393 * @param int $reply_id
394 */
395 public function validate_reply_to_id( $reply_id = 0 ) {
396
397 // Bail if no reply
398 if ( empty( $reply_id ) ) {
399 return;
400 }
401
402 // Get posted reply to
403 $new_reply_to = ! empty( $_POST['bbp_reply_to'] )
404 ? absint( $_POST['bbp_reply_to'] )
405 : 0;
406
407 // Bail if no reply to (assumes topic ID)
408 if ( empty( $new_reply_to ) ) {
409 return;
410 }
411
412 // Get current forum ID
413 $forum_id = bbp_get_reply_forum_id( $reply_id );
414
415 // Bail if not a group forum
416 if ( ! bbp_is_forum_group_forum( $forum_id ) ) {
417 return;
418 }
419
420 // Get current group ID
421 $group_id = bp_get_current_group_id();
422
423 // Bail if unknown group ID
424 if ( empty( $group_id ) ) {
425 return;
426 }
427
428 // Get current topic ID
429 $topic_id = bbp_get_reply_topic_id( $reply_id );
430
431 // Get topic reply IDs
432 $reply_ids = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() );
433
434 // Avoid recursion
435 unset( $reply_ids[ $reply_id ] );
436
437 // Bail if new reply parent ID is in this topic
438 if ( in_array( $new_reply_to, $reply_ids, true ) ) {
439 return;
440 }
441
442 // Add an error to prevent this reply from saving.
443 bbp_add_error( 'bbp_reply_to_id', __( '<strong>Error</strong>: Reply To is invalid.', 'bbpress' ) );
444 }
445
446 /** Edit ******************************************************************/
447
448 /**
449 * Show forums and new forum form when editing a group
450 *
451 * @since 2.1.0 bbPress (r3563)
452 *
453 * @param object $group (the group to edit if in Group Admin UI)
454 */
455 public function edit_screen( $group = false ) {
456 $forum_id = 0;
457 $group_id = empty( $group->id ) ? bp_get_new_group_id() : $group->id;
458 $forum_ids = bbp_get_group_forum_ids( $group_id );
459
460 // Get the first forum ID
461 if ( ! empty( $forum_ids ) ) {
462 $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids;
463 }
464
465 // Should box be checked already?
466 $checked = is_admin() ? bp_group_is_forum_enabled( $group ) : bp_get_new_group_enable_forum() || bp_group_is_forum_enabled( bp_get_group_id() ); ?>
467
468 <h2><?php esc_html_e( 'Group Forum Settings', 'bbpress' ); ?></h2>
469
470 <fieldset>
471 <legend class="screen-reader-text"><?php esc_html_e( 'Group Forum Settings', 'bbpress' ); ?></legend>
472 <p><?php esc_html_e( 'Create a discussion forum to allow members of this group to communicate in a structured, bulletin-board style fashion.', 'bbpress' ); ?></p>
473
474 <div class="field-group">
475 <div class="checkbox">
476 <label for="bbp-edit-group-forum"><input type="checkbox" name="bbp-edit-group-forum" id="bbp-edit-group-forum" value="1"<?php checked( $checked ); ?> /> <?php esc_html_e( 'Yes. I want this group to have a forum.', 'bbpress' ); ?></label>
477 </div>
478
479 <p class="description"><?php esc_html_e( 'Saying no will not delete existing forum content.', 'bbpress' ); ?></p>
480 </div>
481
482 <?php if ( bbp_is_user_keymaster() ) : ?>
483 <div class="field-group">
484 <label for="bbp_group_forum_id"><?php esc_html_e( 'Group Forum:', 'bbpress' ); ?></label>
485 <?php
486 bbp_dropdown( array(
487 'select_id' => 'bbp_group_forum_id',
488 'show_none' => esc_html__( '&mdash; No forum &mdash;', 'bbpress' ),
489 'selected' => $forum_id
490 ) );
491 ?>
492 <p class="description"><?php esc_html_e( 'Network administrators can reconfigure which forum belongs to this group.', 'bbpress' ); ?></p>
493 </div>
494 <?php endif; ?>
495
496 <?php if ( ! is_admin() ) : ?>
497 <input type="submit" value="<?php esc_attr_e( 'Save Settings', 'bbpress' ); ?>" />
498 <?php endif; ?>
499
500 </fieldset>
501
502 <?php
503
504 // Verify intent
505 if ( is_admin() ) {
506 wp_nonce_field( 'groups_edit_save_' . $this->slug, 'forum_group_admin_ui' );
507 } else {
508 wp_nonce_field( 'groups_edit_save_' . $this->slug );
509 }
510 }
511
512 /**
513 * Save the Group Forum data on edit
514 *
515 * @since 2.0.0 bbPress (r3465)
516 *
517 * @param int $group_id (to handle Group Admin UI hook bp_group_admin_edit_after )
518 */
519 public function edit_screen_save( $group_id = 0 ) {
520
521 // Bail if not a POST action
522 if ( ! bbp_is_post_request() ) {
523 return;
524 }
525
526 // Admin Nonce check
527 if ( is_admin() ) {
528 check_admin_referer( 'groups_edit_save_' . $this->slug, 'forum_group_admin_ui' );
529
530 // Theme-side Nonce check
531 } elseif ( ! bbp_verify_nonce_request( 'groups_edit_save_' . $this->slug ) ) {
532 bbp_add_error( 'bbp_edit_group_forum_screen_save', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
533 return;
534 }
535
536 $edit_forum = ! empty( $_POST['bbp-edit-group-forum'] ) ? true : false;
537 $forum_id = 0;
538 $group_id = ! empty( $group_id ) ? $group_id : bp_get_current_group_id();
539
540 // Keymasters have the ability to reconfigure forums
541 if ( bbp_is_user_keymaster() ) {
542 $forum_ids = ! empty( $_POST['bbp_group_forum_id'] ) ? (array) (int) $_POST['bbp_group_forum_id'] : array();
543
544 // Use the existing forum IDs
545 } else {
546 $forum_ids = array_values( bbp_get_group_forum_ids( $group_id ) );
547 }
548
549 // Normalize group forum relationships now
550 if ( ! empty( $forum_ids ) ) {
551
552 // Loop through forums, and make sure they exist
553 foreach ( $forum_ids as $forum_id ) {
554
555 // Look for forum
556 $forum = bbp_get_forum( $forum_id );
557
558 // No forum exists, so break the relationship
559 if ( empty( $forum ) ) {
560 $this->remove_forum( array( 'forum_id' => $forum_id ) );
561 unset( $forum_ids[ $forum_id ] );
562 }
563 }
564
565 // No support for multiple forums yet
566 $forum_id = (int) ( is_array( $forum_ids )
567 ? $forum_ids[0]
568 : $forum_ids );
569 }
570
571 // Update the group ID and forum ID relationships
572 bbp_update_group_forum_ids( $group_id, (array) $forum_ids );
573 bbp_update_forum_group_ids( $forum_id, (array) $group_id );
574
575 // Update the group forum setting
576 $group = $this->toggle_group_forum( $group_id, $edit_forum );
577
578 // Create a new forum
579 if ( empty( $forum_id ) && ( true === $edit_forum ) ) {
580
581 // Set the default forum status
582 switch ( $group->status ) {
583 case 'hidden' :
584 $status = bbp_get_hidden_status_id();
585 break;
586 case 'private' :
587 $status = bbp_get_private_status_id();
588 break;
589 case 'public' :
590 default :
591 $status = bbp_get_public_status_id();
592 break;
593 }
594
595 // Create the initial forum
596 $forum_id = bbp_insert_forum( array(
597 'post_parent' => bbp_get_group_forums_root_id(),
598 'post_title' => $group->name,
599 'post_content' => $group->description,
600 'post_status' => $status
601 ) );
602
603 // Setup forum args with forum ID
604 $new_forum_args = array( 'forum_id' => $forum_id );
605
606 // If in admin, also include the group ID
607 if ( is_admin() && ! empty( $group_id ) ) {
608 $new_forum_args['group_id'] = $group_id;
609 }
610
611 // Run the BP-specific functions for new groups
612 $this->new_forum( $new_forum_args );
613 }
614
615 // Redirect after save when not in admin
616 if ( ! is_admin() ) {
617 bp_core_redirect( trailingslashit( $this->group_url( buddypress()->groups->current_group ) . '/admin/' . $this->slug ) );
618 }
619 }
620
621 /**
622 * Adds a meta-box to BuddyPress Group Admin UI
623 *
624 * @since 2.3.0 bbPress (r4814)
625 */
626 public function group_admin_ui_edit_screen() {
627 add_meta_box(
628 'bbpress_group_admin_ui_meta_box',
629 _x( 'Discussion Forum', 'group admin edit screen', 'bbpress' ),
630 array( $this, 'group_admin_ui_display_metabox' ),
631 get_current_screen()->id,
632 'side',
633 'core'
634 );
635 }
636
637 /**
638 * Displays the bbPress meta-box in BuddyPress Group Admin UI
639 *
640 * @since 2.3.0 bbPress (r4814)
641 *
642 * @param object $item (group object)
643 */
644 public function group_admin_ui_display_metabox( $item ) {
645 $this->edit_screen( $item );
646 }
647
648 /** Create ****************************************************************/
649
650 /**
651 * Show forums and new forum form when creating a group
652 *
653 * @since 2.0.0 bbPress (r3465)
654 */
655 public function create_screen( $group_id = 0 ) {
656
657 // Bail if not looking at this screen
658 if ( !bp_is_group_creation_step( $this->slug ) ) {
659 return false;
660 }
661
662 // Check for possibly empty group_id
663 if ( empty( $group_id ) ) {
664 $group_id = bp_get_new_group_id();
665 }
666
667 $checked = bp_get_new_group_enable_forum() || groups_get_groupmeta( $group_id, 'forum_id' ); ?>
668
669 <h2><?php esc_html_e( 'Group Forum', 'bbpress' ); ?></h2>
670
671 <p><?php esc_html_e( 'Create a discussion forum to allow members of this group to communicate in a structured, bulletin-board style fashion.', 'bbpress' ); ?></p>
672
673 <div class="checkbox">
674 <label for="bbp-create-group-forum"><input type="checkbox" name="bbp-create-group-forum" id="bbp-create-group-forum" value="1"<?php checked( $checked ); ?> /> <?php esc_html_e( 'Yes. I want this group to have a forum.', 'bbpress' ); ?></label>
675 </div>
676
677 <?php
678 }
679
680 /**
681 * Save the Group Forum data on create
682 *
683 * @since 2.0.0 bbPress (r3465)
684 */
685 public function create_screen_save( $group_id = 0 ) {
686
687 // Nonce check
688 if ( ! bbp_verify_nonce_request( 'groups_create_save_' . $this->slug ) ) {
689 bbp_add_error( 'bbp_create_group_forum_screen_save', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
690 return;
691 }
692
693 // Check for possibly empty group_id
694 if ( empty( $group_id ) ) {
695 $group_id = bp_get_new_group_id();
696 }
697
698 $create_forum = ! empty( $_POST['bbp-create-group-forum'] ) ? true : false;
699 $forum_id = 0;
700 $forum_ids = bbp_get_group_forum_ids( $group_id );
701
702 if ( ! empty( $forum_ids ) ) {
703 $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids;
704 }
705
706 // Create a forum, or not
707 switch ( $create_forum ) {
708 case true :
709
710 // Bail if initial content was already created
711 if ( ! empty( $forum_id ) ) {
712 return;
713 }
714
715 // Set the default forum status
716 switch ( bp_get_new_group_status() ) {
717 case 'hidden' :
718 $status = bbp_get_hidden_status_id();
719 break;
720 case 'private' :
721 $status = bbp_get_private_status_id();
722 break;
723 case 'public' :
724 default :
725 $status = bbp_get_public_status_id();
726 break;
727 }
728
729 // Create the initial forum
730 $forum_id = bbp_insert_forum( array(
731 'post_parent' => bbp_get_group_forums_root_id(),
732 'post_title' => bp_get_new_group_name(),
733 'post_content' => bp_get_new_group_description(),
734 'post_status' => $status
735 ) );
736
737 // Run the BP-specific functions for new groups
738 $this->new_forum( array( 'forum_id' => $forum_id ) );
739
740 // Update forum active
741 groups_update_groupmeta( bp_get_new_group_id(), '_bbp_forum_enabled_' . $forum_id, true );
742
743 // Toggle forum on
744 $this->toggle_group_forum( bp_get_new_group_id(), true );
745
746 break;
747 case false :
748
749 // Forum was created but is now being undone
750 if ( ! empty( $forum_id ) ) {
751
752 // Delete the forum
753 wp_delete_post( $forum_id, true );
754
755 // Delete meta values
756 groups_delete_groupmeta( bp_get_new_group_id(), 'forum_id' );
757 groups_delete_groupmeta( bp_get_new_group_id(), '_bbp_forum_enabled_' . $forum_id );
758
759 // Toggle forum off
760 $this->toggle_group_forum( bp_get_new_group_id(), false );
761 }
762
763 break;
764 }
765 }
766
767 /**
768 * Used to start an output buffer
769 *
770 * @since 2.1.0 bbPress (r3746)
771 */
772 public function ob_start() {
773 ob_start();
774 }
775
776 /**
777 * Used to end an output buffer
778 *
779 * @since 2.1.0 bbPress (r3746)
780 */
781 public function ob_end_clean() {
782 ob_end_clean();
783 }
784
785 /**
786 * Creating a group forum or category (including root for group)
787 *
788 * @since 2.1.0 bbPress (r3653)
789 *
790 * @param array $forum_args
791 *
792 * @return void if no forum_id is available
793 */
794 public function new_forum( $forum_args = array() ) {
795
796 // Bail if no forum_id was passed
797 if ( empty( $forum_args['forum_id'] ) ) {
798 return;
799 }
800
801 // Validate forum_id
802 $forum_id = bbp_get_forum_id( $forum_args['forum_id'] );
803 $group_id = ! empty( $forum_args['group_id'] ) ? $forum_args['group_id'] : bp_get_current_group_id();
804
805 bbp_add_forum_id_to_group( $group_id, $forum_id );
806 bbp_add_group_id_to_forum( $forum_id, $group_id );
807 }
808
809 /**
810 * Removing a group forum or category (including root for group)
811 *
812 * @since 2.1.0 bbPress (r3653)
813 *
814 * @param array $forum_args
815 *
816 * @return void if no forum_id is available
817 */
818 public function remove_forum( $forum_args = array() ) {
819
820 // Bail if no forum_id was passed
821 if ( empty( $forum_args['forum_id'] ) ) {
822 return;
823 }
824
825 // Validate forum_id
826 $forum_id = bbp_get_forum_id( $forum_args['forum_id'] );
827 $group_id = ! empty( $forum_args['group_id'] ) ? $forum_args['group_id'] : bp_get_current_group_id();
828
829 bbp_remove_forum_id_from_group( $group_id, $forum_id );
830 bbp_remove_group_id_from_forum( $forum_id, $group_id );
831 }
832
833 /**
834 * Listening to BuddyPress Group deletion to remove the forum
835 *
836 * @since 2.3.0 bbPress (r4815)
837 *
838 * @param int $group_id The group ID
839 */
840 public function disconnect_forum_from_group( $group_id = 0 ) {
841
842 // Bail if no group ID available
843 if ( empty( $group_id ) ) {
844 return;
845 }
846
847 // Get the forums for the current group
848 $forum_ids = bbp_get_group_forum_ids( $group_id );
849
850 // Bail if no forum IDs available
851 if ( empty( $forum_ids ) ) {
852 return;
853 }
854
855 // Get the first forum ID
856 $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids;
857 $this->remove_forum( array(
858 'forum_id' => $forum_id,
859 'group_id' => $group_id
860 ) );
861 }
862
863 /**
864 * Update forum attributes to match those of the associated group.
865 *
866 * Fired whenever a group is saved
867 *
868 * @since 2.6.7 bbPress (r7208)
869 *
870 * @param BP_Groups_Group $group Group object.
871 */
872 public static function update_group_forum( BP_Groups_Group $group ) {
873
874 // Get group forum IDs
875 $forum_ids = bbp_get_group_forum_ids( $group->id );
876
877 // Bail if no forum IDs available
878 if ( empty( $forum_ids ) ) {
879 return;
880 }
881
882 // Loop through forum IDs
883 foreach ( $forum_ids as $forum_id ) {
884
885 // Get forum from ID
886 $forum = bbp_get_forum( $forum_id );
887
888 // Check for change
889 if ( $group->status !== $forum->post_status ) {
890 switch ( $group->status ) {
891
892 // Changed to hidden
893 case 'hidden' :
894 bbp_hide_forum( $forum_id, $forum->post_status );
895 break;
896
897 // Changed to private
898 case 'private' :
899 bbp_privatize_forum( $forum_id, $forum->post_status );
900 break;
901
902 // Changed to public
903 case 'public' :
904 default :
905 bbp_publicize_forum( $forum_id, $forum->post_status );
906 break;
907 }
908 }
909 }
910
911 // Maybe update the first group forum title, content, and slug
912 if ( ! empty( $forum_ids[0] ) ) {
913
914 // Get forum from ID
915 $forum = bbp_get_forum( $forum_ids[0] );
916
917 // Only update the forum if changes are being made
918 if (
919
920 // Title
921 ( $forum->post_title !== $group->name )
922
923 ||
924
925 // Content
926 ( $forum->post_content !== $group->description )
927
928 ||
929
930 // Slug
931 ( $forum->post_name !== $group->slug )
932 ) {
933 wp_update_post(
934 array(
935 'ID' => $forum->ID,
936 'post_title' => $group->name,
937 'post_content' => $group->description,
938 'post_name' => $group->slug
939 )
940 );
941 }
942 }
943 }
944
945 /**
946 * Toggle the enable_forum group setting on or off
947 *
948 * @since 2.3.0 bbPress (r4612)
949 *
950 * @param int $group_id The group to toggle
951 * @param bool $enabled True for on, false for off
952 * @return False if group is not found, otherwise return the group
953 */
954 public function toggle_group_forum( $group_id = 0, $enabled = false ) {
955
956 // Get the group
957 $group = groups_get_group( array( 'group_id' => $group_id ) );
958
959 // Bail if group cannot be found
960 if ( empty( $group ) ) {
961 return false;
962 }
963
964 // Set forum enabled status
965 $group->enable_forum = (int) $enabled;
966
967 // Save the group
968 $group->save();
969
970 // Maybe disconnect forum from group
971 if ( empty( $enabled ) ) {
972 $this->disconnect_forum_from_group( $group_id );
973 }
974
975 // Update internal private and forum ID variables
976 bbp_repair_forum_visibility();
977
978 // Return the group
979 return $group;
980 }
981
982 /** Display Methods *******************************************************/
983
984 /**
985 * Output the forums for a group in the edit screens
986 *
987 * As of right now, bbPress only supports 1-to-1 group forum relationships.
988 * In the future, many-to-many should be allowed.
989 *
990 * @since 2.1.0 bbPress (r3653)
991 */
992 public function display_forums( $offset = 0 ) {
993
994 // Allow actions immediately before group forum output
995 do_action( 'bbp_before_group_forum_display' );
996
997 // Load up bbPress once
998 $bbp = bbpress();
999
1000 /** Query Resets ******************************************************/
1001
1002 // Forum data
1003 $forum_action = bp_action_variable( $offset );
1004 $forum_ids = bbp_get_group_forum_ids( bp_get_current_group_id() );
1005 $forum_id = array_shift( $forum_ids );
1006
1007 // Always load up the group forum
1008 bbp_has_forums( array(
1009 'p' => $forum_id,
1010 'post_parent' => null
1011 ) );
1012
1013 // Set the global forum ID
1014 $bbp->current_forum_id = $forum_id;
1015
1016 // Assume forum query
1017 bbp_set_query_name( 'bbp_single_forum' ); ?>
1018
1019 <div id="bbpress-forums" class="bbpress-wrapper">
1020
1021 <?php switch ( $forum_action ) :
1022
1023 /** Single Forum **********************************************/
1024
1025 case false :
1026 case 'page' :
1027
1028 // Strip the super stickies from topic query
1029 add_filter( 'bbp_get_super_stickies', array( $this, 'no_super_stickies' ), 10, 1 );
1030
1031 // Unset the super sticky option on topic form
1032 add_filter( 'bbp_get_topic_types', array( $this, 'unset_super_sticky' ), 10, 1 );
1033
1034 // Query forums and show them if they exist
1035 if ( bbp_forums() ) :
1036
1037 // Setup the forum
1038 bbp_the_forum();
1039
1040 // Only wrap title in H2 if not empty
1041 $title = bbp_get_forum_title();
1042 if ( ! empty( $title ) ) {
1043 echo '<h2>' . $title . '</h2>';
1044 }
1045
1046 // Output forum
1047 bbp_get_template_part( 'content', 'single-forum' );
1048
1049 // No forums found
1050 else : ?>
1051
1052 <div id="message" class="info">
1053 <p><?php esc_html_e( 'This group does not currently have a forum.', 'bbpress' ); ?></p>
1054 </div>
1055
1056 <?php endif;
1057
1058 break;
1059
1060 /** Single Topic **********************************************/
1061
1062 case $this->topic_slug :
1063
1064 // hide the 'to front' admin links
1065 add_filter( 'bbp_get_topic_stick_link', array( $this, 'hide_super_sticky_admin_link' ), 10, 2 );
1066
1067 // Get the topic
1068 bbp_has_topics( array(
1069 'name' => bp_action_variable( $offset + 1 ),
1070 'posts_per_page' => 1,
1071 'show_stickies' => false
1072 ) );
1073
1074 // If no topic, 404
1075 if ( ! bbp_topics() ) {
1076 bp_do_404( bbp_get_forum_permalink( $forum_id ) );
1077
1078 // Only wrap title in H2 if not empty
1079 $title = bbp_get_forum_title();
1080 if ( ! empty( $title ) ) {
1081 echo '<h2>' . $title . '</h2>';
1082 }
1083
1084 // No topics
1085 bbp_get_template_part( 'feedback', 'no-topics' );
1086 break;
1087 }
1088
1089 // Setup the topic
1090 bbp_the_topic();
1091
1092 // Only wrap title in H2 if not empty
1093 $title = bbp_get_topic_title();
1094 if ( ! empty( $title ) ) {
1095 echo '<h2>' . $title . '</h2>';
1096 }
1097
1098 // Topic edit
1099 if ( bp_action_variable( $offset + 2 ) === bbp_get_edit_slug() ) :
1100
1101 // Unset the super sticky link on edit topic template
1102 add_filter( 'bbp_get_topic_types', array( $this, 'unset_super_sticky' ), 10, 1 );
1103
1104 // Get the main query object
1105 $wp_query = bbp_get_wp_query();
1106
1107 // Set the edit switches
1108 $wp_query->bbp_is_edit = true;
1109 $wp_query->bbp_is_topic_edit = true;
1110
1111 // Setup the global forum ID
1112 $bbp->current_topic_id = get_the_ID();
1113
1114 // Merge
1115 if ( ! empty( $_GET['action'] ) && 'merge' === $_GET['action'] ) :
1116 bbp_set_query_name( 'bbp_topic_merge' );
1117 bbp_get_template_part( 'form', 'topic-merge' );
1118
1119 // Split
1120 elseif ( ! empty( $_GET['action'] ) && 'split' === $_GET['action'] ) :
1121 bbp_set_query_name( 'bbp_topic_split' );
1122 bbp_get_template_part( 'form', 'topic-split' );
1123
1124 // Edit
1125 else :
1126 bbp_set_query_name( 'bbp_topic_form' );
1127 bbp_get_template_part( 'form', 'topic' );
1128
1129 endif;
1130
1131 // Single Topic
1132 else :
1133 bbp_set_query_name( 'bbp_single_topic' );
1134 bbp_get_template_part( 'content', 'single-topic' );
1135 endif;
1136 break;
1137
1138 /** Single Reply **********************************************/
1139
1140 case $this->reply_slug :
1141
1142 // Get the reply
1143 bbp_has_replies( array(
1144 'name' => bp_action_variable( $offset + 1 ),
1145 'posts_per_page' => 1
1146 ) );
1147
1148 // If no topic, 404
1149 if ( ! bbp_replies() ) {
1150 bp_do_404( bbp_get_forum_permalink( $forum_id ) );
1151
1152 // Only wrap title in H2 if not empty
1153 $title = bbp_get_forum_title();
1154 if ( ! empty( $title ) ) {
1155 echo '<h2>' . $title . '</h2>';
1156 }
1157
1158 // No replies
1159 bbp_get_template_part( 'feedback', 'no-replies' );
1160 break;
1161 }
1162
1163 // Setup the reply
1164 bbp_the_reply();
1165
1166 // Only wrap title in H2 if not empty
1167 $title = bbp_get_reply_title();
1168 if ( ! empty( $title ) ) {
1169 echo '<h2>' . $title . '</h2>';
1170 }
1171
1172 if ( bp_action_variable( $offset + 2 ) === bbp_get_edit_slug() ) :
1173
1174 // Get the main query object
1175 $wp_query = bbp_get_wp_query();
1176
1177 // Set the edit switches
1178 $wp_query->bbp_is_edit = true;
1179 $wp_query->bbp_is_reply_edit = true;
1180
1181 // Setup the global reply ID
1182 $bbp->current_reply_id = get_the_ID();
1183
1184 // Move
1185 if ( ! empty( $_GET['action'] ) && ( 'move' === $_GET['action'] ) ) :
1186 bbp_set_query_name( 'bbp_reply_move' );
1187 bbp_get_template_part( 'form', 'reply-move' );
1188
1189 // Edit
1190 else :
1191 bbp_set_query_name( 'bbp_reply_form' );
1192 bbp_get_template_part( 'form', 'reply' );
1193 endif;
1194 endif;
1195 break;
1196 endswitch;
1197
1198 // Reset the query
1199 wp_reset_query(); ?>
1200
1201 </div><!-- #bbpress-forums -->
1202
1203 <?php
1204
1205 // Allow actions immediately after group forum output
1206 do_action( 'bbp_after_group_forum_display' );
1207 }
1208
1209 /** Super sticky filters ***************************************************/
1210
1211 /**
1212 * Strip super stickies from the topic query
1213 *
1214 * @since 2.3.0 bbPress (r4810)
1215 *
1216 * @access private
1217 * @param array $super the super sticky post ID's
1218 * @return array (empty)
1219 */
1220 public function no_super_stickies( $super = array() ) {
1221 $super = array();
1222 return $super;
1223 }
1224
1225 /**
1226 * Unset the type super sticky from topic type
1227 *
1228 * @since 2.3.0 bbPress (r4810)
1229 *
1230 * @access private
1231 * @param array $args
1232 * @return array $args without the to-front link
1233 */
1234 public function unset_super_sticky( $args = array() ) {
1235 if ( isset( $args['super'] ) ) {
1236 unset( $args['super'] );
1237 }
1238 return $args;
1239 }
1240
1241 /**
1242 * Ugly preg_replace to hide the to front admin link
1243 *
1244 * @since 2.3.0 bbPress (r4810)
1245 *
1246 * @access private
1247 * @param string $retval
1248 * @param array $args
1249 * @return string $retval without the to-front link
1250 */
1251 public function hide_super_sticky_admin_link( $retval = '', $args = array() ) {
1252 if ( strpos( $retval, '(' ) ) {
1253 $retval = preg_replace( '/(\(.+?)+(\))/i', '', $retval );
1254 }
1255
1256 return $retval;
1257 }
1258
1259 /** Redirect Helpers ******************************************************/
1260
1261 /**
1262 * Redirect to the group forum screen
1263 *
1264 * @since 2.1.0 bbPress (r3653)
1265 *
1266 * @param str $redirect_url
1267 * @param str $redirect_to
1268 */
1269 public function new_topic_redirect_to( $redirect_url = '', $redirect_to = '', $topic_id = 0 ) {
1270 if ( bp_is_group() ) {
1271 $topic = bbp_get_topic( $topic_id );
1272 $topic_hash = '#post-' . $topic_id;
1273 $redirect_url = trailingslashit( $this->group_url( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name ) . $topic_hash;
1274 }
1275
1276 return $redirect_url;
1277 }
1278
1279 /**
1280 * Redirect to the group forum screen
1281 *
1282 * @since 2.1.0 bbPress (r3653)
1283 */
1284 public function new_reply_redirect_to( $redirect_url = '', $redirect_to = '', $reply_id = 0 ) {
1285
1286 if ( bp_is_group() ) {
1287 $topic_id = bbp_get_reply_topic_id( $reply_id );
1288 $topic = bbp_get_topic( $topic_id );
1289 $reply_position = bbp_get_reply_position( $reply_id, $topic_id );
1290 $reply_page = ceil( (int) $reply_position / (int) bbp_get_replies_per_page() );
1291 $reply_hash = '#post-' . $reply_id;
1292 $topic_url = trailingslashit( $this->group_url( groups_get_current_group() ) ) . trailingslashit( $this->slug ) . trailingslashit( $this->topic_slug ) . trailingslashit( $topic->post_name );
1293
1294 // Don't include pagination if on first page
1295 if ( 1 >= $reply_page ) {
1296 $redirect_url = trailingslashit( $topic_url ) . $reply_hash;
1297
1298 // Include pagination
1299 } else {
1300 $redirect_url = trailingslashit( $topic_url ) . trailingslashit( bbp_get_paged_slug() ) . trailingslashit( $reply_page ) . $reply_hash;
1301 }
1302
1303 // Add topic view query arg back to end if it is set
1304 if ( bbp_get_view_all() ) {
1305 $redirect_url = bbp_add_view_all( $redirect_url );
1306 }
1307 }
1308
1309 return $redirect_url;
1310 }
1311
1312 /**
1313 * Redirect to the group admin forum edit screen
1314 *
1315 * @since 2.1.0 bbPress (r3653)
1316 */
1317 public function edit_redirect_to( $redirect_url = '' ) {
1318
1319 // Get the current group, if there is one
1320 $group = groups_get_current_group();
1321
1322 // If this is a group of any kind, empty out the redirect URL
1323 if ( bp_is_group_admin_screen( $this->slug ) ) {
1324 $redirect_url = trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group->slug . '/admin/' . $this->slug );
1325 }
1326
1327 return $redirect_url;
1328 }
1329
1330 /** Form Helpers **********************************************************/
1331
1332 /**
1333 * Make Forum Parent a hidden field instead of a selectable one.
1334 *
1335 * @since 2.1.0 bbPress (r3746)
1336 */
1337 public function forum_parent() {
1338 ?>
1339
1340 <input type="hidden" name="bbp_forum_parent_id" id="bbp_forum_parent_id" value="<?php bbp_group_forums_root_id(); ?>" />
1341
1342 <?php
1343 }
1344
1345 /**
1346 * Output a dropdown for picking which group forum this topic is for.
1347 *
1348 * @since 2.1.0 bbPress (r3746)
1349 */
1350 public function topic_parent() {
1351
1352 // Get the group ID
1353 $gid = bp_get_current_group_id();
1354
1355 // Get the forum IDs for this group
1356 $forum_ids = bbp_get_group_forum_ids( $gid );
1357
1358 // Attempt to get the current topic forum ID
1359 $topic_id = bbp_get_topic_id();
1360 $forum_id = bbp_get_topic_forum_id( $topic_id );
1361
1362 // Setup the query arguments - note that these may be overridden later
1363 // by various bbPress visibility and capability filters.
1364 $args = array(
1365 'post_type' => bbp_get_forum_post_type(),
1366 'post_status' => bbp_get_public_status_id(),
1367 'include' => $forum_ids,
1368 'numberposts' => -1,
1369 'orderby' => 'menu_order',
1370 'order' => 'ASC',
1371 );
1372
1373 // Get the forum objects for these forum IDs
1374 $forums = get_posts( $args );
1375
1376 // Setup the dropdown arguments
1377 $dd_args = array(
1378 'posts' => $forums,
1379 'selected' => $forum_id,
1380 ); ?>
1381
1382 <p>
1383 <label for="bbp_forum_id"><?php esc_html_e( 'Forum:', 'bbpress' ); ?></label><br />
1384 <?php bbp_dropdown( $dd_args ); ?>
1385 </p>
1386
1387 <?php
1388 }
1389
1390 /**
1391 * Permissions to view the 'New Topic'/'Reply To' form in a BuddyPress group.
1392 *
1393 * @since 2.3.0 bbPress (r4608)
1394 *
1395 * @param bool $retval Are we allowed to view the reply form?
1396 *
1397 * @return bool
1398 */
1399 public function form_permissions( $retval = false ) {
1400
1401 // Bail if user is not logged in
1402 if ( ! is_user_logged_in() ) {
1403 return $retval;
1404
1405 // Keymasters can always pass go
1406 } elseif ( bbp_is_user_keymaster() ) {
1407 $retval = true;
1408
1409 // Non-members cannot see forms
1410 } elseif ( ! bbp_group_is_member() ) {
1411 $retval = false;
1412
1413 // Banned users cannot see forms
1414 } elseif ( bbp_group_is_banned() ) {
1415 $retval = false;
1416 }
1417
1418 return $retval;
1419 }
1420
1421 /**
1422 * Add a hidden input field on the group settings page if the group forum is
1423 * enabled.
1424 *
1425 * Due to the way BuddyPress' group admin settings page saves its settings,
1426 * we need to let BP know that bbPress added a forum.
1427 *
1428 * @since 2.4.0 bbPress (r5026)
1429 *
1430 * @link https://bbpress.trac.wordpress.org/ticket/2339/
1431 * @see groups_screen_group_admin_settings()
1432 */
1433 public function group_settings_hidden_field() {
1434
1435 // if a forum is not enabled, we don't need to add this field
1436 if ( ! bp_group_is_forum_enabled() ) {
1437 return;
1438 } ?>
1439
1440 <input type="hidden" name="group-show-forum" id="group-show-forum" value="1" />
1441
1442 <?php
1443 }
1444
1445 /** Permalink Mappers *****************************************************/
1446
1447 /**
1448 * Get the URL for a group.
1449 *
1450 * @since 2.6.14
1451 *
1452 * @param int $group_id
1453 * @return string
1454 */
1455 private function group_url( $group_id = 0 ) {
1456
1457 // Default return value
1458 $retval = '';
1459
1460 // BuddyPress < 12.0 (deprecated code is intentionally included)
1461 if ( function_exists( 'bp_get_group_permalink' ) ) {
1462 $retval = bp_get_group_permalink( $group_id );
1463
1464 // BuddyPress > 12.0 (rewrite rules)
1465 } elseif ( function_exists( 'bp_get_group_url' ) ) {
1466 $retval = bp_get_group_url( $group_id );
1467 }
1468
1469 // Return
1470 return $retval;
1471 }
1472
1473 /**
1474 * Get the management URL for a group.
1475 *
1476 * @since 2.6.14
1477 *
1478 * @param int $group_id
1479 * @return string
1480 */
1481 private function group_manage_url( $group_id = 0 ) {
1482
1483 // Default return value
1484 $retval = '';
1485
1486 // BuddyPress < 12.0 (deprecated code is intentionally included)
1487 if ( function_exists( 'bp_get_group_admin_permalink' ) ) {
1488 $retval = bp_get_group_admin_permalink( $group_id );
1489
1490 // BuddyPress > 12.0 (rewrite rules)
1491 } elseif ( function_exists( 'bp_get_group_manage_url' ) ) {
1492 $retval = bp_get_group_manage_url( $group_id );
1493 }
1494
1495 // Return
1496 return $retval;
1497 }
1498
1499 /**
1500 * Maybe map a bbPress forum/topic/reply permalink to the corresponding group
1501 *
1502 * @since 2.2.0 bbPress (r4266)
1503 *
1504 * @param int $post_id
1505 * @return Bail early if not a group forum post
1506 * @return string
1507 */
1508 private function maybe_map_permalink_to_group( $post_id = 0, $url = false ) {
1509
1510 switch ( get_post_type( $post_id ) ) {
1511
1512 // Reply
1513 case bbp_get_reply_post_type() :
1514 $forum_id = bbp_get_reply_forum_id( $post_id );
1515 $url_end = trailingslashit( $this->reply_slug ) . get_post_field( 'post_name', $post_id );
1516 break;
1517
1518 // Topic
1519 case bbp_get_topic_post_type() :
1520 $forum_id = bbp_get_topic_forum_id( $post_id );
1521 $url_end = trailingslashit( $this->topic_slug ) . get_post_field( 'post_name', $post_id );
1522 break;
1523
1524 // Forum
1525 case bbp_get_forum_post_type() :
1526 $forum_id = $post_id;
1527 $url_end = ''; //get_post_field( 'post_name', $post_id );
1528 break;
1529
1530 // Unknown
1531 default :
1532 return $url;
1533 }
1534
1535 // Get group ID's for this forum
1536 $group_ids = bbp_get_forum_group_ids( $forum_id );
1537
1538 // Bail if the post isn't associated with a group
1539 if ( empty( $group_ids ) ) {
1540 return $url;
1541 }
1542
1543 // @todo Multiple group forums/forum groups
1544 $group_id = $group_ids[0];
1545 $group = groups_get_group( array( 'group_id' => $group_id ) );
1546
1547 // Admin
1548 $group_permalink = bp_is_group_admin_screen( $this->slug )
1549 ? $this->group_manage_url( $group )
1550 : $this->group_url( $group );
1551
1552 return trailingslashit( trailingslashit( trailingslashit( $group_permalink ) . $this->slug ) . $url_end );
1553 }
1554
1555 /**
1556 * Map a forum permalink to its corresponding group
1557 *
1558 * @since 2.1.0 bbPress (r3802)
1559 *
1560 * @param string $url
1561 * @param int $forum_id
1562 * @return string
1563 */
1564 public function map_forum_permalink_to_group( $url, $forum_id ) {
1565 return $this->maybe_map_permalink_to_group( $forum_id, $url );
1566 }
1567
1568 /**
1569 * Map a topic permalink to its group forum
1570 *
1571 * @since 2.1.0 bbPress (r3802)
1572 *
1573 * @param string $url
1574 * @param int $topic_id
1575 * @return string
1576 */
1577 public function map_topic_permalink_to_group( $url, $topic_id ) {
1578 return $this->maybe_map_permalink_to_group( $topic_id, $url );
1579 }
1580
1581 /**
1582 * Map a reply permalink to its group forum
1583 *
1584 * @since 2.1.0 bbPress (r3802)
1585 *
1586 * @param string $url
1587 * @param int $reply_id
1588 * @return string
1589 */
1590 public function map_reply_permalink_to_group( $url, $reply_id ) {
1591 return $this->maybe_map_permalink_to_group( bbp_get_reply_topic_id( $reply_id ), $url );
1592 }
1593
1594 /**
1595 * Map a reply edit link to its group forum
1596 *
1597 * @since 2.2.0 bbPress (r4266)
1598 *
1599 * @param string $url
1600 * @param int $reply_id
1601 * @return string
1602 */
1603 public function map_reply_edit_url_to_group( $url, $reply_id ) {
1604 $new = $this->maybe_map_permalink_to_group( $reply_id );
1605
1606 if ( empty( $new ) ) {
1607 return $url;
1608 }
1609
1610 return trailingslashit( $new ) . bbpress()->edit_id . '/';
1611 }
1612
1613 /**
1614 * Map a post link to its group forum
1615 *
1616 * @since 2.2.0 bbPress (r4266)
1617 *
1618 * @param string $url
1619 * @param obj $post
1620 * @param boolean $leavename
1621 * @return string
1622 */
1623 public function post_link( $url, $post ) {
1624 return $this->maybe_map_permalink_to_group( $post->ID, $url );
1625 }
1626
1627 /**
1628 * Map a page link to its group forum
1629 *
1630 * @since 2.2.0 bbPress (r4266)
1631 *
1632 * @param string $url
1633 * @param int $post_id
1634 * @param $sample
1635 * @return string
1636 */
1637 public function page_link( $url, $post_id ) {
1638 return $this->maybe_map_permalink_to_group( $post_id, $url );
1639 }
1640
1641 /**
1642 * Map a custom post type link to its group forum
1643 *
1644 * @since 2.2.0 bbPress (r4266)
1645 *
1646 * @param string $url
1647 * @param obj $post
1648 * @param $leavename
1649 * @param $sample
1650 * @return string
1651 */
1652 public function post_type_link( $url, $post ) {
1653 return $this->maybe_map_permalink_to_group( $post->ID, $url );
1654 }
1655
1656 /**
1657 * Fix pagination of topics on forum view
1658 *
1659 * @since 2.2.0 bbPress (r4266)
1660 *
1661 * @param array $args
1662 * @return array
1663 */
1664 public function topic_pagination( $args ) {
1665 $new = $this->maybe_map_permalink_to_group( bbp_get_forum_id() );
1666
1667 if ( empty( $new ) ) {
1668 return $args;
1669 }
1670
1671 $args['base'] = trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
1672
1673 return $args;
1674 }
1675
1676 /**
1677 * Fix pagination of replies on topic view
1678 *
1679 * @since 2.2.0 bbPress (r4266)
1680 *
1681 * @param array $args
1682 * @return array
1683 */
1684 public function replies_pagination( $args ) {
1685 $new = $this->maybe_map_permalink_to_group( bbp_get_topic_id() );
1686 if ( empty( $new ) ) {
1687 return $args;
1688 }
1689
1690 $args['base'] = trailingslashit( $new ) . bbp_get_paged_slug() . '/%#%/';
1691
1692 return $args;
1693 }
1694
1695 /**
1696 * Fixes rewrite pagination in BuddyPress Group Forums & Topics.
1697 *
1698 * Required for compatibility with BuddyPress > 12.0, where the /groups/
1699 * rewrite rule will be caught before bbPress's /page/ rule.
1700 *
1701 * @since 2.6.14
1702 *
1703 * @param object object Verified object
1704 * @param string $type Type of variable to check with `is_a()`
1705 * @return mixed $object Verified object if valid, Default or null if invalid
1706 */
1707 public function rewrite_pagination( $object, $type = '' ) {
1708
1709 // Bail if wrong global
1710 if ( 'wp_query' !== $type ) {
1711 return $object;
1712 }
1713
1714 // Bail if not inside a BuddyPress Group
1715 if ( ! bp_is_group() ) {
1716 return $object;
1717 }
1718
1719 // Bail if not inside a BuddyPress Group Forum
1720 if ( ! bp_is_current_action( 'forum' ) ) {
1721 return $object;
1722 }
1723
1724 // Default "paged" value
1725 $page_number = null;
1726
1727 // Can't use bbp_is_single_topic() because it triggers a loop.
1728 $is_single_topic = bp_is_action_variable( 'topic', 0 );
1729
1730 // Single Topic
1731 if ( true === $is_single_topic ) {
1732
1733 // Get the page number from 3rd position
1734 if ( bp_is_action_variable( 'page', 2 ) ) {
1735 $page_number = bp_action_variable( 3 );
1736 }
1737
1738 // Single Forum
1739 } else {
1740
1741 // Get the page number from 1st position
1742 if ( bp_is_action_variable( 'page', 0 ) ) {
1743 $page_number = bp_action_variable( 1 );
1744 }
1745 }
1746
1747 // Bail if no page number
1748 if ( empty( $page_number ) ) {
1749 return $object;
1750 }
1751
1752 // Set the 'paged' WP_Query var to the new action-based value
1753 $object->set( 'paged', $page_number );
1754
1755 // Return the filtered/modified object
1756 return $object;
1757 }
1758
1759 /**
1760 * Ensure that forum content associated with a BuddyPress group can only be
1761 * viewed via the group URL.
1762 *
1763 * @since 2.1.0 bbPress (r3802)
1764 */
1765 public function redirect_canonical() {
1766
1767 // Bail if on a RSS feed
1768 if ( is_feed() ) {
1769 return;
1770 }
1771
1772 // Viewing a single forum
1773 if ( bbp_is_single_forum() ) {
1774 $forum_id = get_the_ID();
1775 $group_ids = bbp_get_forum_group_ids( $forum_id );
1776
1777 // Viewing a single topic
1778 } elseif ( bbp_is_single_topic() ) {
1779 $topic_id = get_the_ID();
1780 $slug = get_post_field( 'post_name', $topic_id );
1781 $forum_id = bbp_get_topic_forum_id( $topic_id );
1782 $group_ids = bbp_get_forum_group_ids( $forum_id );
1783
1784 // Not a forum or topic
1785 } else {
1786 return;
1787 }
1788
1789 // Bail if not a group forum
1790 if ( empty( $group_ids ) ) {
1791 return;
1792 }
1793
1794 // Use the first group ID
1795 $group_id = $group_ids[0];
1796 $group = groups_get_group( array( 'group_id' => $group_id ) );
1797 $group_link = trailingslashit( $this->group_url( $group ) );
1798 $redirect_to = trailingslashit( $group_link . $this->slug );
1799
1800 // Add topic slug to URL
1801 if ( bbp_is_single_topic() ) {
1802 $redirect_to = trailingslashit( $redirect_to . $this->topic_slug . '/' . $slug );
1803 }
1804
1805 bp_core_redirect( $redirect_to );
1806 }
1807
1808 /** Activity **************************************************************/
1809
1810 /**
1811 * Map a forum post to its corresponding group in the group activity stream.
1812 *
1813 * @since 2.2.0 bbPress (r4396)
1814 *
1815 * @param array $args Arguments from BBP_BuddyPress_Activity::record_activity()
1816 *
1817 * @return array
1818 */
1819 public function map_activity_to_group( $args = array() ) {
1820
1821 // Get current BP group
1822 $group = groups_get_current_group();
1823
1824 // Not posting from a BuddyPress group? stop now!
1825 if ( empty( $group ) ) {
1826 return $args;
1827 }
1828
1829 // Set the component to 'groups' so the activity item shows up in the group
1830 $args['component'] = buddypress()->groups->id;
1831
1832 // Move the forum post ID to the secondary item ID
1833 $args['secondary_item_id'] = $args['item_id'];
1834
1835 // Set the item ID to the group ID so the activity item shows up in the group
1836 $args['item_id'] = $group->id;
1837
1838 // Update the group's last activity
1839 groups_update_last_activity( $group->id );
1840
1841 return $args;
1842 }
1843 }
1844 endif;
1845