PluginProbe
bbPress / 2.6.15
bbPress v2.6.15
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 2.2.2 All 71 releases
bbpress / includes / users / capabilities.php

capabilities.php in bbPress 2.6.15, at includes/users/capabilities.php

866 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress User Capabilites
5 *
6 * Used to map user capabilities to WordPress's existing capabilities.
7 *
8 * @package bbPress
9 * @subpackage Capabilities
10 */
11
12 /**
13 * Maps primary capabilities
14 *
15 * @since 2.2.0 bbPress (r4242)
16 *
17 * @param array $caps Capabilities for meta capability.
18 * @param string $cap Capability name.
19 * @param int $user_id User id.
20 * @param array $args Arguments.
21 *
22 * @return array Actual capabilities for meta capability
23 */
24 function bbp_map_primary_meta_caps( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
25
26 // What capability is being checked?
27 switch ( $cap ) {
28 case 'spectate' :
29
30 // Do not allow inactive users.
31 if ( bbp_is_user_inactive( $user_id ) ) {
32 $caps = array( 'do_not_allow' );
33
34 // Default to the current cap.
35 } else {
36 $caps = array( $cap );
37 }
38 break;
39
40 case 'participate' :
41
42 // Do not allow inactive users.
43 if ( bbp_is_user_inactive( $user_id ) ) {
44 $caps = array( 'do_not_allow' );
45
46 // Default to the current cap.
47 } else {
48 $caps = array( $cap );
49 }
50 break;
51
52 case 'moderate' :
53
54 // Do not allow inactive users.
55 if ( bbp_is_user_inactive( $user_id ) ) {
56 $caps = array( 'do_not_allow' );
57
58 // Keymasters can always moderate.
59 } elseif ( bbp_is_user_keymaster( $user_id ) ) {
60 $caps = array( 'spectate' );
61
62 // Check if user can moderate forum.
63 } elseif ( bbp_allow_forum_mods() ) {
64 $caps = array( $cap );
65
66 // Bail if no post to check.
67 if ( empty( $args[0] ) ) {
68 break;
69 }
70
71 // Get the post.
72 $_post = get_post( $args[0] );
73 if ( empty( $_post ) ) {
74 break;
75 }
76
77 // Get forum ID for specific type of post.
78 switch ( $_post->post_type ) {
79
80 // Forum.
81 case bbp_get_forum_post_type() :
82 $forum_id = bbp_get_forum_id( $_post->ID );
83 break;
84
85 // Topic.
86 case bbp_get_topic_post_type() :
87 $forum_id = bbp_get_topic_forum_id( $_post->ID );
88 break;
89
90 // Reply.
91 case bbp_get_reply_post_type() :
92 $forum_id = bbp_get_reply_forum_id( $_post->ID );
93 break;
94
95 // Any other post type defaults to 0.
96 default :
97 $forum_id = 0;
98 break;
99 }
100
101 // Bail if no forum ID.
102 if ( empty( $forum_id ) ) {
103 break;
104 }
105
106 // User is mod of this forum
107 if ( bbp_is_object_of_user( $forum_id, $user_id, '_bbp_moderator_id' ) ) {
108 $caps = array( 'spectate' );
109 }
110 }
111
112 break;
113
114 /** Super Moderators **************************************************/
115
116 case 'edit_user' :
117 case 'edit_users' :
118
119 // Moderators can edit users if super moderators is enabled
120 if ( bbp_allow_super_mods() ) {
121
122 // Get the user ID
123 $_user_id = ! empty( $args[0] )
124 ? (int) $args[0]
125 : bbp_get_displayed_user_id();
126
127 // Users can always edit themselves, so only map for others
128 if ( ! empty( $_user_id ) && ( $_user_id !== $user_id ) ) {
129
130 // Super moderators cannot edit keymasters
131 if ( ! bbp_is_user_keymaster( $_user_id ) ) {
132 $caps = array( 'moderate' );
133 }
134 }
135 }
136
137 break;
138 }
139
140 // Filter & return
141 return (array) apply_filters( 'bbp_map_primary_meta_caps', $caps, $cap, $user_id, $args );
142 }
143
144 /**
145 * Set a user's role in the forums
146 *
147 * @since 2.1.0 bbPress (r3860)
148 *
149 * @param int $user_id
150 *
151 * @return mixed False if no change. String of new role if changed.
152 */
153 function bbp_set_user_role( $user_id = 0, $new_role = '' ) {
154
155 // Validate user id
156 $user_id = bbp_get_user_id( $user_id, false, false );
157 $user = get_userdata( $user_id );
158
159 // User exists
160 if ( ! empty( $user ) ) {
161
162 // Get user forum role
163 $role = bbp_get_user_role( $user_id );
164
165 // User already has this role so no new role is set
166 if ( $new_role === $role ) {
167 $new_role = false;
168
169 // User role is different than the new (valid) role
170 } elseif ( bbp_is_valid_role( $new_role ) ) {
171
172 // Remove the old role
173 if ( ! empty( $role ) ) {
174 $user->remove_role( $role );
175 }
176
177 // Add the new role
178 if ( ! empty( $new_role ) ) {
179 $user->add_role( $new_role );
180 }
181 }
182
183 // User does not exist so return false
184 } else {
185 $new_role = false;
186 }
187
188 // Filter & return
189 return apply_filters( 'bbp_set_user_role', $new_role, $user_id, $user );
190 }
191
192 /**
193 * Return a user's forums role
194 *
195 * @since 2.1.0 bbPress (r3860)
196 *
197 * @param int $user_id
198 *
199 * @return string
200 */
201 function bbp_get_user_role( $user_id = 0 ) {
202
203 // Validate user id
204 $user_id = bbp_get_user_id( $user_id );
205 $user = get_userdata( $user_id );
206 $role = false;
207
208 // User has roles so look for a bbPress one
209 if ( ! empty( $user->roles ) ) {
210
211 // Look for a bbPress role
212 $roles = array_intersect(
213 array_values( $user->roles ),
214 array_keys( bbp_get_dynamic_roles() )
215 );
216
217 // If there's a role in the array, use the first one. This isn't very
218 // smart, but since roles aren't exactly hierarchical, and bbPress
219 // does not yet have a UI for multiple user roles, it's fine for now.
220 if ( ! empty( $roles ) ) {
221 $role = array_shift( $roles );
222 }
223 }
224
225 // Filter & return
226 return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
227 }
228
229 /**
230 * Return a user's blog role
231 *
232 * @since 2.3.0 bbPress (r4446)
233 *
234 * @param int $user_id
235 *
236 * @return string
237 */
238 function bbp_get_user_blog_role( $user_id = 0 ) {
239
240 // Validate user id
241 $user_id = bbp_get_user_id( $user_id );
242 $user = get_userdata( $user_id );
243 $role = false;
244
245 // User has roles so lets
246 if ( ! empty( $user->roles ) ) {
247
248 // Look for a non bbPress role
249 $roles = array_intersect(
250 array_values( $user->roles ),
251 array_keys( bbp_get_blog_roles() )
252 );
253
254 // If there's a role in the array, use the first one. This isn't very
255 // smart, but since roles aren't exactly hierarchical, and WordPress
256 // does not yet have a UI for multiple user roles, it's fine for now.
257 if ( ! empty( $roles ) ) {
258 $role = array_shift( $roles );
259 }
260 }
261
262 // Filter & return
263 return apply_filters( 'bbp_get_user_blog_role', $role, $user_id, $user );
264 }
265
266 /**
267 * Helper function to save or update user roles and capabilities.
268 *
269 * By default, this is hooked to the `bbp_profile_update` action which fires
270 * after a user profile is updated to avoid being stomped by set_role().
271 *
272 * @since 2.2.0 bbPress (r4235)
273 *
274 * @param int $user_id
275 *
276 * @return void If no user ID passed, invalid request, trying to set own role,
277 * current user cannot promote the passing user, or no role passed
278 */
279 function bbp_profile_update_role( $user_id = 0 ) {
280
281 // Bail if no user ID
282 $user_id = bbp_get_user_id( $user_id, false, false );
283 if ( empty( $user_id ) ) {
284 return;
285 }
286
287 // Bail if not a user profile form post request
288 if ( ! bbp_is_user_profile_form_post_request( $user_id ) ) {
289 return;
290 }
291
292 // Bail if trying to set their own role
293 if ( bbp_is_user_home_edit() ) {
294 return;
295 }
296
297 // Bail if current user cannot promote the passing user
298 if ( ! current_user_can( 'promote_user', $user_id ) ) {
299 return;
300 }
301
302 // Bail if no role
303 if ( ! isset( $_POST['bbp-forums-role'] ) || ! is_string( $_POST['bbp-forums-role'] ) ) {
304 return;
305 }
306
307 // Forums role we want the user to have
308 $new_role = sanitize_key( $_POST['bbp-forums-role'] );
309
310 // Set the new forums role
311 bbp_set_user_role( $user_id, $new_role );
312 }
313
314 /**
315 * Check if a role string is valid
316 *
317 * @since 2.6.5
318 *
319 * @param string $role
320 *
321 * @return bool True if role is valid. False if role is not valid.
322 */
323 function bbp_is_valid_role( $role = '' ) {
324
325 // Default return value
326 $retval = false;
327
328 // Skip if no role to check
329 if ( ! empty( $role ) && is_string( $role ) ) {
330
331 // Get the dynamic role IDs
332 $roles = array_keys( bbp_get_dynamic_roles() );
333
334 // Skip if no known role IDs
335 if ( ! empty( $roles ) ) {
336
337 // Is role in dynamic roles array?
338 $retval = in_array( $role, $roles, true );
339 }
340 }
341
342 // Filter & return
343 return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role );
344 }
345
346 /**
347 * Add the default role to the current user if needed
348 *
349 * This function will bail if the forum is not global in a multisite
350 * installation of WordPress, or if the user is marked as spam or deleted.
351 *
352 * @since 2.0.0 bbPress (r3380)
353 *
354 * @return void If not multisite, not global, or user is deleted/spammed
355 */
356 function bbp_set_current_user_default_role() {
357
358 /** Sanity ****************************************************************/
359
360 // Bail if deactivating bbPress
361 if ( bbp_is_deactivation() ) {
362 return;
363 }
364
365 // Catch all, to prevent premature user initialization
366 if ( ! did_action( 'set_current_user' ) ) {
367 return;
368 }
369
370 // Bail if not logged in or already a member of this site
371 if ( ! is_user_logged_in() ) {
372 return;
373 }
374
375 // Get the current user ID
376 $user_id = bbp_get_current_user_id();
377
378 // Bail if user already has a forums role
379 if ( bbp_get_user_role( $user_id ) ) {
380 return;
381 }
382
383 // Bail if user is marked as spam or is deleted
384 if ( bbp_is_user_inactive( $user_id ) ) {
385 return;
386 }
387
388 /** Ready *****************************************************************/
389
390 // Load up bbPress once
391 $bbp = bbpress();
392
393 // Get whether or not to add a role to the user account
394 $add_to_site = bbp_allow_global_access();
395
396 // Get the current user's WordPress role. Set to empty string if none found.
397 $user_role = bbp_get_user_blog_role( $user_id );
398
399 // Get the role map
400 $role_map = bbp_get_user_role_map();
401
402 /** Forum Role ************************************************************/
403
404 // Use a mapped role or default role
405 $new_role = empty( $user_role ) || ! isset( $role_map[ $user_role ] )
406 ? bbp_get_default_role()
407 : $role_map[ $user_role ];
408
409 /** Add or Map ************************************************************/
410
411 // Add the user to the site
412 if ( true === $add_to_site ) {
413 bbp_set_user_role( $user_id, $new_role );
414
415 // Don't add the user, but still give them the correct caps dynamically
416 } else {
417 $bbp->current_user->caps[ $new_role ] = true;
418 $bbp->current_user->get_role_caps();
419 }
420 }
421
422 /**
423 * Return a map of WordPress roles to bbPress roles. Used to automatically grant
424 * appropriate bbPress roles to WordPress users that wouldn't already have a
425 * role in the forums. Also guarantees WordPress admins get the Keymaster role.
426 *
427 * @since 2.2.0 bbPress (r4334)
428 *
429 * @return array Filtered array of WordPress roles to bbPress roles
430 */
431 function bbp_get_user_role_map() {
432
433 // Get the default role once here
434 $default_role = bbp_get_default_role();
435
436 // Filter & return
437 return (array) apply_filters( 'bbp_get_user_role_map', array(
438 'administrator' => bbp_get_keymaster_role(),
439 'editor' => $default_role,
440 'author' => $default_role,
441 'contributor' => $default_role,
442 'subscriber' => $default_role
443 ) );
444 }
445
446 /** User Status ***************************************************************/
447
448 /**
449 * Checks if the user has been marked as a spammer.
450 *
451 * @since 2.0.0 bbPress (r3355)
452 *
453 * @param int $user_id int The ID for the user.
454 * @return bool True if spammer, False if not.
455 */
456 function bbp_is_user_spammer( $user_id = 0 ) {
457
458 // Default to current user
459 if ( empty( $user_id ) && is_user_logged_in() ) {
460 $user_id = bbp_get_current_user_id();
461 }
462
463 // No user to check
464 if ( empty( $user_id ) ) {
465 return false;
466 }
467
468 // Assume user is not spam
469 $is_spammer = false;
470
471 // Get user data
472 $user = get_userdata( $user_id );
473
474 // No user found
475 if ( empty( $user ) ) {
476 $is_spammer = false;
477
478 // Check if spam
479 } elseif ( ! empty( $user->spam ) ) {
480 $is_spammer = true;
481 }
482
483 // Filter & return
484 return (bool) apply_filters( 'bbp_core_is_user_spammer', $is_spammer );
485 }
486
487 /**
488 * Mark a users topics and replies as spam when the user is marked as spam
489 *
490 * @since 2.0.0 bbPress (r3405)
491 *
492 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
493 *
494 * @return bool If no user ID passed.
495 */
496 function bbp_make_spam_user( $user_id = 0 ) {
497
498 // Use displayed user if it's not yourself
499 if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) {
500 $user_id = bbp_get_displayed_user_id();
501 }
502
503 // Bail if no user ID
504 if ( empty( $user_id ) ) {
505 return false;
506 }
507
508 // Bail if user ID is keymaster
509 if ( bbp_is_user_keymaster( $user_id ) ) {
510 return false;
511 }
512
513 // Arm the torpedos
514 $bbp_db = bbp_db();
515
516 // Get the blog IDs of the user to mark as spam
517 $blogs = get_blogs_of_user( $user_id, true );
518
519 // If user has no blogs, they are a guest on this site
520 if ( empty( $blogs ) ) {
521 $blogs[ $bbp_db->blogid ] = array();
522 }
523
524 // Get array of post types to mark as spam
525 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
526 $post_types = "'" . implode( "', '", $post_types ) . "'";
527
528 // Get array of statuses to mark as spam
529 $post_statuses = bbp_get_public_topic_statuses();
530 $post_statuses = "'" . implode( "', '", $post_statuses ) . "'";
531
532 // Loop through blogs and remove their posts
533 foreach ( (array) array_keys( $blogs ) as $blog_id ) {
534
535 // Switch to the site ID
536 bbp_switch_to_site( $blog_id );
537
538 // Get topics and replies
539 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type IN ( {$post_types} )", $user_id );
540 $posts = $bbp_db->get_col( $query );
541
542 // Loop through posts and spam them
543 if ( ! empty( $posts ) ) {
544 foreach ( $posts as $post_id ) {
545
546 // The routines for topics ang replies are different, so use the
547 // correct one based on the post type
548 switch ( get_post_type( $post_id ) ) {
549
550 case bbp_get_topic_post_type() :
551 bbp_spam_topic( $post_id );
552 break;
553
554 case bbp_get_reply_post_type() :
555 bbp_spam_reply( $post_id );
556 break;
557 }
558 }
559 }
560
561 // Switch back to current site
562 bbp_restore_current_site();
563 }
564
565 // Delete user options
566 bbp_delete_user_options( $user_id );
567
568 // Success
569 return true;
570 }
571
572 /**
573 * Mark a users topics and replies as spam when the user is marked as spam
574 *
575 * @since 2.0.0 bbPress (r3405)
576 *
577 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
578 *
579 * @return bool If no user ID passed.
580 */
581 function bbp_make_ham_user( $user_id = 0 ) {
582
583 // Use displayed user if it's not yourself
584 if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) {
585 $user_id = bbp_get_displayed_user_id();
586 }
587
588 // Bail if no user ID
589 if ( empty( $user_id ) ) {
590 return false;
591 }
592
593 // Bail if user ID is keymaster
594 if ( bbp_is_user_keymaster( $user_id ) ) {
595 return false;
596 }
597
598 // Arm the torpedos
599 $bbp_db = bbp_db();
600
601 // Get the blog IDs of the user to mark as spam
602 $blogs = get_blogs_of_user( $user_id, true );
603
604 // If user has no blogs, they are a guest on this site
605 if ( empty( $blogs ) ) {
606 $blogs[ $bbp_db->blogid ] = array();
607 }
608
609 // Get array of post types to mark as spam
610 $post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() );
611 $post_types = "'" . implode( "', '", $post_types ) . "'";
612
613 // Get array of statuses to unmark as spam
614 $post_statuses = array( bbp_get_spam_status_id() );
615 $post_statuses = "'" . implode( "', '", $post_statuses ) . "'";
616
617 // Loop through blogs and remove their posts
618 foreach ( (array) array_keys( $blogs ) as $blog_id ) {
619
620 // Switch to the site ID
621 bbp_switch_to_site( $blog_id );
622
623 // Get topics and replies
624 $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status IN ( {$post_statuses} ) AND post_type IN ( {$post_types} )", $user_id );
625 $posts = $bbp_db->get_col( $query );
626
627 // Loop through posts and spam them
628 if ( ! empty( $posts ) ) {
629 foreach ( $posts as $post_id ) {
630
631 // The routines for topics ang replies are different, so use the
632 // correct one based on the post type
633 switch ( get_post_type( $post_id ) ) {
634
635 case bbp_get_topic_post_type() :
636 bbp_unspam_topic( $post_id );
637 break;
638
639 case bbp_get_reply_post_type() :
640 bbp_unspam_reply( $post_id );
641 break;
642 }
643 }
644 }
645
646 // Switch back to current site
647 bbp_restore_current_site();
648 }
649
650 // Update topic & reply counts
651 bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) );
652 bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) );
653
654 // Update last posted (to now)
655 bbp_update_user_last_posted( $user_id );
656
657 // Success
658 return true;
659 }
660
661 /**
662 * Checks if the user has been marked as deleted.
663 *
664 * @since 2.0.0 bbPress (r3355)
665 *
666 * @param int $user_id int The ID for the user.
667 * @return bool True if deleted, False if not.
668 */
669 function bbp_is_user_deleted( $user_id = 0 ) {
670
671 // Default to current user
672 if ( empty( $user_id ) && is_user_logged_in() ) {
673 $user_id = bbp_get_current_user_id();
674 }
675
676 // No user to check
677 if ( empty( $user_id ) ) {
678 return false;
679 }
680
681 // Assume user is not deleted
682 $is_deleted = false;
683
684 // Get user data
685 $user = get_userdata( $user_id );
686
687 // No user found
688 if ( empty( $user ) ) {
689 $is_deleted = true;
690
691 // Check if deleted
692 } elseif ( ! empty( $user->deleted ) ) {
693 $is_deleted = true;
694 }
695
696 // Filter & return
697 return (bool) apply_filters( 'bbp_core_is_user_deleted', $is_deleted );
698 }
699
700 /**
701 * Checks if user is active
702 *
703 * @since 2.0.0 bbPress (r3502)
704 *
705 * @param int $user_id The user ID to check
706 * @return bool True if public, false if not
707 */
708 function bbp_is_user_active( $user_id = 0 ) {
709
710 // No user to check
711 $user_id = bbp_get_user_id( $user_id, false, true );
712 if ( empty( $user_id ) ) {
713 return false;
714 }
715
716 // Check spam
717 if ( bbp_is_user_spammer( $user_id ) ) {
718 return false;
719 }
720
721 // Check deleted
722 if ( bbp_is_user_deleted( $user_id ) ) {
723 return false;
724 }
725
726 // Assume true if not spam or deleted
727 return true;
728 }
729
730 /**
731 * Checks if user is not active.
732 *
733 * @since 2.0.0 bbPress (r3502)
734 *
735 * @param int $user_id The user ID to check. Defaults to current user ID
736 * @return bool True if inactive, false if active
737 */
738 function bbp_is_user_inactive( $user_id = 0 ) {
739 return ! bbp_is_user_active( $user_id );
740 }
741
742 /**
743 * Checks if user is a keymaster
744 *
745 * @since 2.3.0 bbPress (r4783)
746 *
747 * @param int $user_id
748 * @return bool True if keymaster, false if not
749 */
750 function bbp_is_user_keymaster( $user_id = 0 ) {
751 $_user_id = bbp_get_user_id( $user_id, false, true );
752 $retval = user_can( $_user_id, 'keep_gate' );
753
754 // Filter & return
755 return (bool) apply_filters( 'bbp_is_user_keymaster', $retval, $_user_id, $user_id );
756 }
757
758 /**
759 * Does a user have a profile for the current site
760 *
761 * @since 2.2.0 bbPress (r4362)
762 *
763 * @param int $user_id User ID to check
764 *
765 * @return bool Whether or not the user has a profile on this blog_id.
766 */
767 function bbp_user_has_profile( $user_id = 0 ) {
768
769 // Assume every user has a profile
770 $retval = true;
771
772 // Validate user ID, default to displayed or current user
773 $user_id = bbp_get_user_id( $user_id, true, true );
774
775 // Try to get this user's data
776 $user = get_userdata( $user_id );
777
778 // No user found, return false
779 if ( empty( $user ) ) {
780 $retval = false;
781
782 // User is inactive, and current user is not a keymaster
783 } elseif ( ! bbp_is_user_keymaster() && bbp_is_user_inactive( $user->ID ) ) {
784 $retval = false;
785 }
786
787 // Filter & return
788 return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id );
789 }
790
791 /** Moderators ****************************************************************/
792
793 /**
794 * Add a moderator to an object
795 *
796 * @since 2.6.0 bbPress (r6056)
797 *
798 * @param int $object_id Traditionally a post ID
799 * @param int $user_id User ID
800 * @param string $object_type Type of meta (post,term,user,comment)
801 *
802 * @return bool
803 */
804 function bbp_add_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) {
805 return bbp_add_user_to_object( $object_id, $user_id, '_bbp_moderator_id', $object_type );
806 }
807
808 /**
809 * Remove a moderator user ID from an object
810 *
811 * @since 2.6.0 bbPress (r6056)
812 *
813 * @param int $object_id Traditionally a post ID
814 * @param int $user_id User ID
815 * @param string $object_type Type of meta (post,term,user,comment)
816 *
817 * @return bool
818 */
819 function bbp_remove_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) {
820 return bbp_remove_user_from_object( $object_id, $user_id, '_bbp_moderator_id', $object_type );
821 }
822
823 /**
824 * Get user IDs of moderators for an object
825 *
826 * @since 2.6.0 bbPress (r6056)
827 *
828 * @param int $object_id Traditionally a post ID
829 * @param string $object_type Type of meta (post,term,user,comment)
830 *
831 * @return array
832 */
833 function bbp_get_moderator_ids( $object_id = 0, $object_type = 'post' ) {
834 return bbp_get_users_for_object( $object_id, '_bbp_moderator_id', $object_type );
835 }
836
837 /**
838 * Get moderators for a specific object ID. Will return global moderators when
839 * object ID is empty.
840 *
841 * @since 2.6.0 bbPress (r6056)
842 *
843 * @param int $object_id Traditionally a post ID
844 * @param string $object_type Type of meta (post,term,user,comment)
845 *
846 * @return array
847 */
848 function bbp_get_moderators( $object_id = 0, $object_type = 'post' ) {
849
850 // Get global moderators
851 if ( empty( $object_id ) ) {
852 $users = get_users( array(
853 'role__in' => bbp_get_moderator_role(),
854 ) );
855
856 // Get object moderators
857 } else {
858 $users = get_users( array(
859 'include' => bbp_get_moderator_ids( $object_id, $object_type ),
860 ) );
861 }
862
863 // Filter & return
864 return (array) apply_filters( 'bbp_get_moderators', $users, $object_id, $object_type );
865 }
866