diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 7fc0dfb584ff6..867df81e16650 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -26,13 +26,24 @@ * * @global string $auth_secure_cookie * - * @param array $credentials Optional. User info in order to sign on. + * @param array $credentials { + * Optional. User info in order to sign on. + * + * @type string $user_login Username. + * @type string $user_password User password. + * @type bool $remember Whether to 'remember' the user. Increases the time + * that the cookie will be kept. Default false. + * } * @param string|bool $secure_cookie Optional. Whether to use secure cookie. * @return WP_User|WP_Error WP_User on success, WP_Error on failure. */ function wp_signon( $credentials = array(), $secure_cookie = '' ) { if ( empty( $credentials ) ) { - $credentials = array(); // Back-compat for plugins passing an empty string. + $credentials = array( + 'user_login' => '', + 'user_password' => '', + 'remember' => false, + ); if ( ! empty( $_POST['log'] ) ) { $credentials['user_login'] = wp_unslash( $_POST['log'] ); diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index 4aa1d882c6229..1a2bd9a92df99 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -444,6 +444,25 @@ public function test_wp_signon_using_email_with_an_apostrophe() { $this->assertInstanceOf( 'WP_User', wp_signon() ); } + /** + * Tests that PHP 8.1 "passing null to non-nullable" deprecation notices + * are not thrown when `user_login` and `user_password` parameters are empty. + * + * The notices that we should not see: + * `Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated`. + * `Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated`. + * + * @ticket 56850 + */ + public function test_wp_signon_does_not_throw_deprecation_notices_with_default_parameters() { + $error = wp_signon(); + $this->assertWPError( $error, 'The result should be an instance of WP_Error.' ); + + $error_codes = $error->get_error_codes(); + $this->assertContains( 'empty_username', $error_codes, 'The "empty_username" error code should be present.' ); + $this->assertContains( 'empty_password', $error_codes, 'The "empty_password" error code should be present.' ); + } + /** * HTTP Auth headers are used to determine the current user. *