bb_manage_user_fields

Users.

Description

bb_manage_user_fields( (string) $edit_user = '' ); 

Parameters (1)

0. $edit_user — Optional. (string) => ''
The edit user.

Usage

  1. if ( !function_exists( 'bb_manage_user_fields' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-admin/includes/functions.bb-admin.php'; 
  3.  
  4. // The edit user. 
  5. $edit_user = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bb_manage_user_fields($edit_user); 
  9.  

Defined (1)

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

/bp-forums/bbpress/bb-admin/includes/functions.bb-admin.php  
  1. function bb_manage_user_fields( $edit_user = '' ) { 
  2. global $wp_roles, $wp_users_object, $bbdb; 
  3.  
  4. // Cap checks 
  5. $user_roles = $wp_roles->role_names; 
  6. $can_keep_gate = bb_current_user_can( 'keep_gate' ); 
  7.  
  8. if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) ) { 
  9.  
  10. bb_check_admin_referer( 'user-manage' ); 
  11.  
  12. // Instantiate required vars 
  13. $_POST = stripslashes_deep( $_POST ); 
  14. $create_user_errors = new WP_Error; 
  15.  
  16. // User login 
  17. $trimmed_user_login = str_replace( ' ', '', $_POST['user_login'] ); 
  18. $user_login = sanitize_user( $_POST['user_login'], true ); 
  19. $user_meta['first_name'] = $_POST['first_name']; 
  20. $user_meta['last_name'] = $_POST['last_name']; 
  21. $user_display_name = $_POST['display_name']; 
  22. $user_email = $_POST['user_email']; 
  23. $user_url = $_POST['user_url']; 
  24. $user_meta['from'] = $_POST['from']; 
  25. $user_meta['occ'] = $_POST['occ']; 
  26. $user_meta['interest'] = $_POST['interest']; 
  27. $user_role = $_POST['userrole']; 
  28. $user_meta['throttle'] = $_POST['throttle']; 
  29. $user_pass1 = $_POST['pass1']; 
  30. $user_pass2 = $_POST['pass2']; 
  31. $user_status = 0; 
  32. $user_pass = false; 
  33. $user_url = $user_url ? bb_fix_link( $user_url ) : ''; 
  34.  
  35. // Check user_login 
  36. if ( !isset( $_GET['action'] ) && empty( $user_login ) ) { 
  37. $create_user_errors->add( 'user_login', __( 'Username is a required field.' ) ); 
  38. } else { 
  39. if ( $user_login !== $trimmed_user_login ) { 
  40. $create_user_errors->add( 'user_login', sprintf( __( '%s is an invalid username. How\'s this one?' ), esc_html( $_POST['user_login'] ) ) ); 
  41. $user_login = $trimmed_user_login; 
  42.  
  43. // Check email 
  44. if ( isset( $user_email ) && empty( $user_email ) ) 
  45. $create_user_errors->add( 'user_email', __( 'Email address is a required field.' ) ); 
  46.  
  47. // Password Sanity Check 
  48. if ( ( !empty( $user_pass1 ) || !empty( $user_pass2 ) ) && $user_pass1 !== $user_pass2 ) 
  49. $create_user_errors->add( 'pass', __( 'You must enter the same password twice.' ) ); 
  50. elseif ( !isset( $_GET['action'] ) && ( empty( $user_pass1 ) && empty( $user_pass2 ) ) ) 
  51. $create_user_errors->add( 'pass', __( 'You must enter a password.' ) ); 
  52. elseif ( isset( $_GET['action'] ) && ( empty( $user_pass1 ) && empty( $user_pass2 ) ) ) 
  53. $user_pass = ''; 
  54. else 
  55. $user_pass = $user_pass1; 
  56.  
  57. // No errors 
  58. if ( !$create_user_errors->get_error_messages() ) { 
  59.  
  60. // Create or udpate 
  61. switch ( $_POST['action'] ) { 
  62. case 'create' : 
  63. $goback = bb_get_uri( 'bb-admin/users.php', array( 'created' => 'true' ), BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); 
  64. $user = $wp_users_object->new_user( compact( 'user_login', 'user_email', 'user_url', 'user_nicename', 'user_status', 'user_pass' ) ); 
  65.  
  66. // Error handler 
  67. if ( is_wp_error( $user ) ) { 
  68. bb_admin_notice( $user ); 
  69. unset( $goback ); 
  70.  
  71. // Update additional user data 
  72. } else { 
  73. // Update caps 
  74. bb_update_usermeta( $user['ID'], $bbdb->prefix . 'capabilities', array( $user_role => true ) ); 
  75.  
  76. // Update all user meta 
  77. foreach ( $user_meta as $key => $value ) 
  78. bb_update_usermeta( $user['ID'], $key, $value ); 
  79.  
  80. // Don't send email if empty 
  81. if ( !empty( $user_pass ) ) 
  82. bb_send_pass( $user['ID'], $user_pass ); 
  83.  
  84. do_action( 'bb_new_user', $user['ID'], $user_pass ); 
  85.  
  86. break; 
  87.  
  88. case 'update' : 
  89. $goback = bb_get_uri( 'bb-admin/users.php', array( 'updated' => 'true' ), BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); 
  90. $user = $wp_users_object->get_user( $_GET['user_id'], array( 'output' => ARRAY_A ) ); 
  91. bb_update_user( $user['ID'], $user_email, $user_url, $user_display_name ); 
  92.  
  93. // Don't change PW if empty 
  94. if ( !empty( $user_pass ) ) 
  95. bb_update_user_password( $user['ID'], $user_pass ); 
  96.  
  97. // Error handler 
  98. if ( is_wp_error( $user ) ) { 
  99. bb_admin_notice( $user ); 
  100. unset( $goback ); 
  101.  
  102. // Update additional user data 
  103. } else { 
  104. // Update caps 
  105. bb_update_usermeta( $user['ID'], $bbdb->prefix . 'capabilities', array( $user_role => true ) ); 
  106.  
  107. // Update all user meta 
  108. foreach ( $user_meta as $key => $value ) 
  109. bb_update_usermeta( $user['ID'], $key, $value ); 
  110.  
  111. // Don't send email if empty 
  112. if ( !empty( $user_pass ) ) 
  113. bb_send_pass( $user['ID'], $user_pass ); 
  114.  
  115. do_action( 'bb_update_user', $user['ID'], $user_pass ); 
  116.  
  117. break; 
  118.  
  119. // Redirect 
  120. if ( isset( $goback ) && !empty( $goback ) ) 
  121. bb_safe_redirect( $goback ); 
  122.  
  123. // Error handler 
  124. } else { 
  125. bb_admin_notice( $create_user_errors ); 
  126. } elseif ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' ) { 
  127. if ( isset( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ) { 
  128. $disabled = true; 
  129.  
  130. // Get the user 
  131. if ( empty( $edit_user ) ) 
  132. $edit_user = bb_get_user( bb_get_user_id( $_GET['user_id'] ) ); 
  133.  
  134. // Instantiate required vars 
  135. $user_login = $edit_user->user_login; 
  136. $user_meta['first_name'] = $edit_user->first_name; 
  137. $user_meta['last_name'] = $edit_user->last_name; 
  138. $user_display_name = $edit_user->display_name; 
  139. $user_email = $edit_user->user_email; 
  140. $user_url = $edit_user->user_url; 
  141. $user_meta['from'] = $edit_user->from; 
  142. $user_meta['occ'] = $edit_user->occ; 
  143. $user_meta['interest'] = $edit_user->interest; 
  144. $user_role = array_search( 'true', $edit_user->capabilities ); 
  145. $user_meta['throttle'] = $edit_user->throttle; 
  146.  
  147. // Keymasters can't demote themselves 
  148. if ( ( $edit_user->ID == bb_get_current_user_info( 'id' ) && $can_keep_gate ) || ( isset( $edit_user->capabilities ) && is_array( $edit_user->capabilities ) && array_key_exists( 'keymaster', $edit_user->capabilities ) && !$can_keep_gate ) ) 
  149. $user_roles = array( 'keymaster' => $user_roles['keymaster'] ); 
  150.  
  151. // only keymasters can promote others to keymaster status 
  152. elseif ( !$can_keep_gate ) 
  153. unset( $user_roles['keymaster'] ); 
  154.  
  155. // Load password strength checker 
  156. wp_enqueue_script( 'password-strength-meter' ); 
  157. wp_enqueue_script( 'profile-edit' ); 
  158.  
  159. // Generate a few PW hints 
  160. $some_pass_hints = ''; 
  161. for ( $l = 3; $l != 0; $l-- ) 
  162. $some_pass_hints .= '<p>' . bb_generate_password() . '</p>'; 
  163.  
  164. // Create the user fields 
  165. $user_fields = array( 
  166. 'user_login' => array( 
  167. 'title' => __( 'Username' ),  
  168. 'note' => __( 'Required! Unique identifier for new user.' ),  
  169. 'value' => $user_login,  
  170. 'disabled' => $disabled 
  171. ),  
  172. 'first_name' => array( 
  173. 'title' => __( 'First Name' ),  
  174. 'value' => $user_meta['first_name'] 
  175. ),  
  176. 'last_name' => array( 
  177. 'title' => __( 'Last Name' ),  
  178. 'value' => $user_meta['last_name'] 
  179. ),  
  180. 'display_name' => array( 
  181. 'title' => __( 'Display Name' ),  
  182. 'value' => $user_display_name 
  183. ),  
  184. 'user_email' => array( 
  185. 'title' => __( 'Email' ),  
  186. 'note' => __( 'Required! Will be used for notifications and profile settings changes.' ),  
  187. 'value' => $user_email 
  188. ),  
  189. 'user_url' => array( 
  190. 'title' => __( 'Website' ),  
  191. 'class' => array( 'long', 'code' ),  
  192. 'note' => __( 'The full URL of user\'s homepage or blog.' ),  
  193. 'value' => $user_url 
  194. ),  
  195. 'from' => array( 
  196. 'title' => __( 'Location' ),  
  197. 'class' => array( 'long' ),  
  198. 'value' => $user_meta['from'] 
  199. ),  
  200. 'occ' => array( 
  201. 'title' => __( 'Occupation' ),  
  202. 'class' => array( 'long' ),  
  203. 'value' => $user_meta['occ'] 
  204. ),  
  205. 'interest' => array( 
  206. 'title' => __( 'Interests' ),  
  207. 'class' => array( 'long' ),  
  208. 'value' => $user_meta['interest'] 
  209. ),  
  210. 'userrole' => array( 
  211. 'title' => __( 'User Role' ),  
  212. 'type' => 'select',  
  213. 'options' => $user_roles,  
  214. 'note' => __( 'Allow user the above privileges.' ),  
  215. 'value' => $user_role,  
  216. ),  
  217. 'pass1' => array( 
  218. 'title' => __( 'New Password' ),  
  219. 'type' => 'password',  
  220. 'class' => array( 'short', 'text', 'code' ),  
  221. 'note' => __( 'Hints: ' ) . $some_pass_hints,  
  222. 'value' => $user_pass1,  
  223. ),  
  224. 'pass2' => array( 
  225. 'title' => __( 'Repeat New Password' ),  
  226. 'type' => 'password',  
  227. 'class' => array( 'short', 'text', 'code' ),  
  228. 'note' => __( 'If you ignore hints, remember: the password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).' ),  
  229. 'value' => $user_pass2,  
  230. ),  
  231. 'email_pass' => array( 
  232. 'title' => '',  
  233. 'type' => 'checkbox',  
  234. 'options' => array( 
  235. '1' => array( 
  236. 'label' => __( 'Email the new password.' ),  
  237. 'attributes' => array( 'checked' => true ) 
  238. ),  
  239. ),  
  240. 'pass-strength-fake-input' => array( 
  241. 'title' => __( 'Password Strength' ),  
  242. 'type' => 'hidden',  
  243. ),  
  244. ); 
  245.  
  246. return apply_filters( 'bb_manage_user_fields', $user_fields );