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