bp_settings_action_general()
Handles the changing and saving of user email addresses and passwords.
Description
We do quite a bit of logic and error handling here to make sure that users do not accidentally lock themselves out of their accounts. We also try to provide as accurate of feedback as possible without exposing anyone else’s information to them.
Special considerations are made for super admins that are able to edit any users accounts already, without knowing their existing password.
Source
File: bp-settings/actions/general.php
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | function bp_settings_action_general() { if ( ! bp_is_post_request() ) { return ; } // Bail if no submit action. if ( ! isset( $_POST [ 'submit' ] ) ) { return ; } // Bail if not in settings. if ( ! bp_is_settings_component() || ! bp_is_current_action( 'general' ) ) { return ; } // 404 if there are any additional action variables attached if ( bp_action_variables() ) { bp_do_404(); return ; } // Define local defaults $bp = buddypress(); // The instance $email_error = false; // invalid|blocked|taken|empty|nochange $pass_error = false; // invalid|mismatch|empty|nochange $pass_changed = false; // true if the user changes their password $email_changed = false; // true if the user changes their email $feedback_type = 'error' ; // success|error $feedback = array (); // array of strings for feedback. // Nonce check. check_admin_referer( 'bp_settings_general' ); // Validate the user again for the current password when making a big change. if ( ( is_super_admin() ) || ( ! empty ( $_POST [ 'pwd' ] ) && wp_check_password( $_POST [ 'pwd' ], $bp ->displayed_user->userdata->user_pass, bp_displayed_user_id() ) ) ) { $update_user = get_userdata( bp_displayed_user_id() ); /* Email Change Attempt ******************************************/ if ( ! empty ( $_POST [ 'email' ] ) ) { // What is missing from the profile page vs signup - // let's double check the goodies. $user_email = sanitize_email( esc_html( trim( $_POST [ 'email' ] ) ) ); $old_user_email = $bp ->displayed_user->userdata->user_email; // User is changing email address. if ( $old_user_email != $user_email ) { // Run some tests on the email address. $email_checks = bp_core_validate_email_address( $user_email ); if ( true !== $email_checks ) { if ( isset( $email_checks [ 'invalid' ] ) ) { $email_error = 'invalid' ; } if ( isset( $email_checks [ 'domain_banned' ] ) || isset( $email_checks [ 'domain_not_allowed' ] ) ) { $email_error = 'blocked' ; } if ( isset( $email_checks [ 'in_use' ] ) ) { $email_error = 'taken' ; } } // Store a hash to enable email validation. if ( false === $email_error ) { $hash = wp_generate_password( 32, false ); $pending_email = array ( 'hash' => $hash , 'newemail' => $user_email , ); bp_update_user_meta( bp_displayed_user_id(), 'pending_email_change' , $pending_email ); $verify_link = bp_displayed_user_domain() . bp_get_settings_slug() . '/?verify_email_change=' . $hash ; // Send the verification email. $args = array ( 'tokens' => array ( 'displayname' => bp_core_get_user_displayname( bp_displayed_user_id() ), 'old-user.email' => $old_user_email , 'user.email' => $user_email , 'verify.url' => esc_url( $verify_link ), ), ); bp_send_email( 'settings-verify-email-change' , bp_displayed_user_id(), $args ); // We mark that the change has taken place so as to ensure a // success message, even though verification is still required. $_POST [ 'email' ] = $update_user ->user_email; $email_changed = true; } // No change. } else { $email_error = false; } // Email address cannot be empty. } else { $email_error = 'empty' ; } /* Password Change Attempt ***************************************/ if ( ! empty ( $_POST [ 'pass1' ] ) && ! empty ( $_POST [ 'pass2' ] ) ) { if ( ( $_POST [ 'pass1' ] == $_POST [ 'pass2' ] ) && ! strpos ( " " . wp_unslash( $_POST [ 'pass1' ] ), "\\" ) ) { // Password change attempt is successful. if ( ( ! empty ( $_POST [ 'pwd' ] ) && $_POST [ 'pwd' ] != $_POST [ 'pass1' ] ) || is_super_admin() ) { $update_user ->user_pass = $_POST [ 'pass1' ]; $pass_changed = true; // The new password is the same as the current password. } else { $pass_error = 'same' ; } // Password change attempt was unsuccessful. } else { $pass_error = 'mismatch' ; } // Both password fields were empty. } elseif ( empty ( $_POST [ 'pass1' ] ) && empty ( $_POST [ 'pass2' ] ) ) { $pass_error = false; // One of the password boxes was left empty. } elseif ( ( empty ( $_POST [ 'pass1' ] ) && ! empty ( $_POST [ 'pass2' ] ) ) || ( ! empty ( $_POST [ 'pass1' ] ) && empty ( $_POST [ 'pass2' ] ) ) ) { $pass_error = 'empty' ; } // The structure of the $update_user object changed in WP 3.3, but // wp_update_user() still expects the old format. if ( isset( $update_user ->data ) && is_object ( $update_user ->data ) ) { $update_user = $update_user ->data; $update_user = get_object_vars( $update_user ); // Unset the password field to prevent it from emptying out the // user's user_pass field in the database. // @see wp_update_user(). if ( false === $pass_changed ) { unset( $update_user [ 'user_pass' ] ); } } // Clear cached data, so that the changed settings take effect // on the current page load. if ( ( false === $email_error ) && ( false === $pass_error ) && ( wp_update_user( $update_user ) ) ) { $bp ->displayed_user->userdata = bp_core_get_core_userdata( bp_displayed_user_id() ); } // Password Error. } else { $pass_error = 'invalid' ; } // Email feedback. switch ( $email_error ) { case 'invalid' : $feedback [ 'email_invalid' ] = __( 'That email address is invalid. Check the formatting and try again.' , 'buddyboss' ); break ; case 'blocked' : $feedback [ 'email_blocked' ] = __( 'That email address is currently unavailable for use.' , 'buddyboss' ); break ; case 'taken' : $feedback [ 'email_taken' ] = __( 'That email address is already taken.' , 'buddyboss' ); break ; case 'empty' : $feedback [ 'email_empty' ] = __( 'Email address cannot be empty.' , 'buddyboss' ); break ; case false : // No change. break ; } // Password feedback. switch ( $pass_error ) { case 'invalid' : $feedback [ 'pass_error' ] = __( 'Your current password is invalid.' , 'buddyboss' ); break ; case 'mismatch' : $feedback [ 'pass_mismatch' ] = __( 'The new password fields did not match.' , 'buddyboss' ); break ; case 'empty' : $feedback [ 'pass_empty' ] = __( 'One of the password fields was empty.' , 'buddyboss' ); break ; case 'same' : $feedback [ 'pass_same' ] = __( 'The new password must be different from the current password.' , 'buddyboss' ); break ; case false : // No change. break ; } // No errors so show a simple success message. if ( ( ( false === $email_error ) || ( false == $pass_error ) ) && ( ( true === $pass_changed ) || ( true === $email_changed ) ) ) { $feedback [] = __( 'Your settings have been saved.' , 'buddyboss' ); $feedback_type = 'success' ; // Some kind of errors occurred. } elseif ( ( ( false === $email_error ) || ( false === $pass_error ) ) && ( ( false === $pass_changed ) || ( false === $email_changed ) ) ) { if ( bp_is_my_profile() ) { $feedback [ 'nochange' ] = __( 'No changes were made to your account.' , 'buddyboss' ); } else { $feedback [ 'nochange' ] = __( 'No changes were made to this account.' , 'buddyboss' ); } } // Set the feedback. bp_core_add_message( implode( "\n" , $feedback ), $feedback_type ); /** * Fires after the general settings have been saved, and before redirect. * * @since BuddyPress 1.5.0 */ do_action( 'bp_core_general_settings_after_save' ); // Redirect to prevent issues with browser back button. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_settings_slug() . '/general' ) ); } |
Changelog
Version | Description |
---|---|
BuddyPress 1.6.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.