bbp_admin_repair_user_roles

This repair tool will map each user of the current site to their respective forums role.

Description

bbp_admin_repair_user_roles(); 

By default, Admins will be Key Masters, and every other role will be the default role defined in Settings > Forums (Participant).


Usage

  1. if ( !function_exists( 'bbp_admin_repair_user_roles' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/admin/tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bbp_admin_repair_user_roles(); 
  7.  

Defined (1)

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

/includes/admin/tools.php  
  1. function bbp_admin_repair_user_roles() { 
  2.  
  3. $statement = __( 'Remapping forum role for each user on this site… %s', bbpress ); 
  4. $changed = 0; 
  5. $role_map = bbp_get_user_role_map(); 
  6. $default_role = bbp_get_default_role(); 
  7.  
  8. // Bail if no role map exists 
  9. if ( empty( $role_map ) ) 
  10. return array( 1, sprintf( $statement, __( 'Failed!', bbpress ) ) ); 
  11.  
  12. // Iterate through each role... 
  13. foreach ( array_keys( bbp_get_blog_roles() ) as $role ) { 
  14.  
  15. // Reset the offset 
  16. $offset = 0; 
  17.  
  18. // If no role map exists, give the default forum role (bbp-participant) 
  19. $new_role = isset( $role_map[$role] ) ? $role_map[$role] : $default_role; 
  20.  
  21. // Get users of this site, limited to 1000 
  22. while ( $users = get_users( array( 
  23. 'role' => $role,  
  24. 'fields' => 'ID',  
  25. 'number' => 1000,  
  26. 'offset' => $offset 
  27. ) ) ) { 
  28.  
  29. // Iterate through each user of $role and try to set it 
  30. foreach ( (array) $users as $user_id ) { 
  31. if ( bbp_set_user_role( $user_id, $new_role ) ) { 
  32. ++$changed; // Keep a count to display at the end 
  33.  
  34. // Bump the offset for the next query iteration 
  35. $offset = $offset + 1000; 
  36.  
  37. $result = sprintf( __( 'Complete! %s users updated.', bbpress ), bbp_number_format( $changed ) ); 
  38. return array( 0, sprintf( $statement, $result ) );