Skip to content

Commit 7104aa0

Browse files
Login and Registration: Set correct default values in wp_signon().
The `$credentials['user_login']` and `$credentials['user_password']` parameters are passed by reference to the `wp_authenticate` action, and are at that point [https://www.php.net/manual/en/language.references.pass.php#124383 created as null] if they don't exist in the array. This commit sets those values to an empty string, resolving two PHP 8.1 deprecation notices: * One from `preg_replace()` in `wp_strip_all_tags()` via `sanitize_user()` in `wp_authenticate()`: {{{ Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated }}} * One from `trim()` in `wp_authenticate()` itself: {{{ Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated }}} Includes documenting the `$credentials` parameter using hash notation. Follow-up to [6643], [37697]. Props lenasterg, TobiasBg, ocean90, afragen, lkraav, SergeyBiryukov. Fixes #56850. git-svn-id: https://develop.svn.wordpress.org/trunk@55301 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 33ba8c5 commit 7104aa0

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/wp-includes/user.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,24 @@
2626
*
2727
* @global string $auth_secure_cookie
2828
*
29-
* @param array $credentials Optional. User info in order to sign on.
29+
* @param array $credentials {
30+
* Optional. User info in order to sign on.
31+
*
32+
* @type string $user_login Username.
33+
* @type string $user_password User password.
34+
* @type bool $remember Whether to 'remember' the user. Increases the time
35+
* that the cookie will be kept. Default false.
36+
* }
3037
* @param string|bool $secure_cookie Optional. Whether to use secure cookie.
3138
* @return WP_User|WP_Error WP_User on success, WP_Error on failure.
3239
*/
3340
function wp_signon( $credentials = array(), $secure_cookie = '' ) {
3441
if ( empty( $credentials ) ) {
35-
$credentials = array(); // Back-compat for plugins passing an empty string.
42+
$credentials = array(
43+
'user_login' => '',
44+
'user_password' => '',
45+
'remember' => false,
46+
);
3647

3748
if ( ! empty( $_POST['log'] ) ) {
3849
$credentials['user_login'] = wp_unslash( $_POST['log'] );

tests/phpunit/tests/auth.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,25 @@ public function test_wp_signon_using_email_with_an_apostrophe() {
444444
$this->assertInstanceOf( 'WP_User', wp_signon() );
445445
}
446446

447+
/**
448+
* Tests that PHP 8.1 "passing null to non-nullable" deprecation notices
449+
* are not thrown when `user_login` and `user_password` parameters are empty.
450+
*
451+
* The notices that we should not see:
452+
* `Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated`.
453+
* `Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated`.
454+
*
455+
* @ticket 56850
456+
*/
457+
public function test_wp_signon_does_not_throw_deprecation_notices_with_default_parameters() {
458+
$error = wp_signon();
459+
$this->assertWPError( $error, 'The result should be an instance of WP_Error.' );
460+
461+
$error_codes = $error->get_error_codes();
462+
$this->assertContains( 'empty_username', $error_codes, 'The "empty_username" error code should be present.' );
463+
$this->assertContains( 'empty_password', $error_codes, 'The "empty_password" error code should be present.' );
464+
}
465+
447466
/**
448467
* HTTP Auth headers are used to determine the current user.
449468
*

0 commit comments

Comments
 (0)