Skip to content

Commit 9098796

Browse files
committed
Users: Fire wp_set_password action when creating or updating a user's password.
Various filters and actions fire during user creation and editing, making available all manner of user data to be acted upon by custom code. However, a user's password was not included in the data that was made available. This change now fires an existing action, `wp_set_password`, during initial user creation and when an existing user's password is updated. Props ChloeD, scribu, dd32, pento, chriscct7, johnbillion, logicrays, nimeshatxecurify. Fixes #22114. git-svn-id: https://develop.svn.wordpress.org/trunk@60634 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 50086eb commit 9098796

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/wp-includes/user.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,11 @@ function wp_insert_user( $userdata ) {
25012501

25022502
$user = new WP_User( $user_id );
25032503

2504+
if ( ! $update ) {
2505+
/** This action is documented in wp-includes/pluggable.php */
2506+
do_action( 'wp_set_password', $userdata['user_pass'], $user_id, $userdata );
2507+
}
2508+
25042509
/**
25052510
* Filters a user's meta values and keys immediately after the user is created or updated
25062511
* and before any user meta is inserted or updated.
@@ -2684,6 +2689,9 @@ function wp_update_user( $userdata ) {
26842689
$plaintext_pass = $userdata['user_pass'];
26852690
$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
26862691

2692+
/** This action is documented in wp-includes/pluggable.php */
2693+
do_action( 'wp_set_password', $plaintext_pass, $user_id, $user_obj );
2694+
26872695
/**
26882696
* Filters whether to send the password change email.
26892697
*

tests/phpunit/tests/user.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,4 +2353,69 @@ function ( $meta_id, $object_id, $meta_key ) use ( &$db_update_count ) {
23532353
// Verify there are no updates to 'use_ssl' user meta.
23542354
$this->assertSame( 1, $db_update_count );
23552355
}
2356+
2357+
/**
2358+
* Tests that `wp_set_password` action is triggered correctly during `wp_insert_user()`.
2359+
*
2360+
* @ticket 22114
2361+
*/
2362+
public function test_set_password_action_fires_during_wp_insert_user() {
2363+
$mock_action = new MockAction();
2364+
2365+
add_action( 'wp_set_password', array( $mock_action, 'action' ), 10, 3 );
2366+
2367+
$userdata = array(
2368+
'user_login' => 'testuser_' . wp_rand(),
2369+
'user_pass' => 'initialpassword',
2370+
'user_email' => '[email protected]',
2371+
);
2372+
2373+
$user_id = wp_insert_user( $userdata );
2374+
2375+
// Assert that `wp_set_password` was triggered once during user creation.
2376+
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during user creation.' );
2377+
2378+
$args = $mock_action->get_args();
2379+
2380+
$this->assertSame( $userdata['user_pass'], $args[0][0], 'Wrong password argument in action.' );
2381+
$this->assertSame( $user_id, $args[0][1], 'Wrong user ID in action.' );
2382+
}
2383+
2384+
/**
2385+
* Tests that `wp_set_password` action is triggered correctly during `wp_update_user()`.
2386+
*
2387+
* @ticket 22114
2388+
*/
2389+
public function test_set_password_action_on_user_update() {
2390+
$mock_action = new MockAction();
2391+
2392+
add_action( 'wp_set_password', array( $mock_action, 'action' ), 10, 3 );
2393+
2394+
$user_id = $this->factory()->user->create(
2395+
array(
2396+
'role' => 'subscriber',
2397+
'user_login' => 'testuser_update',
2398+
'user_email' => '[email protected]',
2399+
'user_pass' => 'initialpassword',
2400+
)
2401+
);
2402+
2403+
$mock_action->reset();
2404+
2405+
$updated_password = 'newpassword123';
2406+
2407+
$userdata = array(
2408+
'ID' => $user_id,
2409+
'user_pass' => $updated_password,
2410+
);
2411+
2412+
wp_update_user( $userdata );
2413+
2414+
$this->assertSame( 1, $mock_action->get_call_count(), 'wp_set_password was not triggered during password update.' );
2415+
2416+
$args = $mock_action->get_args();
2417+
2418+
$this->assertSame( $updated_password, $args[0][0], 'Invalid password in wp_set_password action.' );
2419+
$this->assertSame( $user_id, $args[0][1], 'Invalid user ID in wp_set_password action.' );
2420+
}
23562421
}

0 commit comments

Comments
 (0)