PluginProbe
bbPress / trunk
bbPress vtrunk
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 / functions.php

functions.php in bbPress trunk, at includes/extend/buddypress/functions.php

952 lines 24.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Main bbPress BuddyPress Class.
5 *
6 * @package bbPress
7 * @subpackage BuddyPress
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 // Hooks
14 add_filter( 'bp_modify_page_title', 'bbp_filter_modify_page_title', 10, 3 );
15 add_filter( 'bbp_get_user_id', 'bbp_filter_user_id', 10, 3 );
16 add_filter( 'bbp_is_single_user', 'bbp_filter_is_single_user', 10, 1 );
17 add_filter( 'bbp_is_user_home', 'bbp_filter_is_user_home', 10, 1 );
18
19 // Group Forum Root
20 add_action( 'load-settings_page_bbpress', 'bbp_maybe_create_group_forum_root' );
21 add_action( 'bbp_delete_forum', 'bbp_maybe_delete_group_forum_root' );
22
23 /** BuddyPress Helpers ********************************************************/
24
25 /**
26 * Return component name/ID ('forums' by default).
27 *
28 * This is used primarily for Notifications integration.
29 *
30 * @since 2.6.0 bbPress (r5232)
31 *
32 * @return string
33 */
34 function bbp_get_component_name() {
35
36 // Use existing ID or default
37 $retval = ! empty( bbpress()->extend->buddypress->id )
38 ? bbpress()->extend->buddypress->id
39 : 'forums';
40
41 // Filter & return
42 return apply_filters( 'bbp_get_component_name', $retval );
43 }
44
45 /**
46 * Filter the current bbPress user ID with the current BuddyPress user ID.
47 *
48 * @since 2.1.0 bbPress (r3552)
49 *
50 * @param int $user_id
51 * @param bool $displayed_user_fallback
52 * @param bool $current_user_fallback
53 *
54 * @return int User ID
55 */
56 function bbp_filter_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
57
58 // Define local variable
59 $bbp_user_id = 0;
60
61 // Get possible user ID's
62 $did = bp_displayed_user_id();
63 $lid = bp_loggedin_user_id();
64
65 // Easy empty checking
66 if ( ! empty( $user_id ) && is_numeric( $user_id ) ) {
67 $bbp_user_id = $user_id;
68
69 // Currently viewing or editing a user
70 } elseif ( ( true === $displayed_user_fallback ) && ! empty( $did ) ) {
71 $bbp_user_id = $did;
72
73 // Maybe fallback on the current_user ID
74 } elseif ( ( true === $current_user_fallback ) && ! empty( $lid ) ) {
75 $bbp_user_id = $lid;
76 }
77
78 return $bbp_user_id;
79 }
80
81 /**
82 * Filter the bbPress is_single_user function with BuddyPress equivalent.
83 *
84 * @since 2.1.0 bbPress (r3552)
85 *
86 * @param bool $is Optional. Default false.
87 * @return bool True if viewing single user, false if not.
88 */
89 function bbp_filter_is_single_user( $is = false ) {
90 if ( ! empty( $is ) ) {
91 return $is;
92 }
93
94 return bp_is_user();
95 }
96
97 /**
98 * Filter the bbPress is_user_home function with BuddyPress equivalent.
99 *
100 * @since 2.1.0 bbPress (r3552)
101 *
102 * @param bool $is Optional. Default false.
103 * @return bool True if viewing single user, false if not.
104 */
105 function bbp_filter_is_user_home( $is = false ) {
106 if ( ! empty( $is ) ) {
107 return $is;
108 }
109
110 return bp_is_my_profile();
111 }
112
113 /**
114 * Add the topic title to the <title> if viewing a single group forum topic.
115 *
116 * @since 2.5.0 bbPress (r5161)
117 *
118 * @param string $new_title The title to filter.
119 * @param string $old_title (Not used).
120 * @param string $sep The separator to u.se
121 * @return string The possibly modified title.
122 */
123 function bbp_filter_modify_page_title( $new_title = '', $old_title = '', $sep = '' ) {
124
125 // Only filter if group forums are active
126 if ( bbp_is_group_forums_active() ) {
127
128 // Only filter for single group forum topics
129 if ( bp_is_group_forum_topic() || bp_is_group_forum_topic_edit() ) {
130 if ( ! bp_group_is_visible( bp_get_current_group_id() ) ) {
131 return $new_title;
132 }
133
134 $forum_ids = bbp_get_group_forum_ids();
135
136 if ( empty( $forum_ids ) ) {
137 return $new_title;
138 }
139
140 // Get a topic belonging to the current group's forums
141 $topic = get_posts(
142 array(
143 'name' => bp_action_variable( 1 ),
144 'post_parent__in' => $forum_ids,
145 'post_status' => array_keys( bbp_get_topic_statuses() ),
146 'post_type' => bbp_get_topic_post_type(),
147 'numberposts' => 1
148 )
149 );
150
151 // Add the title only when the topic and its forum are readable
152 if ( ! empty( $topic ) && bbp_user_can_view_forum( array( 'forum_id' => $topic[0]->post_parent ) ) && ( bbp_is_topic_public( $topic[0]->ID ) || current_user_can( 'read_topic', $topic[0]->ID ) ) ) {
153 $new_title .= bbp_get_topic_title( $topic[0]->ID ) . ' ' . $sep . ' ';
154 }
155 }
156 }
157
158 // Return the title
159 return $new_title;
160 }
161
162 /** BuddyPress Screens ********************************************************/
163
164 /**
165 * Hook bbPress topics template into plugins template.
166 *
167 * @since 2.1.0 bbPress (r3552)
168 */
169 function bbp_member_forums_screen_topics() {
170 add_action( 'bp_template_content', 'bbp_member_forums_topics_content' );
171 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_topics', 'members/single/plugins' ) );
172 }
173
174 /**
175 * Hook bbPress replies template into plugins template.
176 *
177 * @since 2.1.0 bbPress (r3552)
178 */
179 function bbp_member_forums_screen_replies() {
180 add_action( 'bp_template_content', 'bbp_member_forums_replies_content' );
181 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_replies', 'members/single/plugins' ) );
182 }
183
184 /**
185 * Hook bbPress engagements template into plugins template.
186 *
187 * @since 2.6.0 bbPress (r6320)
188 */
189 function bbp_member_forums_screen_engagements() {
190 add_action( 'bp_template_content', 'bbp_member_forums_engagements_content' );
191 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_engagements', 'members/single/plugins' ) );
192 }
193
194 /**
195 * Hook bbPress favorites template into plugins template.
196 *
197 * @since 2.1.0 bbPress (r3552)
198 */
199 function bbp_member_forums_screen_favorites() {
200 add_action( 'bp_template_content', 'bbp_member_forums_favorites_content' );
201 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_favorites', 'members/single/plugins' ) );
202 }
203
204 /**
205 * Hook bbPress subscriptions template into plugins template.
206 *
207 * @since 2.1.0 bbPress (r3552)
208 */
209 function bbp_member_forums_screen_subscriptions() {
210 add_action( 'bp_template_content', 'bbp_member_forums_subscriptions_content' );
211 bp_core_load_template( apply_filters( 'bbp_member_forums_screen_subscriptions', 'members/single/plugins' ) );
212 }
213
214 /** BuddyPress Templates ******************************************************/
215
216 /**
217 * Get the topics created template part.
218 *
219 * @since 2.1.0 bbPress (r3552)
220 */
221 function bbp_member_forums_topics_content() {
222 ?>
223
224 <div id="bbpress-forums" class="bbpress-wrapper">
225
226 <?php bbp_get_template_part( 'user', 'topics-created' ); ?>
227
228 </div>
229
230 <?php
231 }
232
233 /**
234 * Get the topics replied to template part.
235 *
236 * @since 2.1.0 bbPress (r3552)
237 */
238 function bbp_member_forums_replies_content() {
239 ?>
240
241 <div id="bbpress-forums" class="bbpress-wrapper">
242
243 <?php bbp_get_template_part( 'user', 'replies-created' ); ?>
244
245 </div>
246
247 <?php
248 }
249
250 /**
251 * Get the topic engagements template part.
252 *
253 * @since 2.6.0 bbPress (r6320)
254 */
255 function bbp_member_forums_engagements_content() {
256 ?>
257
258 <div id="bbpress-forums" class="bbpress-wrapper">
259
260 <?php bbp_get_template_part( 'user', 'engagements' ); ?>
261
262 </div>
263
264 <?php
265 }
266
267 /**
268 * Get the topics favorited template part.
269 *
270 * @since 2.1.0 bbPress (r3552)
271 */
272 function bbp_member_forums_favorites_content() {
273 ?>
274
275 <div id="bbpress-forums" class="bbpress-wrapper">
276
277 <?php bbp_get_template_part( 'user', 'favorites' ); ?>
278
279 </div>
280
281 <?php
282 }
283
284 /**
285 * Get the topics subscribed template part.
286 *
287 * @since 2.1.0 bbPress (r3552)
288 */
289 function bbp_member_forums_subscriptions_content() {
290 ?>
291
292 <div id="bbpress-forums" class="bbpress-wrapper">
293
294 <?php bbp_get_template_part( 'user', 'subscriptions' ); ?>
295
296 </div>
297
298 <?php
299 }
300
301 /** Forum Group Root **********************************************************/
302
303 /**
304 * Clean up the group root setting if the forum is being deleted.
305 *
306 * @since 2.6.0 bbPress (r6485)
307 *
308 * @param int $forum_id The forum ID being deleted.
309 */
310 function bbp_maybe_delete_group_forum_root( $forum_id = 0 ) {
311
312 // Bail if no forum ID
313 $forum_id = bbp_get_forum_id();
314 if ( empty( $forum_id ) ) {
315 return;
316 }
317
318 // Get the group root
319 $group_root = (int) get_option( '_bbp_group_forums_root_id', 0 );
320
321 // Delete the group root if the forum just got deleted
322 if ( $group_root === $forum_id ) {
323 delete_option( '_bbp_group_forums_root_id' );
324 }
325 }
326
327 /**
328 * Handle the new group forum root creation.
329 *
330 * @since 2.6.0 bbPress (r6485)
331 *
332 * @return void
333 */
334 function bbp_maybe_create_group_forum_root() {
335
336 // Bail if no nonce
337 if ( empty( $_GET['_wpnonce'] ) || ( empty( $_GET['create'] ) || ( 'bbp-group-forum-root' !== $_GET['create'] ) ) ) {
338 return;
339 }
340
341 // Bail if user cannot publish forums
342 if ( ! current_user_can( 'publish_forums' ) ) {
343 return;
344 }
345
346 // Bail if nonce check fails
347 if ( ! wp_verify_nonce( $_GET['_wpnonce'], '_bbp_group_forums_root_id' ) ) {
348 return;
349 }
350
351 // Create new forum
352 $forum_id = bbp_insert_forum(
353 array( 'post_title' => esc_html__( 'Group Forums', 'bbpress' ) ),
354 array( 'forum_type' => 'category' )
355 );
356
357 // Update & redirect
358 if ( ! empty( $forum_id ) ) {
359
360 // Create
361 update_option( '_bbp_group_forums_root_id', $forum_id );
362
363 // Redirect
364 bbp_redirect(
365 add_query_arg(
366 array(
367 'page' => 'bbpress',
368 'updated' => true
369 ),
370 admin_url( 'options-general.php' )
371 )
372 );
373 }
374 }
375
376 /** Forum/Group Sync **********************************************************/
377
378 /**
379 * These functions are used to keep the many-to-many relationships between
380 * groups and forums synchronized. Each forum and group stores pointers to each
381 * other in their respective meta. This way if a group or forum is deleted
382 * their associations can be updated without much effort.
383 */
384
385 /**
386 * Get forum ID's for a group.
387 *
388 * @since 2.1.0 bbPress (r3654)
389 *
390 * @param int $group_id Group id.
391 *
392 * @return array
393 */
394 function bbp_get_group_forum_ids( $group_id = 0 ) {
395
396 // Assume no forums
397 $forum_ids = array();
398
399 // Use current group if none is set
400 if ( empty( $group_id ) ) {
401 $group_id = bp_get_current_group_id();
402 }
403
404 // Get the forums
405 if ( ! empty( $group_id ) ) {
406 $forum_ids = groups_get_groupmeta( $group_id, 'forum_id' );
407 }
408
409 // Make sure result is an array of ints
410 $forum_ids = array_filter( wp_parse_id_list( $forum_ids ) );
411
412 // Filter & return
413 return (array) apply_filters( 'bbp_get_group_forum_ids', $forum_ids, $group_id );
414 }
415
416 /**
417 * Get group ID's for a forum.
418 *
419 * @since 2.1.0 bbPress (r3654)
420 *
421 * @param int $forum_id Forum id.
422 *
423 * @return array
424 */
425 function bbp_get_forum_group_ids( $forum_id = 0 ) {
426
427 // Assume no forums
428 $group_ids = array();
429
430 // Use current group if none is set
431 if ( empty( $forum_id ) ) {
432 $forum_id = bbp_get_forum_id();
433 }
434
435 // Get the forums
436 if ( ! empty( $forum_id ) ) {
437 $group_ids = get_post_meta( $forum_id, '_bbp_group_ids', true );
438 }
439
440 // Make sure result is an array of ints
441 $group_ids = array_filter( wp_parse_id_list( $group_ids ) );
442
443 // Filter & return
444 return (array) apply_filters( 'bbp_get_forum_group_ids', $group_ids, $forum_id );
445 }
446
447 /**
448 * Get forum ID's for a group.
449 *
450 * @since 2.1.0 bbPress (r3654)
451 *
452 * @param int $group_id Group id.
453 *
454 * @return int|bool
455 */
456 function bbp_update_group_forum_ids( $group_id = 0, $forum_ids = array() ) {
457
458 // Use current group if none is set
459 if ( empty( $group_id ) ) {
460 $group_id = bp_get_current_group_id();
461 }
462
463 // Trim out any empties
464 $forum_ids = array_filter( wp_parse_id_list( $forum_ids ) );
465
466 // Get the forums
467 return groups_update_groupmeta( $group_id, 'forum_id', $forum_ids );
468 }
469
470 /**
471 * Update group ID's for a forum.
472 *
473 * @since 2.1.0 bbPress (r3654)
474 *
475 * @param int $forum_id Forum id.
476 *
477 * @return int|bool
478 */
479 function bbp_update_forum_group_ids( $forum_id = 0, $group_ids = array() ) {
480 $forum_id = bbp_get_forum_id( $forum_id );
481
482 // Trim out any empties
483 $group_ids = array_filter( wp_parse_id_list( $group_ids ) );
484
485 // Get the forums
486 return update_post_meta( $forum_id, '_bbp_group_ids', $group_ids );
487 }
488
489 /**
490 * Add a group to a forum.
491 *
492 * @since 2.1.0 bbPress (r3654)
493 *
494 * @param int $group_id Group id.
495 *
496 * @return int|bool
497 */
498 function bbp_add_group_id_to_forum( $forum_id = 0, $group_id = 0 ) {
499
500 // Validate forum_id
501 $forum_id = bbp_get_forum_id( $forum_id );
502
503 // Use current group if none is set
504 if ( empty( $group_id ) ) {
505 $group_id = bp_get_current_group_id();
506 }
507
508 // Get current group IDs
509 $group_ids = bbp_get_forum_group_ids( $forum_id );
510
511 // Maybe update the groups forums
512 if ( ! in_array( $group_id, $group_ids, true ) ) {
513 $group_ids[] = $group_id;
514 return bbp_update_forum_group_ids( $forum_id, $group_ids );
515 }
516 }
517
518 /**
519 * Remove a forum from a group.
520 *
521 * @since 2.1.0 bbPress (r3654)
522 *
523 * @param int $group_id Group id.
524 *
525 * @return int|bool
526 */
527 function bbp_add_forum_id_to_group( $group_id = 0, $forum_id = 0 ) {
528
529 // Validate forum_id
530 $forum_id = bbp_get_forum_id( $forum_id );
531
532 // Use current group if none is set
533 if ( empty( $group_id ) ) {
534 $group_id = bp_get_current_group_id();
535 }
536
537 // Get current group IDs
538 $forum_ids = bbp_get_group_forum_ids( $group_id );
539
540 // Maybe update the groups forums
541 if ( ! in_array( $forum_id, $forum_ids, true ) ) {
542 $forum_ids[] = $forum_id;
543 return bbp_update_group_forum_ids( $group_id, $forum_ids );
544 }
545 }
546
547 /**
548 * Remove a group from a forum.
549 *
550 * @since 2.1.0 bbPress (r3654)
551 *
552 * @param int $group_id Group id.
553 *
554 * @return int|bool
555 */
556 function bbp_remove_group_id_from_forum( $forum_id = 0, $group_id = 0 ) {
557
558 // Validate forum_id
559 $forum_id = bbp_get_forum_id( $forum_id );
560
561 // Use current group if none is set
562 if ( empty( $group_id ) ) {
563 $group_id = bp_get_current_group_id();
564 }
565
566 // Get current group IDs
567 $group_ids = bbp_get_forum_group_ids( $forum_id );
568
569 // Maybe update the groups forums
570 if ( in_array( $group_id, $group_ids, true ) ) {
571 $group_ids = array_diff( array_values( $group_ids ), (array) $group_id );
572 return bbp_update_forum_group_ids( $forum_id, $group_ids );
573 }
574 }
575
576 /**
577 * Remove a forum from a group.
578 *
579 * @since 2.1.0 bbPress (r3654)
580 *
581 * @param int $group_id Group id.
582 *
583 * @return int|bool
584 */
585 function bbp_remove_forum_id_from_group( $group_id = 0, $forum_id = 0 ) {
586
587 // Validate forum_id
588 $forum_id = bbp_get_forum_id( $forum_id );
589
590 // Use current group if none is set
591 if ( empty( $group_id ) ) {
592 $group_id = bp_get_current_group_id();
593 }
594
595 // Get current group IDs
596 $forum_ids = bbp_get_group_forum_ids( $group_id );
597
598 // Maybe update the groups forums
599 if ( in_array( $forum_id, $forum_ids, true ) ) {
600 $forum_ids = array_diff( array_values( $forum_ids ), (array) $forum_id );
601 return bbp_update_group_forum_ids( $group_id, $forum_ids );
602 }
603 }
604
605 /**
606 * Remove a group from all forums.
607 *
608 * @since 2.1.0 bbPress (r3654)
609 *
610 * @param int $group_id Group id.
611 */
612 function bbp_remove_group_id_from_all_forums( $group_id = 0 ) {
613
614 // Use current group if none is set
615 if ( empty( $group_id ) ) {
616 $group_id = bp_get_current_group_id();
617 }
618
619 // Get current group IDs
620 $forum_ids = bbp_get_group_forum_ids( $group_id );
621
622 // Loop through forums and remove this group from each one
623 foreach ( $forum_ids as $forum_id ) {
624 bbp_remove_group_id_from_forum( $group_id, $forum_id );
625 }
626 }
627
628 /**
629 * Remove a forum from all groups.
630 *
631 * @since 2.1.0 bbPress (r3654)
632 *
633 * @param int $forum_id Forum id.
634 */
635 function bbp_remove_forum_id_from_all_groups( $forum_id = 0 ) {
636
637 // Validate
638 $forum_id = bbp_get_forum_id( $forum_id );
639 $group_ids = bbp_get_forum_group_ids( $forum_id );
640
641 // Loop through groups and remove this forum from each one
642 foreach ( $group_ids as $group_id ) {
643 bbp_remove_forum_id_from_group( $forum_id, $group_id );
644 }
645 }
646
647 /**
648 * Return true if a forum is a group forum.
649 *
650 * @since 2.3.0 bbPress (r4572)
651 *
652 * @param int $forum_id Forum id.
653 *
654 * @return bool True if it is a group forum, false if not.
655 */
656 function bbp_is_forum_group_forum( $forum_id = 0 ) {
657
658 // Validate
659 $forum_id = bbp_get_forum_id( $forum_id );
660
661 // Check for group ID's
662 $group_ids = bbp_get_forum_group_ids( $forum_id );
663
664 // Check if the forum has groups
665 $retval = (bool) ! empty( $group_ids );
666
667 // Filter & return
668 return (bool) apply_filters( 'bbp_is_forum_group_forum', $retval, $forum_id, $group_ids );
669 }
670
671 /*** Group Member Status ******************************************************/
672
673 /**
674 * Is the current user an admin of the current group.
675 *
676 * @since 2.3.0 bbPress (r4633)
677 *
678 * @return bool If current user is an admin of the current group.
679 */
680 function bbp_group_is_admin() {
681
682 // Bail if user is not logged in or not looking at a group
683 if ( ! is_user_logged_in() || ! bp_is_group() ) {
684 return false;
685 }
686
687 $bbp = bbpress();
688
689 // Set the global if not set
690 if ( ! isset( $bbp->current_user->is_group_admin ) ) {
691 $bbp->current_user->is_group_admin = groups_is_user_admin( bp_loggedin_user_id(), bp_get_current_group_id() );
692 }
693
694 // Return the value
695 return (bool) $bbp->current_user->is_group_admin;
696 }
697
698 /**
699 * Is the current user a moderator of the current group.
700 *
701 * @since 2.3.0 bbPress (r4633)
702 *
703 * @return bool If current user is a moderator of the current group.
704 */
705 function bbp_group_is_mod() {
706
707 // Bail if user is not logged in or not looking at a group
708 if ( ! is_user_logged_in() || ! bp_is_group() ) {
709 return false;
710 }
711
712 $bbp = bbpress();
713
714 // Set the global if not set
715 if ( ! isset( $bbp->current_user->is_group_mod ) ) {
716 $bbp->current_user->is_group_mod = groups_is_user_mod( bp_loggedin_user_id(), bp_get_current_group_id() );
717 }
718
719 // Return the value
720 return (bool) $bbp->current_user->is_group_mod;
721 }
722
723 /**
724 * Is the current user a member of the current group.
725 *
726 * @since 2.3.0 bbPress (r4633)
727 *
728 * @return bool If current user is a member of the current group.
729 */
730 function bbp_group_is_member() {
731
732 // Bail if user is not logged in or not looking at a group
733 if ( ! is_user_logged_in() || ! bp_is_group() ) {
734 return false;
735 }
736
737 $bbp = bbpress();
738
739 // Set the global if not set
740 if ( ! isset( $bbp->current_user->is_group_member ) ) {
741 $bbp->current_user->is_group_member = groups_is_user_member( bp_loggedin_user_id(), bp_get_current_group_id() );
742 }
743
744 // Return the value
745 return (bool) $bbp->current_user->is_group_member;
746 }
747
748 /**
749 * Is the current user banned from the current group.
750 *
751 * @since 2.3.0 bbPress (r4633)
752 *
753 * @return bool If current user is banned from the current group.
754 */
755 function bbp_group_is_banned() {
756
757 // Bail if user is not logged in or not looking at a group
758 if ( ! is_user_logged_in() || ! bp_is_group() ) {
759 return false;
760 }
761
762 $bbp = bbpress();
763
764 // Set the global if not set
765 if ( ! isset( $bbp->current_user->is_group_banned ) ) {
766 $bbp->current_user->is_group_banned = groups_is_user_banned( bp_loggedin_user_id(), bp_get_current_group_id() );
767 }
768
769 // Return the value
770 return (bool) $bbp->current_user->is_group_banned;
771 }
772
773 /**
774 * Is the current user the creator of the current group.
775 *
776 * @since 2.3.0 bbPress (r4633)
777 *
778 * @return bool If current user the creator of the current group.
779 */
780 function bbp_group_is_creator() {
781
782 // Bail if user is not logged in or not looking at a group
783 if ( ! is_user_logged_in() || ! bp_is_group() ) {
784 return false;
785 }
786
787 $bbp = bbpress();
788
789 // Set the global if not set
790 if ( ! isset( $bbp->current_user->is_group_creator ) ) {
791 $bbp->current_user->is_group_creator = groups_is_user_creator( bp_loggedin_user_id(), bp_get_current_group_id() );
792 }
793
794 // Return the value
795 return (bool) $bbp->current_user->is_group_creator;
796 }
797
798 /* BuddyPress Activity Action Callbacks ***************************************/
799
800 /**
801 * Return an array of allowed activity actions.
802 *
803 * @since 2.6.0 bbPress (r6370)
804 *
805 * @return array
806 */
807 function bbp_get_activity_actions() {
808
809 // Define activity actions with proper translation support
810 $actions = array(
811
812 'topic' =>
813 /* translators: 1: User linked profile, 2: Topic linked title, 3: Forum linked title */
814 esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ),
815
816 'reply' =>
817 /* translators: 1: User linked profile, 2: Topic linked title, 3: Forum linked title */
818 esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' )
819 );
820
821 // Filter & return
822 return (array) apply_filters( 'bbp_get_activity_actions', $actions );
823 }
824
825 /**
826 * Generic function to format the dynamic BuddyPress activity action for new
827 * topics/replies.
828 *
829 * @since 2.6.0 bbPress (r6370)
830 *
831 * @param string $type The type of post. Expects `topic` or `reply`.
832 * @param string $action The current action string.
833 * @param BP_Activity_Activity $activity The BuddyPress activity object.
834 *
835 * @return string The formatted activity action.
836 */
837 function bbp_format_activity_action_new_post( $type = '', $action = '', $activity = false ) {
838
839 // Get actions
840 $actions = bbp_get_activity_actions();
841
842 // Bail early if we don't have a valid type
843 if ( ! in_array( $type, array_keys( $actions ), true ) ) {
844 return $action;
845 }
846
847 // Bail if intercepted
848 // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
849 $intercept = bbp_maybe_intercept( __FUNCTION__, func_get_args() );
850 if ( bbp_is_intercepted( $intercept ) ) {
851 return $intercept;
852 }
853
854 // Groups component
855 if ( 'groups' === $activity->component ) {
856 if ( 'topic' === $type ) {
857 $topic_id = bbp_get_topic_id( $activity->secondary_item_id );
858 $forum_id = bbp_get_topic_forum_id( $topic_id );
859 } else {
860 $topic_id = bbp_get_reply_topic_id( $activity->secondary_item_id );
861 $forum_id = bbp_get_topic_forum_id( $topic_id );
862 }
863
864 // General component (bbpress/forums/other)
865 // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
866 } else {
867 if ( 'topic' === $type ) {
868 $topic_id = bbp_get_topic_id( $activity->item_id );
869 $forum_id = bbp_get_forum_id( $activity->secondary_item_id );
870 } else {
871 $topic_id = bbp_get_topic_id( $activity->secondary_item_id );
872 $forum_id = bbp_get_topic_forum_id( $topic_id );
873 }
874 }
875
876 // User link for topic author
877 $user_link = bbp_get_user_profile_link( $activity->user_id );
878
879 // Topic link
880 $topic_permalink = bbp_get_topic_permalink( $topic_id );
881 $topic_title = get_post_field( 'post_title', $topic_id, 'raw' );
882 $topic_link = '<a href="' . esc_url( $topic_permalink ) . '">' . esc_html( $topic_title ) . '</a>';
883
884 // Forum link
885 $forum_permalink = bbp_get_forum_permalink( $forum_id );
886 $forum_title = get_post_field( 'post_title', $forum_id, 'raw' );
887 $forum_link = '<a href="' . esc_url( $forum_permalink ) . '">' . esc_html( $forum_title ) . '</a>';
888
889 // Format
890 $activity_action = sprintf( $actions[ $type ], $user_link, $topic_link, $forum_link );
891
892 /**
893 * Filters the formatted activity action new activity string.
894 *
895 * @since 2.6.0 bbPress (r6370)
896 *
897 * @param string $activity_action Activity action string value.
898 * @param string $type The type of post. Expects `topic` or `reply`.
899 * @param string $action The current action string.
900 * @param BP_Activity_Activity $activity The BuddyPress activity object.
901 */
902 return apply_filters( 'bbp_format_activity_action_new_post', $activity_action, $type, $action, $activity );
903 }
904
905 /**
906 * Formats the dynamic BuddyPress activity action for new topics.
907 *
908 * @since 2.6.0 bbPress (r6370)
909 *
910 * @param string $action The current action string.
911 * @param object $activity The BuddyPress activity object.
912 *
913 * @return string The formatted activity action.
914 */
915 function bbp_format_activity_action_new_topic( $action, $activity ) {
916 $action = bbp_format_activity_action_new_post( bbp_get_topic_post_type(), $action, $activity );
917
918 /**
919 * Filters the formatted activity action new topic string.
920 *
921 * @since 2.6.0 bbPress (r6370)
922 *
923 * @param string $action Activity action string value
924 * @param BP_Activity_Activity $activity Activity item object
925 */
926 return apply_filters( 'bbp_format_activity_action_new_topic', $action, $activity );
927 }
928
929 /**
930 * Formats the dynamic BuddyPress activity action for new replies.
931 *
932 * @since 2.6.0 bbPress (r6370)
933 *
934 * @param string $action The current action string.
935 * @param object $activity The BuddyPress activity object.
936 *
937 * @return string The formatted activity action
938 */
939 function bbp_format_activity_action_new_reply( $action, $activity ) {
940 $action = bbp_format_activity_action_new_post( bbp_get_reply_post_type(), $action, $activity );
941
942 /**
943 * Filters the formatted activity action new reply string.
944 *
945 * @since 2.6.0 bbPress (r6370)
946 *
947 * @param string $action Activity action string value
948 * @param BP_Activity_Activity $activity Activity item object
949 */
950 return apply_filters( 'bbp_format_activity_action_new_reply', $action, $activity );
951 }
952