Skip to content

Commit fc3fbcb

Browse files
committed
PHP 8.1 | WP_REST_Users_Controller::update_item(): fix passing null to non-nullable
Not all requests are accompanied by a `$request['email']`. This leads to a PHP 8.1 "passing null to non-nullable" deprecation notice when the `WP_REST_Users_Controller::update_item()` method passes a `null` email address onto `email_exists()`, which eventually reached the `WP_User::get_data_by()` method where things go wrong. In the next condition in the code of the `WP_REST_Users_Controller::update_item()` method - `if ( $owner_id && $owner_id !== $id )` - you can see that the code already takes this into account as it will not throw a `WP_Error` if `$owner_id` is falsey. `WP_User::get_data_by()` returns `false` for a failed field request. The other functions through which the return value is passed through, do the same. So, by setting a default value for `$owner_id` of `false` and only checking `email_exists()` when there is an email to check, the "passing null to non-nullable" deprecation notice is bypassed without breaking BC. Fixes a whole slew of test errors along the lines of: ``` 6) WP_Test_REST_Users_Controller::test_update_item_en_US_locale trim(): Passing null to parameter #1 ($string) of type string is deprecated /var/www/src/wp-includes/class-wp-user.php:211 /var/www/src/wp-includes/pluggable.php:105 /var/www/src/wp-includes/user.php:1953 /var/www/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php:728 /var/www/src/wp-includes/rest-api/class-wp-rest-server.php:1143 /var/www/src/wp-includes/rest-api/class-wp-rest-server.php:990 /var/www/tests/phpunit/includes/spy-rest-server.php:67 /var/www/tests/phpunit/tests/rest-api/rest-users-controller.php:1719 /var/www/vendor/bin/phpunit:123 ```
1 parent 2d361b7 commit fc3fbcb

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,10 @@ public function update_item( $request ) {
725725
);
726726
}
727727

728-
$owner_id = email_exists( $request['email'] );
728+
$owner_id = false;
729+
if ( is_string( $request['email'] ) ) {
730+
$owner_id = email_exists( $request['email'] );
731+
}
729732

730733
if ( $owner_id && $owner_id !== $id ) {
731734
return new WP_Error(

0 commit comments

Comments
 (0)