Skip to content

Commit

Permalink
Users: Pass the previous state of the user as context to the `wp_set_…
Browse files Browse the repository at this point in the history
…password` hook.

Follow-up to [55056], [55250].

Props dd32.
Fixes #61541.

git-svn-id: https://develop.svn.wordpress.org/trunk@58653 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jul 3, 2024
1 parent 4454f4a commit 5466b9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2768,6 +2768,8 @@ function wp_rand( $min = null, $max = null ) {
function wp_set_password( $password, $user_id ) {
global $wpdb;

$old_user_data = get_userdata( $user_id );

$hash = wp_hash_password( $password );
$wpdb->update(
$wpdb->users,
Expand All @@ -2784,11 +2786,13 @@ function wp_set_password( $password, $user_id ) {
* Fires after the user password is set.
*
* @since 6.2.0
* @since 6.7.0 The `$old_user_data` parameter was added.
*
* @param string $password The plaintext password just set.
* @param int $user_id The ID of the user whose password was just set.
* @param string $password The plaintext password just set.
* @param int $user_id The ID of the user whose password was just set.
* @param WP_User $old_user_data Object containing user's data prior to update.
*/
do_action( 'wp_set_password', $password, $user_id );
do_action( 'wp_set_password', $password, $user_id, $old_user_data );
}
endif;

Expand Down
10 changes: 8 additions & 2 deletions tests/phpunit/tests/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,22 @@ public function test_password_trimming() {
* Tests hooking into wp_set_password().
*
* @ticket 57436
* @ticket 61541
*
* @covers ::wp_set_password
*/
public function test_wp_set_password_action() {
$action = new MockAction();

add_action( 'wp_set_password', array( $action, 'action' ) );
wp_set_password( 'A simple password', self::$user_id );
$previous_user_pass = get_user_by( 'id', $this->user->ID )->user_pass;

add_action( 'wp_set_password', array( $action, 'action' ), 10, 3 );
wp_set_password( 'A simple password', $this->user->ID );

$this->assertSame( 1, $action->get_call_count() );

// Check that the old data passed through the hook is correct.
$this->assertSame( $previous_user_pass, $action->get_args()[0][2]->user_pass );
}

/**
Expand Down

0 comments on commit 5466b9c

Please sign in to comment.