bp_core_activate_signup

Activate a signup, as identified by an activation key.

Description

(int|bool) bp_core_activate_signup( (string) $key ); 

Returns (int|bool)

User ID on success, false on failure.

Parameters (1)

0. $key (string)
Activation key.

Usage

  1. if ( !function_exists( 'bp_core_activate_signup' ) ) { 
  2. require_once '/bp-members/bp-members-functions.php'; 
  3.  
  4. // Activation key. 
  5. $key = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_core_activate_signup($key); 
  9.  

Defined (1)

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

/bp-members/bp-members-functions.php  
  1. function bp_core_activate_signup( $key ) { 
  2. global $wpdb; 
  3.  
  4. $user = false; 
  5.  
  6. // Multisite installs have their own activation routine. 
  7. if ( is_multisite() ) { 
  8. $user = wpmu_activate_signup( $key ); 
  9.  
  10. // If there were errors, add a message and redirect. 
  11. if ( ! empty( $user->errors ) ) { 
  12. return $user; 
  13.  
  14. $user_id = $user['user_id']; 
  15.  
  16. } else { 
  17. $signups = BP_Signup::get( array( 
  18. 'activation_key' => $key,  
  19. ) ); 
  20.  
  21. if ( empty( $signups['signups'] ) ) { 
  22. return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) ); 
  23.  
  24. $signup = $signups['signups'][0]; 
  25.  
  26. if ( $signup->active ) { 
  27. if ( empty( $signup->domain ) ) { 
  28. return new WP_Error( 'already_active', __( 'The user is already active.', 'buddypress' ), $signup ); 
  29. } else { 
  30. return new WP_Error( 'already_active', __( 'The site is already active.', 'buddypress' ), $signup ); 
  31.  
  32. // Password is hashed again in wp_insert_user. 
  33. $password = wp_generate_password( 12, false ); 
  34.  
  35. $user_id = username_exists( $signup->user_login ); 
  36.  
  37. // Create the user. This should only be necessary if BP_SIGNUPS_SKIP_USER_CREATION is true. 
  38. if ( ! $user_id ) { 
  39. $user_id = wp_create_user( $signup->user_login, $password, $signup->user_email ); 
  40.  
  41. // Otherwise, update the existing user's status. 
  42. } elseif ( $key === bp_get_user_meta( $user_id, 'activation_key', true ) || $key === wp_hash( $user_id ) ) { 
  43.  
  44. // Change the user's status so they become active. 
  45. if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id ) ) ) { 
  46. return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) ); 
  47.  
  48. bp_delete_user_meta( $user_id, 'activation_key' ); 
  49.  
  50. $member = get_userdata( $user_id ); 
  51. $member->set_role( get_option('default_role') ); 
  52.  
  53. $user_already_created = true; 
  54.  
  55. } else { 
  56. $user_already_exists = true; 
  57.  
  58. if ( ! $user_id ) { 
  59. return new WP_Error( 'create_user', __( 'Could not create user', 'buddypress' ), $signup ); 
  60.  
  61. // Fetch the signup so we have the data later on. 
  62. $signups = BP_Signup::get( array( 
  63. 'activation_key' => $key,  
  64. ) ); 
  65.  
  66. $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false; 
  67.  
  68. // Activate the signup. 
  69. BP_Signup::validate( $key ); 
  70.  
  71. if ( isset( $user_already_exists ) ) { 
  72. return new WP_Error( 'user_already_exists', __( 'That username is already activated.', 'buddypress' ), $signup ); 
  73.  
  74. // Set up data to pass to the legacy filter. 
  75. $user = array( 
  76. 'user_id' => $user_id,  
  77. 'password' => $signup->meta['password'],  
  78. 'meta' => $signup->meta,  
  79. ); 
  80.  
  81. // Notify the site admin of a new user registration. 
  82. wp_new_user_notification( $user_id ); 
  83.  
  84. if ( isset( $user_already_created ) ) { 
  85.  
  86. /** 
  87. * Fires if the user has already been created. 
  88. * @since 1.2.2 
  89. * @param int $user_id ID of the user being checked. 
  90. * @param string $key Activation key. 
  91. * @param array $user Array of user data. 
  92. */ 
  93. do_action( 'bp_core_activated_user', $user_id, $key, $user ); 
  94. return $user_id; 
  95.  
  96. // Set any profile data. 
  97. if ( bp_is_active( 'xprofile' ) ) { 
  98. if ( ! empty( $user['meta']['profile_field_ids'] ) ) { 
  99. $profile_field_ids = explode( ', ', $user['meta']['profile_field_ids'] ); 
  100.  
  101. foreach( (array) $profile_field_ids as $field_id ) { 
  102. $current_field = isset( $user['meta']["field_{$field_id}"] ) ? $user['meta']["field_{$field_id}"] : false; 
  103.  
  104. if ( !empty( $current_field ) ) { 
  105. xprofile_set_field_data( $field_id, $user_id, $current_field ); 
  106.  
  107. // Save the visibility level. 
  108. $visibility_level = ! empty( $user['meta']['field_' . $field_id . '_visibility'] ) ? $user['meta']['field_' . $field_id . '_visibility'] : 'public'; 
  109. xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level ); 
  110.  
  111. // Replace the password automatically generated by WordPress by the one the user chose. 
  112. if ( ! empty( $user['meta']['password'] ) ) { 
  113. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id ) ); 
  114.  
  115. /** 
  116. * Make sure to clean the user's cache as we've 
  117. * directly edited the password without using 
  118. * wp_update_user(). 
  119. * If we can't use wp_update_user() that's because 
  120. * we already hashed the password at the signup step. 
  121. */ 
  122. $uc = wp_cache_get( $user_id, 'users' ); 
  123.  
  124. if ( ! empty( $uc->ID ) ) { 
  125. clean_user_cache( $uc->ID ); 
  126.  
  127. /** 
  128. * Fires at the end of the user activation process. 
  129. * @since 1.2.2 
  130. * @param int $user_id ID of the user being checked. 
  131. * @param string $key Activation key. 
  132. * @param array $user Array of user data. 
  133. */ 
  134. do_action( 'bp_core_activated_user', $user_id, $key, $user ); 
  135.  
  136. return $user_id;