get_password_reset_key()
最后更新于:2021-11-26 11:26:15
get_password_reset_key( GC_User$user)Creates, stores, then returns a password reset key for user.
参数
- $user
-
(GC_User) (Required) User to retrieve password reset key for.
响应
(string|GC_Error) Password reset key on success. GC_Error on error.
源文件
文件: gc-includes/user.php
function get_password_reset_key( $user ) {
global $gc_hasher;
if ( ! ( $user instanceof GC_User ) ) {
return new GC_Error( 'invalidcombo', __( '<strong>Error</strong>: There is no account with that username or email address.' ) );
}
/**
* Fires before a new password is retrieved.
*
* Use the {@see 'retrieve_password'} hook instead.
*
* @since 1.5.0
* @deprecated 1.5.1 Misspelled. Use {@see 'retrieve_password'} hook instead.
*
* @param string $user_login The user login name.
*/
do_action_deprecated( 'retreive_password', array( $user->user_login ), '1.5.1', 'retrieve_password' );
/**
* Fires before a new password is retrieved.
*
* @since 1.5.1
*
* @param string $user_login The user login name.
*/
do_action( 'retrieve_password', $user->user_login );
$allow = true;
if ( is_multisite() && is_user_spammy( $user ) ) {
$allow = false;
}
/**
* Filters whether to allow a password to be reset.
*
* @since 2.7.0
*
* @param bool $allow Whether to allow the password to be reset. Default true.
* @param int $ID The ID of the user attempting to reset a password.
*/
$allow = apply_filters( 'allow_password_reset', $allow, $user->ID );
if ( ! $allow ) {
return new GC_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) );
} elseif ( is_gc_error( $allow ) ) {
return $allow;
}
// Generate something random for a password reset key.
$key = gc_generate_password( 20, false );
/**
* Fires when a password reset key is generated.
*
* @since 2.5.0
*
* @param string $user_login The username for the user.
* @param string $key The generated password reset key.
*/
do_action( 'retrieve_password_key', $user->user_login, $key );
// Now insert the key, hashed, into the DB.
if ( empty( $gc_hasher ) ) {
require_once ABSPATH . GCINC . '/class-phpass.php';
$gc_hasher = new PasswordHash( 8, true );
}
$hashed = time() . ':' . $gc_hasher->HashPassword( $key );
$key_saved = gc_update_user(
array(
'ID' => $user->ID,
'user_activation_key' => $hashed,
)
);
if ( is_gc_error( $key_saved ) ) {
return $key_saved;
}
return $key;
}