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

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

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