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