wppaste
WordPress

check_password_reset_key( string $key, string $login ): WP_User|WP_Error

Since
3.1.0
Source
wp-includes/user.php:3086
Retrieves a user row based on password reset key and login.

Description

A key is considered 'expired' if it exactly matches the value of the user_activation_key field, rather than being matched after going through the hashing process. This field is now hashed; old values are no longer accepted but have a different WP_Error code so good user feedback can be provided.

Compatibility

WordPress
since 3.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$keystring
The password reset key.
$loginstring
The user login.

Return value

WP_User|WP_Error
WP_User object on success, WP_Error object for invalid or expired keys.

Performance profile

How much work a call to check_password_reset_key() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–76

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 127.

Plugin surface
2 hooks

Third-party callbacks on 'password_reset_expiration', 'password_reset_key_expired' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_user_by()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 18 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost check_password_reset_key() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15–21__()
!empty($key) && is_string($key) && !empty($login) && is_string($login)27get_user_by(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && !$user39get_user_by(), apply_filters(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && $expiration_time43get_user_by(), apply_filters(), wp_verify_fast_hash(), time()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user48get_user_by(), apply_filters(), wp_verify_fast_hash(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && !$user48get_user_by(), apply_filters(), explode(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !hash_equals()52–56get_user_by(), apply_filters(), wp_verify_fast_hash(), hash_equals(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time52get_user_by(), apply_filters(), wp_verify_fast_hash(), time(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && $expiration_time52get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user57get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user58–63get_user_by(), apply_filters(), wp_verify_fast_hash(), hash_equals(), __(), apply_filters()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time && !hash_equals()58–60get_user_by(), apply_filters(), wp_verify_fast_hash(), time(), hash_equals(), __()
6 further outcomes, up to 76 instructions
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !hash_equals()61–65get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), hash_equals(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time61get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time64–67get_user_by(), apply_filters(), wp_verify_fast_hash(), time(), hash_equals(), __(), apply_filters()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user67–72get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), hash_equals(), __(), apply_filters()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time && !hash_equals()67–69get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time(), hash_equals(), __()
!empty($key) && is_string($key) && !empty($login) && is_string($login) && $user && !$expiration_time73–76get_user_by(), apply_filters(), explode(), wp_verify_fast_hash(), time(), hash_equals(), __(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12715–7615
8.512715–7615
8.412715–76156 fewer instructions than PHP 8.3
8.313318–8215
8.213318–8215
8.113318–82151 fewer instruction than PHP 7.4
7.413418–8315

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while check_password_reset_key() runs, in this order:

  1. apply_filters( password_reset_expiration )filterline 3114 (+28 into the body)

    Filters the expiration time of password reset keys.

  2. apply_filters( password_reset_key_expired )filterline 3152 (+66 into the body)

    Filters the return value of check_password_reset_key() when an old-style key or an expired key is used.

Uses · 6

Source code

function check_password_reset_key(	#[\SensitiveParameter]	$key,	$login) {	$key = preg_replace( '/[^a-z0-9]/i', '', $key ); 	if ( empty( $key ) || ! is_string( $key ) ) {		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );	} 	if ( empty( $login ) || ! is_string( $login ) ) {		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );	} 	$user = get_user_by( 'login', $login ); 	if ( ! $user ) {		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );	} 	/**	 * Filters the expiration time of password reset keys.	 *	 * @since 4.3.0	 *	 * @param int $expiration The expiration time in seconds.	 */	$expiration_duration = apply_filters( 'password_reset_expiration', DAY_IN_SECONDS ); 	if ( str_contains( $user->user_activation_key, ':' ) ) {		list( $pass_request_time, $pass_key ) = explode( ':', $user->user_activation_key, 2 );		$expiration_time                      = $pass_request_time + $expiration_duration;	} else {		$pass_key        = $user->user_activation_key;		$expiration_time = false;	} 	if ( ! $pass_key ) {		return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );	} 	$hash_is_correct = wp_verify_fast_hash( $key, $pass_key ); 	if ( $hash_is_correct && $expiration_time && time() < $expiration_time ) {		return $user;	} elseif ( $hash_is_correct && $expiration_time ) {		// Key has an expiration time that's passed.		return new WP_Error( 'expired_key', __( 'Invalid key.' ) );	} 	if ( hash_equals( $user->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) {		$return  = new WP_Error( 'expired_key', __( 'Invalid key.' ) );		$user_id = $user->ID; 		/**		 * Filters the return value of check_password_reset_key() when an		 * old-style key or an expired key is used.		 *		 * @since 3.7.0 Previously plain-text keys were stored in the database.		 * @since 4.3.0 Previously key hashes were stored without an expiration time.		 *		 * @param WP_Error $return  A WP_Error object denoting an expired key.		 *                          Return a WP_User object to validate the key.		 * @param int      $user_id The matched user ID.		 */		return apply_filters( 'password_reset_key_expired', $return, $user_id );	} 	return new WP_Error( 'invalid_key', __( 'Invalid key.' ) );}

Changelog

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.