diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 2dbdd584b385d..03593ea3e64e6 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -30,7 +30,7 @@ function wp_set_current_user( $id, $name = '' ) { // If `$id` matches the current user, there is nothing to do. if ( isset( $current_user ) && ( $current_user instanceof WP_User ) - && ( $id === $current_user->ID ) + && ( (int) $id === $current_user->ID ) && ( null !== $id ) ) { return $current_user; diff --git a/tests/phpunit/tests/user/wpSetCurrentUser.php b/tests/phpunit/tests/user/wpSetCurrentUser.php index bebee961ececc..868b3bc71b38a 100644 --- a/tests/phpunit/tests/user/wpSetCurrentUser.php +++ b/tests/phpunit/tests/user/wpSetCurrentUser.php @@ -57,4 +57,34 @@ public function test_should_set_by_name_if_id_is_null() { $this->assertSame( $user, wp_get_current_user() ); $this->assertSame( self::$user_id2, get_current_user_id() ); } + + /** + * Ensure user switching doesn't occur for the same user, even if type is non-int. + * + * @ticket 64628 + * + * @dataProvider data_should_not_switch_to_same_user_type_equivalency + */ + public function test_should_not_switch_to_same_user_type_equivalency( string $type_function ) { + wp_set_current_user( self::$user_id ); + $this->assertSame( self::$user_id, get_current_user_id(), "Current user's ID should match the ID of the user switched to." ); + + $action = new MockAction(); + add_action( 'set_current_user', array( $action, 'action' ) ); + + wp_set_current_user( $type_function( self::$user_id ) ); + $this->assertSame( 0, $action->get_call_count(), 'set_current_user should not be fired when switching to the same user.' ); + } + + /** + * Data provider for test_should_not_switch_to_same_user_type_equivalency. + * + * @return array[] Data provider. + */ + public function data_should_not_switch_to_same_user_type_equivalency(): array { + return array( + 'integer' => array( 'type_function' => 'intval' ), + 'string' => array( 'type_function' => 'strval' ), + ); + } }