From ad51bbf1a344de15a55c7036074973e8f19d5a6f Mon Sep 17 00:00:00 2001 From: Nate Allen Date: Thu, 19 Mar 2026 10:19:58 -0400 Subject: [PATCH 1/3] REST API: Restore `is_array()` check in `rest_convert_error_to_response()`. The null coalescing conversion in [61429] removed the `is_array()` guard when accessing `$error_data['status']`. Since `WP_Error` data can be any type (including `stdClass`), this causes a PHP fatal error when non-array data is passed. This restores the type check while still using null coalescing for the array case. Props costdev, westonruter. Fixes #64901. See #63430. --- src/wp-includes/rest-api.php | 2 +- tests/phpunit/tests/rest-api/rest-server.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index cb6833772a765..50b25690f1f6c 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -3428,7 +3428,7 @@ function rest_convert_error_to_response( $error ) { $status = array_reduce( $error->get_all_error_data(), static function ( $status, $error_data ) { - return $error_data['status'] ?? $status; + return is_array( $error_data ) ? ( $error_data['status'] ?? $status ) : $status; }, 500 ); diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 692c363ac7595..bd3a9346f70b2 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -592,6 +592,19 @@ public function test_error_to_response_with_additional_data() { $this->assertSame( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] ); } + /** + * @ticket 64901 + */ + public function test_error_to_response_with_stdclass_data() { + $error = new WP_Error( 'test', 'test', (object) array( 'status' => 400 ) ); + + $response = rest_convert_error_to_response( $error ); + $this->assertInstanceOf( 'WP_REST_Response', $response ); + + // stdClass data should not cause a fatal, status should default to 500. + $this->assertSame( 500, $response->get_status() ); + } + public function test_rest_error() { $data = array( 'code' => 'wp-api-test-error', From e0601390152c7d1affa32f9f846695cdeb30f18d Mon Sep 17 00:00:00 2001 From: Nate Allen Date: Thu, 19 Mar 2026 10:21:24 -0400 Subject: [PATCH 2/3] Revert to original isset() ternary instead of null coalescing with is_array(). --- src/wp-includes/rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 50b25690f1f6c..fe99e416f2387 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -3428,7 +3428,7 @@ function rest_convert_error_to_response( $error ) { $status = array_reduce( $error->get_all_error_data(), static function ( $status, $error_data ) { - return is_array( $error_data ) ? ( $error_data['status'] ?? $status ) : $status; + return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; }, 500 ); From d88933d6dc2dbfb75e403993b27e1a6c918a14a7 Mon Sep 17 00:00:00 2001 From: Nate Allen Date: Thu, 19 Mar 2026 12:27:49 -0400 Subject: [PATCH 3/3] Add inline comment explaining the is_array() check. --- src/wp-includes/rest-api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index fe99e416f2387..b97dfd014be41 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -3428,6 +3428,7 @@ function rest_convert_error_to_response( $error ) { $status = array_reduce( $error->get_all_error_data(), static function ( $status, $error_data ) { + // Note: $error_data may not be an array (e.g. stdClass), so is_array() check is intentional. return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; }, 500