PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 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 All 74 releases
bbpress / includes / extend / buddypress / groups.php

groups.php in bbPress 2.6.19, at includes/extend/buddypress/groups.php

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