Changeset 4289
- Timestamp:
- 11/02/2012 05:26:26 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/users/functions.php
r4274 r4289 1380 1380 * @return string 1381 1381 */ 1382 function bbp_ get_user_role( $user_id = 0) {1382 function bbp_set_user_role( $user_id = 0, $new_role = '', $context = '' ) { 1383 1383 1384 1384 // Validate user id … … 1391 1391 1392 1392 // Get the user's main role 1393 $role = isset( $user->roles ) ? array_shift( $user->roles ) : ''; 1394 1395 return apply_filters( 'bbp_get_user_role', $role, $user_id, $user ); 1393 switch ( strtolower( $context ) ) { 1394 1395 // bbPress context 1396 case 'forum' : 1397 case 'forums' : 1398 case 'bbpress' : 1399 $forum_roles = array_intersect( array_values( $user->roles ), array_keys( bbp_get_forums_editable_roles() ) ); 1400 1401 foreach ( $forum_roles as $forum_role ) { 1402 $user->remove_role( $forum_role ); 1403 } 1404 1405 $user->add_role( $new_role ); 1406 1407 break; 1408 1409 // WordPress core context 1410 case '' : 1411 case 'blog' : 1412 case 'wordpress' : 1413 default : 1414 $blog_roles = array_intersect( array_values( $user->roles ), array_keys( get_editable_roles() ) ); 1415 1416 foreach ( $blog_roles as $blog_role ) { 1417 $user->remove_role( $blog_role ); 1418 } 1419 1420 $user->add_role( $new_role ); 1421 1422 break; 1423 } 1424 1425 return apply_filters( 'bbp_get_user_role', $new_role, $context, $user_id, $user ); 1426 } 1427 1428 /** 1429 * Return a user's main role 1430 * 1431 * @since bbPress (r3860) 1432 * 1433 * @param int $user_id 1434 * @uses bbp_get_user_id() To get the user id 1435 * @uses get_userdata() To get the user data 1436 * @uses apply_filters() Calls 'bbp_get_user_role' with the 1437 * role and user id 1438 * @return string 1439 */ 1440 function bbp_get_user_role( $user_id = 0, $context = '' ) { 1441 1442 // Validate user id 1443 $user_id = bbp_get_user_id( $user_id, false, false ); 1444 if ( empty( $user_id ) ) 1445 return false; 1446 1447 // Get userdata 1448 $user = get_userdata( $user_id ); 1449 1450 // User has no roles so set as empty string 1451 if ( empty( $user->roles ) ) { 1452 1453 // Assume the anonymous role if looking for a bbPress role 1454 if ( in_array( $context, array( 'forum', 'forums', 'bbpress' ) ) ) { 1455 $role = bbp_get_spectator_role(); 1456 1457 // Otherwise trust that there is no role 1458 } else { 1459 $role = ''; 1460 } 1461 1462 // User has roles so lets 1463 } else { 1464 1465 // Get the user's main role 1466 switch ( strtolower( $context ) ) { 1467 1468 // bbPress context 1469 case 'forum' : 1470 case 'forums' : 1471 case 'bbpress' : 1472 $role = array_intersect( array_values( $user->roles ), array_keys( bbp_get_forums_editable_roles() ) ); 1473 1474 // If there's a role in the array, use the first one 1475 if ( !empty( $role ) ) { 1476 $role = array_shift( array_values( $role ) ); 1477 1478 // Otherwise, give them the anonymous role 1479 } else { 1480 $role = bbp_get_spectator_role(); 1481 } 1482 1483 break; 1484 1485 // WordPress core context 1486 case '' : 1487 case 'blog' : 1488 case 'wordpress' : 1489 default : 1490 $role = array_intersect( array_values( $user->roles ), array_keys( get_editable_roles() ) ); 1491 1492 // If there's a role in the array, use the first one 1493 if ( !empty( $role ) ) { 1494 $role = array_shift( array_values( $role ) ); 1495 } 1496 1497 break; 1498 } 1499 } 1500 1501 return apply_filters( 'bbp_get_user_role', $role, $context, $user_id, $user ); 1396 1502 } 1397 1503
Note: See TracChangeset
for help on using the changeset viewer.