bb_apply_wp_role_map_to_user

The BuddyPress bb apply wp role map to user function.

Description

bb_apply_wp_role_map_to_user( $user, (bool) $reload = true ); 

Parameters (2)

0. $user
The user.
1. $reload — Optional. (bool) => true
The reload.

Usage

  1. if ( !function_exists( 'bb_apply_wp_role_map_to_user' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-users.php'; 
  3.  
  4. // The user. 
  5. $user = null; 
  6.  
  7. // The reload. 
  8. $reload = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_apply_wp_role_map_to_user($user, $reload); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-users.php  
  1. function bb_apply_wp_role_map_to_user( $user, $reload = true ) { 
  2. // Expects only user ids 
  3. if ( !is_numeric( $user ) ) { 
  4. return; 
  5.  
  6. $user = (int) $user; 
  7.  
  8. if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) { 
  9. return; 
  10.  
  11. if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) { 
  12. $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_'; 
  13.  
  14. if ( !$wordpress_roles_map = bb_get_option( 'wp_roles_map' ) ) { 
  15. return; 
  16.  
  17. global $bbdb; 
  18. global $wp_roles; 
  19. global $bb; 
  20.  
  21. static $bbpress_roles_map = false; 
  22.  
  23. if ( !$bbpress_roles_map ) { 
  24. $bbpress_roles_map = array(); 
  25. foreach ( $wp_roles->get_names() as $_bbpress_role => $_bbpress_rolename ) { 
  26. $bbpress_roles_map[$_bbpress_role] = 'subscriber'; 
  27. unset( $_bbpress_role, $_bbpress_rolename ); 
  28. $bbpress_roles_map = array_merge( $bbpress_roles_map, array_flip( $wordpress_roles_map ) ); 
  29. unset( $bbpress_roles_map['inactive'], $bbpress_roles_map['blocked'] ); 
  30.  
  31. static $wordpress_userlevel_map = array( 
  32. 'administrator' => 10,  
  33. 'editor' => 7,  
  34. 'author' => 2,  
  35. 'contributor' => 1,  
  36. 'subscriber' => 0 
  37. ); 
  38.  
  39. $bbpress_roles = bb_get_usermeta( $user, $bbdb->prefix . 'capabilities' ); 
  40. $wordpress_roles = bb_get_usermeta( $user, $wordpress_table_prefix . 'capabilities' ); 
  41.  
  42. if ( !$bbpress_roles && is_array( $wordpress_roles ) ) { 
  43. $bbpress_roles_new = array(); 
  44.  
  45. foreach ( $wordpress_roles as $wordpress_role => $wordpress_role_value ) { 
  46. if ( $wordpress_roles_map[strtolower( $wordpress_role )] && $wordpress_role_value ) { 
  47. $bbpress_roles_new[$wordpress_roles_map[strtolower( $wordpress_role )]] = true; 
  48.  
  49. if ( count( $bbpress_roles_new ) ) { 
  50. bb_update_usermeta( $user, $bbdb->prefix . 'capabilities', $bbpress_roles_new ); 
  51. if ( $reload ) { 
  52. header( 'Location: ' . bb_get_uri( null, null, BB_URI_CONTEXT_HEADER ) ); 
  53. exit; 
  54. } elseif ( !$wordpress_roles && is_array( $bbpress_roles ) ) { 
  55. $wordpress_roles_new = array(); 
  56.  
  57. foreach ( $bbpress_roles as $bbpress_role => $bbpress_role_value ) { 
  58. if ( $bbpress_roles_map[strtolower( $bbpress_role )] && $bbpress_role_value ) { 
  59. $wordpress_roles_new[$bbpress_roles_map[strtolower( $bbpress_role )]] = true; 
  60. $wordpress_userlevels_new[] = $wordpress_userlevel_map[$bbpress_roles_map[strtolower( $bbpress_role )]]; 
  61.  
  62. if ( count( $wordpress_roles_new ) ) { 
  63. bb_update_usermeta( $user, $wordpress_table_prefix . 'capabilities', $wordpress_roles_new ); 
  64. bb_update_usermeta( $user, $wordpress_table_prefix . 'user_level', max( $wordpress_userlevels_new ) );