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 / functions.php

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

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