From 5466b9c82674c30c885076fce8b99b4bfece8ab0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 3 Jul 2024 16:08:55 +0000 Subject: [PATCH] Users: Pass the previous state of the user as context to the `wp_set_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 --- src/wp-includes/pluggable.php | 10 +++++++--- tests/phpunit/tests/auth.php | 10 ++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 5ed63e539c..04a4ef8b55 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -2768,6 +2768,8 @@ if ( ! function_exists( 'wp_set_password' ) ) : 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, @@ -2784,11 +2786,13 @@ if ( ! function_exists( 'wp_set_password' ) ) : * 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; diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index a28051dc09..5c196b499f 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -115,16 +115,22 @@ class Tests_Auth extends WP_UnitTestCase { * 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 ); } /**