| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress User Capabilities. |
| 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 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 don 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 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 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 hooked to 'bbp_profile_update' action to save or |
| 268 |
* update user roles and capabilities. |
| 269 |
* |
| 270 |
* @since 2.2.0 bbPress (r4235) |
| 271 |
* |
| 272 |
* @param int $user_id User id. |
| 273 |
*/ |
| 274 |
function bbp_profile_update_role( $user_id = 0 ) { |
| 275 |
|
| 276 |
// Bail if no user ID |
| 277 |
$user_id = bbp_get_user_id( $user_id, false, false ); |
| 278 |
if ( empty( $user_id ) ) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
// Bail if not a user profile form post request |
| 283 |
if ( ! bbp_is_user_profile_form_post_request( $user_id ) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
// Bail if trying to set their own role |
| 288 |
if ( bbp_is_user_home_edit() ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
// Bail if current user cannot promote the passing user |
| 293 |
if ( ! current_user_can( 'promote_user', $user_id ) ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
// Bail if no role |
| 298 |
if ( ! isset( $_POST['bbp-forums-role'] ) || ! is_string( $_POST['bbp-forums-role'] ) ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
// Forums role we want the user to have |
| 303 |
$new_role = sanitize_key( $_POST['bbp-forums-role'] ); |
| 304 |
|
| 305 |
// Set the new forums role |
| 306 |
bbp_set_user_role( $user_id, $new_role ); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Check if a role ID is valid. |
| 311 |
* |
| 312 |
* This helper function accepts a role ID as a string, and compares it against |
| 313 |
* the array of registered dynamic roles. |
| 314 |
* |
| 315 |
* Use this function anytime you are manually attempting to set a user role |
| 316 |
* without using the bbp_set_user_role() function, or if you need to halt |
| 317 |
* additional processing during role validation. |
| 318 |
* |
| 319 |
* @since 2.6.5 |
| 320 |
* |
| 321 |
* @param string $role A well-formed (string) role ID to validate. |
| 322 |
* |
| 323 |
* @return bool True if role is valid. False if role is not valid. |
| 324 |
*/ |
| 325 |
function bbp_is_valid_role( $role = '' ) { |
| 326 |
|
| 327 |
// Default return value |
| 328 |
$retval = false; |
| 329 |
|
| 330 |
// Skip if no role to check |
| 331 |
if ( ! empty( $role ) && is_string( $role ) ) { |
| 332 |
|
| 333 |
// Get the dynamic role IDs |
| 334 |
$roles = array_keys( bbp_get_dynamic_roles() ); |
| 335 |
|
| 336 |
// Skip if no known role IDs |
| 337 |
if ( ! empty( $roles ) ) { |
| 338 |
|
| 339 |
// Is role in dynamic roles array? |
| 340 |
$retval = in_array( $role, $roles, true ); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
// Filter & return |
| 345 |
return (bool) apply_filters( 'bbp_is_valid_role', $retval, $role ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Add the default role to the current user if needed. |
| 350 |
* |
| 351 |
* This function will bail if the forum is not global in a multisite |
| 352 |
* installation of WordPress, or if the user is marked as spam or deleted. |
| 353 |
* |
| 354 |
* @since 2.0.0 bbPress (r3380) |
| 355 |
* |
| 356 |
* @return If not multisite, not global, or user is deleted/spammed. |
| 357 |
*/ |
| 358 |
function bbp_set_current_user_default_role() { |
| 359 |
|
| 360 |
/** Sanity ****************************************************************/ |
| 361 |
|
| 362 |
// Bail if deactivating bbPress |
| 363 |
if ( bbp_is_deactivation() ) { |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
// Catch all, to prevent premature user initialization |
| 368 |
if ( ! did_action( 'set_current_user' ) ) { |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
// Bail if not logged in or already a member of this site |
| 373 |
if ( ! is_user_logged_in() ) { |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
// Get the current user ID |
| 378 |
$user_id = bbp_get_current_user_id(); |
| 379 |
|
| 380 |
// Bail if user already has a forums role |
| 381 |
if ( bbp_get_user_role( $user_id ) ) { |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
// Bail if user is marked as spam or is deleted |
| 386 |
if ( bbp_is_user_inactive( $user_id ) ) { |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
/** Ready *****************************************************************/ |
| 391 |
|
| 392 |
// Load up bbPress once |
| 393 |
$bbp = bbpress(); |
| 394 |
|
| 395 |
// Get whether or not to add a role to the user account |
| 396 |
$add_to_site = bbp_allow_global_access(); |
| 397 |
|
| 398 |
// Get the current user's WordPress role. Set to empty string if none found. |
| 399 |
$user_role = bbp_get_user_blog_role( $user_id ); |
| 400 |
|
| 401 |
// Get the role map |
| 402 |
$role_map = bbp_get_user_role_map(); |
| 403 |
|
| 404 |
/** Forum Role ************************************************************/ |
| 405 |
|
| 406 |
// Use a mapped role or default role |
| 407 |
$new_role = empty( $user_role ) || ! isset( $role_map[ $user_role ] ) |
| 408 |
? bbp_get_default_role() |
| 409 |
: $role_map[ $user_role ]; |
| 410 |
|
| 411 |
/** Add or Map ************************************************************/ |
| 412 |
|
| 413 |
// Add the user to the site |
| 414 |
if ( true === $add_to_site ) { |
| 415 |
bbp_set_user_role( $user_id, $new_role ); |
| 416 |
|
| 417 |
// Don't add the user, but still give them the correct caps dynamically |
| 418 |
} else { |
| 419 |
$bbp->current_user->caps[ $new_role ] = true; |
| 420 |
$bbp->current_user->get_role_caps(); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Return a map of WordPress roles to bbPress roles. Used to automatically grant |
| 426 |
* appropriate bbPress roles to WordPress users that wouldn't already have a |
| 427 |
* role in the forums. Also guarantees WordPress admins get the Keymaster role. |
| 428 |
* |
| 429 |
* @since 2.2.0 bbPress (r4334) |
| 430 |
* |
| 431 |
* @return array Filtered array of WordPress roles to bbPress roles. |
| 432 |
*/ |
| 433 |
function bbp_get_user_role_map() { |
| 434 |
|
| 435 |
// Get the default role once here |
| 436 |
$default_role = bbp_get_default_role(); |
| 437 |
|
| 438 |
// Filter & return |
| 439 |
return (array) apply_filters( |
| 440 |
'bbp_get_user_role_map', |
| 441 |
array( |
| 442 |
'administrator' => bbp_get_keymaster_role(), |
| 443 |
'editor' => $default_role, |
| 444 |
'author' => $default_role, |
| 445 |
'contributor' => $default_role, |
| 446 |
'subscriber' => $default_role |
| 447 |
) |
| 448 |
); |
| 449 |
} |
| 450 |
|
| 451 |
/** User Status ***************************************************************/ |
| 452 |
|
| 453 |
/** |
| 454 |
* Checks if the user has been marked as a spammer. |
| 455 |
* |
| 456 |
* @since 2.0.0 bbPress (r3355) |
| 457 |
* |
| 458 |
* @param int $user_id int The ID for the user. |
| 459 |
* @return bool True if spammer, False if not. |
| 460 |
*/ |
| 461 |
function bbp_is_user_spammer( $user_id = 0 ) { |
| 462 |
|
| 463 |
// Default to current user |
| 464 |
if ( empty( $user_id ) && is_user_logged_in() ) { |
| 465 |
$user_id = bbp_get_current_user_id(); |
| 466 |
} |
| 467 |
|
| 468 |
// No user to check |
| 469 |
if ( empty( $user_id ) ) { |
| 470 |
return false; |
| 471 |
} |
| 472 |
|
| 473 |
// Assume user is not spam |
| 474 |
$is_spammer = false; |
| 475 |
|
| 476 |
// Get user data |
| 477 |
$user = get_userdata( $user_id ); |
| 478 |
|
| 479 |
// No user found |
| 480 |
if ( empty( $user ) ) { |
| 481 |
$is_spammer = false; |
| 482 |
|
| 483 |
// Check if spam |
| 484 |
} elseif ( ! empty( $user->spam ) ) { |
| 485 |
$is_spammer = true; |
| 486 |
} |
| 487 |
|
| 488 |
// Filter & return |
| 489 |
return (bool) apply_filters( 'bbp_core_is_user_spammer', $is_spammer ); |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Mark a users topics and replies as spam when the user is marked as spam. |
| 494 |
* |
| 495 |
* @since 2.0.0 bbPress (r3405) |
| 496 |
* |
| 497 |
* @param int $user_id Optional. User ID to spam. Defaults to displayed user. |
| 498 |
* |
| 499 |
* @return bool If no user ID passed. |
| 500 |
*/ |
| 501 |
function bbp_make_spam_user( $user_id = 0 ) { |
| 502 |
|
| 503 |
// Use displayed user if it's not yourself |
| 504 |
if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) { |
| 505 |
$user_id = bbp_get_displayed_user_id(); |
| 506 |
} |
| 507 |
|
| 508 |
// Bail if no user ID |
| 509 |
if ( empty( $user_id ) ) { |
| 510 |
return false; |
| 511 |
} |
| 512 |
|
| 513 |
// Bail if user ID is keymaster |
| 514 |
if ( bbp_is_user_keymaster( $user_id ) ) { |
| 515 |
return false; |
| 516 |
} |
| 517 |
|
| 518 |
// Arm the torpedos |
| 519 |
$bbp_db = bbp_db(); |
| 520 |
|
| 521 |
// Get the blog IDs of the user to mark as spam |
| 522 |
$blogs = get_blogs_of_user( $user_id, true ); |
| 523 |
|
| 524 |
// If user has no blogs, they are a guest on this site |
| 525 |
if ( empty( $blogs ) ) { |
| 526 |
$blogs[ $bbp_db->blogid ] = array(); |
| 527 |
} |
| 528 |
|
| 529 |
// Get array of post types to mark as spam |
| 530 |
$post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ); |
| 531 |
$post_types = "'" . implode( "', '", $post_types ) . "'"; |
| 532 |
|
| 533 |
// Get array of statuses to mark as spam |
| 534 |
$post_statuses = bbp_get_public_topic_statuses(); |
| 535 |
$post_statuses = "'" . implode( "', '", $post_statuses ) . "'"; |
| 536 |
|
| 537 |
// Loop through blogs and remove their posts |
| 538 |
foreach ( (array) array_keys( $blogs ) as $blog_id ) { |
| 539 |
|
| 540 |
// Switch to the site ID |
| 541 |
bbp_switch_to_site( $blog_id ); |
| 542 |
|
| 543 |
// Get topics and replies |
| 544 |
$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 ); |
| 545 |
$posts = $bbp_db->get_col( $query ); |
| 546 |
|
| 547 |
// Loop through posts and spam them |
| 548 |
if ( ! empty( $posts ) ) { |
| 549 |
foreach ( $posts as $post_id ) { |
| 550 |
|
| 551 |
// The routines for topics ang replies are different, so use the |
| 552 |
// correct one based on the post type |
| 553 |
switch ( get_post_type( $post_id ) ) { |
| 554 |
|
| 555 |
case bbp_get_topic_post_type() : |
| 556 |
bbp_spam_topic( $post_id ); |
| 557 |
break; |
| 558 |
|
| 559 |
case bbp_get_reply_post_type() : |
| 560 |
bbp_spam_reply( $post_id ); |
| 561 |
break; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
// Switch back to current site |
| 567 |
bbp_restore_current_site(); |
| 568 |
} |
| 569 |
|
| 570 |
// Delete user options |
| 571 |
bbp_delete_user_options( $user_id ); |
| 572 |
|
| 573 |
// Success |
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Mark a users topics and replies as spam when the user is marked as spam. |
| 579 |
* |
| 580 |
* @since 2.0.0 bbPress (r3405) |
| 581 |
* |
| 582 |
* @param int $user_id Optional. User ID to unspam. Defaults to displayed user. |
| 583 |
* |
| 584 |
* @return bool If no user ID passed. |
| 585 |
*/ |
| 586 |
function bbp_make_ham_user( $user_id = 0 ) { |
| 587 |
|
| 588 |
// Use displayed user if it's not yourself |
| 589 |
if ( empty( $user_id ) && bbp_is_single_user() && ! bbp_is_user_home() ) { |
| 590 |
$user_id = bbp_get_displayed_user_id(); |
| 591 |
} |
| 592 |
|
| 593 |
// Bail if no user ID |
| 594 |
if ( empty( $user_id ) ) { |
| 595 |
return false; |
| 596 |
} |
| 597 |
|
| 598 |
// Bail if user ID is keymaster |
| 599 |
if ( bbp_is_user_keymaster( $user_id ) ) { |
| 600 |
return false; |
| 601 |
} |
| 602 |
|
| 603 |
// Arm the torpedos |
| 604 |
$bbp_db = bbp_db(); |
| 605 |
|
| 606 |
// Get the blog IDs of the user to mark as spam |
| 607 |
$blogs = get_blogs_of_user( $user_id, true ); |
| 608 |
|
| 609 |
// If user has no blogs, they are a guest on this site |
| 610 |
if ( empty( $blogs ) ) { |
| 611 |
$blogs[ $bbp_db->blogid ] = array(); |
| 612 |
} |
| 613 |
|
| 614 |
// Get array of post types to mark as spam |
| 615 |
$post_types = array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ); |
| 616 |
$post_types = "'" . implode( "', '", $post_types ) . "'"; |
| 617 |
|
| 618 |
// Get array of statuses to unmark as spam |
| 619 |
$post_statuses = array( bbp_get_spam_status_id() ); |
| 620 |
$post_statuses = "'" . implode( "', '", $post_statuses ) . "'"; |
| 621 |
|
| 622 |
// Loop through blogs and remove their posts |
| 623 |
foreach ( (array) array_keys( $blogs ) as $blog_id ) { |
| 624 |
|
| 625 |
// Switch to the site ID |
| 626 |
bbp_switch_to_site( $blog_id ); |
| 627 |
|
| 628 |
// Get topics and replies |
| 629 |
$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 ); |
| 630 |
$posts = $bbp_db->get_col( $query ); |
| 631 |
|
| 632 |
// Loop through posts and spam them |
| 633 |
if ( ! empty( $posts ) ) { |
| 634 |
foreach ( $posts as $post_id ) { |
| 635 |
|
| 636 |
// The routines for topics ang replies are different, so use the |
| 637 |
// correct one based on the post type |
| 638 |
switch ( get_post_type( $post_id ) ) { |
| 639 |
|
| 640 |
case bbp_get_topic_post_type() : |
| 641 |
bbp_unspam_topic( $post_id ); |
| 642 |
break; |
| 643 |
|
| 644 |
case bbp_get_reply_post_type() : |
| 645 |
bbp_unspam_reply( $post_id ); |
| 646 |
break; |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
// Switch back to current site |
| 652 |
bbp_restore_current_site(); |
| 653 |
} |
| 654 |
|
| 655 |
// Update topic & reply counts |
| 656 |
bbp_update_user_topic_count( $user_id, bbp_get_user_topic_count_raw( $user_id ) ); |
| 657 |
bbp_update_user_reply_count( $user_id, bbp_get_user_reply_count_raw( $user_id ) ); |
| 658 |
|
| 659 |
// Update last posted (to now) |
| 660 |
bbp_update_user_last_posted( $user_id ); |
| 661 |
|
| 662 |
// Success |
| 663 |
return true; |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Checks if the user has been marked as deleted. |
| 668 |
* |
| 669 |
* @since 2.0.0 bbPress (r3355) |
| 670 |
* |
| 671 |
* @param int $user_id int The ID for the user. |
| 672 |
* @return bool True if deleted, False if not. |
| 673 |
*/ |
| 674 |
function bbp_is_user_deleted( $user_id = 0 ) { |
| 675 |
|
| 676 |
// Default to current user |
| 677 |
if ( empty( $user_id ) && is_user_logged_in() ) { |
| 678 |
$user_id = bbp_get_current_user_id(); |
| 679 |
} |
| 680 |
|
| 681 |
// No user to check |
| 682 |
if ( empty( $user_id ) ) { |
| 683 |
return false; |
| 684 |
} |
| 685 |
|
| 686 |
// Assume user is not deleted |
| 687 |
$is_deleted = false; |
| 688 |
|
| 689 |
// Get user data |
| 690 |
$user = get_userdata( $user_id ); |
| 691 |
|
| 692 |
// No user found |
| 693 |
if ( empty( $user ) ) { |
| 694 |
$is_deleted = true; |
| 695 |
|
| 696 |
// Check if deleted |
| 697 |
} elseif ( ! empty( $user->deleted ) ) { |
| 698 |
$is_deleted = true; |
| 699 |
} |
| 700 |
|
| 701 |
// Filter & return |
| 702 |
return (bool) apply_filters( 'bbp_core_is_user_deleted', $is_deleted ); |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Checks if user is active. |
| 707 |
* |
| 708 |
* @since 2.0.0 bbPress (r3502) |
| 709 |
* |
| 710 |
* @param int $user_id The user ID to check. |
| 711 |
* @return bool True if public, false if not. |
| 712 |
*/ |
| 713 |
function bbp_is_user_active( $user_id = 0 ) { |
| 714 |
|
| 715 |
// No user to check |
| 716 |
$user_id = bbp_get_user_id( $user_id, false, true ); |
| 717 |
if ( empty( $user_id ) ) { |
| 718 |
return false; |
| 719 |
} |
| 720 |
|
| 721 |
// Check spam |
| 722 |
if ( bbp_is_user_spammer( $user_id ) ) { |
| 723 |
return false; |
| 724 |
} |
| 725 |
|
| 726 |
// Check deleted |
| 727 |
if ( bbp_is_user_deleted( $user_id ) ) { |
| 728 |
return false; |
| 729 |
} |
| 730 |
|
| 731 |
// Assume true if not spam or deleted |
| 732 |
return true; |
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* Checks if user is not active. |
| 737 |
* |
| 738 |
* @since 2.0.0 bbPress (r3502) |
| 739 |
* |
| 740 |
* @param int $user_id The user ID to check. Defaults to current user ID. |
| 741 |
* @return bool True if inactive, false if active. |
| 742 |
*/ |
| 743 |
function bbp_is_user_inactive( $user_id = 0 ) { |
| 744 |
return ! bbp_is_user_active( $user_id ); |
| 745 |
} |
| 746 |
|
| 747 |
/** |
| 748 |
* Checks if user is a keymaster. |
| 749 |
* |
| 750 |
* @since 2.3.0 bbPress (r4783) |
| 751 |
* |
| 752 |
* @param int $user_id The user ID to check. Defaults to current user ID. |
| 753 |
* @return bool True if keymaster, false if not. |
| 754 |
*/ |
| 755 |
function bbp_is_user_keymaster( $user_id = 0 ) { |
| 756 |
$_user_id = bbp_get_user_id( $user_id, false, true ); |
| 757 |
$retval = user_can( $_user_id, 'keep_gate' ); |
| 758 |
|
| 759 |
// Filter & return |
| 760 |
return (bool) apply_filters( 'bbp_is_user_keymaster', $retval, $_user_id, $user_id ); |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* Does a user have a profile for the current site. |
| 765 |
* |
| 766 |
* @since 2.2.0 bbPress (r4362) |
| 767 |
* |
| 768 |
* @param int $user_id User ID to check. |
| 769 |
* |
| 770 |
* @return bool Whether or not the user has a profile on this blog_id. |
| 771 |
*/ |
| 772 |
function bbp_user_has_profile( $user_id = 0 ) { |
| 773 |
|
| 774 |
// Assume every user has a profile |
| 775 |
$retval = true; |
| 776 |
|
| 777 |
// Validate user ID, default to displayed or current user |
| 778 |
$user_id = bbp_get_user_id( $user_id, true, true ); |
| 779 |
|
| 780 |
// Try to get this user's data |
| 781 |
$user = get_userdata( $user_id ); |
| 782 |
|
| 783 |
// No user found, return false |
| 784 |
if ( empty( $user ) ) { |
| 785 |
$retval = false; |
| 786 |
|
| 787 |
// User is inactive, and current user is not a keymaster |
| 788 |
} elseif ( ! bbp_is_user_keymaster() && bbp_is_user_inactive( $user->ID ) ) { |
| 789 |
$retval = false; |
| 790 |
} |
| 791 |
|
| 792 |
// Filter & return |
| 793 |
return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id ); |
| 794 |
} |
| 795 |
|
| 796 |
/** Moderators ****************************************************************/ |
| 797 |
|
| 798 |
/** |
| 799 |
* Add a moderator to an object. |
| 800 |
* |
| 801 |
* @since 2.6.0 bbPress (r6056) |
| 802 |
* |
| 803 |
* @param int $object_id Traditionally a post ID. |
| 804 |
* @param int $user_id User ID. |
| 805 |
* @param string $object_type Type of meta (post,term,user,comment). |
| 806 |
* |
| 807 |
* @return bool |
| 808 |
*/ |
| 809 |
function bbp_add_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) { |
| 810 |
return bbp_add_user_to_object( $object_id, $user_id, '_bbp_moderator_id', $object_type ); |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* Remove a moderator user ID from an object. |
| 815 |
* |
| 816 |
* @since 2.6.0 bbPress (r6056) |
| 817 |
* |
| 818 |
* @param int $object_id Traditionally a post ID. |
| 819 |
* @param int $user_id User ID. |
| 820 |
* @param string $object_type Type of meta (post,term,user,comment). |
| 821 |
* |
| 822 |
* @return bool |
| 823 |
*/ |
| 824 |
function bbp_remove_moderator( $object_id = 0, $user_id = 0, $object_type = 'post' ) { |
| 825 |
return bbp_remove_user_from_object( $object_id, $user_id, '_bbp_moderator_id', $object_type ); |
| 826 |
} |
| 827 |
|
| 828 |
/** |
| 829 |
* Get user IDs of moderators for an object. |
| 830 |
* |
| 831 |
* @since 2.6.0 bbPress (r6056) |
| 832 |
* |
| 833 |
* @param int $object_id Traditionally a post ID. |
| 834 |
* @param string $object_type Type of meta (post,term,user,comment). |
| 835 |
* |
| 836 |
* @return array |
| 837 |
*/ |
| 838 |
function bbp_get_moderator_ids( $object_id = 0, $object_type = 'post' ) { |
| 839 |
return bbp_get_users_for_object( $object_id, '_bbp_moderator_id', $object_type ); |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* Get moderators for a specific object ID. Will return global moderators when |
| 844 |
* object ID is empty. |
| 845 |
* |
| 846 |
* @since 2.6.0 bbPress (r6056) |
| 847 |
* |
| 848 |
* @param int $object_id Traditionally a post ID. |
| 849 |
* @param string $object_type Type of meta (post,term,user,comment). |
| 850 |
* |
| 851 |
* @return array |
| 852 |
*/ |
| 853 |
function bbp_get_moderators( $object_id = 0, $object_type = 'post' ) { |
| 854 |
|
| 855 |
// Get global moderators |
| 856 |
if ( empty( $object_id ) ) { |
| 857 |
$users = get_users( |
| 858 |
array( |
| 859 |
'role__in' => bbp_get_moderator_role(), |
| 860 |
) |
| 861 |
); |
| 862 |
|
| 863 |
// Get object moderators |
| 864 |
} else { |
| 865 |
$users = get_users( |
| 866 |
array( |
| 867 |
'include' => bbp_get_moderator_ids( $object_id, $object_type ), |
| 868 |
) |
| 869 |
); |
| 870 |
} |
| 871 |
|
| 872 |
// Filter & return |
| 873 |
return (array) apply_filters( 'bbp_get_moderators', $users, $object_id, $object_type ); |
| 874 |
} |
| 875 |
|